mirror of
https://gitee.com/ShopeX/ECShopX_mobile-frontend
synced 2026-08-07 21:15:47 +08:00
bug(custom): s引入
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
import Taro, { getCurrentInstance } from '@tarojs/taro'
|
||||
import qs from 'qs'
|
||||
import S from '@/spx'
|
||||
import spxInstance from '@/spx'
|
||||
import {
|
||||
isAlipay,
|
||||
isWeixin,
|
||||
@@ -17,6 +17,15 @@ import {
|
||||
import log from '@/utils/log'
|
||||
import { HTTP_STATUS } from './consts'
|
||||
|
||||
// 惰性获取 spx:req 与 spx 存在循环依赖(spx→api→req→spx),加载时 spxInstance 可能尚未就绪
|
||||
function getS() {
|
||||
return (
|
||||
(typeof global !== 'undefined' && global.__SPX__) ||
|
||||
(typeof globalThis !== 'undefined' && globalThis.__SPX__) ||
|
||||
spxInstance
|
||||
)
|
||||
}
|
||||
|
||||
function addQuery(url, query) {
|
||||
return url + (url.indexOf('?') >= 0 ? '&' : '?') + query
|
||||
}
|
||||
@@ -109,18 +118,8 @@ class API {
|
||||
}
|
||||
this.baseURL = baseURL
|
||||
|
||||
// 从域名获取 company_id,例如 m38.shopex123.com -> 38
|
||||
let company_id = process.env.APP_COMPANY_ID
|
||||
if (isWeb && typeof global !== 'undefined' && global.location) {
|
||||
const hostname = global.location.hostname
|
||||
const match = hostname.match(/^m(\d+)\./)
|
||||
if (match && match[1]) {
|
||||
company_id = match[1]
|
||||
}
|
||||
}
|
||||
|
||||
const options = {
|
||||
company_id: company_id
|
||||
company_id: process.env.APP_COMPANY_ID
|
||||
}
|
||||
if (isWeixin) {
|
||||
const extConfig = getExtConfigData()
|
||||
@@ -154,7 +153,7 @@ class API {
|
||||
handleLogout() {
|
||||
this.requestQueue.destroy()
|
||||
this.isRefreshingToken = false
|
||||
S.logout()
|
||||
getS().logout()
|
||||
setTimeout(() => {
|
||||
console.log(getCurrentInstance().router)
|
||||
const { path, params } = getCurrentInstance().router
|
||||
@@ -187,7 +186,7 @@ class API {
|
||||
if (company_id) {
|
||||
query['company_id'] = company_id
|
||||
}
|
||||
const lang = Taro.getStorageSync('lang') || process.env.APP_DEFAULT_LANGUAGE
|
||||
const lang = Taro.getStorageSync('lang')
|
||||
if (lang) {
|
||||
const langMap = {
|
||||
zhcn: 'zh-CN',
|
||||
@@ -195,13 +194,13 @@ class API {
|
||||
zhtw: 'zh-TW',
|
||||
ar: 'ar-SA'
|
||||
}
|
||||
query['country_code'] = langMap[lang]
|
||||
query['country_code'] = langMap[lang || process.env.APP_COUNTRY_CODE]
|
||||
}
|
||||
if (!methodIsGet) {
|
||||
header['content-type'] = header['content-type'] || 'application/x-www-form-urlencoded'
|
||||
}
|
||||
|
||||
const token = S.getAuthToken()
|
||||
const token = getS().getAuthToken()
|
||||
if (token) {
|
||||
header['Authorization'] = `Bearer ${token}`
|
||||
}
|
||||
@@ -280,7 +279,7 @@ class API {
|
||||
|
||||
async refreshToken() {
|
||||
this.isRefreshingToken = true
|
||||
const token = S.getAuthToken()
|
||||
const token = getS().getAuthToken()
|
||||
console.log('refreshToken', 66)
|
||||
try {
|
||||
await this.makeReq(
|
||||
@@ -300,7 +299,7 @@ class API {
|
||||
}
|
||||
|
||||
const newToken = res.header.Authorization.split(' ')[1]
|
||||
S.setAuthToken(newToken)
|
||||
getS().setAuthToken(newToken)
|
||||
}
|
||||
)
|
||||
} catch (e) {
|
||||
@@ -338,7 +337,7 @@ class API {
|
||||
if (
|
||||
res.statusCode === HTTP_STATUS.UNAUTHORIZED &&
|
||||
(res.data.data && res.data.data.code) === HTTP_STATUS.TOKEN_NEEDS_REFRESH &&
|
||||
S.getAuthToken()
|
||||
getS().getAuthToken()
|
||||
) {
|
||||
// token失效时重造请求,并刷新token
|
||||
if (!this.isRefreshingToken) {
|
||||
|
||||
@@ -3,12 +3,13 @@
|
||||
* See LICENSE file for license details.
|
||||
*/
|
||||
import Taro, { getCurrentInstance } from '@tarojs/taro'
|
||||
import api from '@/api'
|
||||
import { isWeixin, isAlipay, log, isGoodsShelves, showToast, isMerchantModule } from '@/utils'
|
||||
import { SG_TOKEN, SG_USER_INFO, MERCHANT_TOKEN } from '@/consts/localstorage'
|
||||
import dayjs from 'dayjs'
|
||||
import qs from 'qs'
|
||||
import configStore from '@/store'
|
||||
// 按需加载 api,避免 spx→api→req→spx 循环依赖导致冷启动时 S 为 undefined
|
||||
const getApi = () => require('@/api').default
|
||||
|
||||
const { store } = configStore()
|
||||
|
||||
@@ -136,7 +137,7 @@ export class Spx {
|
||||
desc: '用于完善会员资料',
|
||||
success: async (data) => {
|
||||
const { userInfo } = data
|
||||
await api.member.updateMemberInfo({
|
||||
await getApi().member.updateMemberInfo({
|
||||
username: userInfo.nickName,
|
||||
avatar: userInfo.avatarUrl
|
||||
})
|
||||
@@ -155,7 +156,7 @@ export class Spx {
|
||||
|
||||
// 获取会员信息
|
||||
async getMemberInfo() {
|
||||
const userInfo = await api.member.memberInfo()
|
||||
const userInfo = await getApi().member.memberInfo()
|
||||
store.dispatch({
|
||||
type: 'user/updateUserInfo',
|
||||
payload: userInfo
|
||||
@@ -205,7 +206,7 @@ export class Spx {
|
||||
async login(ctx, isRedirect = false) {
|
||||
let code, token
|
||||
if (isWeixin) {
|
||||
let { update_time } = await api.wx.getPrivacyTime()
|
||||
let { update_time } = await getApi().wx.getPrivacyTime()
|
||||
let privacy_time = Taro.getStorageSync('policy_updatetime')
|
||||
if (!String(privacy_time) || privacy_time != update_time) {
|
||||
return true
|
||||
@@ -214,7 +215,7 @@ export class Spx {
|
||||
const resLogin = (await Taro.login()) || {}
|
||||
code = resLogin.code
|
||||
try {
|
||||
const tokenLogin = (await api.wx.login({ code })) || {}
|
||||
const tokenLogin = (await getApi().wx.login({ code })) || {}
|
||||
token = tokenLogin.token
|
||||
} catch (e) {
|
||||
return true
|
||||
@@ -222,7 +223,7 @@ export class Spx {
|
||||
} else if (isAlipay) {
|
||||
const authLogin = await my.getAuthCode({ scopes: ['auth_base'] })
|
||||
code = authLogin.authCode
|
||||
const tokenLogin = await api.alipay.login({ code })
|
||||
const tokenLogin = await getApi().alipay.login({ code })
|
||||
token = tokenLogin.token
|
||||
}
|
||||
if (token) {
|
||||
@@ -247,14 +248,14 @@ export class Spx {
|
||||
async loginQW(ctx) {
|
||||
console.log('[loginQW] 企微登录 执行')
|
||||
let { code } = await this.getQyLoginCode()
|
||||
const QwUserInfo = await api.user.getQwUserInfo({
|
||||
const QwUserInfo = await getApi().user.getQwUserInfo({
|
||||
appname: `${APP_NAME}`,
|
||||
code
|
||||
})
|
||||
let { salesperson_id, distributor_id, session3rd } = QwUserInfo
|
||||
this.setAuthToken(session3rd)
|
||||
//查询当前导购门店信息是否有效
|
||||
const { status } = await api.guide.is_valid({
|
||||
const { status } = await getApi().guide.is_valid({
|
||||
salesperson_id,
|
||||
distributor_id
|
||||
})
|
||||
@@ -295,7 +296,7 @@ export class Spx {
|
||||
let uvstamp = Taro.getStorageSync('userVisitTime')
|
||||
let today = formatDateTime(new Date())
|
||||
if (!uvstamp || (uvstamp && new Date(today).getTime() > uvstamp)) {
|
||||
api.user.getuservisit()
|
||||
getApi().user.getuservisit()
|
||||
uvstamp = new Date(today).getTime() + 5 * 60 * 1000
|
||||
Taro.setStorageSync('userVisitTime', uvstamp)
|
||||
}
|
||||
@@ -341,4 +342,12 @@ export class Spx {
|
||||
}
|
||||
}
|
||||
|
||||
export default new Spx()
|
||||
const instance = new Spx()
|
||||
// 冷启动从公众号直接打开分包页时,主包未先执行可能导致 import S 未就绪,挂到全局供 req 等使用
|
||||
if (typeof global !== 'undefined') {
|
||||
global.__SPX__ = instance
|
||||
}
|
||||
if (typeof globalThis !== 'undefined') {
|
||||
globalThis.__SPX__ = instance
|
||||
}
|
||||
export default instance
|
||||
|
||||
Reference in New Issue
Block a user