
实现用户中心完整功能,包括: 1. 新增登录页面及登录逻辑 2. 添加个人资料、修改密码、关于我们等子页面 3. 实现头像上传功能 4. 添加js-cookie依赖处理token存储 5. 完善用户信息类型定义和API接口 6. 新增tabbar"我的"入口及相关路由配置 新增工具函数: 1. 添加auth.ts处理认证相关逻辑 2. 实现toast.ts统一消息提示 3. 添加uploadFile.ts处理文件上传 4. 新增isTableBar判断页面是否为tabbar页
65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
/**
|
||
* by 菲鸽 on 2024-03-06
|
||
* 路由拦截,通常也是登录拦截
|
||
* 可以设置路由白名单,或者黑名单,看业务需要选哪一个
|
||
* 我这里应为大部分都可以随便进入,所以使用黑名单
|
||
*/
|
||
import { useUserStore } from '@/store'
|
||
import { needLoginPages as _needLoginPages, getNeedLoginPages, getLastPage } from '@/utils'
|
||
|
||
// TODO Check
|
||
const loginRoute = '/pages/login/index'
|
||
|
||
const isLogined = () => {
|
||
const userStore = useUserStore()
|
||
return !!userStore.userInfo.username
|
||
}
|
||
|
||
const isDev = import.meta.env.DEV
|
||
|
||
// 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录)
|
||
const navigateToInterceptor = {
|
||
// 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
|
||
// 增加对相对路径的处理,BY 网友 @ideal
|
||
invoke({ url }: { url: string }) {
|
||
// console.log(url) // /pages/route-interceptor/index?name=feige&age=30
|
||
let path = url.split('?')[0]
|
||
|
||
// 处理相对路径
|
||
if (!path.startsWith('/')) {
|
||
const currentPath = getLastPage().route
|
||
const normalizedCurrentPath = currentPath.startsWith('/') ? currentPath : `/${currentPath}`
|
||
const baseDir = normalizedCurrentPath.substring(0, normalizedCurrentPath.lastIndexOf('/'))
|
||
path = `${baseDir}/${path}`
|
||
}
|
||
|
||
let needLoginPages: string[] = []
|
||
// 为了防止开发时出现BUG,这里每次都获取一下。生产环境可以移到函数外,性能更好
|
||
if (isDev) {
|
||
needLoginPages = getNeedLoginPages()
|
||
} else {
|
||
needLoginPages = _needLoginPages
|
||
}
|
||
const isNeedLogin = needLoginPages.includes(path)
|
||
if (!isNeedLogin) {
|
||
return true
|
||
}
|
||
const hasLogin = isLogined()
|
||
if (hasLogin) {
|
||
return true
|
||
}
|
||
const redirectRoute = `${loginRoute}?redirect=${encodeURIComponent(url)}`
|
||
uni.navigateTo({ url: redirectRoute })
|
||
return false
|
||
},
|
||
}
|
||
|
||
export const routeInterceptor = {
|
||
install() {
|
||
uni.addInterceptor('navigateTo', navigateToInterceptor)
|
||
uni.addInterceptor('reLaunch', navigateToInterceptor)
|
||
uni.addInterceptor('redirectTo', navigateToInterceptor)
|
||
uni.addInterceptor('switchTab', navigateToInterceptor)
|
||
},
|
||
}
|