功能:改造Salon小程序OpenID快捷登录
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
const userApi = require('../../api/user')
|
||||
const authApi = require('../../api/auth')
|
||||
const auth = require('../../utils/auth')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
profile: {},
|
||||
loading: true,
|
||||
binding: false,
|
||||
smsForm: { phone: '', code: '' },
|
||||
codeLoading: false,
|
||||
countdown: 0,
|
||||
timer: null
|
||||
},
|
||||
|
||||
onShow() { this.loadProfile() },
|
||||
|
||||
async loadProfile() {
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const profile = await userApi.info()
|
||||
this.setData({ profile, 'smsForm.phone': profile.phone || '' })
|
||||
const app = getApp()
|
||||
const session = auth.getSession()
|
||||
app.setSession({ ...session, userInfo: { ...(session.userInfo || {}), ...profile } })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '账号信息加载失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
onInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
const value = field === 'phone' ? String(event.detail.value || '').replace(/\D/g, '').slice(0, 11) : event.detail.value
|
||||
this.setData({ [`smsForm.${field}`]: value })
|
||||
},
|
||||
|
||||
async bindWechatPhone(event) {
|
||||
const phoneCode = event.detail && event.detail.code
|
||||
if (!phoneCode) {
|
||||
wx.showToast({ title: '未获得手机号授权', icon: 'none' }); return
|
||||
}
|
||||
this.setData({ binding: true })
|
||||
try {
|
||||
await userApi.bindWechatPhone(phoneCode)
|
||||
await this.loadProfile()
|
||||
wx.showToast({ title: '手机号已绑定' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '绑定失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ binding: false })
|
||||
}
|
||||
},
|
||||
|
||||
async sendCode() {
|
||||
const phone = String(this.data.smsForm.phone || '')
|
||||
if (!/^1[3-9]\d{9}$/.test(phone)) {
|
||||
wx.showToast({ title: '请输入正确手机号', icon: 'none' }); return
|
||||
}
|
||||
if (this.data.codeLoading || this.data.countdown) return
|
||||
this.setData({ codeLoading: true })
|
||||
try {
|
||||
await userApi.sendBindPhoneCode(phone)
|
||||
this.startCountdown()
|
||||
wx.showToast({ title: '验证码已发送' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '验证码发送失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ codeLoading: false })
|
||||
}
|
||||
},
|
||||
|
||||
startCountdown() {
|
||||
if (this.data.timer) clearInterval(this.data.timer)
|
||||
this.setData({ countdown: 60 })
|
||||
const timer = setInterval(() => {
|
||||
const next = this.data.countdown - 1
|
||||
if (next <= 0) { clearInterval(timer); this.setData({ countdown: 0, timer: null }); return }
|
||||
this.setData({ countdown: next })
|
||||
}, 1000)
|
||||
this.setData({ timer })
|
||||
},
|
||||
|
||||
async bindSmsPhone() {
|
||||
const form = this.data.smsForm
|
||||
if (!/^1[3-9]\d{9}$/.test(form.phone) || !/^\d{6}$/.test(String(form.code || ''))) {
|
||||
wx.showToast({ title: '请输入正确手机号和6位验证码', icon: 'none' }); return
|
||||
}
|
||||
this.setData({ binding: true })
|
||||
try {
|
||||
await userApi.bindPhone({ phone: form.phone, verificationCode: form.code })
|
||||
await this.loadProfile()
|
||||
wx.showToast({ title: '手机号已绑定' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '绑定失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ binding: false })
|
||||
}
|
||||
},
|
||||
|
||||
async logout() {
|
||||
const confirmed = await new Promise(resolve => wx.showModal({ title: '退出登录', content: '退出后将关闭自动登录,并清除本机提醒设置。', success: result => resolve(result.confirm), fail: () => resolve(false) }))
|
||||
if (!confirmed) return
|
||||
try { await authApi.logout() } catch (error) {}
|
||||
getApp().logoutDevice()
|
||||
wx.reLaunch({ url: '/pages/login/index' })
|
||||
},
|
||||
|
||||
onUnload() { if (this.data.timer) clearInterval(this.data.timer) }
|
||||
})
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "账号设置"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<view class="page settings-page">
|
||||
<view class="card identity-card"><view class="identity-mark">微</view><view class="identity-copy"><text class="identity-title">微信身份</text><text class="identity-status">已安全关联当前账号</text></view><text class="linked-badge">已关联</text></view>
|
||||
<view class="card phone-card"><view class="section-title">手机号</view><view class="phone-status"><text>{{profile.phoneVerified == 1 ? (profile.maskedPhone || profile.phone) : '尚未绑定'}}</text><text class="status-tag {{profile.phoneVerified == 1 ? 'done' : ''}}">{{profile.phoneVerified == 1 ? '已验证' : '待绑定'}}</text></view><button class="wechat-phone-btn" open-type="getPhoneNumber" loading="{{binding}}" bindgetphonenumber="bindWechatPhone">微信一键{{profile.phoneVerified == 1 ? '更换' : '绑定'}}</button><view class="divider"><text>或使用短信验证码</text></view><view class="field"><input value="{{smsForm.phone}}" data-field="phone" bindinput="onInput" type="number" maxlength="11" placeholder="请输入手机号" /></view><view class="code-row"><input value="{{smsForm.code}}" data-field="code" bindinput="onInput" type="number" maxlength="6" placeholder="6位验证码" /><button bindtap="sendCode" disabled="{{codeLoading || countdown > 0}}">{{countdown > 0 ? countdown + 's' : '获取验证码'}}</button></view><button class="sms-bind-btn" loading="{{binding}}" bindtap="bindSmsPhone">验证并绑定</button></view>
|
||||
<view class="account-note">手机号仅用于账号安全与必要联系,不作为微信快捷登录标识。</view>
|
||||
<button class="logout-btn" bindtap="logout">退出登录</button>
|
||||
</view>
|
||||
@@ -0,0 +1,2 @@
|
||||
.settings-page { padding-top:24rpx; }.identity-card { display:flex; align-items:center; padding:26rpx; }.identity-mark { display:flex; align-items:center; justify-content:center; width:68rpx; height:68rpx; border-radius:20rpx; background:#dcebe1; color:#35634d; font-size:28rpx; font-weight:800; }.identity-copy { display:flex; flex:1; flex-direction:column; margin-left:18rpx; }.identity-title,.section-title { color:#30483c; font-size:28rpx; font-weight:800; }.identity-status { margin-top:6rpx; color:#8a9a91; font-size:21rpx; }.linked-badge,.status-tag { padding:7rpx 14rpx; border-radius:999rpx; background:#e5f2e8; color:#397055; font-size:20rpx; }
|
||||
.phone-card { margin-top:20rpx; padding:28rpx; }.phone-status { display:flex; align-items:center; justify-content:space-between; margin-top:20rpx; padding:20rpx; border-radius:14rpx; background:#f4f8f5; color:#425b4e; font-size:26rpx; }.status-tag { background:#fff0db; color:#9a682e; }.status-tag.done { background:#e5f2e8; color:#397055; }.wechat-phone-btn,.sms-bind-btn { height:80rpx; margin-top:22rpx; border-radius:14rpx; background:#35634d; color:#fff; font-size:26rpx; line-height:80rpx; }.divider { display:flex; align-items:center; justify-content:center; margin:28rpx 0 18rpx; color:#9aa79f; font-size:20rpx; }.field input,.code-row input { box-sizing:border-box; height:78rpx; padding:0 20rpx; border:1rpx solid #dde8e0; border-radius:12rpx; background:#f8faf8; }.code-row { display:flex; gap:12rpx; margin-top:14rpx; }.code-row input { min-width:0; flex:1; }.code-row button { width:190rpx; height:78rpx; padding:0; border-radius:12rpx; background:#e7f0ea; color:#426c57; font-size:23rpx; line-height:78rpx; }.sms-bind-btn { background:#eef4f0; color:#35634d; }.account-note { margin:24rpx 18rpx; color:#8e9c94; font-size:21rpx; line-height:1.6; }.logout-btn { margin-top:52rpx; background:transparent; color:#a24f42; font-size:25rpx; }
|
||||
Reference in New Issue
Block a user