更新密码找回uniapp

This commit is contained in:
hero920103 committed 2026-11-16 17:22:50 +08:00
1 parent 7f9a66da0f
commit 3fdda89833
1304 files changed
+3503 -26132

No files matched your search

+306 -140
View File
@@ -8,61 +8,90 @@
<view class="header" :style="headPad">
<text class="title">忘记密码</text>
<text class="subtitle">
{{ currentStep === 1 ? '验证您的账号' : currentStep === 2 ? '通过手机号验证身份' : '设置新密码' }}
</text>
<text class="subtitle">{{ stepSubtitle }}</text>
</view>
<view class="form-card">
<!-- 第一步:验证租户和账号 -->
<view v-if="currentStep === 1" class="form">
<view class="field">
<input
class="field-input"
v-model="form.tenant_name"
type="text"
placeholder="租户名称"
placeholder-class="field-placeholder"
/>
</view>
<view class="field">
<input
class="field-input"
v-model="form.account"
type="text"
placeholder="账号"
placeholder-class="field-placeholder"
/>
<view class="mode-tabs">
<view
v-for="item in MODES"
:key="item.key"
class="mode-tab"
:class="{ on: mode === item.key }"
@tap="pickMode(item.key)"
>
<text class="mode-tab-text">{{ item.label }}</text>
</view>
</view>
<view class="btn-primary" :class="{ disabled: loading }" @tap="handleVerifyAccount">
<template v-if="mode === 'account'">
<view class="field">
<input
class="field-input"
v-model="form.tenant_name"
type="text"
placeholder="租户名称"
placeholder-class="field-placeholder"
/>
</view>
<view class="field">
<input
class="field-input"
v-model="form.account"
type="text"
placeholder="账号"
placeholder-class="field-placeholder"
/>
</view>
<text class="mode-tip">验证通过后,验证码将发送到该账号绑定的手机号;忘记租户名称可用手机号或邮箱找回</text>
</template>
<template v-else-if="mode === 'phone'">
<view class="field">
<input
class="field-input"
v-model="form.mobile"
type="number"
maxlength="11"
placeholder="手机号"
placeholder-class="field-placeholder"
/>
</view>
<text class="mode-tip">验证码将以短信形式发送到该手机号</text>
</template>
<template v-else>
<view class="field">
<input
class="field-input"
v-model="form.email"
type="text"
placeholder="邮箱"
placeholder-class="field-placeholder"
/>
</view>
<text class="mode-tip">验证码将以邮件形式发送到该邮箱</text>
</template>
<view class="btn-primary" :class="{ disabled: loading }" @tap="handleNextFromIdentity">
{{ loading ? '验证中...' : '下一步' }}
</view>
</view>
<!-- 第二步:验证手机号并发送验证码 -->
<view v-if="currentStep === 2" class="form">
<view class="info-box">
<view v-else-if="currentStep === 2" class="form">
<view v-if="mode === 'account'" class="info-box">
<text class="info-label">租户</text>
<text class="info-value">{{ form.tenant_name }}</text>
</view>
<view class="info-box">
<view v-if="mode === 'account'" class="info-box">
<text class="info-label">账号</text>
<text class="info-value">{{ form.account }}</text>
</view>
<view v-if="verifyResult.phone" class="field">
<input
class="field-input"
v-model="form.phone"
type="tel"
maxlength="11"
placeholder="手机号"
placeholder-class="field-placeholder"
/>
</view>
<view v-else class="empty-contact">
<text>账号未绑定手机号,请联系管理员</text>
<view class="info-box">
<text class="info-label">{{ targetLabel }}</text>
<text class="info-value">{{ maskedTarget }}</text>
</view>
<view class="field field-row">
@@ -71,36 +100,36 @@
v-model="form.sms_code"
type="number"
maxlength="6"
placeholder="短信验证码"
placeholder="验证码"
placeholder-class="field-placeholder"
/>
<text
class="code-link"
:class="{ disabled: countdown > 0 || codeLoading || !verifyResult.phone }"
:class="{ disabled: countdown > 0 || codeLoading }"
@tap="handleSendCode"
>{{ countdown > 0 ? `${countdown}s` : (codeLoading ? '发送中' : '获取验证码') }}</text>
</view>
<text class="code-tip">{{ codeTip }}</text>
<view class="btn-primary" :class="{ disabled: loading }" @tap="handleVerifyPhone">
<view class="btn-primary" :class="{ disabled: loading }" @tap="handleNextFromCode">
{{ loading ? '验证中...' : '下一步' }}
</view>
<view class="action-row">
<text class="link-text" @tap="currentStep = 1">返回上一步</text>
<text class="link-text" @tap="backToPrev">返回上一步</text>
<text class="link-text" @tap="goBack">返回登录</text>
</view>
</view>
<!-- 第三步:重置密码 -->
<view v-if="currentStep === 3" class="form">
<view class="info-box">
<text class="info-label">租户</text>
<text class="info-value">{{ form.tenant_name }}</text>
</view>
<view class="info-box">
<view v-else class="form">
<view v-if="mode === 'account'" class="info-box">
<text class="info-label">账号</text>
<text class="info-value">{{ form.account }}</text>
</view>
<view class="info-box">
<text class="info-label">{{ targetLabel }}</text>
<text class="info-value">{{ maskedTarget }}</text>
</view>
<view class="field">
<input
@@ -126,7 +155,7 @@
</view>
<view class="action-row">
<text class="link-text" @tap="currentStep = 2">返回上一步</text>
<text class="link-text" @tap="backToPrev">返回上一步</text>
<text class="link-text" @tap="goBack">返回登录</text>
</view>
</view>
@@ -136,13 +165,29 @@
</template>
<script setup>
import { reactive, ref, onUnmounted } from 'vue'
import { reactive, ref, computed, onUnmounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { mpHeadPad } from '@/utils/mpHead.js'
const headPad = mpHeadPad()
import { verifyAccount, resetPassword, sendResetCode } from '@/api/auth.js'
import { setTenantName, getTenantName } from '@/utils/auth.js'
import {
verifyAccount,
resetPassword,
sendResetCode,
sendResetSmsCode,
resetPasswordByMobile,
sendResetEmailCode,
resetPasswordByEmail
} from '@/api/auth.js'
import { setTenantName, getTenantName, getRememberLogin } from '@/utils/auth.js'
const MODES = [
{ key: 'account', label: '账号' },
{ key: 'phone', label: '手机号' },
{ key: 'email', label: '邮箱' }
]
const mode = ref('account')
const currentStep = ref(1)
const loading = ref(false)
const codeLoading = ref(false)
@@ -153,14 +198,71 @@ const form = reactive({
tenant_name: getTenantName() || '',
account: '',
phone: '',
mobile: '',
email: '',
sms_code: '',
new_password: '',
confirm_password: ''
})
const verifyResult = reactive({
phone: '',
email: ''
const stepSubtitle = computed(() => {
if (currentStep.value === 1) {
if (mode.value === 'account') return '填写租户与账号'
if (mode.value === 'phone') return '填写账号绑定的手机号'
return '填写账号绑定的邮箱'
}
if (currentStep.value === 2) return '输入验证码验证身份'
return '设置新密码'
})
const targetLabel = computed(() => (mode.value === 'email' ? '邮箱' : '手机号'))
const maskedTarget = computed(() => {
if (mode.value === 'email') return maskEmail(form.email)
if (mode.value === 'phone') return maskPhone(form.mobile)
return maskPhone(form.phone)
})
const codeTip = computed(() =>
mode.value === 'email' ? '验证码将发送至上面的邮箱' : '验证码将发送至上面的手机号'
)
function maskPhone(phone) {
const s = String(phone || '')
if (s.length < 7) return s
return s.slice(0, 3) + '****' + s.slice(-4)
}
function maskEmail(email) {
const s = String(email || '')
const at = s.indexOf('@')
if (at <= 0) return s
const name = s.slice(0, at)
const keep = name.length <= 2 ? 1 : 2
return name.slice(0, keep) + '***' + s.slice(at)
}
onLoad((options = {}) => {
const remembered = getRememberLogin() || {}
let handed = {}
try {
handed = uni.getStorageSync('app_forget_prefill') || {}
uni.removeStorageSync('app_forget_prefill')
} catch (e) {
handed = {}
}
const tenant = String(options.tenant_name || handed.tenant_name || '').trim()
const acc = String(options.account || handed.account || remembered.account || '').trim()
if (tenant) form.tenant_name = tenant
if (/^1[3-9]\d{9}$/.test(acc)) {
form.mobile = acc
mode.value = 'phone'
} else if (acc.indexOf('@') > 0) {
form.email = acc
mode.value = 'email'
} else if (acc) {
form.account = acc
}
})
function startCountdown() {
@@ -175,94 +277,99 @@ function startCountdown() {
}, 1000)
}
// 第一步:验证租户和账号
async function handleVerifyAccount() {
if (loading.value) return
if (!form.tenant_name.trim() || !form.account.trim()) {
uni.showToast({ title: '请填写租户和账号', icon: 'none' })
return
}
loading.value = true
try {
const data = await verifyAccount({
tenant_name: form.tenant_name.trim(),
account: form.account.trim()
})
verifyResult.phone = data?.phone || ''
verifyResult.email = data?.email || ''
if (!verifyResult.phone && !verifyResult.email) {
uni.showToast({ title: '账号未绑定验证方式', icon: 'none' })
return
}
form.phone = verifyResult.phone || ''
currentStep.value = 2
} catch (err) {
// error handled by request interceptor
} finally {
loading.value = false
}
function resetCountdown() {
if (timer) clearInterval(timer)
timer = null
countdown.value = 0
}
function pickMode(key) {
if (mode.value === key) return
mode.value = key
form.sms_code = ''
resetCountdown()
}
async function handleNextFromIdentity() {
if (loading.value) return
if (mode.value === 'account') {
if (!form.tenant_name.trim() || !form.account.trim()) {
uni.showToast({ title: '请填写租户和账号', icon: 'none' })
return
}
loading.value = true
try {
const data = await verifyAccount({
tenant_name: form.tenant_name.trim(),
account: form.account.trim()
})
form.phone = String(data?.phone || '').trim()
if (!form.phone) {
uni.showToast({ title: '该账号未绑定手机号,请联系管理员重置', icon: 'none' })
return
}
currentStep.value = 2
} catch (err) {
} finally {
loading.value = false
}
return
}
if (mode.value === 'phone') {
const phone = form.mobile.trim()
if (!/^1[3-9]\d{9}$/.test(phone)) {
uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
return
}
currentStep.value = 2
return
}
const email = form.email.trim()
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
uni.showToast({ title: '请输入正确的邮箱地址', icon: 'none' })
return
}
currentStep.value = 2
}
// 第二步:发送验证码
async function handleSendCode() {
if (codeLoading.value || countdown.value > 0) return
if (!form.phone.trim()) {
uni.showToast({ title: '请输入手机号', icon: 'none' })
return
}
if (!/^1\d{10}$/.test(form.phone)) {
uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
return
}
codeLoading.value = true
try {
await sendResetCode({
tenant_name: form.tenant_name.trim(),
account: form.account.trim(),
phone: form.phone.trim(),
channel: 'sms'
})
if (mode.value === 'account') {
await sendResetCode({
tenant_name: form.tenant_name.trim(),
account: form.account.trim(),
phone: form.phone,
channel: 'sms'
})
} else if (mode.value === 'phone') {
await sendResetSmsCode({ phone: form.mobile.trim() })
} else {
await sendResetEmailCode({ email: form.email.trim() })
}
uni.showToast({ title: '验证码已发送', icon: 'success' })
startCountdown()
} catch (err) {
// error handled by request interceptor
} finally {
codeLoading.value = false
}
}
// 第二步验证:验证手机和验证码
async function handleVerifyPhone() {
if (loading.value) return
if (!form.phone.trim()) {
uni.showToast({ title: '请输入手机号', icon: 'none' })
return
}
function handleNextFromCode() {
if (!form.sms_code.trim()) {
uni.showToast({ title: '请输入验证码', icon: 'none' })
return
}
loading.value = true
try {
// 后端的验证码验证在重置密码时进行,这里只是提交到第三步
currentStep.value = 3
} catch (err) {
// error handled by request interceptor
} finally {
loading.value = false
}
currentStep.value = 3
}
// 第三步:重置密码
async function handleResetPassword() {
if (loading.value) return
if (!form.tenant_name.trim() || !form.account.trim() || !form.phone.trim()) {
uni.showToast({ title: '请填写租户、账号和手机号', icon: 'none' })
return
}
if (!form.new_password) {
uni.showToast({ title: '请输入新密码', icon: 'none' })
return
@@ -276,32 +383,54 @@ async function handleResetPassword() {
return
}
if (!form.sms_code.trim()) {
uni.showToast({ title: '请输入验证码', icon: 'none' })
uni.showToast({ title: '请先获取并输入验证码', icon: 'none' })
return
}
loading.value = true
try {
await resetPassword({
tenant_name: form.tenant_name.trim(),
account: form.account.trim(),
phone: form.phone.trim(),
sms_code: form.sms_code.trim(),
new_password: form.new_password,
confirm_password: form.confirm_password
})
setTenantName(form.tenant_name.trim())
if (mode.value === 'account') {
await resetPassword({
tenant_name: form.tenant_name.trim(),
account: form.account.trim(),
phone: form.phone,
sms_code: form.sms_code.trim(),
new_password: form.new_password,
confirm_password: form.confirm_password
})
setTenantName(form.tenant_name.trim())
} else if (mode.value === 'phone') {
await resetPasswordByMobile({
phone: form.mobile.trim(),
code: form.sms_code.trim(),
password: form.new_password,
confirm: form.confirm_password
})
} else {
await resetPasswordByEmail({
email: form.email.trim(),
code: form.sms_code.trim(),
password: form.new_password,
confirm: form.confirm_password
})
}
resetCountdown()
uni.showToast({ title: '重置成功,请登录', icon: 'success' })
setTimeout(() => {
uni.reLaunch({ url: '/pages/login/login' })
}, 500)
}, 600)
} catch (err) {
// error handled by request interceptor
} finally {
loading.value = false
}
}
function backToPrev() {
resetCountdown()
form.sms_code = ''
currentStep.value = Math.max(1, currentStep.value - 1)
}
function goBack() {
uni.navigateBack({ fail: () => uni.reLaunch({ url: '/pages/login/login' }) })
}
@@ -370,6 +499,44 @@ onUnmounted(() => {
gap: 20rpx;
}
.mode-tabs {
display: flex;
gap: 8rpx;
padding: 6rpx;
border-radius: $radius-md;
background: $color-bg-page;
}
.mode-tab {
flex: 1;
height: 72rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 12rpx;
&.on {
background: $color-card;
box-shadow: 0 2rpx 8rpx rgba(15, 23, 42, 0.08);
}
}
.mode-tab-text {
font-size: 28rpx;
color: $color-text-muted;
}
.mode-tab.on .mode-tab-text {
font-weight: 600;
color: $color-primary;
}
.mode-tip {
font-size: 22rpx;
line-height: 1.5;
color: $color-text-muted;
}
.field {
background: $color-bg-page;
border-radius: $radius-md;
@@ -417,6 +584,12 @@ onUnmounted(() => {
}
}
.code-tip {
font-size: 22rpx;
line-height: 1.5;
color: $color-text-muted;
}
.info-box {
background: $color-bg-page;
border-radius: $radius-md;
@@ -482,11 +655,4 @@ onUnmounted(() => {
color: $color-primary;
padding: 12rpx 0;
}
.footer-link {
text-align: center;
font-size: 26rpx;
color: $color-primary;
padding: 16rpx 0 4rpx;
}
</style>