preCheck.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if (!openIdSt) {
  46. const code = getWxQuery('code')
  47. console.log(code,'code');
  48. if (code) {
  49. getOpendId({ code: code }).then(res => {
  50. const { accessToken,username,wxOpenid } = res
  51. uni.setStorageSync('wxOpenid', wxOpenid)
  52. // const username = uni.getStorageSync('username')
  53. // setTimeout(() => {
  54. if (accessToken) { // 登录成功 后更新用户信息
  55. uni.setStorageSync('accessToken', accessToken)
  56. uni.setStorageSync('username', username)
  57. // uni.setStorageSync('wxOpenid', wxOpenid)
  58. // uni.setStorageSync('userInfo', ress)
  59. window.history.go(-1)
  60. }
  61. if (!accessToken) { // 未登录过
  62. // const channel = getUrlParamsNormal('channel')
  63. // if (channel === 'qrcode') { // 扫码进入的页面
  64. // uni.reLaunch({
  65. // url: '/pages/gallery/index'
  66. // })
  67. // } else { // 进入登录
  68. uni.redirectTo({
  69. url: '/pages/login/index'
  70. })
  71. }
  72. // }
  73. // },100)
  74. }).catch(err => {console.log(err,'err')})
  75. } else {
  76. uni.setStorageSync('indexHref', window.location.href)
  77. const state = ''
  78. const redirect_uri = window.encodeURIComponent(window.location.href)
  79. const appid = config.appid
  80. const scope = 'snsapi_base'
  81. window.location.href =
  82. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`
  83. }
  84. } else {
  85. uni.setStorageSync('indexHref', window.location.href)
  86. const userInfo = uni.getStorageSync('userInfo')
  87. if (!userInfo) { // 站点切团长
  88. // logout().then(() => {
  89. // uni.removeStorageSync('accessToken')
  90. // uni.removeStorageSync('tokenType')
  91. // uni.removeStorageSync('userInfo')
  92. uni.navigateTo({
  93. url: '/pages/login/index'
  94. })
  95. // })
  96. }
  97. }
  98. }