App.vue 11 KB

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