Browse Source

修改保存登录token,自动登录

master
邓宏 1 year ago
parent
commit
756e3f1855
  1. 6
      App.vue
  2. 2
      pages/home/index.vue
  3. 6
      pages/home/login.vue
  4. 23
      store/index.js

6
App.vue

@ -16,10 +16,8 @@
</style>
<script>
export default {
onLaunch: function() {
if(this.$store.getters.token){
}else{
onLaunch: async function() {
if(await this.$store.dispatch('getUserInfo', this.$store.getters.token||uni.getStorageSync('token'))){
this.$store.commit('outLogin')
}
},

2
pages/home/index.vue

@ -433,7 +433,7 @@ import request,{host} from '@/common/request'
this.plantBase=[res.data]
this.$store.commit("setBaseId", {id:res.data[0].id,name:res.data[0].baseName})
}else{
uni.showToast({title:"请在管理端维护基地!",icon:"none",duration:5000})
uni.showToast({title:"请先至管理端维护基地信息!",icon:"none",duration:5000})
}
}else{
uni.showModal({

6
pages/home/login.vue

@ -112,6 +112,7 @@ export default {
//#ifdef APP-PLUS
this.getVersion()
plus.runtime.getProperty(plus.runtime.appid, async info=>{
console.log("app ",info)
this.versionCode = info.versionCode
})
//#endif
@ -123,8 +124,8 @@ export default {
body:this.formData
})
if(res.statu){
this.$store.commit("setUserInfo",res.data)
uni.switchTab({url:'/pages/home/index'})
this.$store.dispatch("getUserInfo",res.data.token)
// uni.switchTab({url:'/pages/home/index'})
}else{
uni.showModal({
title:"提示",
@ -140,6 +141,7 @@ export default {
params:{type:1}
})
if(res.statu){
console.log(res.data.appVersionCode,'----appVersion--')
if(res.data.appVersionCode!=this.versionCode){
this.isShowModal=true
this.appDownLoadUrl=host("version",res.data.appDownloadUrl)

23
store/index.js

@ -21,17 +21,16 @@ export default new Vuex.Store({
},
mutations:{
setUserInfo(state, val){
state.userInfo={
...val.userInfo,
companyName:val.departs[0].departName
}
state.userInfo=val.userInfo
state.token=val.token
uni.setStorageSync('token', val.token)
this.dispatch('getSysMsg')
},
outLogin(state){
state.userInfo={}
state.baseInfo={}
state.token=''
uni.removeStorageSync('token')
clearTimeout(timer)
uni.reLaunch({
url:"/pages/home/login",
@ -63,5 +62,21 @@ export default new Vuex.Store({
}
timer=setTimeout(()=>dispatch('getSysMsg'),30*60*1000)
},
async getUserInfo({commit},val){
console.log(val,'------------token---------------')
if(val){
var res=await request("/common/api/dict/queryUserInfoByToken",{
params:{token:val}
})
if(res.statu){
commit('setUserInfo',{userInfo:res.data, token:val})
uni.switchTab({url:'/pages/home/index'})
return false
}else{
return true
}
}
return true
},
}
})

Loading…
Cancel
Save