feat(用户认证): 实现基于角色的页面跳转和登录状态检查

- 在App.vue中添加登录状态检查和用户信息获取
- 修改index.vue实现根据用户角色跳转到不同页面
- 更新pages.json调整底部导航栏和页面配置
- 优化user.js中的用户信息存储逻辑
This commit is contained in:
砂糖
2025-11-03 14:52:41 +08:00
parent d677c293e8
commit 42d858bc6b
5 changed files with 190 additions and 136 deletions

View File

@@ -31,7 +31,9 @@
checkLogin() { checkLogin() {
if (!getToken()) { if (!getToken()) {
this.$tab.reLaunch('/pages/login') this.$tab.reLaunch('/pages/login')
return;
} }
this.$store.dispatch('GetInfo')
} }
} }
} }

View File

@@ -45,7 +45,8 @@
"path" : "pages/line/line", "path" : "pages/line/line",
"style" : "style" :
{ {
"navigationBarTitleText" : "产线监控" "navigationBarTitleText" : "产线监控",
"navigationStyle": "custom"
} }
} }
// { // {
@@ -110,7 +111,7 @@
"list": [ "list": [
{ {
"text": "产线", "text": "产线",
"pagePath": "pages/index", "pagePath": "pages/line/line",
"selectedIconPath": "/static/images/tabbar/home_.png", "selectedIconPath": "/static/images/tabbar/home_.png",
"iconPath": "/static/images/tabbar/home.png" "iconPath": "/static/images/tabbar/home.png"
}, },
@@ -125,13 +126,13 @@
"pagePath": "pages/mine/index", "pagePath": "pages/mine/index",
"selectedIconPath": "/static/images/tabbar/mine_.png", "selectedIconPath": "/static/images/tabbar/mine_.png",
"iconPath": "/static/images/tabbar/mine.png" "iconPath": "/static/images/tabbar/mine.png"
},
{
"text": "扫码2",
"pagePath": "pages/easycode/easycode",
"selectedIconPath": "/static/images/tabbar/work_.png",
"iconPath": "/static/images/tabbar/work.png"
} }
// {
// "text": "扫码2",
// "pagePath": "pages/easycode/easycode",
// "selectedIconPath": "/static/images/tabbar/work_.png",
// "iconPath": "/static/images/tabbar/work.png"
// }
] ]
}, },

View File

@@ -51,7 +51,7 @@
} }
}, },
mounted() { mounted() {
getDicts('easycode_type').then(res => { getDicts('action_type').then(res => {
this.types = res.data this.types = res.data
}) })
} }

View File

@@ -1,5 +1,5 @@
<template> <template>
<view class="production-page"> <!-- <view class="production-page">
<klp-header @lineChange="setActive" class="line-header"></klp-header> <klp-header @lineChange="setActive" class="line-header"></klp-header>
@@ -10,42 +10,74 @@
<Zinc2 v-else-if="active == 3"></Zinc2> <Zinc2 v-else-if="active == 3"></Zinc2>
<Zinc3 v-else-if="active == 4"></Zinc3> <Zinc3 v-else-if="active == 4"></Zinc3>
</view> </view>
</view> </view> -->
</template> </template>
<script> <script>
import Acidity from '@/components/lines/acidity.vue' // 根据不同的全责跳转到不同的页面
import Paint from '@/components/lines/paint.vue' // 如果是工人跳转到扫码页面
import Zinc1 from '@/components/lines/zinc1.vue' // 如果是管理员跳转到产线
import Zinc2 from '@/components/lines/zinc2.vue'
import Zinc3 from '@/components/lines/zinc3.vue'
export default { export default {
components: { created() {
Acidity, // 检查用户角色
Paint, this.$store.dispatch('GetInfo').then(res => {
Zinc1, const roles = res.data.roles;
Zinc2,
Zinc3 if (roles.includes('admin')) {
}, // 管理员角色,跳转到产线页面
data() { uni.switchTab({
return { url: '/pages/line/line'
active: 0 });
} } else if (roles.includes('worker')) {
}, // 工人角色,跳转到扫码页面
methods: { uni.navigateTo({
next() { url: '/pages/easycode/easycode'
if (this.active >= 5) { });
this.active = 0
} else {
this.active += 1
}
},
setActive({ index, line }) {
this.active = index;
} }
// else {
// // 其他角色,跳转到扫码页面
// uni.navigateTo({
// url: '/pages/easycode/easycode'
// });
// }
})
} }
} }
// import Acidity from '@/components/lines/acidity.vue'
// import Paint from '@/components/lines/paint.vue'
// import Zinc1 from '@/components/lines/zinc1.vue'
// import Zinc2 from '@/components/lines/zinc2.vue'
// import Zinc3 from '@/components/lines/zinc3.vue'
// export default {
// components: {
// Acidity,
// Paint,
// Zinc1,
// Zinc2,
// Zinc3
// },
// data() {
// return {
// active: 0
// }
// },
// methods: {
// next() {
// if (this.active >= 5) {
// this.active = 0
// } else {
// this.active += 1
// }
// },
// setActive({ index, line }) {
// this.active = index;
// }
// }
// }
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">

View File

@@ -1,9 +1,20 @@
import config from '@/config' import config from '@/config'
import storage from '@/utils/storage' import storage from '@/utils/storage'
import constant from '@/utils/constant' import constant from '@/utils/constant'
import { isHttp, isEmpty } from "@/utils/validate" import {
import { login, logout, getInfo } from '@/api/login' isHttp,
import { getToken, setToken, removeToken } from '@/utils/auth' isEmpty
} from "@/utils/validate"
import {
login,
logout,
getInfo
} from '@/api/login'
import {
getToken,
setToken,
removeToken
} from '@/utils/auth'
import defAva from '@/static/images/avatar.png' import defAva from '@/static/images/avatar.png'
const baseUrl = config.baseUrl const baseUrl = config.baseUrl
@@ -46,7 +57,9 @@ const user = {
actions: { actions: {
// 登录 // 登录
Login({ commit }, userInfo) { Login({
commit
}, userInfo) {
const username = userInfo.username.trim() const username = userInfo.username.trim()
const password = userInfo.password const password = userInfo.password
const code = userInfo.code const code = userInfo.code
@@ -64,7 +77,10 @@ const user = {
}, },
// 获取用户信息 // 获取用户信息
GetInfo({ commit, state }) { GetInfo({
commit,
state
}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
getInfo().then(res => { getInfo().then(res => {
console.log('个人信息', res) console.log('个人信息', res)
@@ -92,7 +108,10 @@ const user = {
}, },
// 退出系统 // 退出系统
LogOut({ commit, state }) { LogOut({
commit,
state
}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
logout(state.token).then(() => { logout(state.token).then(() => {
commit('SET_TOKEN', '') commit('SET_TOKEN', '')