preCheck.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. name,
  33. token,
  34. } = res
  35. if (token) { // 登录成功 后更新用户信息
  36. uni.setStorageSync('accessToken', token)
  37. uni.setStorageSync('name', name)
  38. // 设置默认地址
  39. // addressFind().then(addrList => {
  40. // const defaultAddrIndex = addrList.findIndex(item => item.defaultFlag === 1);
  41. // defaultAddrIndex !== -1 && uni.setStorageSync('currentBuyerAddr', addrList[
  42. // defaultAddrIndex]);
  43. // })
  44. }
  45. if (!token) { // 未登录过
  46. // const channel = getUrlParamsNormal('channel')
  47. // if (channel === 'qrcode') { // 扫码进入的页面
  48. // uni.redirectTo({
  49. // url: '/pages/card/cardPhysical/index'
  50. // })
  51. // } else { // 进入登录
  52. // uni.redirectTo({
  53. // url: '/pages/login/index'
  54. // })
  55. // }
  56. uni.switchTab({
  57. url: '/pages/user/index'
  58. })
  59. }
  60. })
  61. } else {
  62. // uni.setStorageSync('indexHref', window.location.href)
  63. // if (getEnv() !== 'mock') {
  64. // const state = Math.random()
  65. // const redirect_uri = encodeURIComponent(window.location.href)
  66. // const appid = config.appid
  67. // const scope = 'snsapi_base'
  68. // window.location.href =
  69. // `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`
  70. // }
  71. }
  72. }
  73. }