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> </style>
<script> <script>
export default { export default {
onLaunch: function() { onLaunch: async function() {
if(this.$store.getters.token){ if(await this.$store.dispatch('getUserInfo', this.$store.getters.token||uni.getStorageSync('token'))){
}else{
this.$store.commit('outLogin') this.$store.commit('outLogin')
} }
}, },

2
pages/home/index.vue

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

6
pages/home/login.vue

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

23
store/index.js

@ -21,17 +21,16 @@ export default new Vuex.Store({
}, },
mutations:{ mutations:{
setUserInfo(state, val){ setUserInfo(state, val){
state.userInfo={ state.userInfo=val.userInfo
...val.userInfo,
companyName:val.departs[0].departName
}
state.token=val.token state.token=val.token
uni.setStorageSync('token', val.token)
this.dispatch('getSysMsg') this.dispatch('getSysMsg')
}, },
outLogin(state){ outLogin(state){
state.userInfo={} state.userInfo={}
state.baseInfo={} state.baseInfo={}
state.token='' state.token=''
uni.removeStorageSync('token')
clearTimeout(timer) clearTimeout(timer)
uni.reLaunch({ uni.reLaunch({
url:"/pages/home/login", url:"/pages/home/login",
@ -63,5 +62,21 @@ export default new Vuex.Store({
} }
timer=setTimeout(()=>dispatch('getSysMsg'),30*60*1000) 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