feat: 登录支持采集用户名,登录守卫统一到 utils/auth.js

- 我的页未登录态用 type=nickname 输入框采集昵称,wxSignin 随手机号一并提交
- 新建共享登录守卫 ensureLogin,替换 itemdetail/itemorder/home 三处副本
- 首页预约记录/就诊人/立即预约入口补充登录拦截
This commit is contained in:
lik
2026-11-08 11:59:47 +08:00
parent 1402b5a91d
commit 46627a73b9
7 changed files with 206 additions and 76 deletions
+7 -32
View File
@@ -1,5 +1,6 @@
const API = require('../../utils/api.js')
const { formatDate, LATEST_APPOINTMENT_TIME, buildTimeSlots } = require('../../utils/date.js')
const { ensureLogin } = require('../../utils/auth.js')
Page({
data: {
@@ -137,40 +138,14 @@ Page({
return 'assist-icon-3'
},
async ensureLogin() {
const app = getApp()
await app.waitForAuth()
if (app.globalData.user?._id) return true
const modal = await new Promise((resolve) => {
wx.showModal({
title: '登录后预约',
content: '登录成功后将返回当前服务并继续预约',
// showModal 的按钮文案上限 4 个字:写「登录并继续」会让弹窗直接 fail,
// 用户看到的就是「点了预约没反应」,所以这里必须压在 4 字内
confirmText: '去登录',
confirmColor: '#B8520A',
success: resolve,
fail: (err) => {
// 弹窗本身失败也不能静默:按「未确认」处理,避免整条链路无声中断
console.log('登录提示弹窗失败', err)
resolve({ confirm: false })
}
})
})
if (!modal.confirm) return null
// 用户明确点击登录时,解除主动退出对静默登录的限制。
wx.removeStorageSync('has_logout')
return app.silentSignin()
},
async openOrderPopup() {
if (!this.data.service.id || this.data.submitting) return
const loggedIn = await this.ensureLogin()
if (loggedIn === null) return
if (!loggedIn || !getApp().globalData.user?._id) {
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
return
}
// 登录守卫统一走 utils/auth.js:取消静默返回,失败由守卫内 toast
const loggedIn = await ensureLogin({
title: '登录后预约',
content: '登录成功后将返回当前服务并继续预约'
})
if (!loggedIn) return
this.idempotencyKey = `order_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 12)}`
this.setData({
patientName: '',
+7 -28
View File
@@ -1,5 +1,6 @@
const API = require('../../utils/api.js')
const { formatDate, LATEST_APPOINTMENT_TIME, buildTimeSlots } = require('../../utils/date.js')
const { ensureLogin } = require('../../utils/auth.js')
// 陪诊项目下拉单行高度:图标 72rpx + 单行文字 + 上下留白
const SERVICE_OPTION_HEIGHT = 108
@@ -185,28 +186,6 @@ Page({
return 'assist-icon-3'
},
async ensureLogin() {
const app = getApp()
await app.waitForAuth()
if (app.globalData.user?._id) return true
const modal = await new Promise((resolve) => {
wx.showModal({
title: '登录后预约',
content: '登录成功后将继续预约',
confirmText: '去登录',
confirmColor: '#B8520A',
success: resolve,
fail: (err) => {
console.log('登录提示弹窗失败', err)
resolve({ confirm: false })
}
})
})
if (!modal.confirm) return null
wx.removeStorageSync('has_logout')
return app.silentSignin()
},
loadProfiles() {
return API.healthProfile.getMyProfiles({ page: 1, pageSize: 50 })
.then((res) => {
@@ -362,12 +341,12 @@ Page({
if (this.data.submitting) return
this.setData({ submitting: true, showPayPopup: false })
try {
const loggedIn = await this.ensureLogin()
if (loggedIn === null) return
if (!loggedIn || !getApp().globalData.user?._id) {
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
return
}
// 登录守卫统一走 utils/auth.js:取消静默返回,失败由守卫内 toast
const loggedIn = await ensureLogin({
title: '登录后预约',
content: '登录成功后将继续预约'
})
if (!loggedIn) return
if (this.data.profiles.length === 0) await this.loadProfiles()
const error = this.validateOrder()