Files
yunzerwebsiteallinone/uniapp/pages/login/forget.vue
T
2026-11-16 17:22:50 +08:00

659 lines
15 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view class="auth-page">
<view class="page-inner">
<view class="nav-back" @tap="goBack">
<FaIcon name="chevron-left" color="#303133" :size="18" />
<text class="nav-text">返回</text>
</view>
<view class="header" :style="headPad">
<text class="title">忘记密码</text>
<text class="subtitle">{{ stepSubtitle }}</text>
</view>
<view class="form-card">
<view v-if="currentStep === 1" class="form">
<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>
<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-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 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 field-row">
<input
class="field-input"
v-model="form.sms_code"
type="number"
maxlength="6"
placeholder="验证码"
placeholder-class="field-placeholder"
/>
<text
class="code-link"
: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="handleNextFromCode">
{{ loading ? '验证中...' : '下一步' }}
</view>
<view class="action-row">
<text class="link-text" @tap="backToPrev">返回上一步</text>
<text class="link-text" @tap="goBack">返回登录</text>
</view>
</view>
<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
class="field-input"
v-model="form.new_password"
type="password"
placeholder="新密码"
placeholder-class="field-placeholder"
/>
</view>
<view class="field">
<input
class="field-input"
v-model="form.confirm_password"
type="password"
placeholder="确认新密码"
placeholder-class="field-placeholder"
/>
</view>
<view class="btn-primary" :class="{ disabled: loading }" @tap="handleResetPassword">
{{ loading ? '提交中...' : '重置密码' }}
</view>
<view class="action-row">
<text class="link-text" @tap="backToPrev">返回上一步</text>
<text class="link-text" @tap="goBack">返回登录</text>
</view>
</view>
</view>
</view>
</view>
</template>
<script setup>
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,
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)
const countdown = ref(0)
let timer = null
const form = reactive({
tenant_name: getTenantName() || '',
account: '',
phone: '',
mobile: '',
email: '',
sms_code: '',
new_password: '',
confirm_password: ''
})
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() {
countdown.value = 60
if (timer) clearInterval(timer)
timer = setInterval(() => {
countdown.value -= 1
if (countdown.value <= 0) {
clearInterval(timer)
timer = null
}
}, 1000)
}
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
codeLoading.value = true
try {
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) {
} finally {
codeLoading.value = false
}
}
function handleNextFromCode() {
if (!form.sms_code.trim()) {
uni.showToast({ title: '请输入验证码', icon: 'none' })
return
}
currentStep.value = 3
}
async function handleResetPassword() {
if (loading.value) return
if (!form.new_password) {
uni.showToast({ title: '请输入新密码', icon: 'none' })
return
}
if (form.new_password !== form.confirm_password) {
uni.showToast({ title: '两次密码不一致', icon: 'none' })
return
}
if (form.new_password.length < 6) {
uni.showToast({ title: '密码长度不能少于6个字符', icon: 'none' })
return
}
if (!form.sms_code.trim()) {
uni.showToast({ title: '请先获取并输入验证码', icon: 'none' })
return
}
loading.value = true
try {
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' })
}, 600)
} catch (err) {
} 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' }) })
}
onUnmounted(() => {
if (timer) clearInterval(timer)
})
</script>
<style lang="scss" scoped>
@import '@/src/styles/page-common.scss';
.auth-page {
min-height: 100vh;
background: $color-bg-page;
}
.page-inner {
padding: 0 48rpx;
padding-top: calc(var(--status-bar-height, 44px) + 24rpx);
/* #ifdef MP-WEIXIN */
padding-top: calc(env(safe-area-inset-top, 25px) + 88rpx);
/* #endif */
padding-bottom: 60rpx;
}
.nav-back {
display: flex;
align-items: center;
gap: 4rpx;
margin-bottom: 32rpx;
padding: 8rpx 0;
}
.nav-text {
font-size: 28rpx;
color: $color-text;
}
.header {
margin-bottom: 40rpx;
}
.title {
display: block;
font-size: 44rpx;
font-weight: 600;
color: $color-text;
}
.subtitle {
display: block;
font-size: 26rpx;
color: $color-text-muted;
margin-top: 12rpx;
}
.form-card {
@include card;
padding: 32rpx 28rpx;
}
.form {
display: flex;
flex-direction: column;
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;
padding: 0 24rpx;
min-height: 96rpx;
}
.field-input {
width: 100%;
height: 96rpx;
line-height: 96rpx;
font-size: 32rpx;
color: $color-text;
background: transparent;
border: none;
outline: none;
box-sizing: border-box;
-webkit-appearance: none;
}
.field-placeholder {
color: $color-text-muted;
font-size: 32rpx;
}
.field-row {
display: flex;
align-items: center;
.field-input {
flex: 1;
min-width: 0;
}
}
.code-link {
flex-shrink: 0;
font-size: 26rpx;
color: $color-primary;
padding-left: 16rpx;
white-space: nowrap;
&.disabled {
color: $color-text-muted;
}
}
.code-tip {
font-size: 22rpx;
line-height: 1.5;
color: $color-text-muted;
}
.info-box {
background: $color-bg-page;
border-radius: $radius-md;
padding: 24rpx;
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8rpx;
}
.info-label {
font-size: 26rpx;
color: $color-text-muted;
}
.info-value {
font-size: 26rpx;
color: $color-text;
font-weight: 500;
}
.empty-contact {
background: $color-bg-page;
border-radius: $radius-md;
padding: 24rpx;
text-align: center;
font-size: 26rpx;
color: $color-text-muted;
}
.btn-primary {
height: 96rpx;
background: $color-primary;
border-radius: $radius-md;
display: flex;
align-items: center;
justify-content: center;
font-size: 32rpx;
font-weight: 600;
color: #fff;
margin-top: 12rpx;
&:active {
background: $color-primary-dark;
}
&.disabled {
opacity: 0.7;
}
}
.action-row {
display: flex;
justify-content: space-between;
gap: 16rpx;
margin-top: 8rpx;
}
.link-text {
flex: 1;
text-align: center;
font-size: 26rpx;
color: $color-primary;
padding: 12rpx 0;
}
</style>