文件
fenleixinxi/uniapp/pages/auth/login.vue
MiAn 25a3a3d52f feat: 新增安全导航工具并全量替换原生路由调用,解决页面连击、栈超限与白屏问题
新增全局安全导航工具 safeNavigateTo/safeRedirectTo/safeNavigateBack,实现防连击锁、页面栈超限降级、无历史页平滑重定向逻辑。全量替换所有页面及组件中的 uni 原生路由调用,包括充值页、商家详情页、优惠券页、收藏页、订单页、会员中心、首页、登录页等多个页面,同时更新 tabbar 跳转逻辑杜绝 reLaunch 白屏,新增路由工具单元测试,优化分类页空数据兜底提示,提升页面跳转稳定性与用户体验。
2026-11-04 10:59:49 +08:00

807 行
19 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="login fc-page">
<!-- 1. 顶部沉浸天幕插画(底部自然羽化融入纯净背景) -->
<view class="login__hero-wrap">
<image
class="login__hero-img"
src="/static/banners/login-hero-planet.svg"
mode="aspectFill"
/>
</view>
<!-- 2. 主体流式交互区:左对齐大标题 -> 核心操作按钮 -> 底部协议行 -->
<view class="login__content">
<!-- 大标题区 -->
<view class="login__header">
<text class="login__title">欢迎登录</text>
<text class="login__subtitle">{{ siteName }} · 本地生活便民服务</text>
</view>
<!-- 核心操作区 -->
<view class="login__action">
<!-- #ifdef MP-WEIXIN -->
<!-- 微信小程序端:已勾选协议时渲染原生微信手机号授权按钮 -->
<button
v-if="agreed"
key="btn-agreed"
class="login__pill-btn"
:class="{ 'login__pill-btn--disabled': loading }"
open-type="getPhoneNumber"
@getphonenumber="onGetPhoneNumber"
>
<text class="login__pill-btn-text">{{ loading ? '正在安全登录…' : '手机号快捷登录' }}</text>
</button>
<!-- 微信小程序端:未勾选协议时渲染普通按钮,点击立即弹出服务与隐私协议确认弹窗 -->
<button
v-else
key="btn-unagreed"
class="login__pill-btn"
:class="{ 'login__pill-btn--disabled': loading }"
@click="openAgreeModal"
>
<text class="login__pill-btn-text">{{ loading ? '正在安全登录…' : '手机号快捷登录' }}</text>
</button>
<!-- #endif -->
<!-- #ifndef MP-WEIXIN -->
<!-- 非微信环境:手机号 + 密码表单 -->
<view class="login__h5-form">
<view class="login__input-wrap">
<image class="login__input-icon" :src="iconUrl('phone')" mode="aspectFit" />
<input
class="login__input"
type="number"
v-model="h5Form.phone"
maxlength="11"
placeholder="请输入11位手机号"
placeholder-class="login__placeholder"
/>
</view>
<view class="login__input-wrap">
<image class="login__input-icon" :src="iconUrl('lock')" mode="aspectFit" />
<input
class="login__input"
type="password"
v-model="h5Form.password"
maxlength="32"
placeholder="请输入登录密码(默认 123456)"
placeholder-class="login__placeholder"
/>
</view>
<button
class="login__pill-btn"
:class="{ 'login__pill-btn--disabled': loading }"
@click="onPasswordLoginClick"
>
<text class="login__pill-btn-text">{{ loading ? '正在登录…' : '登 录' }}</text>
</button>
</view>
<!-- #endif -->
<!-- 暂不登录辅助入口(极简呼吸感,符合微信审核规范:不得强制阻断) -->
<view class="login__skip-wrap" @click.stop="handleSkipLogin">
<text class="login__skip-text">暂不登录,先去逛逛 ›</text>
</view>
</view>
<!-- 3. 协议主动勾选行(支持微摇晃动画提示) -->
<view
class="login__agreement-row"
:class="{ 'login__agreement-row--shake': shakeAgreement }"
>
<view
class="login__checkbox"
:class="{ 'login__checkbox--active': agreed }"
@click="toggleAgree"
>
<text v-if="agreed" class="login__check-tick">✓</text>
</view>
<view class="login__agreement-text">
<text class="login__text-plain" @click="toggleAgree">已阅读并同意</text>
<text class="login__text-link" @click.stop="goAgreement('service')">《服务条款》</text>
<text class="login__text-plain" @click="toggleAgree">和</text>
<text class="login__text-link" @click.stop="goAgreement('privacy')">《隐私权政策》</text>
</view>
</view>
</view>
<!-- 4. 自定义协议确认模态弹窗(极简文案,清晰直接) -->
<view v-if="showAgreeModal" class="login__modal-mask" @touchmove.stop.prevent>
<view class="login__modal-box">
<view class="login__modal-header">
<text class="login__modal-title">服务与隐私政策</text>
</view>
<view class="login__modal-body">
<view class="login__modal-text-wrap">
<text class="login__modal-desc">请阅读并同意</text>
<text class="login__modal-link" @click.stop="goAgreement('service')">《服务条款》</text>
<text class="login__modal-desc">与</text>
<text class="login__modal-link" @click.stop="goAgreement('privacy')">《隐私权政策》</text>
</view>
</view>
<view class="login__modal-footer">
<button class="login__modal-btn login__modal-btn--cancel" @click="closeAgreeModal">
<text class="login__modal-btn-text login__modal-btn-text--cancel">不同意</text>
</button>
<button class="login__modal-btn login__modal-btn--confirm" @click="confirmAndAgree">
<text class="login__modal-btn-text login__modal-btn-text--confirm">同意并继续</text>
</button>
</view>
</view>
</view>
</view>
</template>
<script>
/**
* 登录页面
* 微信小程序合规与交互规范:
* 1. 按钮明确展示“手机号快捷登录”,调用 open-type="getPhoneNumber" 授权获取手机号;
* 2. 授权后通过后端微信官方 API 实时换取手机号并建档登录;
* 3. 遵照微信审核规范提供显著的“返回”和“暂不登录,先去逛逛”通道;
* 4. 未勾选协议时点击登录:弹出自定义确认弹窗,确认后自动勾选协议,然后用户再次点击登录调起微信手机号授权。
*/
import { useUserStore } from '@/common/store/index.js'
import authApi from '@/common/api/auth.js'
import { iconUrl, logoUrl, loadUiConfig, uiState } from '@/common/store/ui.js'
import { getNavMetrics } from '@/common/utils/navbar.js'
import { safeNavigateTo, safeRedirectTo, safeNavigateBack } from '@/common/utils/nav.js'
export default {
data() {
return {
loading: false,
agreed: false, // 协议勾选状态
shakeAgreement: false, // 未勾选时的微摇晃动画
showAgreeModal: false, // 协议确认模态弹窗显隐
redirectUrl: '', // 登录成功后回跳地址
navTop: 24, // 导航栏顶部安全距离(根据设备动态计算)
navBarHeight: 44, // 导航栏标准高度
h5Form: {
phone: '',
password: '',
},
}
},
computed: {
/**
* 站点品牌名称
* @returns {string}
*/
siteName() {
return uiState.siteName || '同城分类信息'
},
},
/**
* 页面加载
* @param {Object} query 路由参数(支持携带 redirect 回跳地址)
*/
onLoad(query) {
const metrics = getNavMetrics()
if (metrics) {
this.navTop = metrics.statusBarHeight || 24
this.navBarHeight = metrics.navBarHeight || 44
}
if (query && query.redirect) {
try {
this.redirectUrl = decodeURIComponent(query.redirect)
} catch (e) {
this.redirectUrl = query.redirect
}
}
loadUiConfig()
},
methods: {
iconUrl,
logoUrl,
/**
* 取消登录或返回上一页(若无页面栈历史则平滑安全重定向至首页)
*/
handleCancelOrBack() {
const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []
if (pages && pages.length > 1) {
const prevPage = pages[pages.length - 2]
const prevRoute = prevPage && (prevPage.route || (prevPage.$page && prevPage.$page.route) || '')
// 若上一页是必须登录的受限页面,返回上一页会导致受限页面再次触发登录拦截造成死循环,因此直接回首页
const restrictedPages = ['pages/member/orders', 'pages/member/coupons', 'pages/distribution', 'pages/post/create']
const isRestricted = restrictedPages.some(r => prevRoute.includes(r))
if (!isRestricted && !prevRoute.includes('pages/auth/login')) {
safeNavigateBack('/pages/index/index', 1)
return
}
}
// 兜底平滑返回首页,杜绝 switchTab 报错及 reLaunch 白屏
safeRedirectTo('/pages/index/index')
},
/**
* 暂不登录,直接返回首页以游客身份浏览无需登录公开内容(符合微信审核规范)
*/
handleSkipLogin() {
safeRedirectTo('/pages/index/index')
},
/**
* 切换协议勾选
*/
toggleAgree() {
this.agreed = !this.agreed
if (this.agreed) {
this.shakeAgreement = false
}
},
/**
* 跳转协议独立页面
* @param {'service'|'privacy'} type 协议类型
*/
goAgreement(type) {
safeNavigateTo(`/pages/auth/agreement?type=${type}`)
},
/**
* 未勾选协议时点击登录:打开自定义协议确认模态弹窗
*/
openAgreeModal() {
if (this.loading) return
this.showAgreeModal = true
},
/**
* 关闭协议确认模态弹窗
*/
closeAgreeModal() {
this.showAgreeModal = false
},
/**
* 弹窗中点击“同意并继续”:关闭弹窗,自动勾选协议,并轻提示引导再次点击登录
*/
confirmAndAgree() {
this.agreed = true
this.showAgreeModal = false
this.shakeAgreement = false
uni.showToast({
title: '已同意,请点击登录',
icon: 'none',
duration: 2000,
})
},
/**
* 获取微信登录临时凭证 code
* @returns {Promise<string>}
*/
getWxCode() {
return new Promise((resolve, reject) => {
const fail = () => reject(new Error('微信授权失败,请重试'))
// #ifdef MP-WEIXIN
try {
if (typeof uni !== 'undefined' && uni.login) {
uni.login({
provider: 'weixin',
success: (r) => (r && r.code ? resolve(r.code) : fail()),
fail,
})
return
}
} catch (e) {}
// #endif
fail()
})
},
/**
* 微信手机号授权回调处理
* @param {Object} e 微信原生组件回调
*/
async onGetPhoneNumber(e) {
if (this.loading) return
const detail = (e && e.detail) || {}
const phoneCode = detail.code || ''
const errMsg = detail.errMsg || ''
if (!phoneCode) {
if (errMsg.includes('deny') || errMsg.includes('cancel') || errMsg.includes('reject')) {
uni.showToast({ title: '已取消手机号授权', icon: 'none' })
} else if (errMsg) {
uni.showToast({ title: '授权异常,请重试', icon: 'none' })
}
return
}
await this.executeWxLogin(phoneCode)
},
/**
* 执行快捷登录(凭证换取账号及手机号)
* @param {string} phoneCode 微信手机号凭证
*/
async executeWxLogin(phoneCode) {
this.loading = true
const store = useUserStore()
try {
const wxCode = await this.getWxCode()
await store.login({
code: wxCode,
phoneCode,
})
try {
await store.fetchProfile()
} catch (e) {}
uni.showToast({ title: '登录成功', icon: 'success' })
setTimeout(() => {
this.redirectAfterLogin()
}, 400)
} catch (err) {
uni.showToast({ title: (err && err.message) || '登录失败,请重试', icon: 'none' })
} finally {
this.loading = false
}
},
/**
* 检查协议勾选状态(供非微信环境密码登录使用)
* @returns {boolean} 是否已勾选协议
*/
checkAgreement() {
if (this.agreed) {
return true
}
this.shakeAgreement = false
this.$nextTick(() => {
this.shakeAgreement = true
setTimeout(() => {
this.shakeAgreement = false
}, 600)
})
this.openAgreeModal()
return false
},
/**
* 手机号+密码登录提交处理(供非小程序端调试使用)
*/
async onPasswordLoginClick() {
if (this.loading) return
const phone = String(this.h5Form.phone || '').trim()
const password = String(this.h5Form.password || '')
if (!phone) {
uni.showToast({ title: '请输入手机号', icon: 'none' })
return
}
if (!/^1\d{10}$/.test(phone)) {
uni.showToast({ title: '请输入正确的11位手机号', icon: 'none' })
return
}
if (!password) {
uni.showToast({ title: '请输入登录密码', icon: 'none' })
return
}
if (password.length < 6) {
uni.showToast({ title: '密码长度不能少于6位', icon: 'none' })
return
}
if (!this.checkAgreement()) return
this.loading = true
try {
const res = await authApi.passwordLogin({ phone, password })
const store = useUserStore()
store.state.token = res.token
store.state.userInfo = res.userInfo
store.state.userType = Number(res.userInfo.userType || 1)
uni.setStorageSync('fc_token', res.token)
uni.setStorageSync('fc_user_info', res.userInfo)
uni.showToast({ title: '登录成功', icon: 'success' })
setTimeout(() => {
this.redirectAfterLogin()
}, 400)
} catch (err) {
uni.showToast({ title: (err && err.message) || '登录失败', icon: 'none' })
} finally {
this.loading = false
}
},
/**
* 登录成功后智能跳转
*/
redirectAfterLogin() {
if (this.redirectUrl && !this.redirectUrl.includes('pages/auth/login')) {
safeRedirectTo(this.redirectUrl)
return
}
const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []
if (pages && pages.length > 1) {
const prevPage = pages[pages.length - 2]
const prevRoute = prevPage && (prevPage.route || (prevPage.$page && prevPage.$page.route) || '')
if (prevRoute && !prevRoute.includes('pages/auth/login')) {
safeNavigateBack('/pages/mine/index', 1)
return
}
}
safeRedirectTo('/pages/mine/index')
},
},
}
</script>
<style lang="scss" scoped>
/* 页面根容器:纯净底色,通透轻盈 */
.login {
min-height: 100vh;
position: relative;
background: #ffffff;
display: flex;
flex-direction: column;
box-sizing: border-box;
overflow-x: hidden;
}
/* 1. 顶部沉浸天幕背景:羽化融入底部纯白 */
.login__hero-wrap {
position: relative;
width: 100%;
height: 540rpx;
overflow: hidden;
background: #ffffff;
mask-image: linear-gradient(to bottom, #000000 68%, transparent 100%);
-webkit-mask-image: linear-gradient(to bottom, #000000 68%, transparent 100%);
}
.login__hero-img {
width: 100%;
height: 100%;
display: block;
}
/* 2. 主体交互内容区 */
.login__content {
position: relative;
z-index: 2;
flex: 1;
display: flex;
flex-direction: column;
padding: 10rpx 56rpx calc(60rpx + env(safe-area-inset-bottom));
box-sizing: border-box;
}
/* 大标题区(左对齐,现代高级字色搭配微光) */
.login__header {
display: flex;
flex-direction: column;
align-items: flex-start;
margin-bottom: 56rpx;
}
.login__title {
font-size: 56rpx;
font-weight: 800;
letter-spacing: 2rpx;
line-height: 1.2;
color: #1e293b; /* 规范现代高级黑灰 */
}
.login__subtitle {
margin-top: 14rpx;
font-size: 26rpx;
color: #64748b; /* 规范中性辅助灰,杜绝暗墨绿 */
letter-spacing: 1rpx;
}
/* 核心操作区 */
.login__action {
width: 100%;
}
/* 饱满流光大药丸主按钮 */
.login__pill-btn {
width: 100%;
height: 104rpx;
border-radius: 52rpx;
background: linear-gradient(90deg, #00e070 0%, #00d668 50%, #00d26a 100%);
box-shadow: 0 14rpx 36rpx rgba(0, 224, 112, 0.32);
display: flex;
align-items: center;
justify-content: center;
border: none;
padding: 0;
margin: 0;
box-sizing: border-box;
transition: transform 0.15s ease, opacity 0.15s ease;
&::after {
border: none;
}
&:active {
transform: scale(0.985);
opacity: 0.92;
}
}
.login__pill-btn--disabled {
opacity: 0.55;
pointer-events: none;
}
.login__pill-btn-text {
font-size: 34rpx;
font-weight: 700;
color: #04301e;
letter-spacing: 2rpx;
pointer-events: none;
}
/* 暂不登录辅助入口(极简轻盈,扩大点击判定热区) */
.login__skip-wrap {
display: flex;
align-items: center;
justify-content: center;
margin-top: 28rpx;
padding: 24rpx 0;
cursor: pointer;
}
.login__skip-text {
font-size: 28rpx;
color: #64748b;
font-weight: 500;
letter-spacing: 1rpx;
transition: color 0.15s ease;
&:active {
color: #00d26a;
}
}
/* H5 调试模式表单 */
.login__h5-form {
width: 100%;
display: flex;
flex-direction: column;
}
.login__input-wrap {
width: 100%;
height: 96rpx;
background: #f7faf8;
border-radius: 24rpx;
border: 1rpx solid #e2eee6;
display: flex;
align-items: center;
padding: 0 24rpx;
margin-bottom: 24rpx;
box-sizing: border-box;
transition: border-color 0.2s ease, background 0.2s ease;
&:focus-within {
background: #ffffff;
border-color: #00e070;
}
}
.login__input-icon {
width: 36rpx;
height: 36rpx;
margin-right: 18rpx;
opacity: 0.55;
flex-shrink: 0;
}
.login__input {
flex: 1;
font-size: 28rpx;
color: #1e293b;
height: 100%;
}
.login__placeholder {
font-size: 26rpx;
color: #94a3b8;
}
/* 协议主动勾选行(支持 Shake 微抖动动画) */
.login__agreement-row {
margin-top: 50rpx;
display: flex;
align-items: center;
justify-content: center;
padding: 8rpx 0;
transition: transform 0.15s ease;
&--shake {
animation: shake-anim 0.5s ease-in-out;
}
}
@keyframes shake-anim {
0%, 100% {
transform: translateX(0);
}
20%, 60% {
transform: translateX(-10rpx);
}
40%, 80% {
transform: translateX(10rpx);
}
}
.login__checkbox {
width: 30rpx;
height: 30rpx;
border-radius: 8rpx;
border: 2rpx solid #99c2aa;
background: #ffffff;
display: flex;
align-items: center;
justify-content: center;
margin-right: 12rpx;
flex-shrink: 0;
transition: all 0.15s ease;
cursor: pointer;
&--active {
background: #00d26a;
border-color: #00d26a;
}
}
.login__check-tick {
color: #ffffff;
font-size: 20rpx;
font-weight: 900;
line-height: 1;
}
.login__agreement-text {
display: flex;
flex-wrap: wrap;
align-items: center;
line-height: 1.4;
}
.login__text-plain {
font-size: 24rpx;
color: #64748b;
cursor: pointer;
}
.login__text-link {
font-size: 24rpx;
color: #00a854; /* 规范强调绿,高对比度清爽 */
font-weight: 500;
cursor: pointer;
}
/* 4. 自定义协议确认模态弹窗 */
.login__modal-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.62);
z-index: 9999;
display: flex;
align-items: center;
justify-content: center;
padding: 0 48rpx;
box-sizing: border-box;
}
.login__modal-box {
width: 100%;
background: #ffffff;
border-radius: 32rpx;
padding: 48rpx 40rpx 40rpx;
box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: center;
box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.2);
animation: modalPop 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes modalPop {
from {
opacity: 0;
transform: scale(0.9);
}
to {
opacity: 1;
transform: scale(1);
}
}
.login__modal-header {
margin-bottom: 24rpx;
}
.login__modal-title {
font-size: 34rpx;
font-weight: 700;
color: #1e293b;
}
.login__modal-body {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
margin-bottom: 36rpx;
}
.login__modal-text-wrap {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
line-height: 1.6;
}
.login__modal-desc {
font-size: 28rpx;
color: #475569;
}
.login__modal-link {
font-size: 28rpx;
font-weight: 600;
color: #00d26a;
}
.login__modal-footer {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24rpx;
}
.login__modal-btn {
flex: 1;
height: 84rpx;
border-radius: 42rpx;
display: flex;
align-items: center;
justify-content: center;
border: none;
padding: 0;
margin: 0;
box-sizing: border-box;
&::after {
border: none;
}
}
.login__modal-btn--cancel {
background: #f1f5f9;
&:active {
background: #e2e8f0;
}
}
.login__modal-btn--confirm {
background: linear-gradient(90deg, #00e070 0%, #00d26a 100%);
box-shadow: 0 8rpx 20rpx rgba(0, 210, 106, 0.3);
&:active {
opacity: 0.9;
}
}
.login__modal-btn-text {
font-size: 28rpx;
font-weight: 600;
pointer-events: none;
}
.login__modal-btn-text--cancel {
color: #64748b;
}
.login__modal-btn-text--confirm {
color: #04301e;
}
</style>