App.vue 6.5 KB

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