feat(apply): 登录改为手机号手输,移除微信手机号组件依赖

This commit is contained in:
lik
2026-11-10 20:51:03 +08:00
parent e6ec051441
commit 3bca677f30
3 changed files with 103 additions and 88 deletions
+54 -73
View File
@@ -19,7 +19,8 @@ Page({
loading: true,
view: 'form', // form 填写 / pending 待审核 / approved 已通过但角色未刷新
editing: false, // 待审核期间修改基本信息
needPhoneAuth: false, // 手机号已有平台账号,需验证微信手机号后并入
loggedIn: false, // 是否已完成微信登录
loggingIn: false, // 微信登录请求中
submitting: false,
rejectReason: '',
application: null,
@@ -49,7 +50,7 @@ Page({
refresh() {
// 修改资料时前后台切换不重置表单
if (this.data.editing) return
this.setData({ loading: true, needPhoneAuth: false })
this.setData({ loading: true })
role
.ensureRoleSafe(true)
.then((detail) => {
@@ -66,24 +67,32 @@ Page({
const app = getApp()
const user = app && app.globalData ? app.globalData.user : null
const accountName = user && user.profile && user.profile.name ? user.profile.name : '微信用户'
const loggedIn = !!(user && user.security && user.security.token)
API.attendantApplication
.getMine()
.then((res) => {
if (res.code !== 0) {
// 未登录(新用户走到这里时还没有账号)不弹错误,直接展示表单
// 未登录(新用户走到这里时还没有账号)不弹错误,直接展示登录入口
if (res.code !== 401) {
wx.showToast({ title: res.msg || '加载失败', icon: 'none' })
} else {
console.info('[apply] 未登录,展示申请表单')
console.info('[apply] 未登录,展示登录入口')
}
this.setData({ loading: false, accountName })
this.setData({ loading: false, accountName, loggedIn })
return
}
const application = res.data ? res.data.application : null
if (!application) {
this.setData({ loading: false, view: 'form', accountName })
// 新账号的手机号/姓名来自微信登录,表单为空时自动带出
const profile = (user && user.profile) || {}
const patch = { loading: false, view: 'form', accountName, loggedIn }
if (!this.data.form.mobile.trim() && profile.mobile) patch['form.mobile'] = profile.mobile
if (!this.data.form.name.trim() && profile.name && profile.name !== profile.mobile) {
patch['form.name'] = profile.name
}
this.setData(patch)
return
}
@@ -92,6 +101,7 @@ Page({
loading: false,
view: 'pending',
editing: false,
loggedIn: true,
application,
accountName,
serviceAreaText: (application.applicant.serviceAreas || []).map(cityLabel).join('、'),
@@ -102,7 +112,7 @@ Page({
if (application.status === 'approved') {
// 走到这里说明员工档案还没查到(接口抖动或数据延迟),给一个手动重试入口
this.setData({ loading: false, view: 'approved', application, accountName })
this.setData({ loading: false, view: 'approved', loggedIn: true, application, accountName })
return
}
@@ -112,6 +122,7 @@ Page({
this.setData({
loading: false,
view: 'form',
loggedIn: true,
application,
accountName,
rejectReason: application.rejectReason || '',
@@ -126,7 +137,7 @@ Page({
})
})
.catch(() => {
this.setData({ loading: false, accountName })
this.setData({ loading: false, accountName, loggedIn })
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
})
},
@@ -200,83 +211,66 @@ Page({
},
/**
* 微信手机号快速验证后并入已有账号
* 第一步:手机号登录
*
* phoneCode 是一次性凭证,只能交给 wxSignin 在服务端消费一次;
* 成功后账号已绑定当前微信,继续走正常提交
* 陪诊师端没有开通「手机号快速验证」组件,手机号由用户自己输入;
* 后端在「手机号 + 姓名」与已有账号完全一致时,会把当前微信并入该账号
*/
onPhoneAuth(e) {
const phoneCode = e.detail && e.detail.code ? e.detail.code : ''
if (!phoneCode) {
console.warn('[apply] 未获得手机号授权:', e.detail && e.detail.errMsg)
wx.showToast({ title: '需要授权手机号才能继续', icon: 'none' })
return
}
if (this.data.submitting) return
this.setData({ submitting: true })
onLogin() {
const name = this.data.form.name.trim()
const mobile = this.data.form.mobile.trim()
if (!name) return this.toast('请填写姓名')
if (!MOBILE_REGEX.test(mobile)) return this.toast('请填写正确的手机号')
if (this.data.loggingIn) return
this.setData({ loggingIn: true })
wx.login({
success: (res) => {
if (!res.code) {
this.setData({ submitting: false })
this.setData({ loggingIn: false })
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
return
}
API.user
.wxSignin({
phoneCode,
code: res.code,
name: this.data.form.name.trim()
})
.wxSignin({ code: res.code, phoneNumber: mobile, name })
.then((data) => {
if (data.code !== 0) {
this.setData({ submitting: false })
wx.showToast({ title: data.msg || '登录失败', icon: 'none' })
return
}
// 账号已并入当前微信,先落全局用户,后续提交直接复用该身份
const app = getApp()
app.globalData.user = data.data.user
app.eventBus.emit('user-login', data.data.user)
// 验证回来的号码与填写不一致时,以微信验证的号码为准,避免把申请挂到别人的手机号上
const verifiedMobile =
(data.data.user && data.data.user.profile && data.data.user.profile.mobile) || ''
const formMobile = this.data.form.mobile.trim()
if (verifiedMobile && formMobile && verifiedMobile !== formMobile) {
this.setData({
submitting: false,
needPhoneAuth: false,
'form.mobile': verifiedMobile
})
this.setData({ loggingIn: false })
wx.showModal({
title: '手机号已更新',
content: `当前微信验证的手机号为 ${verifiedMobile},与填写的 ${formMobile} 不一致,已自动改用验证手机号,请确认后重新提交。`,
title: '登录失败',
content: data.msg || '请重试',
showCancel: false
})
return
}
// submit 内部有 submitting 守卫,先复位再继续提交
this.setData({ submitting: false, needPhoneAuth: false })
this.submit()
const app = getApp()
app.globalData.user = data.data.user
app.eventBus.emit('user-login', data.data.user)
// 清掉登录失败时缓存的角色结果,登录成功后重新解析
role.resetRole()
// 并入已有账号时,姓名与手机号以账号里的为准
const profile = (data.data.user && data.data.user.profile) || {}
const form = { ...this.data.form }
if (profile.mobile) form.mobile = profile.mobile
if (profile.name && profile.name !== profile.mobile) form.name = profile.name
this.setData({ loggingIn: false, loggedIn: true, form })
wx.showToast({ title: '登录成功', icon: 'success' })
this.loadApplication()
})
.catch((err) => {
console.warn('[apply] 手机号验证登录失败:', err)
this.setData({ submitting: false })
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
console.warn('[apply] 登录失败:', err)
this.setData({ loggingIn: false })
wx.showToast({ title: (err && err.message) || '网络错误,请重试', icon: 'none' })
})
},
fail: () => {
this.setData({ submitting: false })
this.setData({ loggingIn: false })
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
}
})
},
cancelPhoneAuth() {
this.setData({ needPhoneAuth: false })
},
/**
* 待审核 → 修改模式:回填已提交的基本信息
*/
@@ -407,21 +401,8 @@ Page({
const msg = err && err.message ? err.message : ''
console.warn('[apply] 提交失败:', msg)
this.setData({ submitting: false })
// 登录环节的业务拒绝(手机号已被注册/微信已绑定其他账号)需要弹窗说明
if (/已注册/.test(msg)) {
// 手机号属于已有平台账号(如就诊人端注册过):手动填写的手机号不可信,
// 必须走微信手机号快速验证,由服务端确认号码归属后并入原账号
wx.showModal({
title: '手机号已有账号',
content: `${msg}。可点击「验证手机号」用当前微信绑定的手机号完成验证后继续。`,
confirmText: '验证手机号',
success: (res) => {
if (res.confirm) this.setData({ needPhoneAuth: true })
}
})
return
}
if (/已绑定|手机号/.test(msg)) {
// 登录环节的业务拒绝(手机号已注册且姓名不一致/微信已绑定其他账号)需要弹窗说明
if (/已注册|已绑定|手机号/.test(msg)) {
wx.showModal({
title: '无法提交',
content: msg,