Files

56 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// pages/launch/index.js
// 启动页:冷启动先解析身份再决定去首页还是入驻申请页,
// 避免默认首页先渲染一帧再被全局守卫重定向。
//
// 特殊分支:从未使用过的新用户没有账号,wxSignin 只带 code 会返回「缺少手机号」,
// 这不是网络故障——直接进申请页,由申请页用表单里的姓名+手机号完成注册。
const role = require('../../utils/role.js')
Page({
data: {
error: ''
},
onShow() {
this.enter()
},
enter() {
this.setData({ error: '' })
const app = getApp()
// 先单独走一次登录,才能拿到失败原因(ensureRoleSafe 会把错误吞成 ok:false)
app.ensureLogin()
.then((user) => {
console.info('[launch] 登录成功', user && user.profile && user.profile.mobile)
return role.ensureRoleSafe(true)
})
.then((detail) => {
console.info('[launch] 角色解析:', detail.ok ? detail.role : '解析失败')
if (!detail.ok) {
this.setData({ error: '网络异常,请重试' })
return
}
if (detail.role === 'none') {
wx.reLaunch({ url: '/pages/apply/index' })
return
}
wx.switchTab({ url: '/pages/home/index' })
})
.catch((err) => {
const msg = err && err.message ? err.message : ''
console.warn('[launch] 进入失败:', msg)
// 新用户首次使用:后端返回「缺少手机号」,进申请页注册
if (msg.indexOf('手机号') > -1) {
console.info('[launch] 新用户未注册,转入申请页')
wx.reLaunch({ url: '/pages/apply/index' })
return
}
this.setData({ error: '网络异常,请重试' })
})
},
onRetry() {
this.enter()
}
})