App.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 { userInfoStore, userCurrentRole } from './stores/counter'
  8. const route = useRoute()
  9. const store = userInfoStore()
  10. const CurrentRole = userCurrentRole()
  11. const name = ref('')
  12. onMounted(() => {
  13. // 通过 grant_code 获取登录信息
  14. let isGrantCode = getUrlParam(window.parent.location.href, 'grant_code')
  15. axios.defaults.headers = { 'content-type': 'application/json;charset=utf-8' }
  16. if (isGrantCode) {
  17. let params = {
  18. grantCode: isGrantCode,
  19. redirectUri: "https://cloud.cocorobo.cn/testapi"
  20. }
  21. axios.post('//cloud.cocorobo.cn/api/getAccessToken', JSON.stringify(params)).then(res => {
  22. console.log(res)
  23. let accessTokenParams = {
  24. accessToken: res.data.data.accessToken
  25. }
  26. axios.post('//cloud.cocorobo.cn/api/getUserInfo', JSON.stringify(accessTokenParams)).then(r => {
  27. console.log(r, r.data.code, r.data.data)
  28. if (r.data.code == 0) {
  29. // store.user = r.data.data
  30. name.value = r.data.data.userName
  31. userInfo(r.data.data)
  32. } else {
  33. }
  34. })
  35. })
  36. }
  37. })
  38. const linkLogin = () => {
  39. window.location.href = `http://szedutest.changyan.cn/thirdauth/oauth2/authorize?service=initService&scope=all&response_type=code&app_id=94c720f0634b4bad890dc9223de01166&redirect_uri=https://cloud.cocorobo.cn/testapi`
  40. }
  41. // 同步登录信息
  42. const userInfo = (data) => {
  43. // areaAdministrator - 安全管理员
  44. // userAdministrator - 用户管理员(市区级)
  45. // securityAuditor - 安全审计员(市区级)
  46. // schoolAdministrator - 用户管理员(校级)
  47. // schoolSecurityAuditor - 安全审计员(校级)
  48. // edupersonnel - 单位用户
  49. // teacher - 教师
  50. // visitorstudent - 学生
  51. // visitorparent - 家长
  52. // visitor - 访客
  53. // parentagency - 家长代办
  54. let userIdentity = 2
  55. CurrentRole.currentRole == data.currentRole
  56. if (data.currentRole == "areaAdministrator" || data.currentRole == "userAdministrator" || data.currentRole == "securityAuditor" || data.currentRole == "schoolAdministrator" || data.currentRole == "schoolSecurityAuditor" || data.currentRole == "teacher") {
  57. userIdentity = 1
  58. }
  59. let userGrand = ""
  60. if (data.childList && data.childList.length > 0) {
  61. userGrand = data.childList[0].gradeName + data.childList[0].className
  62. }
  63. let params = {
  64. openid: data.userId,
  65. edu: true
  66. }
  67. axios.defaults.headers = { 'content-type': 'application/x-www-form-urlencoded' }
  68. axios.defaults.withCredentials = true;
  69. axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(res => {
  70. console.log(res, '222222222222')
  71. if (res.data && res.data[0][0].active == 1) {
  72. let obj = res.data[0][0]
  73. obj.userName = name
  74. store.user = obj
  75. } else {
  76. 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) {
  77. console.log('rrrrr', r)
  78. axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(d => {
  79. console.log(d, 'ddddddd')
  80. let obj = d.data[0][0]
  81. obj.userName = name
  82. store.user = obj
  83. })
  84. }, [], { "type": "POST", "withCredentials": true });
  85. }
  86. }).catch(e => {
  87. 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) {
  88. console.log('rrrrr', r)
  89. axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(d => {
  90. let obj = d.data[0][0]
  91. obj.userName = name
  92. store.user = obj
  93. })
  94. }, [], { "type": "POST", "withCredentials": true });
  95. })
  96. }
  97. const getUrlParam = (urlStr, urlKey) => {
  98. const url = new URL(urlStr) // 字符串转换成url格式
  99. const paramsStr = url.search.slice(1) // 获取'?'后面的参数字符串
  100. const paramsArr = paramsStr.split('&') // 分割'&'字符 获得参数数组
  101. for (let i = 0; i < paramsArr.length; i++) {
  102. const tempArr = paramsArr[i].split('=')
  103. if (tempArr[0] === urlKey) {
  104. return tempArr[1]
  105. }
  106. }
  107. }
  108. </script>
  109. <template>
  110. <!-- <keep-alive> -->
  111. <router-view></router-view>
  112. <!-- </keep-alive> -->
  113. </template>
  114. <style scoped></style>