preCheck.js 3.3 KB

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