Files
Wechat/utils/fighter-only.js

154 lines
4.4 KiB
JavaScript

/**
* 纯打手端开关:点单端页面保留但不作为入口/回落目标。
* 回退:git checkout backup/before-fighter-only-20260727
* 或:git reset --hard backup-fighter-only-20260727
*/
export const FIGHTER_ONLY = true
/** 无邀请码搜索进入时,自动注册绑定的管事邀请码(前端硬编码,仍正常传给后端) */
export const FIGHTER_ONLY_DEFAULT_INVITE = '0000008VfJKPu4Spmcjt'
/** 已认证打手冷启动默认页 */
export const FIGHTER_HOME = '/pages/accept-order/accept-order'
/** 未登录 / 未认证 / 清缓存后的落地页(注册 + 我的) */
export const FIGHTER_MINE = '/pages/fighter/fighter'
/** 清除缓存后禁止自动登录/自动注册(用户手动点登录后再清) */
export const SKIP_AUTO_AUTH_KEY = 'fighter_skip_auto_auth'
/** 登录页提示文案 */
export const LOGIN_HINT_KEY = 'login_page_hint'
const ROLE_STATUS_KEYS = [
'dashoustatus',
'guanshistatus',
'shangjiastatus',
'zuzhangstatus',
'kaoheguanstatus',
]
const AUTH_CACHE_KEYS = [
'token',
'userInfo',
'uid',
'nicheng',
'touxiang',
'phone',
'dsnc',
'dsjieshao',
'dsdianhua',
'dsweixin',
...ROLE_STATUS_KEYS,
]
export function isFighterOnly() {
return FIGHTER_ONLY === true
}
export function isDashouCertified() {
if (!wx.getStorageSync('token')) return false
return Number(wx.getStorageSync('dashoustatus')) === 1
}
/** 本地是否标记为账号封禁(会话清理后仍可提示) */
export function isAccountBannedLocal() {
try {
const st = wx.getStorageSync('zhanghaostatus')
if (st !== '' && st !== undefined && st !== null && Number(st) !== 1) return true
} catch (e) {}
return false
}
export function markSkipAutoAuth() {
try {
wx.setStorageSync(SKIP_AUTO_AUTH_KEY, '1')
} catch (e) {}
}
export function clearSkipAutoAuth() {
try {
wx.removeStorageSync(SKIP_AUTO_AUTH_KEY)
} catch (e) {}
}
export function shouldSkipAutoAuth() {
try {
return wx.getStorageSync(SKIP_AUTO_AUTH_KEY) === '1'
} catch (e) {
return false
}
}
/**
* 清登录态但保留动态配置(OSS 等)。
* 解决:只删 token、留下 dashoustatus=1 → 卡在「已登录我的」且请求全部假 401。
*/
export function clearAuthSession(opts = {}) {
const app = typeof getApp === 'function' ? getApp() : null
const hint = opts.hint || ''
try {
AUTH_CACHE_KEYS.forEach((k) => {
try { wx.removeStorageSync(k) } catch (e) {}
})
try { wx.removeStorageSync('zhanghaostatus') } catch (e) {}
if (hint) wx.setStorageSync(LOGIN_HINT_KEY, hint)
markSkipAutoAuth()
} catch (e) {}
if (app && app.globalData) {
app.globalData.token = ''
app.globalData.uid = ''
app.globalData.nicheng = ''
app.globalData.dashouNicheng = ''
app.globalData.loginPromise = null
app.globalData.currentUser = null
app.globalData._acceptOrderSessionReady = false
app.globalData._acceptOrderPageCache = null
app.globalData._acceptOrderPopupDone = false
app.globalData._dashouPhoneChecked = false
ROLE_STATUS_KEYS.forEach((k) => { app.globalData[k] = 0 })
}
}
/** 封禁 / 登录失效:清会话并回登录页 */
export function kickToLoginPage(hint) {
const msg =
hint ||
'账号状态异常,您的账号可能已被封禁。请点击一键登录重试;若仍无法进入,请联系客服。'
clearAuthSession({ hint: msg })
const url = getLoginPageUrl()
wx.reLaunch({
url,
fail: () => {
try { wx.redirectTo({ url }) } catch (e) {}
},
})
}
/** 冷启动应去的页面:未登录 / 未认证 → 登录页;已认证 → 抢单池 */
export function resolveFighterOnlyLaunchUrl() {
const token = wx.getStorageSync('token')
if (!token) return FIGHTER_MINE
if (isDashouCertified()) return FIGHTER_HOME
return FIGHTER_MINE
}
/** 是否允许进入抢单池(必须已登录且已是接单员) */
export function canEnterAcceptOrder() {
return !!wx.getStorageSync('token') && isDashouCertified()
}
/** 未登录 / 登录失效时的落地页(绝不再回点单端我的) */
export function getLoginPageUrl() {
return isFighterOnly() ? FIGHTER_MINE : '/pages/mine/mine'
}
/** 严格判断是否已开通商家老板身份 */
export function isShangjiaCertified() {
// 只认数字 1;排除 true/"true"/脏数据
const raw = wx.getStorageSync('shangjiastatus')
if (raw === true || raw === 'true') return false
return Number(raw) === 1
}