import platform from '@/utils/platform.js' import config from '@/config' import getEnv from "@/utils/env" import { getUrlParamsNormal } from '@/utils/utils.js' import { getOpenId, getOpenIdAndLogin, } from '@/apis/index.js' import { login } from '@/apis/user' import { logout } from '@/apis/information.js' const apisFilter = (station, data) => { return new Promise(resolve => { if (station === '1') { getOpenIdAndLogin(data).then((res) => { resolve(res) }) } else { getOpenId(data).then((res) => { resolve(res) }) } }) } if (platform() === 'H5') { const getWxQuery = (name) => { return new URL(window.location.href).searchParams.get(name + '') } let openIdSt = '' let station = getUrlParamsNormal('stationType') || uni.getStorageSync('stationType') if (getEnv() === 'test') { const opId = getUrlParamsNormal('openId') || uni.getStorageSync('openId') uni.setStorageSync('openId', opId) if (station) { uni.setStorageSync('stationType', station) } openIdSt = opId || uni.getStorageSync('openId') } else { openIdSt = uni.getStorageSync('openId') if (station) { uni.setStorageSync('stationType', station) } } if (!openIdSt) { const code = getWxQuery('code') if (code) { apisFilter(station, { code: code }).then(res => { const { openId, customerVo, token, } = res uni.setStorageSync('openId', openId) const userInfo = uni.getStorageSync('userInfo') if (token && token.accessToken) { // 登录成功 后更新用户信息 uni.setStorageSync('accessToken', token.accessToken) uni.setStorageSync('tokenType', token.tokenType) uni.setStorageSync('userInfo', customerVo) } if (!token) { // 未登录过 const channel = getUrlParamsNormal('channel') if (channel === 'qrcode') { // 扫码进入的页面 uni.redirectTo({ url: '/pages/main/index' }) } else { // 进入登录 uni.redirectTo({ url: '/pages/login/index' }) } } }) } else { uni.setStorageSync('indexHref', window.location.href) if (getEnv() !== 'mock') { const state = 'recruit' const redirect_uri = encodeURIComponent(window.location.href) const appid = config.appid const scope = 'snsapi_base' window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect` } } } else { uni.setStorageSync('indexHref', window.location.href) const userInfo = uni.getStorageSync('userInfo') if ((userInfo.type === 0 && station === '0') || (userInfo.type !== 0 && station === '1')) { // 站点切团长 logout().then(() => { uni.removeStorageSync('accessToken') uni.removeStorageSync('tokenType') uni.removeStorageSync('userInfo') uni.navigateTo({ url: '/pages/login/index' }) }) } } }