修复审核不通过的代码
This commit is contained in:
85
huakai-app/pageSubPack/api/auth.js
Normal file
85
huakai-app/pageSubPack/api/auth.js
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* 登录状态管理工具
|
||||
* 仅在用户点击需要身份认证的功能时才要求登录
|
||||
*/
|
||||
|
||||
/**
|
||||
* 检查用户是否已登录
|
||||
* @returns {boolean} true 已登录,false 未登录
|
||||
*/
|
||||
export function isLoggedIn() {
|
||||
const token = uni.getStorageSync('token');
|
||||
return !!token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 要求登录 — 未登录时跳转登录页,已登录直接执行回调
|
||||
* 登录页提供返回/取消按钮,用户可拒绝登录
|
||||
*
|
||||
* @param {Object} options
|
||||
* @param {Function} [options.onLoggedIn] - 已登录或登录成功后执行的回调
|
||||
* @param {string} [options.title] - 展示给用户的提示标题(为什么需要登录)
|
||||
* @param {Function} [options.onCancel] - 用户取消登录后的回调
|
||||
*/
|
||||
export function requireAuth({ onLoggedIn, title, onCancel } = {}) {
|
||||
if (isLoggedIn()) {
|
||||
if (onLoggedIn) onLoggedIn();
|
||||
return;
|
||||
}
|
||||
|
||||
// 弹窗询问用户是否去登录,用户可拒绝("暂不")
|
||||
const tipTitle = title ? `"${title}"需要登录` : '该功能需要登录';
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: `${tipTitle},是否去登录?`,
|
||||
confirmText: '去登录',
|
||||
cancelText: '暂不',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
// 用户点击"去登录"
|
||||
uni.setStorageSync('pendingAuthCallback', {
|
||||
time: Date.now(),
|
||||
title: title || ''
|
||||
});
|
||||
|
||||
uni.navigateTo({
|
||||
url: '/pageSubPack/loginSub/pwd-login',
|
||||
events: {
|
||||
loginSuccess: () => {
|
||||
if (onLoggedIn) onLoggedIn();
|
||||
uni.removeStorageSync('pendingAuthCallback');
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 用户点击"暂不",留在当前页
|
||||
if (onCancel) onCancel();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除登录状态
|
||||
*/
|
||||
export function clearLoginState() {
|
||||
uni.removeStorageSync('token');
|
||||
uni.removeStorageSync('userId');
|
||||
uni.removeStorageSync('currentVillageId');
|
||||
uni.removeStorageSync('pendingAuthCallback');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存登录成功后的 token 和用户信息
|
||||
* @param {Object} data - 登录接口返回的 data
|
||||
*/
|
||||
export function saveLoginData(data) {
|
||||
if (data.token) {
|
||||
uni.setStorageSync('token', data.token);
|
||||
}
|
||||
if (data.userId) {
|
||||
uni.setStorageSync('userId', data.userId);
|
||||
}
|
||||
const currentVillageId = data.currentVillageId || 0;
|
||||
uni.setStorageSync('currentVillageId', currentVillageId);
|
||||
}
|
||||
@@ -42,12 +42,20 @@ export default function request(url, data = {}, method = "GET") {
|
||||
|
||||
const resData = res.data || {};
|
||||
|
||||
// token 过期
|
||||
// token 过期 — 清除登录态,提示用户,但不强制跳转(用户可继续浏览非敏感页面)
|
||||
if (resData.code === 401) {
|
||||
uni.showToast({ title: '登录过期,请重新登录', icon: 'none' });
|
||||
uni.clearStorageSync('token');
|
||||
uni.showToast({ title: '登录已过期,请重新登录', icon: 'none', duration: 2000 });
|
||||
uni.removeStorageSync('token');
|
||||
uni.removeStorageSync('userId');
|
||||
// 使用 navigateTo 而非 reLaunch,用户可通过返回按钮退出登录页
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({ url: '/pageSubPack/loginSub/pwd-login' });
|
||||
uni.navigateTo({
|
||||
url: '/pageSubPack/loginSub/pwd-login',
|
||||
fail: () => {
|
||||
// 如果 navigateTo 失败(如页面栈满),则用 reLaunch
|
||||
uni.reLaunch({ url: '/pageSubPack/loginSub/pwd-login' });
|
||||
}
|
||||
});
|
||||
}, 1500);
|
||||
reject({ msg: '登录过期', code: 401 });
|
||||
return;
|
||||
@@ -102,4 +110,4 @@ export const upload = (filePath, url) => {
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
<template>
|
||||
<view class="login-page">
|
||||
<view class="status_bar"></view>
|
||||
<!-- 自定义导航栏:返回按钮 — 用户可拒绝登录 -->
|
||||
<view class="custom-nav">
|
||||
<view class="back-btn" @click="handleCancel">
|
||||
<uni-icons type="arrow-left" size="24" color="#333"></uni-icons>
|
||||
<text class="back-text">返回</text>
|
||||
</view>
|
||||
</view>
|
||||
<!-- 登录标题 -->
|
||||
<view class="login-title">手机验证码登录</view>
|
||||
|
||||
@@ -13,14 +20,6 @@
|
||||
<uni-easyinput class="input-content" type="number" v-model="phoneNumber"
|
||||
placeholder="请输入手机号" :inputBorder="false" maxlength="11" :placeholderStyle="placeholderStyle">
|
||||
</uni-easyinput>
|
||||
<!-- <input
|
||||
class="input-content"
|
||||
type="number"
|
||||
placeholder="请输入手机号"
|
||||
v-model="phoneNumber"
|
||||
maxlength="11"
|
||||
@input="checkForm"
|
||||
/> -->
|
||||
</view>
|
||||
|
||||
<!-- 验证码输入框 + 获取验证码按钮 -->
|
||||
@@ -56,7 +55,7 @@
|
||||
v-else
|
||||
></uni-icons>
|
||||
</view>
|
||||
<text class="agreement-text" @click.stop="goToPrivacy">我已阅读并同意<text class="agreePrivacy">《用户隐私政策》</text></text>
|
||||
<text class="agreement-text" @click.stop="goToPrivacy">我已阅读并同意<text class="agreePrivacy" @click.stop="goToPrivacy">《用户隐私政策》</text></text>
|
||||
</view>
|
||||
|
||||
<!-- 登录按钮 -->
|
||||
@@ -79,6 +78,7 @@
|
||||
import { ref, computed, onUnmounted } from 'vue';
|
||||
import {getLoginBySms,getSendCode} from '../api/api.js'
|
||||
import { showApiError } from '../api/request.js'
|
||||
import { saveLoginData } from '../api/auth.js'
|
||||
|
||||
// ========== 响应式数据 ==========
|
||||
// 手机号
|
||||
@@ -109,6 +109,16 @@ const canLogin = computed(() => {
|
||||
});
|
||||
|
||||
// ========== 方法 ==========
|
||||
// 用户取消登录 — 返回上一页
|
||||
const handleCancel = () => {
|
||||
const pages = getCurrentPages();
|
||||
if (pages.length > 1) {
|
||||
uni.navigateBack();
|
||||
} else {
|
||||
uni.switchTab({ url: '/pages/paymentIndex/paymentIndex' });
|
||||
}
|
||||
};
|
||||
|
||||
// 检查表单状态
|
||||
const checkForm = () => {
|
||||
// 无需额外逻辑,计算属性已实时监听
|
||||
@@ -187,43 +197,24 @@ const handleLogin = () => {
|
||||
getLoginBySms(params).then(res =>{
|
||||
if(res.code === 200){
|
||||
if(res.data){
|
||||
uni.setStorageSync('token', res.data.token);
|
||||
// 存用户ID
|
||||
if(res.data.userId) {
|
||||
uni.setStorageSync('userId', res.data.userId);
|
||||
}
|
||||
// 存当前绑定的小区ID,0 表示未绑定
|
||||
const currentVillageId = res.data.currentVillageId || 0;
|
||||
uni.setStorageSync('currentVillageId', currentVillageId);
|
||||
saveLoginData(res.data);
|
||||
uni.hideLoading();
|
||||
uni.showToast({ title: '登录成功', icon: 'success' });
|
||||
setTimeout(() =>{
|
||||
uni.switchTab({ url: '/pages/paymentIndex/paymentIndex' });
|
||||
// 登录成功后返回来源页
|
||||
uni.navigateBack({
|
||||
fail: () => {
|
||||
uni.switchTab({ url: '/pages/paymentIndex/paymentIndex' });
|
||||
}
|
||||
});
|
||||
},1500)
|
||||
// }else{
|
||||
// uni.showToast({ title: '登录失败', icon: 'error' });
|
||||
}
|
||||
|
||||
// }else{
|
||||
// uni.showToast({ title:'登录失败', icon: 'error' });
|
||||
}
|
||||
|
||||
}).catch(err => {
|
||||
showApiError(err)
|
||||
})
|
||||
|
||||
|
||||
// 模拟登录请求
|
||||
// uni.showLoading({
|
||||
// title: '登录中...'
|
||||
// });
|
||||
// uni.showToast({
|
||||
// title:"登录成功",
|
||||
// icon:'success'
|
||||
// })
|
||||
// setTimeout(() =>{
|
||||
// uni.switchTab({ url: '/pages/paymentIndex/paymentIndex' });
|
||||
// },1500)
|
||||
uni.hideLoading()
|
||||
};
|
||||
|
||||
@@ -245,17 +236,35 @@ onUnmounted(() => {
|
||||
background: repeating-linear-gradient(to bottom, #E2F0FF, #F6FAFF );
|
||||
}
|
||||
.status_bar {
|
||||
height: 160px;
|
||||
height: var(--status-bar-height, 88rpx);
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* ===== 自定义导航:返回/取消按钮 ===== */
|
||||
.custom-nav {
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
.back-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 10rpx 20rpx;
|
||||
background: rgba(255,255,255,0.9);
|
||||
border-radius: 30rpx;
|
||||
box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.08);
|
||||
}
|
||||
.back-text {
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
margin-left: 6rpx;
|
||||
}
|
||||
|
||||
/* 登录标题 */
|
||||
.login-title {
|
||||
font-size: 52rpx;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
text-align: center;
|
||||
margin-bottom: 142rpx;
|
||||
margin-bottom: 80rpx;
|
||||
}
|
||||
|
||||
/* 输入框项 */
|
||||
@@ -356,9 +365,6 @@ onUnmounted(() => {
|
||||
.agreePrivacy{
|
||||
color: #2F77FD;
|
||||
}
|
||||
/* .agreePrivacy.active{
|
||||
color: #2F77FD;
|
||||
} */
|
||||
/* 登录按钮 */
|
||||
.login-btn {
|
||||
width: 100%;
|
||||
@@ -375,7 +381,6 @@ onUnmounted(() => {
|
||||
.login-btn:disabled {
|
||||
background-color: #99c8ff;
|
||||
color: #fff;
|
||||
/* border-radius: 50px; */
|
||||
}
|
||||
/* 禁用按钮边框 */
|
||||
button:after{
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
<template>
|
||||
<view class="login-page">
|
||||
<view class="status_bar"></view>
|
||||
<!-- 自定义导航栏:返回按钮 — 用户可拒绝登录 -->
|
||||
<view class="custom-nav">
|
||||
<view class="back-btn" @click="handleCancel">
|
||||
<uni-icons type="arrow-left" size="24" color="#333"></uni-icons>
|
||||
<text class="back-text">返回</text>
|
||||
</view>
|
||||
</view>
|
||||
<!-- 标题 -->
|
||||
<view class="login-title">账号密码登录</view>
|
||||
|
||||
@@ -42,11 +49,11 @@
|
||||
size="24"
|
||||
color="#999"
|
||||
v-else
|
||||
></uni-icons>
|
||||
></uni-icons>
|
||||
</view>
|
||||
|
||||
|
||||
<text class="agreement-text" @click.stop="goToPrivacy">我已阅读并同意
|
||||
<text class="agreePrivacy" >《用户隐私政策》</text>
|
||||
<text class="agreePrivacy" @click.stop="goToPrivacy">《用户隐私政策》</text>
|
||||
</text>
|
||||
</view>
|
||||
<navigator class="forget-pwd" url="/pageSubPack/loginSub/forget-pwd">忘记密码?</navigator>
|
||||
@@ -73,6 +80,7 @@ import { ref, computed } from 'vue';
|
||||
import {onLoad} from "@dcloudio/uni-app"
|
||||
import {getLoginPassword} from '../api/api.js'
|
||||
import { showApiError } from '../api/request.js'
|
||||
import { saveLoginData } from '../api/auth.js'
|
||||
|
||||
// 响应式数据
|
||||
const phoneNumber = ref('');
|
||||
@@ -103,6 +111,16 @@ const goToPrivacy = () => {
|
||||
// 检查表单
|
||||
const checkForm = () => {};
|
||||
|
||||
// 用户取消登录 — 返回上一页
|
||||
const handleCancel = () => {
|
||||
const pages = getCurrentPages();
|
||||
if (pages.length > 1) {
|
||||
uni.navigateBack();
|
||||
} else {
|
||||
uni.switchTab({ url: '/pages/paymentIndex/paymentIndex' });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
onLoad(() =>{
|
||||
});
|
||||
@@ -141,25 +159,18 @@ const handleLogin = () => {
|
||||
}
|
||||
getLoginPassword(params).then(res =>{
|
||||
if(res.code === 200){
|
||||
uni.setStorageSync('token', res.data.token);
|
||||
// 存用户ID
|
||||
if(res.data) {
|
||||
// 存当前绑定的小区ID,0 表示未绑定
|
||||
const currentVillageId = res.data.currentVillageId || 0;
|
||||
uni.setStorageSync('currentVillageId', currentVillageId);
|
||||
uni.setStorageSync('userId',res.data.userId)
|
||||
}
|
||||
saveLoginData(res.data);
|
||||
uni.hideLoading();
|
||||
uni.showToast({ title: '登录成功', icon: 'success' });
|
||||
setTimeout(() =>{
|
||||
uni.switchTab({
|
||||
url: '/pages/paymentIndex/paymentIndex'
|
||||
},1000);
|
||||
})
|
||||
// 登录成功后返回来源页
|
||||
uni.navigateBack({
|
||||
fail: () => {
|
||||
uni.switchTab({ url: '/pages/paymentIndex/paymentIndex' });
|
||||
}
|
||||
});
|
||||
},1000)
|
||||
}
|
||||
// else {
|
||||
// uni.showToast({ title: res.msg, icon: 'error' });
|
||||
// }
|
||||
|
||||
}).catch(err => {
|
||||
// console.log(err)
|
||||
@@ -177,15 +188,34 @@ const handleLogin = () => {
|
||||
background: repeating-linear-gradient(180deg, #E2F0FF, #F6FAFF );
|
||||
}
|
||||
.status_bar {
|
||||
height: 160px;
|
||||
height: var(--status-bar-height, 88rpx);
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* ===== 自定义导航:返回/取消按钮 ===== */
|
||||
.custom-nav {
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
.back-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 10rpx 20rpx;
|
||||
background: rgba(255,255,255,0.9);
|
||||
border-radius: 30rpx;
|
||||
box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.08);
|
||||
}
|
||||
.back-text {
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
margin-left: 6rpx;
|
||||
}
|
||||
|
||||
.login-title {
|
||||
font-size: 52rpx;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
text-align: center;
|
||||
margin-bottom: 142rpx;
|
||||
margin-bottom: 80rpx;
|
||||
}
|
||||
|
||||
.input-item {
|
||||
@@ -248,9 +278,6 @@ const handleLogin = () => {
|
||||
.agreePrivacy{
|
||||
color: #2F77FD;
|
||||
}
|
||||
/* .agreePrivacy.active{
|
||||
color: #2F77FD;
|
||||
} */
|
||||
|
||||
.forget-pwd {
|
||||
font-size: 24rpx;
|
||||
@@ -272,7 +299,6 @@ const handleLogin = () => {
|
||||
.login-btn:disabled {
|
||||
background-color: #99c8ff;
|
||||
color: #fff;
|
||||
/* border-radius: 50px; */
|
||||
|
||||
}
|
||||
/* 禁用按钮边框 */
|
||||
@@ -291,4 +317,4 @@ button:after{
|
||||
color: #007aff;
|
||||
text-decoration: none;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user