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, loading: true,
view: 'form', // form 填写 / pending 待审核 / approved 已通过但角色未刷新 view: 'form', // form 填写 / pending 待审核 / approved 已通过但角色未刷新
editing: false, // 待审核期间修改基本信息 editing: false, // 待审核期间修改基本信息
needPhoneAuth: false, // 手机号已有平台账号,需验证微信手机号后并入 loggedIn: false, // 是否已完成微信登录
loggingIn: false, // 微信登录请求中
submitting: false, submitting: false,
rejectReason: '', rejectReason: '',
application: null, application: null,
@@ -49,7 +50,7 @@ Page({
refresh() { refresh() {
// 修改资料时前后台切换不重置表单 // 修改资料时前后台切换不重置表单
if (this.data.editing) return if (this.data.editing) return
this.setData({ loading: true, needPhoneAuth: false }) this.setData({ loading: true })
role role
.ensureRoleSafe(true) .ensureRoleSafe(true)
.then((detail) => { .then((detail) => {
@@ -66,24 +67,32 @@ Page({
const app = getApp() const app = getApp()
const user = app && app.globalData ? app.globalData.user : null const user = app && app.globalData ? app.globalData.user : null
const accountName = user && user.profile && user.profile.name ? user.profile.name : '微信用户' const accountName = user && user.profile && user.profile.name ? user.profile.name : '微信用户'
const loggedIn = !!(user && user.security && user.security.token)
API.attendantApplication API.attendantApplication
.getMine() .getMine()
.then((res) => { .then((res) => {
if (res.code !== 0) { if (res.code !== 0) {
// 未登录(新用户走到这里时还没有账号)不弹错误,直接展示表单 // 未登录(新用户走到这里时还没有账号)不弹错误,直接展示登录入口
if (res.code !== 401) { if (res.code !== 401) {
wx.showToast({ title: res.msg || '加载失败', icon: 'none' }) wx.showToast({ title: res.msg || '加载失败', icon: 'none' })
} else { } else {
console.info('[apply] 未登录,展示申请表单') console.info('[apply] 未登录,展示登录入口')
} }
this.setData({ loading: false, accountName }) this.setData({ loading: false, accountName, loggedIn })
return return
} }
const application = res.data ? res.data.application : null const application = res.data ? res.data.application : null
if (!application) { 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 return
} }
@@ -92,6 +101,7 @@ Page({
loading: false, loading: false,
view: 'pending', view: 'pending',
editing: false, editing: false,
loggedIn: true,
application, application,
accountName, accountName,
serviceAreaText: (application.applicant.serviceAreas || []).map(cityLabel).join('、'), serviceAreaText: (application.applicant.serviceAreas || []).map(cityLabel).join('、'),
@@ -102,7 +112,7 @@ Page({
if (application.status === 'approved') { if (application.status === 'approved') {
// 走到这里说明员工档案还没查到(接口抖动或数据延迟),给一个手动重试入口 // 走到这里说明员工档案还没查到(接口抖动或数据延迟),给一个手动重试入口
this.setData({ loading: false, view: 'approved', application, accountName }) this.setData({ loading: false, view: 'approved', loggedIn: true, application, accountName })
return return
} }
@@ -112,6 +122,7 @@ Page({
this.setData({ this.setData({
loading: false, loading: false,
view: 'form', view: 'form',
loggedIn: true,
application, application,
accountName, accountName,
rejectReason: application.rejectReason || '', rejectReason: application.rejectReason || '',
@@ -126,7 +137,7 @@ Page({
}) })
}) })
.catch(() => { .catch(() => {
this.setData({ loading: false, accountName }) this.setData({ loading: false, accountName, loggedIn })
wx.showToast({ title: '网络错误,请重试', icon: 'none' }) wx.showToast({ title: '网络错误,请重试', icon: 'none' })
}) })
}, },
@@ -200,83 +211,66 @@ Page({
}, },
/** /**
* 微信手机号快速验证后并入已有账号 * 第一步:手机号登录
* *
* phoneCode 是一次性凭证,只能交给 wxSignin 在服务端消费一次; * 陪诊师端没有开通「手机号快速验证」组件,手机号由用户自己输入;
* 成功后账号已绑定当前微信,继续走正常提交 * 后端在「手机号 + 姓名」与已有账号完全一致时,会把当前微信并入该账号
*/ */
onPhoneAuth(e) { onLogin() {
const phoneCode = e.detail && e.detail.code ? e.detail.code : '' const name = this.data.form.name.trim()
if (!phoneCode) { const mobile = this.data.form.mobile.trim()
console.warn('[apply] 未获得手机号授权:', e.detail && e.detail.errMsg) if (!name) return this.toast('请填写姓名')
wx.showToast({ title: '需要授权手机号才能继续', icon: 'none' }) if (!MOBILE_REGEX.test(mobile)) return this.toast('请填写正确的手机号')
return if (this.data.loggingIn) return
} this.setData({ loggingIn: true })
if (this.data.submitting) return
this.setData({ submitting: true })
wx.login({ wx.login({
success: (res) => { success: (res) => {
if (!res.code) { if (!res.code) {
this.setData({ submitting: false }) this.setData({ loggingIn: false })
wx.showToast({ title: '登录失败,请重试', icon: 'none' }) wx.showToast({ title: '登录失败,请重试', icon: 'none' })
return return
} }
API.user API.user
.wxSignin({ .wxSignin({ code: res.code, phoneNumber: mobile, name })
phoneCode,
code: res.code,
name: this.data.form.name.trim()
})
.then((data) => { .then((data) => {
if (data.code !== 0) { if (data.code !== 0) {
this.setData({ submitting: false }) this.setData({ loggingIn: 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({ wx.showModal({
title: '手机号已更新', title: '登录失败',
content: `当前微信验证的手机号为 ${verifiedMobile},与填写的 ${formMobile} 不一致,已自动改用验证手机号,请确认后重新提交。`, content: data.msg || '请重试',
showCancel: false showCancel: false
}) })
return return
} }
// submit 内部有 submitting 守卫,先复位再继续提交 const app = getApp()
this.setData({ submitting: false, needPhoneAuth: false }) app.globalData.user = data.data.user
this.submit() 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) => { .catch((err) => {
console.warn('[apply] 手机号验证登录失败:', err) console.warn('[apply] 登录失败:', err)
this.setData({ submitting: false }) this.setData({ loggingIn: false })
wx.showToast({ title: '网络错误,请重试', icon: 'none' }) wx.showToast({ title: (err && err.message) || '网络错误,请重试', icon: 'none' })
}) })
}, },
fail: () => { fail: () => {
this.setData({ submitting: false }) this.setData({ loggingIn: false })
wx.showToast({ title: '登录失败,请重试', icon: 'none' }) wx.showToast({ title: '登录失败,请重试', icon: 'none' })
} }
}) })
}, },
cancelPhoneAuth() {
this.setData({ needPhoneAuth: false })
},
/** /**
* 待审核 → 修改模式:回填已提交的基本信息 * 待审核 → 修改模式:回填已提交的基本信息
*/ */
@@ -407,21 +401,8 @@ Page({
const msg = err && err.message ? err.message : '' const msg = err && err.message ? err.message : ''
console.warn('[apply] 提交失败:', msg) console.warn('[apply] 提交失败:', msg)
this.setData({ submitting: false }) this.setData({ submitting: false })
// 登录环节的业务拒绝(手机号已被注册/微信已绑定其他账号)需要弹窗说明 // 登录环节的业务拒绝(手机号已注册且姓名不一致/微信已绑定其他账号)需要弹窗说明
if (/已注册/.test(msg)) { if (/已注册|已绑定|手机号/.test(msg)) {
// 手机号属于已有平台账号(如就诊人端注册过):手动填写的手机号不可信,
// 必须走微信手机号快速验证,由服务端确认号码归属后并入原账号
wx.showModal({
title: '手机号已有账号',
content: `${msg}。可点击「验证手机号」用当前微信绑定的手机号完成验证后继续。`,
confirmText: '验证手机号',
success: (res) => {
if (res.confirm) this.setData({ needPhoneAuth: true })
}
})
return
}
if (/已绑定|手机号/.test(msg)) {
wx.showModal({ wx.showModal({
title: '无法提交', title: '无法提交',
content: msg, content: msg,
+40 -15
View File
@@ -58,13 +58,48 @@
<text class="hero-desc">{{editing ? '待审核期间可修改姓名、手机号等基本信息,保存后审核进度不受影响。' : '提交资料并通过审核后成为实习陪诊师,可先查看订单;管理员转正后即可接单。'}}</text> <text class="hero-desc">{{editing ? '待审核期间可修改姓名、手机号等基本信息,保存后审核进度不受影响。' : '提交资料并通过审核后成为实习陪诊师,可先查看订单;管理员转正后即可接单。'}}</text>
</view> </view>
<view class="reject-box" wx:if="{{rejectReason}}"> <!-- 第一步:未登录先完成手机号登录 -->
<view class="card login-card" wx:if="{{!loggedIn}}">
<text class="card-title">第一步 · 登录</text>
<text class="card-tip">填写姓名与手机号完成登录;该手机号已有账号时会自动并入,无需重复注册。</text>
<view class="field">
<text class="field-label">姓名</text>
<input
class="field-input"
value="{{form.name}}"
data-key="name"
maxlength="20"
placeholder="请输入真实姓名"
bindinput="onInput"
/>
</view>
<view class="field">
<text class="field-label">手机号</text>
<input
class="field-input"
type="number"
value="{{form.mobile}}"
data-key="mobile"
maxlength="11"
placeholder="请输入手机号"
bindinput="onInput"
/>
</view>
<button class="submit-btn" loading="{{loggingIn}}" disabled="{{loggingIn}}" bindtap="onLogin">
登录
</button>
</view>
<view class="reject-box" wx:if="{{loggedIn && rejectReason}}">
<text class="reject-title">上次申请未通过</text> <text class="reject-title">上次申请未通过</text>
<text class="reject-text">原因:{{rejectReason}}</text> <text class="reject-text">原因:{{rejectReason}}</text>
<text class="reject-hint">修改后可重新提交。</text> <text class="reject-hint">修改后可重新提交。</text>
</view> </view>
<view class="card"> <view class="card" wx:if="{{loggedIn}}">
<text class="card-title">基本信息</text> <text class="card-title">基本信息</text>
<view class="field"> <view class="field">
@@ -137,23 +172,13 @@
</view> </view>
</view> </view>
<button <block wx:if="{{loggedIn}}">
wx:if="{{needPhoneAuth}}" <button class="submit-btn" loading="{{submitting}}" disabled="{{submitting}}" bindtap="submit">
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 ? '保存修改' : '提交申请'}} {{editing ? '保存修改' : '提交申请'}}
</button> </button>
<button wx:if="{{needPhoneAuth}}" class="cancel-btn" bindtap="cancelPhoneAuth">返回修改手机号</button>
<button wx:if="{{editing}}" class="cancel-btn" bindtap="cancelEdit">取消</button> <button wx:if="{{editing}}" class="cancel-btn" bindtap="cancelEdit">取消</button>
</block>
<view class="foot-note"> <view class="foot-note">
当前微信账号:{{accountName}}。如需更换账号,请在微信中切换后重新打开小程序。 当前微信账号:{{accountName}}。如需更换账号,请在微信中切换后重新打开小程序。
+9
View File
@@ -270,6 +270,15 @@
border: none; border: none;
} }
/* 登录卡片 */
.login-card {
padding-bottom: 32rpx;
}
.login-card .field:last-of-type {
border-bottom: none;
}
/* 待审核:进入修改模式 */ /* 待审核:进入修改模式 */
.edit-btn { .edit-btn {
height: 88rpx; height: 88rpx;