- 实现未登录/已登录两种状态样式 - 添加常用功能入口:我的订单、往期测评、联系我们、邀请新用户 - 添加其他功能入口:关于、用户协议、隐私政策、退出登录 - 实现退出登录二次确认弹窗 - 修复 uni.scss 中 SCSS 导入路径问题 - 整理 .gitignore 文件,移除 unpackage 构建目录
82 lines
1.7 KiB
JavaScript
82 lines
1.7 KiB
JavaScript
/**
|
|
* 认证相关组合式函数
|
|
*/
|
|
|
|
import { useUserStore } from '../store/user.js'
|
|
|
|
/**
|
|
* 认证相关功能
|
|
* @returns {Object}
|
|
*/
|
|
export function useAuth() {
|
|
const userStore = useUserStore()
|
|
|
|
/**
|
|
* 检查登录状态,未登录则跳转登录页
|
|
* @param {string} [redirectUrl] - 登录后重定向的页面路径
|
|
* @returns {boolean} 是否已登录
|
|
*/
|
|
function checkLogin(redirectUrl) {
|
|
if (userStore.isLoggedIn) {
|
|
return true
|
|
}
|
|
|
|
// 保存当前页面路径,登录后返回
|
|
const pages = getCurrentPages()
|
|
const currentPage = pages[pages.length - 1]
|
|
const url = redirectUrl || `/${currentPage.route}`
|
|
|
|
uni.navigateTo({
|
|
url: `/pages/login/index?redirect=${encodeURIComponent(url)}`
|
|
})
|
|
|
|
return false
|
|
}
|
|
|
|
/**
|
|
* 需要登录的操作装饰器
|
|
* @param {Function} fn - 需要登录才能执行的函数
|
|
* @returns {Function}
|
|
*/
|
|
function requireLogin(fn) {
|
|
return function(...args) {
|
|
if (!checkLogin()) {
|
|
return
|
|
}
|
|
return fn.apply(this, args)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 跳转到登录页
|
|
* @param {string} [redirect] - 登录后重定向的页面路径
|
|
*/
|
|
function goLogin(redirect) {
|
|
const url = redirect
|
|
? `/pages/login/index?redirect=${encodeURIComponent(redirect)}`
|
|
: '/pages/login/index'
|
|
|
|
uni.navigateTo({ url })
|
|
}
|
|
|
|
/**
|
|
* 登出并跳转到登录页
|
|
*/
|
|
function logoutAndRedirect() {
|
|
userStore.logout()
|
|
uni.reLaunch({
|
|
url: '/pages/login/index'
|
|
})
|
|
}
|
|
|
|
return {
|
|
checkLogin,
|
|
requireLogin,
|
|
goLogin,
|
|
logoutAndRedirect,
|
|
isLoggedIn: () => userStore.isLoggedIn
|
|
}
|
|
}
|
|
|
|
export default useAuth
|