修复审核不通过的代码
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);
|
||||
}
|
||||
Reference in New Issue
Block a user