修复权限缓存问题

This commit is contained in:
Zy
2026-07-20 16:43:09 +08:00
parent a5a883a652
commit 3a00c3dfe0
10 changed files with 343 additions and 306 deletions

View File

@@ -1,6 +1,8 @@
import App from './App' import App from './App'
import uviewPlus from '@/uni_modules/uview-plus' import uviewPlus from '@/uni_modules/uview-plus'
import {
createPinia
} from 'pinia'
// #ifndef VUE3 // #ifndef VUE3
import Vue from 'vue' import Vue from 'vue'
import './uni.promisify.adaptor' import './uni.promisify.adaptor'
@@ -19,6 +21,8 @@ import {
export function createApp() { export function createApp() {
const app = createSSRApp(App) const app = createSSRApp(App)
app.use(uviewPlus) app.use(uviewPlus)
const pinia = createPinia()
app.use(pinia)
return { return {
app app
} }

View File

@@ -54,9 +54,11 @@
"NSBluetoothPeripheralUsageDescription" : "需要蓝牙权限连接小区智能门禁,实现蓝牙一键开门通行,仅用于小区门禁解锁业务,不采集蓝牙设备信息。", "NSBluetoothPeripheralUsageDescription" : "需要蓝牙权限连接小区智能门禁,实现蓝牙一键开门通行,仅用于小区门禁解锁业务,不采集蓝牙设备信息。",
"NSBluetoothAlwaysUsageDescription" : "本应用需要蓝牙权限,用于扫描、连接小区蓝牙门禁设备实现一键开门,仅和本小区门禁硬件建立通信,不会扫描、连接无关蓝牙设备。" "NSBluetoothAlwaysUsageDescription" : "本应用需要蓝牙权限,用于扫描、连接小区蓝牙门禁设备实现一键开门,仅和本小区门禁硬件建立通信,不会扫描、连接无关蓝牙设备。"
}, },
"urltypes": [{ "urltypes" : [
{
"urlschemes" : [ "unionpaycashier" ] "urlschemes" : [ "unionpaycashier" ]
}], }
],
"idfa" : false "idfa" : false
}, },
"sdkConfigs" : { "sdkConfigs" : {

View File

@@ -2,6 +2,7 @@
"dependencies": { "dependencies": {
"clipboard": "^2.0.11", "clipboard": "^2.0.11",
"dayjs": "^1.11.21", "dayjs": "^1.11.21",
"pinia": "^4.0.2",
"uview-plus": "^3.8.55" "uview-plus": "^3.8.55"
}, },
"devDependencies": { "devDependencies": {

View File

@@ -100,7 +100,9 @@
import { ref, computed, onUnmounted } from 'vue' import { ref, computed, onUnmounted } from 'vue'
import { getLoginBySms, getSendCode } from '../../api/api.js' import { getLoginBySms, getSendCode } from '../../api/api.js'
import PrivacyPopup from '@/components/privacy-popup/privacy-popup.vue' import PrivacyPopup from '@/components/privacy-popup/privacy-popup.vue'
import { usePermStore } from '@/stores/usePermStore'
const permStore = usePermStore()
// ========== 响应式数据 ========== // ========== 响应式数据 ==========
// 手机号 // 手机号
const phoneNumber = ref('') const phoneNumber = ref('')
@@ -246,12 +248,13 @@ const handleLogin = () => {
scene: 'butler_login', scene: 'butler_login',
cid: push_cid cid: push_cid
} }
permStore.clearPerm()
getLoginBySms(params) getLoginBySms(params)
.then(res => { .then(res => {
if (res.code === 200) { if (res.code === 200) {
if (res.data) { if (res.data) {
uni.setStorageSync('token', res.data.token) uni.setStorageSync('token', res.data.token)
uni.setStorageSync('userPermission', res.data.user.permissions) permStore.setPerm(res.data.user.permissions)
// 存用户ID // 存用户ID
if (res.data.user.userId) { if (res.data.user.userId) {
uni.setStorageSync('userId', res.data.user.userId) uni.setStorageSync('userId', res.data.user.userId)

View File

@@ -99,7 +99,9 @@ import { ref, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app' import { onLoad } from '@dcloudio/uni-app'
import { getLoginPassword } from '../../api/api.js' import { getLoginPassword } from '../../api/api.js'
import PrivacyPopup from '@/components/privacy-popup/privacy-popup.vue' import PrivacyPopup from '@/components/privacy-popup/privacy-popup.vue'
import { usePermStore } from '@/stores/usePermStore'
const permStore = usePermStore()
// 响应式数据 // 响应式数据
const phoneNumber = ref('') const phoneNumber = ref('')
const password = ref('') const password = ref('')
@@ -201,6 +203,7 @@ const handleLogin = () => {
password: password.value, password: password.value,
cid: push_cid cid: push_cid
} }
permStore.clearPerm()
getLoginPassword(params) getLoginPassword(params)
.then(res => { .then(res => {
// console.log('33333',res) // console.log('33333',res)
@@ -214,7 +217,11 @@ const handleLogin = () => {
// // const currentVillageId = res.data.currentVillageId || 0; // // const currentVillageId = res.data.currentVillageId || 0;
// // uni.setStorageSync('currentVillageId', currentVillageId); // // uni.setStorageSync('currentVillageId', currentVillageId);
// } // }
uni.setStorageSync('userPermission', res.data.user.permissions) uni.setStorage({
key: 'userPermission',
data: res.data.user.permissions
})
permStore.setPerm(res.data.user.permissions)
uni.hideLoading() uni.hideLoading()
if (rememberPwd.value) { if (rememberPwd.value) {
uni.setStorageSync('rememberPhone', phoneNumber.value) uni.setStorageSync('rememberPhone', phoneNumber.value)

View File

@@ -29,9 +29,7 @@
</view> </view>
<!-- 空数据 --> <!-- 空数据 -->
<view class="empty" v-if="noticeList.length === 0 && !loading"> <view class="empty" v-if="noticeList.length === 0 && !loading"> 暂无通知消息 </view>
暂无通知消息
</view>
</view> </view>
</template> </template>
@@ -39,7 +37,9 @@
import { ref } from 'vue' import { ref } from 'vue'
import { onShow, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app' import { onShow, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
import { getNoticeList } from '../../api/api.js' import { getNoticeList } from '../../api/api.js'
import { usePermStore } from '@/stores/usePermStore.js'
const permStore = usePermStore()
// 列表数据 // 列表数据
const noticeList = ref([]) const noticeList = ref([])
@@ -59,7 +59,8 @@ const getNoticeData = (isRefresh = false) => {
getNoticeList({ getNoticeList({
pageSize: pageSize.value, pageSize: pageSize.value,
pageNum: pageNum.value pageNum: pageNum.value
}).then(res => { })
.then(res => {
if (res.code === 200) { if (res.code === 200) {
total.value = res.total || 0 total.value = res.total || 0
const data = res.rows || [] const data = res.rows || []
@@ -74,9 +75,11 @@ const getNoticeData = (isRefresh = false) => {
// 判断是否还有更多 // 判断是否还有更多
noMore.value = noticeList.value.length >= total.value noMore.value = noticeList.value.length >= total.value
} }
}).catch(() => { })
.catch(() => {
uni.showToast({ title: '加载失败', icon: 'none' }) uni.showToast({ title: '加载失败', icon: 'none' })
}).finally(() => { })
.finally(() => {
loading.value = false loading.value = false
if (isRefresh) { if (isRefresh) {
uni.stopPullDownRefresh() uni.stopPullDownRefresh()
@@ -99,11 +102,17 @@ onPullDownRefresh(() => {
}) })
// 跳详情(即时标记已读) // 跳详情(即时标记已读)
const goToDetail = (item) => { const goToDetail = item => {
const hasMine = permStore.hasPermi('butler:workOrder:mine')
const hasHall = permStore.hasPermi('butler:workOrder:hall')
if (hasMine || hasHall) {
item.isCheck = '1' item.isCheck = '1'
uni.navigateTo({ uni.navigateTo({
url: `/pageSubPack/ticket/repairMain?id=${item.id}` url: `/pageSubPack/ticket/repairMain?id=${item.id}`
}) })
} else {
uni.showToast({ title: '暂无操作权限', icon: 'none' })
}
} }
// 页面显示时刷新(从详情页返回也会触发) // 页面显示时刷新(从详情页返回也会触发)
@@ -111,6 +120,7 @@ onShow(() => {
pageNum.value = 1 pageNum.value = 1
noMore.value = false noMore.value = false
getNoticeData() getNoticeData()
permStore.initPerm()
}) })
</script> </script>
@@ -149,7 +159,7 @@ onShow(() => {
.unread-dot { .unread-dot {
width: 16rpx; width: 16rpx;
height: 16rpx; height: 16rpx;
background-color: #E34D59; background-color: #e34d59;
border-radius: 50%; border-radius: 50%;
margin-top: 6rpx; margin-top: 6rpx;
} }
@@ -189,7 +199,7 @@ onShow(() => {
justify-content: space-between; justify-content: space-between;
.detail-text { .detail-text {
font-size: 28rpx; font-size: 28rpx;
color: #4D5DFA; color: #4d5dfa;
margin-right: 8rpx; margin-right: 8rpx;
} }
} }

View File

@@ -44,7 +44,9 @@
import { ref } from 'vue' import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app' import { onShow } from '@dcloudio/uni-app'
import { logout, getSettingInfo } from '../../api/api.js' import { logout, getSettingInfo } from '../../api/api.js'
import { clearPermList } from '../../utils/permissions.js' import { usePermStore } from '@/stores/usePermStore'
const permStore = usePermStore()
// 响应式数据 // 响应式数据
const phoneNumber = ref('') // 绑定手机号 const phoneNumber = ref('') // 绑定手机号
@@ -118,7 +120,7 @@ const confirmLogout = () => {
logout() logout()
.then(res => { .then(res => {
if (res.code === 200) { if (res.code === 200) {
clearPermList() permStore.clearPerm()
uni.clearStorageSync() uni.clearStorageSync()
uni.showToast({ title: '退出登录成功', icon: 'success' }) uni.showToast({ title: '退出登录成功', icon: 'success' })
uni.redirectTo({ url: '/pageSubPack/loginSub/pwd-login' }) uni.redirectTo({ url: '/pageSubPack/loginSub/pwd-login' })

View File

@@ -100,8 +100,11 @@
<!-- 网格布局功能 --> <!-- 网格布局功能 -->
<view class="function-container"> <view class="function-container">
<!-- 第一行功能:故障上报、一键开门、工单大厅、我的工单 --> <!-- 第一行功能:故障上报、一键开门、工单大厅、我的工单 -->
<view class="function-row"> <view
<view class="function-item" @click="goToReport" v-if="hasPermi('butler:fault:report')"> class="function-item"
@click="goToReport"
v-if="permStore.hasPermi('butler:fault:report')"
>
<image <image
class="item-icon" class="item-icon"
src="https://wuyeadmin.bugtc.com/images/imgs/fault-report.png" src="https://wuyeadmin.bugtc.com/images/imgs/fault-report.png"
@@ -112,7 +115,7 @@
<view <view
class="function-item" class="function-item"
@click="goToOneOpenDoor" @click="goToOneOpenDoor"
v-if="hasPermi('butler:door:open')" v-if="permStore.hasPermi('butler:door:open')"
> >
<image <image
class="item-icon" class="item-icon"
@@ -124,7 +127,7 @@
<view <view
class="function-item" class="function-item"
@tap="goToTicketHall" @tap="goToTicketHall"
v-if="hasPermi('butler:workOrder:hall')" v-if="permStore.hasPermi('butler:workOrder:hall')"
> >
<image <image
class="item-icon" class="item-icon"
@@ -136,7 +139,7 @@
<view <view
class="function-item" class="function-item"
@tap="goToMyTicket" @tap="goToMyTicket"
v-if="hasPermi('butler:workOrder:mine')" v-if="permStore.hasPermi('butler:workOrder:mine')"
> >
<image <image
class="item-icon" class="item-icon"
@@ -145,13 +148,10 @@
></image> ></image>
<text class="item-text">我的工单</text> <text class="item-text">我的工单</text>
</view> </view>
</view>
<!-- 第二行 -->
<view class="function-row" style="margin-top: 2px">
<view <view
class="function-item" class="function-item"
@tap="goToComplaint" @tap="goToComplaint"
v-if="hasPermi('butler:complaint:handle')" v-if="permStore.hasPermi('butler:complaint:handle')"
> >
<image <image
class="item-icon" class="item-icon"
@@ -163,7 +163,7 @@
<view <view
class="function-item" class="function-item"
@tap="goToReplacement_record" @tap="goToReplacement_record"
v-if="hasPermi('butler:meter:replace')" v-if="permStore.hasPermi('butler:meter:replace')"
> >
<image <image
class="item-icon" class="item-icon"
@@ -176,7 +176,6 @@
</view> </view>
</view> </view>
</view> </view>
</view>
</template> </template>
<script setup> <script setup>
@@ -188,7 +187,11 @@ import {
getNewNoticeData, getNewNoticeData,
getBannarList getBannarList
} from '../../api/api.js' } from '../../api/api.js'
import { hasPermi } from '../../utils/permissions.js'
import { usePermStore } from '@/stores/usePermStore'
const permStore = usePermStore()
// 列表数据状态 // 列表数据状态
const currentCommunity = ref('请先绑定房屋') const currentCommunity = ref('请先绑定房屋')
const bannerList = ref([]) const bannerList = ref([])
@@ -197,8 +200,6 @@ const allCount = ref(0) // 全部巡检
const pendingCount = ref(0) //待巡检 const pendingCount = ref(0) //待巡检
const InspectedCount = ref(0) //已巡检 const InspectedCount = ref(0) //已巡检
const statusBarHeight = ref(uni.getSystemInfoSync().statusBarHeight || 0) const statusBarHeight = ref(uni.getSystemInfoSync().statusBarHeight || 0)
console.log(hasPermi('butler:meter:replace'))
// =======方法======= // =======方法=======
// 跳转小区 // 跳转小区
const navigateToPage = () => { const navigateToPage = () => {
@@ -343,10 +344,13 @@ const slelctHomeData = data => {
getNoticeData() getNoticeData()
getInspectionData() getInspectionData()
} }
const permissionlist = ref([])
onShow(() => { onShow(() => {
getbannarData() getbannarData()
getInspectionData() getInspectionData()
currentVillageData() currentVillageData()
permStore.initPerm()
}) })
onLoad(() => { onLoad(() => {
@@ -585,6 +589,9 @@ onUnload(() => {
padding: 30rpx 0; padding: 30rpx 0;
box-sizing: border-box; box-sizing: border-box;
margin-top: 20rpx; margin-top: 20rpx;
display: grid;
grid-template-columns: repeat(4, 1fr);
} }
/* 功能行布局 */ /* 功能行布局 */
@@ -603,7 +610,6 @@ onUnload(() => {
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
width: 25%;
padding: 10rpx 0; padding: 10rpx 0;
} }

View File

@@ -0,0 +1,39 @@
import {
defineStore
} from 'pinia'
export const usePermStore = defineStore('perm', {
state: () => ({
permList: []
}),
getters: {
// 封装权限判断,页面直接调用
hasPermi: (state) => (perm, mode = 'or') => {
const list = state.permList
// 超级管理员
if (list.includes('*')) return true
const checkArr = Array.isArray(perm) ? perm : [perm]
if (mode === 'and') {
return checkArr.every(item => list.includes(item))
} else {
return checkArr.some(item => list.includes(item))
}
}
},
actions: {
// 初始化:页面onShow时从本地缓存加载权限(冷启动用)
initPerm() {
const perms = uni.getStorageSync('userPermission')
this.permList = Array.isArray(perms) ? perms : []
},
// 登录成功:设置新权限,同步storage
setPerm(list) {
this.permList = list
uni.setStorageSync('userPermission', list)
},
// 退出/切换账号:清空内存+本地缓存
clearPerm() {
this.permList = []
uni.removeStorageSync('userPermission')
}
}
})

View File

@@ -1,37 +0,0 @@
/**
* 获取本地存储的用户权限标识数组
*/
export function getPermList() {
const perms = uni.getStorageSync('userPermission')
return Array.isArray(perms) ? perms : []
}
/**
* 权限校验函数
* @param perm 单个权限字符串 / 多个权限数组
* @param mode or满足任一 / and全部满足,默认or
* @returns 是否拥有权限 false 没有权限 true 有权限
*/
export function hasPermi(perm, mode = 'or') {
const permList = getPermList()
// 超级管理员通配符
if (permList.includes('*')) return true
const checkArr = Array.isArray(perm) ? perm : [perm]
if (mode === 'and') {
// 所有权限都要有
return checkArr.every(item => permList.includes(item))
} else {
console.log(permList, checkArr)
// 有一个即可
return checkArr.some(item => permList.includes(item))
}
}
/**
* 退出登录清空权限
*/
export function clearPermList() {
uni.removeStorageSync('userPermission')
}