App.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 } from './stores/counter'
  9. const route = useRoute()
  10. const store = userInfoStore()
  11. const CurrentRole = userCurrentRole()
  12. const name = ref('')
  13. const isShow = ref(true)
  14. const setTimeState = ref(null)
  15. onMounted(() => {
  16. // 通过 grant_code 获取登录信息
  17. let isGrantCode = getUrlParam(window.parent.location.href, 'grant_code')
  18. axios.defaults.headers = { 'content-type': 'application/json;charset=utf-8' }
  19. if (isGrantCode) {
  20. let params = {
  21. grantCode: isGrantCode,
  22. redirectUri: "https://cloud.cocorobo.cn/testapi"
  23. }
  24. axios.post('//cloud.cocorobo.cn/api/getAccessToken', JSON.stringify(params)).then(res => {
  25. // console.log(res)
  26. if (res.data.code == 0) {
  27. let accessTokenParams = {
  28. accessToken: res.data.data.accessToken
  29. }
  30. axios.post('//cloud.cocorobo.cn/api/getUserInfo', JSON.stringify(accessTokenParams)).then(r => {
  31. // console.log(r, r.data.code, r.data.data)
  32. if (r.data.code == 0) {
  33. // store.user = r.data.data
  34. name.value = r.data.data.userName
  35. userInfo(r.data.data)
  36. }
  37. })
  38. } else {
  39. linkLogin()
  40. }
  41. })
  42. } else {
  43. linkLogin()
  44. }
  45. })
  46. const linkLogin = () => {
  47. 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`
  48. }
  49. // 同步登录信息
  50. const userInfo = (data) => {
  51. // areaAdministrator - 安全管理员
  52. // userAdministrator - 用户管理员(市区级)
  53. // securityAuditor - 安全审计员(市区级)
  54. // schoolAdministrator - 用户管理员(校级)
  55. // schoolSecurityAuditor - 安全审计员(校级)
  56. // edupersonnel - 单位用户
  57. // teacher - 教师
  58. // visitorstudent - 学生
  59. // visitorparent - 家长
  60. // visitor - 访客
  61. // parentagency - 家长代办
  62. let userIdentity = 1
  63. if (data.currentRole == "edupersonnel" || data.currentRole == "areaAdministrator" || data.currentRole == "userAdministrator" || data.currentRole == "securityAuditor" || data.currentRole == "schoolAdministrator" || data.currentRole == "schoolSecurityAuditor" || data.currentRole == "teacher") {
  64. isShow.value = true
  65. CurrentRole.currentRole == data.currentRole
  66. userIdentity = 1
  67. let userGrand = ""
  68. if (data.childList && data.childList.length > 0) {
  69. userGrand = data.childList[0].gradeName + data.childList[0].className
  70. }
  71. let params = {
  72. openid: data.userId,
  73. edu: true
  74. }
  75. axios.defaults.headers = { 'content-type': 'application/x-www-form-urlencoded' }
  76. axios.defaults.withCredentials = true;
  77. axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(res => {
  78. // console.log(res, '222222222222')
  79. if (res.data && res.data[0][0].active == 1) {
  80. setStateUserinfo()
  81. } else {
  82. if (data.telephoneNumber) {
  83. top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/batchRegistrationSzdjg", [data.userName, data.telephoneNumber + '@szdjg.com', '16ace517-b5c7-4168-a9bb-a9e0035df840', data.schoolName, data.telephoneNumber, userGrand, userIdentity, data.userId], function (r) {
  84. // console.log('rrrrr', r)
  85. axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(d => {
  86. // store.user = top.US.userInfo
  87. setStateUserinfo()
  88. })
  89. }, [], { "type": "POST", "withCredentials": true });
  90. } else {
  91. isShow.value = false
  92. ElMessageBox.alert('没有绑定手机号,请先去绑定手机号', "提示", {
  93. confirmButtonText: '确定',
  94. })
  95. }
  96. }
  97. }).catch(e => {
  98. if (data.telephoneNumber) {
  99. top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/batchRegistrationSzdjg", [data.userName, data.telephoneNumber + '@szdjg.com', '16ace517-b5c7-4168-a9bb-a9e0035df840', data.schoolName || "", data.telephoneNumber, userGrand, userIdentity, data.userId], function (r) {
  100. // console.log('rrrrr', r)
  101. axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(d => {
  102. // store.user = top.US.userInfo
  103. if (d.data && d.data[0][0].active == 1) {
  104. isShow.value = false
  105. setStateUserinfo()
  106. }
  107. })
  108. }, [], { "type": "POST", "withCredentials": true });
  109. } else {
  110. isShow.value = false
  111. ElMessageBox.alert('没有绑定手机号,请先去绑定手机号', "提示", {
  112. confirmButtonText: '确定',
  113. })
  114. }
  115. })
  116. } else {
  117. isShow.value = false
  118. ElMessageBox.alert('当前账号无使用平台权限,如需体验,您需申请体验账号,查看平台内容', "温馨提示", {
  119. confirmButtonText: '确定',
  120. showClose: false
  121. }).then(() => {
  122. top.location.href = "https://zy.szedu.cn/ai/"
  123. })
  124. }
  125. }
  126. const setStateUserinfo = () => {
  127. // setTimeState
  128. setTimeState.value = setInterval(() => {
  129. if (JSON.stringify(top.US.userInfo) != '{}') {
  130. // store.user = top.US.userInfo
  131. settimeoutUserinfo()
  132. clearInterval(setTimeState.value)
  133. }
  134. }, 1000)
  135. }
  136. const settimeoutUserinfo = () => {
  137. setTimeout(() => {
  138. isShow.value = false
  139. console.log("top.US.userInfo", top.US.userInfo)
  140. store.user = top.US.userInfo
  141. }, 3000)
  142. }
  143. const getUrlParam = (urlStr, urlKey) => {
  144. const url = new URL(urlStr) // 字符串转换成url格式
  145. const paramsStr = url.search.slice(1) // 获取'?'后面的参数字符串
  146. const paramsArr = paramsStr.split('&') // 分割'&'字符 获得参数数组
  147. for (let i = 0; i < paramsArr.length; i++) {
  148. const tempArr = paramsArr[i].split('=')
  149. if (tempArr[0] === urlKey) {
  150. return tempArr[1]
  151. }
  152. }
  153. }
  154. </script>
  155. <template>
  156. <!-- <keep-alive> -->
  157. <div v-loading="isShow" style="height: 100%;">
  158. <router-view></router-view>
  159. </div>
  160. <!-- </keep-alive> -->
  161. </template>
  162. <style scoped></style>