You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
2.7 KiB
52 lines
2.7 KiB
const RETURN_KEY = 'supply_return_after_identity'
|
|
export function supplyReturnPath(path) {
|
|
if (path === '/packages/booksubmit/appointment') return path
|
|
return /^\/packages\/supply\/(index|detail\?id=[1-9]\d*)$/.test(String(path || '')) ? path : '/packages/supply/index'
|
|
}
|
|
export function currentSupplyPath() {
|
|
const pages = getCurrentPages()
|
|
const page = pages[pages.length - 1]
|
|
return supplyReturnPath(page && page.route === 'packages/supply/detail' ? '/packages/supply/detail?id=' + (page.options || {}).id : '/packages/supply/index')
|
|
}
|
|
export async function loadSupplySession(vm) {
|
|
const session = await vm.$u.api.supplyDemandSession()
|
|
if (!session) throw new Error('读取登录状态失败')
|
|
const old = (vm.$store && vm.$store.state.vuex_user) || {}
|
|
vm.$u.vuex('vuex_user', session.user ? { ...(String(old.id) === String(session.user.id) ? old : {}), ...session.user } : {})
|
|
return session
|
|
}
|
|
export async function requireSupplyMember(vm) {
|
|
if (vm.supplyAuthChecking) return false
|
|
vm.supplyAuthChecking = true
|
|
try {
|
|
const session = await loadSupplySession(vm)
|
|
if (session.registered) { vm.supplyAuthVisible = false; return true }
|
|
vm.supplyAuthVisible = true
|
|
return false
|
|
} catch (error) {
|
|
vm.$u.toast('暂时无法确认登录状态,请稍后重试')
|
|
return false
|
|
} finally { vm.supplyAuthChecking = false }
|
|
}
|
|
export async function beginSupplyIdentity(vm, kind, returnPath) {
|
|
const targetPath = supplyReturnPath(returnPath || currentSupplyPath())
|
|
uni.setStorageSync(RETURN_KEY, targetPath)
|
|
// Only establish the WeChat session after the visitor chooses login or registration.
|
|
const result = await new Promise((resolve, reject) => uni.login({ provider: 'weixin', success: resolve, fail: reject }))
|
|
const auth = await vm.$u.api.login({ code: result.code })
|
|
if (!auth || !auth.token) throw new Error('微信登录失败')
|
|
vm.$u.vuex('vuex_token', auth.token)
|
|
const session = await loadSupplySession(vm)
|
|
if (session.registered) return returnToSupply(vm)
|
|
const target = kind === 'register' ? '/packages/register/index' : '/packages/register/login'
|
|
return new Promise((resolve, reject) => uni.navigateTo({ url: target + (targetPath === '/packages/booksubmit/appointment' ? '?from_booking=1' : '?from_supply=1'), success: resolve, fail: reject }))
|
|
}
|
|
export function returnToSupply(vm) {
|
|
const target = supplyReturnPath(uni.getStorageSync(RETURN_KEY))
|
|
// Refresh user information before returning so protected buttons work immediately.
|
|
return loadSupplySession(vm).then(session => {
|
|
if (!session.registered) throw new Error('请先完成登录或注册')
|
|
return new Promise((resolve, reject) => uni.reLaunch({ url: target, success: () => { uni.removeStorageSync(RETURN_KEY); resolve() }, fail: reject }))
|
|
})
|
|
}
|