preCheck.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import platform from '@/utils/platform.js'
  2. import config from '@/config'
  3. import getEnv from "@/utils/env"
  4. import {
  5. getUrlParamsNormal
  6. } from '@/utils/utils.js'
  7. // import {
  8. // getOpenId,
  9. // getOpenIdAndLogin,
  10. // } from '@/apis/index.js'
  11. // import {
  12. // login
  13. // } from '@/apis/user'
  14. // import {
  15. // logout
  16. // } from '@/apis/information.js'
  17. const apisFilter = (station, data) => {
  18. return new Promise(resolve => {
  19. if (station === '1') {
  20. getOpenIdAndLogin(data).then((res) => {
  21. resolve(res)
  22. })
  23. } else {
  24. getOpenId(data).then((res) => {
  25. resolve(res)
  26. })
  27. }
  28. })
  29. }
  30. if (platform() === 'H5') {
  31. const getWxQuery = (name) => {
  32. return new URL(window.location.href).searchParams.get(name + '')
  33. }
  34. let openIdSt = ''
  35. let station = getUrlParamsNormal('stationType') || uni.getStorageSync('stationType')
  36. if (getEnv() === 'test') {
  37. const opId = getUrlParamsNormal('openId') || uni.getStorageSync('openId')
  38. uni.setStorageSync('openId', opId)
  39. if (station) {
  40. uni.setStorageSync('stationType', station)
  41. }
  42. openIdSt = opId || uni.getStorageSync('openId')
  43. } else {
  44. openIdSt = uni.getStorageSync('openId')
  45. if (station) {
  46. uni.setStorageSync('stationType', station)
  47. }
  48. }
  49. if (!openIdSt) {
  50. const code = getWxQuery('code')
  51. if (code) {
  52. apisFilter(station, {
  53. code: code
  54. }).then(res => {
  55. const {
  56. openId,
  57. customerVo,
  58. token,
  59. } = res
  60. uni.setStorageSync('openId', openId)
  61. const userInfo = uni.getStorageSync('userInfo')
  62. if (token && token.accessToken) { // 登录成功 后更新用户信息
  63. uni.setStorageSync('accessToken', token.accessToken)
  64. uni.setStorageSync('tokenType', token.tokenType)
  65. uni.setStorageSync('userInfo', customerVo)
  66. }
  67. if (!token) { // 未登录过
  68. const channel = getUrlParamsNormal('channel')
  69. if (channel === 'qrcode') { // 扫码进入的页面
  70. uni.redirectTo({
  71. url: '/pages/main/index'
  72. })
  73. } else { // 进入登录
  74. uni.redirectTo({
  75. url: '/pages/login/index'
  76. })
  77. }
  78. }
  79. })
  80. } else {
  81. uni.setStorageSync('indexHref', window.location.href)
  82. if (getEnv() !== 'mock') {
  83. const state = 'recruit'
  84. const redirect_uri = encodeURIComponent(window.location.href)
  85. const appid = config.appid
  86. const scope = 'snsapi_base'
  87. window.location.href =
  88. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`
  89. }
  90. }
  91. } else {
  92. uni.setStorageSync('indexHref', window.location.href)
  93. const userInfo = uni.getStorageSync('userInfo')
  94. if ((userInfo.type === 0 && station === '0') || (userInfo.type !== 0 && station === '1')) { // 站点切团长
  95. logout().then(() => {
  96. uni.removeStorageSync('accessToken')
  97. uni.removeStorageSync('tokenType')
  98. uni.removeStorageSync('userInfo')
  99. uni.navigateTo({
  100. url: '/pages/login/index'
  101. })
  102. })
  103. }
  104. }
  105. }