App.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <script setup>
  2. import { onMounted, ref, inject } from 'vue'
  3. import { RouterLink, RouterView, useRoute } from 'vue-router'
  4. import Header from './views/header.vue';
  5. import axios from 'axios';
  6. import qs from 'qs';
  7. import { ElMessageBox } from 'element-plus'
  8. import { userInfoStore, userCurrentRole,userCurrent,pageGotype } from './stores/counter'
  9. const route = useRoute()
  10. const store = userInfoStore()
  11. const CurrentRole = userCurrentRole()
  12. const Current = userCurrent()
  13. const setgotype = pageGotype()
  14. const lang = inject('lang')
  15. const name = ref('')
  16. const isShow = ref(true)
  17. const setTimeState = ref(null)
  18. const topUserInfo = ref({})
  19. onMounted(() => {
  20. // 通过 code 获取登录信息
  21. let isGrantCode = getUrlParam(window.parent.location.href, 'grant_code')
  22. let ticket = getUrlParam(window.parent.location.href, 'ticket')
  23. let userid = getUrlParam(window.parent.location.href, 'userid')
  24. // let userid = '0c3735c9-a2ef-11ef-9b30-005056b86db5'
  25. let gotype = getUrlParam(window.parent.location.href, 'gotype')
  26. console.log('gotype',gotype);
  27. setgotype.setpageType(gotype);
  28. axios.defaults.headers = { 'content-type': 'application/json;charset=utf-8' }
  29. if (isGrantCode) {
  30. let params = {
  31. grantCode: isGrantCode,
  32. redirectUri: "https://cloud.cocorobo.cn/testapi"
  33. }
  34. axios.post('//cloud.cocorobo.cn/api/getAccessToken', JSON.stringify(params)).then(res => {
  35. // console.log(res)
  36. if (res.data.code == 0) {
  37. let accessTokenParams = {
  38. accessToken: res.data.data.accessToken
  39. }
  40. axios.post('//cloud.cocorobo.cn/api/getUserInfo', JSON.stringify(accessTokenParams)).then(r => {
  41. if (r.data.code == 0) {
  42. console.log(r.data.data)
  43. // store.user = r.data.data
  44. name.value = r.data.data.userName
  45. let isBjt = localStorage.getItem('isBjt')
  46. let userid = localStorage.getItem('userid')
  47. if(isBjt == 1){
  48. localStorage.removeItem('isBjt')
  49. localStorage.removeItem('userid')
  50. setEduid(userid, r.data.data.eduId)
  51. setStateUserinfo(userid)
  52. }
  53. else{
  54. userInfo(r.data.data)
  55. }
  56. }
  57. })
  58. } else {
  59. linkLogin()
  60. }
  61. })
  62. }else if(ticket){
  63. axios.get(`https://pbl.cocorobo.cn/api/bat/getToken?ticket=${ticket}`)
  64. .then(r => {
  65. if (r.status === 200 && r.data.data && r.data.data.result) {
  66. console.log(r.data.data.result)
  67. name.value = r.data.data.result.name
  68. userInfo2(r.data.data.result)
  69. }else {
  70. const serviceUrl = lang.lang === 'hk'
  71. ? 'https://cloud.cocorobo.hk/course_resource/index.html'
  72. : 'https://cloud.cocorobo.cn/ai.html'
  73. top.location.href = `https://bjt.baoan.edu.cn/manage/?baojiaotong=1&service=${serviceUrl}`
  74. }
  75. })
  76. }else if(userid){
  77. selectUser2(userid)
  78. } else {
  79. linkLogin()
  80. }
  81. })
  82. const linkLogin = () => {
  83. if(window.location.href.indexOf("localhost")>-1){
  84. isShow.value = false
  85. }else{
  86. top.location.href = `https://sz.edu.cn/thirdauth/oauth2/authorize?service=initService&scope=all&response_type=code&app_id=f03970962b254aa29115fefd42a8eeb7&redirect_uri=https://cloud.cocorobo.cn/api`
  87. }
  88. }
  89. // 同步登录信息
  90. const userInfo = (data) => {
  91. // eduId - 教育ID
  92. // userId - 用户id
  93. // areaAdministrator - 安全管理员
  94. // userAdministrator - 用户管理员(市区级)
  95. // securityAuditor - 安全审计员(市区级)
  96. // schoolAdministrator - 用户管理员(校级)
  97. // schoolSecurityAuditor - 安全审计员(校级)
  98. // edupersonnel - 单位用户
  99. // teacher - 教师
  100. // visitorstudent - 学生
  101. // visitorparent - 家长
  102. // visitor - 访客
  103. // parentagency - 家长代办
  104. let userIdentity = 1
  105. if (data.currentRole == "edupersonnel" || data.currentRole == "areaAdministrator" || data.currentRole == "userAdministrator" || data.currentRole == "securityAuditor" || data.currentRole == "schoolAdministrator" || data.currentRole == "schoolSecurityAuditor" || data.currentRole == "teacher") {
  106. isShow.value = true
  107. CurrentRole.setUserCurrentRole(data.currentRole)
  108. Current.setUserCurrent(data)
  109. userIdentity = 1
  110. let userGrand = ""
  111. if (data.childList && data.childList.length > 0) {
  112. userGrand = data.childList[0].gradeName + data.childList[0].className
  113. }
  114. let params = {
  115. openid: data.userId,
  116. edu: true
  117. }
  118. axios.defaults.headers = { 'content-type': 'application/x-www-form-urlencoded' }
  119. axios.defaults.withCredentials = true;
  120. let diqu = `${data.schoolName}&${data.schoolCode}&${data.orgName}&${data.orgCode}`
  121. if (data.telephoneNumber) {
  122. top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/batchRegistrationSzdjg3", [data.userName, data.telephoneNumber + '@szdjg.com', '16ace517-b5c7-4168-a9bb-a9e0035df840', data.schoolName || "", data.telephoneNumber, userGrand, userIdentity, data.userId, diqu, data.eduId], function (r) {
  123. // console.log('rrrrr', r)
  124. axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(d => {
  125. // store.user = top.US.userInfo
  126. setStateUserinfo(d.data[0][0].userid)
  127. })
  128. }, [], { "type": "POST", "withCredentials": true });
  129. } else {
  130. isShow.value = false
  131. ElMessageBox.alert('没有绑定手机号,请先去绑定手机号', "提示", {
  132. confirmButtonText: '确定',
  133. })
  134. }
  135. } else {
  136. isShow.value = false
  137. ElMessageBox.alert('当前账号无使用平台权限,如有需要,请联系可可乐博公司,联系方式:0755-86525412', "温馨提示", {
  138. confirmButtonText: '确定',
  139. showClose: false
  140. }).then(() => {
  141. const a = document.createElement('a');
  142. a.href = "https://ai.szedu.cn/";
  143. a.target = '_blank';
  144. a.rel = 'noreferrer';
  145. document.body.appendChild(a);
  146. a.click();
  147. document.body.removeChild(a);
  148. // top.location.href = "https://zy.szedu.cn/ai/"
  149. })
  150. }
  151. }
  152. // 同步登录信息
  153. const userInfo2 = (data) => {
  154. let userIdentity = 1
  155. isShow.value = true
  156. CurrentRole.currentRole == data.user_role.join(",")
  157. Current.setUserCurrent(data)
  158. userIdentity = data.init_user_type == '1' ? 1 : 2
  159. let userGrand = ""
  160. let params = {
  161. openid: data.user_id,
  162. edu: true
  163. }
  164. axios.defaults.headers = { 'content-type': 'application/x-www-form-urlencoded' }
  165. axios.defaults.withCredentials = true;
  166. let diqu = `${data.last_top_org_name}&${data.init_v_platform_code}`
  167. top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/batchRegistrationSzdjg3", [data.name, data.user_id + '@szdjg.com', '16ace517-b5c7-4168-a9bb-a9e0035df840', data.last_top_org_name || "", data.phone, userGrand, userIdentity, data.user_id, diqu, data.user_id], function (r) {
  168. // console.log('rrrrr', r)
  169. axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(async d => {
  170. // store.user = top.US.userInfo
  171. // setStateUserinfo(d.data[0][0].userid)
  172. let userinfo = await selectUser3(d.data[0][0].userid)
  173. if(userinfo.eduid){
  174. setStateUserinfo(d.data[0][0].userid)
  175. }else {
  176. localStorage.setItem('userid', userinfo.userid)
  177. localStorage.setItem('isBjt', 1)
  178. linkLogin()
  179. }
  180. })
  181. }, [], { "type": "POST", "withCredentials": true });
  182. }
  183. const setStateUserinfo = (userid) => {
  184. selectUser(userid)
  185. // setTimeState
  186. // setTimeState.value = setInterval(() => {
  187. // if (JSON.stringify(top.US.userInfo) != '{}') {
  188. // // store.user = top.US.userInfo
  189. // settimeoutUserinfo()
  190. // clearInterval(setTimeState.value)
  191. // }
  192. // }, 1000)
  193. }
  194. const settimeoutUserinfo = () => {
  195. setTimeout(() => {
  196. isShow.value = false
  197. console.log("top.US.userInfo", top.US.userInfo)
  198. store.setUserInfo(top.US.userInfo)
  199. }, 1000)
  200. }
  201. // 优化selectUser,结合settimeoutUserinfo的逻辑,统一用户信息拉取和存储
  202. const selectUser = (uid) => {
  203. // 优先使用已存在的用户信息
  204. var userInfo = topUserInfo.value;
  205. var panId = userInfo && userInfo.accountNumber;
  206. if (panId) {
  207. return;
  208. }
  209. // 拉取用户信息并处理
  210. const requestUser = () => {
  211. top.U.A.Request(`${top.US.Config.pbl}selectUser?userid=${uid}`, [], function (res) {
  212. if (res && res.value && res.value.length && res.value[0].length && res.value[0][0].userid) {
  213. // 拉取成功,存储用户信息
  214. isShow.value = false;
  215. top.US.userInfo = res.value[0][0];
  216. topUserInfo.value = res.value[0][0];
  217. top.U.UF.Cookie.set("cocoroboLoginType=2");
  218. store.setUserInfo(res.value[0][0]);
  219. setTimeout(() => {
  220. top.US.userInfo = res.value[0][0];
  221. }, 5000)
  222. setTimeout(() => {
  223. top.US.userInfo = res.value[0][0];
  224. }, 60000);
  225. } else {
  226. top.U.alert("获取用户信息失败,正在重试!");
  227. requestUser();
  228. }
  229. }, [], { "type": "GET", "withCredentials": true });
  230. };
  231. try {
  232. requestUser();
  233. } catch (error) {
  234. top.U.alert("登录失败,正在重试!");
  235. requestUser();
  236. }
  237. }
  238. const selectUser2 = (uid) => {
  239. // 优先使用已存在的用户信息
  240. var userInfo = topUserInfo.value;
  241. var panId = userInfo && userInfo.accountNumber;
  242. if (panId) {
  243. return;
  244. }
  245. // 拉取用户信息并处理
  246. const requestUser = () => {
  247. axios.get(`https://pbl.cocorobo.cn/api/pbl/selectUser?userid=${uid}`, { withCredentials: true })
  248. .then(res => {
  249. console.log(res);
  250. if (res && res.data && res.data.length && res.data[0].length && res.data[0][0].userid) {
  251. // 拉取成功,存储用户信息
  252. isShow.value = false;
  253. topUserInfo.value = res.data[0][0];
  254. store.setUserInfo(res.data[0][0]);
  255. } else {
  256. requestUser();
  257. }
  258. })
  259. };
  260. try {
  261. requestUser();
  262. } catch (error) {
  263. requestUser();
  264. }
  265. }
  266. const selectUser3 = async (uid) => {
  267. // 拉取用户信息并处理
  268. const requestUser = async () => {
  269. try {
  270. const res = await axios.get(`https://pbl.cocorobo.cn/api/pbl/selectUser?userid=${uid}`, { withCredentials: true });
  271. console.log(res);
  272. if (res && res.data && res.data.length && res.data[0].length && res.data[0][0].userid) {
  273. // 拉取成功,存储用户信息
  274. let userinfo = res.data[0][0];
  275. return userinfo;
  276. } else {
  277. return await requestUser();
  278. }
  279. } catch (error) {
  280. return await requestUser();
  281. }
  282. };
  283. return await requestUser();
  284. }
  285. const setEduid = async (userid, eduId) => {
  286. const res = await axios.get(`https://pbl.cocorobo.cn/api/pbl/setEduid?userid=${userid}&eduid=${eduId}`);
  287. console.log(res);
  288. }
  289. const getUrlParam = (urlStr, urlKey) => {
  290. const url = new URL(urlStr) // 字符串转换成url格式
  291. const paramsStr = url.search.slice(1) // 获取'?'后面的参数字符串
  292. const paramsArr = paramsStr.split('&') // 分割'&'字符 获得参数数组
  293. for (let i = 0; i < paramsArr.length; i++) {
  294. const tempArr = paramsArr[i].split('=')
  295. if (tempArr[0] === urlKey) {
  296. return tempArr[1]
  297. }
  298. }
  299. }
  300. </script>
  301. <template>
  302. <!-- <keep-alive> -->
  303. <div v-loading="isShow" style="height: 100vh;">
  304. <router-view></router-view>
  305. </div>
  306. <!-- </keep-alive> -->
  307. </template>
  308. <style scoped></style>