feat(apply): 手机号冲突走微信验证并号,待审核支持修改资料

This commit is contained in:
lik
2026-11-10 20:03:16 +08:00
parent ff1b1664fa
commit e6ec051441
7 changed files with 362 additions and 22 deletions
+226 -6
View File
@@ -18,6 +18,8 @@ Page({
data: {
loading: true,
view: 'form', // form 填写 / pending 待审核 / approved 已通过但角色未刷新
editing: false, // 待审核期间修改基本信息
needPhoneAuth: false, // 手机号已有平台账号,需验证微信手机号后并入
submitting: false,
rejectReason: '',
application: null,
@@ -45,7 +47,9 @@ Page({
* force=true:审批可能刚刚通过,必须绕过角色缓存
*/
refresh() {
this.setData({ loading: true })
// 修改资料时前后台切换不重置表单
if (this.data.editing) return
this.setData({ loading: true, needPhoneAuth: false })
role
.ensureRoleSafe(true)
.then((detail) => {
@@ -67,7 +71,12 @@ Page({
.getMine()
.then((res) => {
if (res.code !== 0) {
wx.showToast({ title: res.msg || '加载失败', icon: 'none' })
// 未登录(新用户走到这里时还没有账号)不弹错误,直接展示表单
if (res.code !== 401) {
wx.showToast({ title: res.msg || '加载失败', icon: 'none' })
} else {
console.info('[apply] 未登录,展示申请表单')
}
this.setData({ loading: false, accountName })
return
}
@@ -82,6 +91,7 @@ Page({
this.setData({
loading: false,
view: 'pending',
editing: false,
application,
accountName,
serviceAreaText: (application.applicant.serviceAreas || []).map(cityLabel).join('、'),
@@ -145,6 +155,186 @@ Page({
this.setData({ cityIndex: Number(e.detail.value) || 0 })
},
/**
* 提交前确保已登录:
* 老用户在启动页已完成静默登录;首次使用的新用户还没有账号,
* 用表单里的手机号+姓名走 wxSignin 注册(后端仅允许未验证手机号用于新建账号,
* 与健康档案手机号无关联)。
*/
ensureLoginForApply() {
const app = getApp()
const current = app && app.globalData ? app.globalData.user : null
if (current && current.security && current.security.token) {
return Promise.resolve(current)
}
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
if (!res.code) {
reject(new Error('wx.login 未返回 code'))
return
}
API.user.wxSignin({
code: res.code,
phoneNumber: this.data.form.mobile.trim(),
name: this.data.form.name.trim()
})
.then((data) => {
if (data.code === 0) {
console.info('[apply] 注册/登录成功',
data.data.user && data.data.user.profile && data.data.user.profile.mobile)
app.globalData.user = data.data.user
app.eventBus.emit('user-login', data.data.user)
resolve(data.data.user)
return
}
console.warn('[apply] 注册/登录被拒:', data.msg)
reject(new Error(data.msg || '登录失败'))
})
.catch(reject)
},
fail: (err) => reject(new Error((err && err.errMsg) || 'wx.login 失败'))
})
})
},
/**
* 微信手机号快速验证后并入已有账号
*
* 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 })
wx.login({
success: (res) => {
if (!res.code) {
this.setData({ submitting: false })
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
return
}
API.user
.wxSignin({
phoneCode,
code: res.code,
name: this.data.form.name.trim()
})
.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
})
wx.showModal({
title: '手机号已更新',
content: `当前微信验证的手机号为 ${verifiedMobile},与填写的 ${formMobile} 不一致,已自动改用验证手机号,请确认后重新提交。`,
showCancel: false
})
return
}
// submit 内部有 submitting 守卫,先复位再继续提交
this.setData({ submitting: false, needPhoneAuth: false })
this.submit()
})
.catch((err) => {
console.warn('[apply] 手机号验证登录失败:', err)
this.setData({ submitting: false })
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
})
},
fail: () => {
this.setData({ submitting: false })
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
}
})
},
cancelPhoneAuth() {
this.setData({ needPhoneAuth: false })
},
/**
* 待审核 → 修改模式:回填已提交的基本信息
*/
startEdit() {
const application = this.data.application
if (!application) return
const applicant = application.applicant || {}
const sexIndex = SEX_OPTIONS.findIndex((item) => item.value === applicant.sex)
this.setData({
view: 'form',
editing: true,
rejectReason: '',
sexIndex: sexIndex > -1 ? sexIndex : 0,
cityIndex: cityIndex((applicant.serviceAreas || [])[0]),
form: {
name: applicant.name || '',
mobile: applicant.mobile || '',
sex: applicant.sex || '',
introduction: applicant.introduction || ''
}
})
},
cancelEdit() {
// 放弃本次修改,回到待审核视图
this.setData({ view: 'pending', editing: false })
},
submitUpdate(payload) {
this.setData({ submitting: true })
API.attendantApplication
.updateMine(payload)
.then((res) => {
if (res.code === 0) {
const application = res.data ? res.data.application : null
wx.showToast({ title: '已保存', icon: 'success' })
this.setData({
submitting: false,
view: 'pending',
editing: false,
application,
serviceAreaText: application
? (application.applicant.serviceAreas || []).map(cityLabel).join('、')
: '',
submitTimeText: application ? this.formatTime(application.createtime) : ''
})
return
}
this.setData({ submitting: false })
wx.showToast({ title: res.msg || '保存失败', icon: 'none' })
})
.catch((err) => {
const msg = err && err.message ? err.message : ''
this.setData({ submitting: false })
wx.showToast({ title: msg || '网络错误,请重试', icon: 'none' })
})
},
submit() {
if (this.data.submitting) return
@@ -167,9 +357,15 @@ Page({
}
}
// 待审核修改:只更新已有的申请单,不重新走提交逻辑
if (this.data.editing) {
this.submitUpdate(payload)
return
}
this.setData({ submitting: true })
API.attendantApplication
.create(payload)
this.ensureLoginForApply()
.then(() => API.attendantApplication.create(payload))
.then((res) => {
if (res.code === 0) {
const next = res.data ? res.data.application : null
@@ -207,9 +403,33 @@ Page({
wx.showToast({ title: res.msg || '提交失败', icon: 'none' })
this.setData({ submitting: false })
})
.catch(() => {
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
.catch((err) => {
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)) {
wx.showModal({
title: '无法提交',
content: msg,
showCancel: false
})
return
}
wx.showToast({ title: msg || '网络错误,请重试', icon: 'none' })
})
},
+23 -7
View File
@@ -6,10 +6,10 @@
<!-- 待审核 -->
<block wx:elif="{{view === 'pending'}}">
<view class="hero">
<view class="hero hero-center">
<view class="hero-badge">待审核</view>
<text class="hero-title">申请已提交</text>
<text class="hero-desc">审核通过后成为实习陪诊师,可先查看订单;管理员转正后即可接单。期间无需重复提交。</text>
<text class="hero-title">申请已提交,请耐心等待</text>
<text class="hero-desc">审核完成后会第一时间更新结果;期间无需重复提交,如需修改资料可点击下方「修改个人信息」。</text>
</view>
<view class="card">
@@ -35,6 +35,8 @@
</view>
</view>
<button class="edit-btn" bindtap="startEdit">修改个人信息</button>
<view class="foot-note">当前微信账号:{{accountName}}</view>
</block>
@@ -52,8 +54,8 @@
<!-- 填写申请表 -->
<block wx:else>
<view class="hero">
<text class="hero-title">成为陪诊师</text>
<text class="hero-desc">提交资料并通过审核后成为实习陪诊师,可先查看订单;管理员转正后即可接单。</text>
<text class="hero-title">{{editing ? '修改个人信息' : '成为陪诊师'}}</text>
<text class="hero-desc">{{editing ? '待审核期间可修改姓名、手机号等基本信息,保存后审核进度不受影响。' : '提交资料并通过审核后成为实习陪诊师,可先查看订单;管理员转正后即可接单。'}}</text>
</view>
<view class="reject-box" wx:if="{{rejectReason}}">
@@ -135,10 +137,24 @@
</view>
</view>
<button class="submit-btn" loading="{{submitting}}" disabled="{{submitting}}" bindtap="submit">
提交申请
<button
wx:if="{{needPhoneAuth}}"
class="submit-btn"
open-type="getPhoneNumber"
bindgetphonenumber="onPhoneAuth"
loading="{{submitting}}"
disabled="{{submitting}}"
>
验证手机号并继续
</button>
<button wx:else class="submit-btn" loading="{{submitting}}" disabled="{{submitting}}" bindtap="submit">
{{editing ? '保存修改' : '提交申请'}}
</button>
<button wx:if="{{needPhoneAuth}}" class="cancel-btn" bindtap="cancelPhoneAuth">返回修改手机号</button>
<button wx:if="{{editing}}" class="cancel-btn" bindtap="cancelEdit">取消</button>
<view class="foot-note">
当前微信账号:{{accountName}}。如需更换账号,请在微信中切换后重新打开小程序。
</view>
+75 -4
View File
@@ -22,13 +22,26 @@
}
.hero-badge {
display: inline-block;
padding: 6rpx 20rpx;
margin-bottom: 16rpx;
display: inline-flex;
align-items: center;
padding: 12rpx 36rpx;
margin-bottom: 20rpx;
border-radius: 999rpx;
background-color: #F7E4CE;
color: #A8551F;
font-size: 24rpx;
font-size: 36rpx;
font-weight: 600;
letter-spacing: 2rpx;
box-shadow: 0 4rpx 16rpx rgba(168, 85, 31, 0.15);
}
.hero-badge::before {
content: '';
width: 14rpx;
height: 14rpx;
margin-right: 14rpx;
border-radius: 50%;
background-color: currentColor;
}
.hero-badge-ok {
@@ -52,6 +65,35 @@
color: #8A8078;
}
/* 待审核:状态置顶居中强化 */
.hero-center {
display: flex;
flex-direction: column;
align-items: center;
padding: 56rpx 8rpx 40rpx;
text-align: center;
}
.hero-center .hero-badge {
margin-bottom: 28rpx;
padding: 18rpx 60rpx;
font-size: 52rpx;
font-weight: 700;
letter-spacing: 4rpx;
box-shadow: 0 8rpx 28rpx rgba(168, 85, 31, 0.22);
}
.hero-center .hero-badge::before {
width: 18rpx;
height: 18rpx;
margin-right: 16rpx;
}
.hero-center .hero-title {
font-size: 34rpx;
line-height: 1.4;
}
/* 卡片 */
.card {
margin-bottom: 24rpx;
@@ -228,6 +270,35 @@
border: none;
}
/* 待审核:进入修改模式 */
.edit-btn {
height: 88rpx;
margin: 0 0 24rpx;
border: 1rpx solid #E7D6C3;
border-radius: 44rpx;
background-color: #fff;
color: #A8551F;
font-size: 30rpx;
line-height: 88rpx;
}
.edit-btn::after {
border: none;
}
/* 修改模式:取消返回待审核视图 */
.cancel-btn {
margin: 0 0 24rpx;
background-color: transparent;
color: #8A8078;
font-size: 28rpx;
line-height: 88rpx;
}
.cancel-btn::after {
border: none;
}
.foot-note {
padding: 0 16rpx;
text-align: center;
+22 -3
View File
@@ -1,6 +1,9 @@
// pages/launch/index.js
// 启动页:冷启动先解析身份再决定去首页还是入驻申请页,
// 避免默认首页先渲染一帧再被全局守卫重定向。
//
// 特殊分支:从未使用过的新用户没有账号,wxSignin 只带 code 会返回「缺少手机号」,
// 这不是网络故障——直接进申请页,由申请页用表单里的姓名+手机号完成注册。
const role = require('../../utils/role.js')
Page({
@@ -14,9 +17,15 @@ Page({
enter() {
this.setData({ error: '' })
role
.ensureRoleSafe(true)
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
@@ -27,7 +36,17 @@ Page({
}
wx.switchTab({ url: '/pages/home/index' })
})
.catch(() => this.setData({ error: '网络异常,请重试' }))
.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() {