230 lines
8.0 KiB
JavaScript
230 lines
8.0 KiB
JavaScript
// 页面权限配置
|
||
const pageConfig = {
|
||
// 公开页面(不需要登录)
|
||
publicPages: [
|
||
'/pages/index/index', // 首页
|
||
'/pages/beer/beerList', // 选酒页面
|
||
'/pages/activityList/activityList', // 活动列表
|
||
'/pagesActivity/activityDetail', // 活动详情
|
||
'/pages/index/chooseLogin', // 登录页面
|
||
'/pages/index/registration', // 注册页面
|
||
'/pages/index/userAgreement', // 用户协议
|
||
'/pages/index/review', // 酒评页面
|
||
],
|
||
|
||
// 需要登录的页面(不需要认证门店)
|
||
needLoginPages: [
|
||
'/pages/index/scan', // 扫码
|
||
'/pages/index/my', // 个人中心
|
||
'/pages/index/myJoin', // 我参与的活动
|
||
'/pages/index/bar' // 酒吧页面
|
||
],
|
||
|
||
// 需要认证门店的页面(必须先登录)
|
||
needAuthPages: [
|
||
'/pages/index/scan', // 扫码
|
||
'/pages/index/myJoin', // 我参与的活动
|
||
'/pages/index/bar' // 酒吧页面
|
||
]
|
||
}
|
||
|
||
// API权限配置
|
||
const apiConfig = {
|
||
// 公开API(不需要登录)
|
||
publicApis: [
|
||
'/beer/list', // 啤酒列表
|
||
'/beer/detail', // 啤酒详情
|
||
'/activity/list', // 活动列表
|
||
'/activity/detail', // 活动详情
|
||
'/bar/detail', // 酒吧详情
|
||
'/bar/list', // 酒吧列表
|
||
'/common/', // 公共接口
|
||
],
|
||
|
||
// 需要登录的API(不需要认证门店)
|
||
needLoginApis: [
|
||
'/user/info', // 用户信息
|
||
'/user/activity/list', // 用户活动列表
|
||
'/user/activity/detail', // 用户活动详情
|
||
'/user/scan', // 用户扫码
|
||
'/user/scan/result', // 扫码结果
|
||
'/user/scan/history', // 扫码历史
|
||
'/user/scan/statistics', // 扫码统计
|
||
'/user/scan/rank', // 扫码排名
|
||
'/user/scan/reward', // 扫码奖励
|
||
'/user/scan/reward/list', // 扫码奖励列表
|
||
'/user/scan/reward/detail', // 扫码奖励详情
|
||
'/user/scan/reward/record', // 扫码奖励记录
|
||
'/user/scan/reward/statistics', // 扫码奖励统计
|
||
'/user/scan/reward/rank', // 扫码奖励排名
|
||
'/user/scan/reward/exchange', // 扫码奖励兑换
|
||
'/user/scan/reward/exchange/list', // 扫码奖励兑换列表
|
||
'/user/scan/reward/exchange/detail', // 扫码奖励兑换详情
|
||
'/user/scan/reward/exchange/record', // 扫码奖励兑换记录
|
||
'/user/scan/reward/exchange/statistics', // 扫码奖励兑换统计
|
||
'/user/scan/reward/exchange/rank', // 扫码奖励兑换排名
|
||
'/user/scan/reward/exchange/exchange', // 扫码奖励兑换
|
||
'/user/scan/reward/exchange/exchange/list', // 扫码奖励兑换列表
|
||
'/user/scan/reward/exchange/exchange/detail', // 扫码奖励兑换详情
|
||
'/user/scan/reward/exchange/exchange/record', // 扫码奖励兑换记录
|
||
'/user/scan/reward/exchange/exchange/statistics', // 扫码奖励兑换统计
|
||
'/user/scan/reward/exchange/exchange/rank' // 扫码奖励兑换排名
|
||
],
|
||
|
||
// 需要认证门店的API(必须先登录)
|
||
needAuthApis: [
|
||
'/bar/info', // 酒吧信息
|
||
'/bar/update', // 更新酒吧信息
|
||
'/bar/activity/create', // 创建活动
|
||
'/bar/activity/update', // 更新活动
|
||
'/bar/activity/delete', // 删除活动
|
||
'/bar/activity/list', // 活动列表
|
||
'/bar/activity/detail', // 活动详情
|
||
'/bar/activity/statistics', // 活动统计
|
||
'/bar/activity/rank', // 活动排名
|
||
'/bar/activity/reward', // 活动奖励
|
||
'/bar/activity/reward/list', // 活动奖励列表
|
||
'/bar/activity/reward/detail', // 活动奖励详情
|
||
'/bar/activity/reward/record', // 活动奖励记录
|
||
'/bar/activity/reward/statistics', // 活动奖励统计
|
||
'/bar/activity/reward/rank', // 活动奖励排名
|
||
'/bar/activity/reward/exchange', // 活动奖励兑换
|
||
'/bar/activity/reward/exchange/list', // 活动奖励兑换列表
|
||
'/bar/activity/reward/exchange/detail', // 活动奖励兑换详情
|
||
'/bar/activity/reward/exchange/record', // 活动奖励兑换记录
|
||
'/bar/activity/reward/exchange/statistics', // 活动奖励兑换统计
|
||
'/bar/activity/reward/exchange/rank' // 活动奖励兑换排名
|
||
]
|
||
}
|
||
|
||
// 检查登录状态
|
||
export const checkLogin = () => {
|
||
const token = uni.getStorageSync('token')
|
||
const userInfo = uni.getStorageSync('userInfo')
|
||
return !!(token && userInfo)
|
||
}
|
||
|
||
// 检查认证门店状态
|
||
export const checkAuth = () => {
|
||
const userInfo = uni.getStorageSync('userInfo')
|
||
return userInfo && userInfo.barId
|
||
}
|
||
|
||
// 获取用户信息
|
||
export const getUserInfo = () => {
|
||
return uni.getStorageSync('userInfo')
|
||
}
|
||
|
||
// 页面权限检查
|
||
export const checkPagePermission = (url) => {
|
||
const path = url.split('?')[0]
|
||
|
||
// 检查是否是公开页面
|
||
if (pageConfig.publicPages.includes(path)) {
|
||
return { needLogin: false, needAuth: false }
|
||
}
|
||
|
||
// 检查是否需要登录
|
||
if (pageConfig.needLoginPages.includes(path)) {
|
||
return { needLogin: true, needAuth: false }
|
||
}
|
||
|
||
// 检查是否需要认证门店
|
||
if (pageConfig.needAuthPages.includes(path)) {
|
||
return { needLogin: true, needAuth: true }
|
||
}
|
||
|
||
// 默认需要登录
|
||
return { needLogin: true, needAuth: false }
|
||
}
|
||
|
||
// API权限检查
|
||
export const checkApiPermission = (url) => {
|
||
// 检查是否是公开API
|
||
if (apiConfig.publicApis.some(api => url.includes(api))) {
|
||
return { needLogin: false, needAuth: false }
|
||
}
|
||
|
||
// 检查是否需要登录
|
||
if (apiConfig.needLoginApis.some(api => url.includes(api))) {
|
||
return { needLogin: true, needAuth: false }
|
||
}
|
||
|
||
// 检查是否需要认证门店
|
||
if (apiConfig.needAuthApis.some(api => url.includes(api))) {
|
||
return { needLogin: true, needAuth: true }
|
||
}
|
||
|
||
// 默认需要登录
|
||
return { needLogin: true, needAuth: false }
|
||
}
|
||
|
||
// 页面访问控制
|
||
export const requirePagePermission = (url) => {
|
||
const { needLogin, needAuth } = checkPagePermission(url)
|
||
|
||
if (needLogin && !checkLogin()) {
|
||
uni.navigateTo({
|
||
url: '/pages/index/chooseLogin'
|
||
})
|
||
return false
|
||
}
|
||
|
||
if (needAuth && !checkAuth()) {
|
||
uni.navigateTo({
|
||
url: '/pages/index/registration'
|
||
})
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
// 清除登录状态
|
||
export const clearLoginStatus = () => {
|
||
uni.removeStorageSync('token')
|
||
uni.removeStorageSync('userInfo')
|
||
uni.removeStorageSync('barInfo')
|
||
}
|
||
|
||
// 更新用户信息
|
||
export const updateUserInfo = (userInfo) => {
|
||
uni.setStorageSync('userInfo', userInfo)
|
||
}
|
||
|
||
// 检查是否需要重新登录
|
||
export const checkNeedRelogin = () => {
|
||
const token = uni.getStorageSync('token')
|
||
if (!token) {
|
||
clearLoginStatus()
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
|
||
// 检查是否需要重新认证门店
|
||
export const checkNeedReAuth = () => {
|
||
const token = uni.getStorageSync('token')
|
||
const userInfo = uni.getStorageSync('userInfo')
|
||
return token && (!userInfo || !userInfo.barId)
|
||
}
|
||
|
||
// 检查是否需要认证门店
|
||
export function checkPageNeedAuth(url) {
|
||
return pageConfig.needAuthPages.some(page => url.startsWith(page))
|
||
}
|
||
|
||
// 检查是否需要认证门店
|
||
export function checkApiNeedAuth(url) {
|
||
return apiConfig.needAuthApis.some(api => url.includes(api))
|
||
}
|
||
|
||
// 获取token
|
||
export const getToken = () => {
|
||
return uni.getStorageSync('token')
|
||
}
|
||
|
||
// 移除token
|
||
export const removeToken = () => {
|
||
uni.removeStorageSync('token')
|
||
}
|