| | |
| | | const roleKeyInfo = ref(null); |
| | | // const baseUrlHt = ref(window.location.origin+'/api'); |
| | | // const baseUrlHt = ref('http://www.qdopo.com:9095'); |
| | | const baseUrlHt = ref('http://192.168.100.10:8080'); |
| | | // const baseUrlHt = ref("http://192.168.100.10:8080"); |
| | | const baseUrlHt = ref("https://opo.qduh.cn:443"); |
| | | |
| | | // getters |
| | | const isLoggedIn = computed(() => !!token.value); |
| | |
| | | userInfo.value = info; |
| | | uni.setStorageSync("userInfo", info); |
| | | } |
| | | function setroleKey(roleKey) { |
| | | function setroleKey(roleKey) { |
| | | // 更灵活的角色权限映射 |
| | | if (ADMIN_ROLES.includes(roleKey)) { |
| | | roleKeyInfo.value = 1; // 管理员权限 |
| | |
| | | } else { |
| | | roleKeyInfo.value = 0; // 默认权限 |
| | | } |
| | | uni.setStorageSync("roleKeyInfo", roleKeyInfo.value); |
| | | uni.setStorageSync("roleKeyInfo", roleKeyInfo.value); |
| | | } |
| | | function clearUser() { |
| | | token.value = null; |
| | |
| | | if (localUser) userInfo.value = localUser; |
| | | if (localroleKey) roleKeyInfo.value = localroleKey; |
| | | } |
| | | |
| | | async function refreshUserInfo() { |
| | | try { |
| | | const res = await uni.$uapi.get("/getInfo"); |
| | | if (res && res.user) { |
| | | setUserInfo(res.user); |
| | | if (res.roles) setroleKey(res.roles); |
| | | return true; |
| | | } |
| | | return false; |
| | | } catch (error) { |
| | | console.error("刷新用户信息失败", error); |
| | | return false; |
| | | } |
| | | } |
| | | return { |
| | | token, |
| | | userInfo, |
| | |
| | | clearUser, |
| | | initFromStorage, |
| | | setroleKey, |
| | | refreshUserInfo |
| | | }; |
| | | }); |