feat(apply): 手机号冲突走微信验证并号,待审核支持修改资料
This commit is contained in:
+226
-6
@@ -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' })
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user