preCheck.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. loginWx,
  9. } from '@/apis/user.js'
  10. // import {
  11. // addressFind
  12. // } from '@/apis/purchase.js'
  13. if (platform() === 'H5') {
  14. const getWxQuery = (name) => {
  15. return new URL(window.location.href).searchParams.get(name + '')
  16. }
  17. let openIdSt = ''
  18. if (getEnv() === 'dev') {
  19. const opId = getUrlParamsNormal('openId') || uni.getStorageSync('openId')
  20. uni.setStorageSync('openId', opId)
  21. openIdSt = opId || uni.getStorageSync('openId')
  22. } else {
  23. openIdSt = uni.getStorageSync('openId')
  24. }
  25. if (!openIdSt) {
  26. const code = getWxQuery('code')
  27. if (code) {
  28. loginWx({
  29. code: code
  30. }).then(res => {
  31. const {
  32. openId,
  33. customerVo,
  34. token,
  35. } = res
  36. uni.setStorageSync('openId', openId)
  37. const userInfo = uni.getStorageSync('userInfo')
  38. if (token && token.accessToken) { // 登录成功 后更新用户信息
  39. uni.setStorageSync('accessToken', token.accessToken)
  40. uni.setStorageSync('tokenType', token.tokenType)
  41. uni.setStorageSync('userInfo', customerVo)
  42. // 设置默认地址
  43. // addressFind().then(addrList => {
  44. // const defaultAddrIndex = addrList.findIndex(item => item.defaultFlag === 1);
  45. // defaultAddrIndex !== -1 && uni.setStorageSync('currentBuyerAddr', addrList[
  46. // defaultAddrIndex]);
  47. // })
  48. }
  49. if (!token) { // 未登录过
  50. // const channel = getUrlParamsNormal('channel')
  51. // if (channel === 'qrcode') { // 扫码进入的页面
  52. // uni.redirectTo({
  53. // url: '/pages/card/cardPhysical/index'
  54. // })
  55. // } else { // 进入登录
  56. // uni.redirectTo({
  57. // url: '/pages/login/index'
  58. // })
  59. // }
  60. uni.switchTab({
  61. url: '/pages/user/index'
  62. })
  63. }
  64. })
  65. } else {
  66. // uni.setStorageSync('indexHref', window.location.href)
  67. // if (getEnv() !== 'mock') {
  68. // const state = Math.random()
  69. // const redirect_uri = encodeURIComponent(window.location.href)
  70. // const appid = config.appid
  71. // const scope = 'snsapi_base'
  72. // window.location.href =
  73. // `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`
  74. // }
  75. }
  76. }
  77. }