New file |
| | |
| | | import Vue from "vue"; |
| | | export default { |
| | | state: { |
| | | user: null, |
| | | tonken:'' |
| | | }, |
| | | mutations: { |
| | | login(state, user) { |
| | | state.user = user; |
| | | // 缓存用户信息 |
| | | Vue.prototype.$cache.set("_userInfo", user, 0); |
| | | }, |
| | | tonken(state, data) { |
| | | state.tonken = data; |
| | | // 缓存用户信息 |
| | | Vue.prototype.$cache.set("state",tonken,0); |
| | | }, |
| | | logout(state) { |
| | | state.user = null; |
| | | // 清理缓存用户信息 |
| | | Vue.prototype.$cache.delete("_userInfo"); |
| | | }, |
| | | }, |
| | | actions: { |
| | | autoLogin({ commit, getters, dispatch }) { |
| | | console.log('Vue.prototype.$storage',Vue.prototype.$storage.get("tonken")) |
| | | let tonken = Vue.prototype.$storage.get("tonken"); |
| | | console.log("tonken", tonken); |
| | | // 判断本地是否有账号信息,如果有,就自动重新登录 |
| | | if (tonken) { |
| | | const params = { |
| | | account: accountInFor.account, |
| | | password: accountInFor.password, |
| | | }; |
| | | uni.showLoading({ |
| | | title: "自动登录中...", |
| | | }); |
| | | dispatch("login", params) |
| | | .then((res) => { |
| | | uni.hideLoading(); |
| | | // uni.showToast({ |
| | | // title: '已自动登录!', |
| | | // icon: 'success' |
| | | // }) |
| | | }) |
| | | .catch(() => { |
| | | uni.hideLoading(); |
| | | uni.showToast({ |
| | | title: "会话过期,请重新登录!", |
| | | icon: "none", |
| | | }); |
| | | setTimeout(() => { |
| | | uni.reLaunch({ |
| | | url: "/pages/login/login", |
| | | }); |
| | | }, 1000); |
| | | }); |
| | | } else { |
| | | // 如果本地没有账号信息,就提示用户必须登录 |
| | | uni.showModal({ |
| | | title: "未登录", |
| | | content: "您未登录,需要登录后才能继续", |
| | | showCancel: false, |
| | | confirmText: "确定", |
| | | success: (res) => { |
| | | if (res.confirm) { |
| | | uni.reLaunch({ |
| | | url: "/pages/login/login", |
| | | }); |
| | | } |
| | | }, |
| | | }); |
| | | } |
| | | }, |
| | | login({ commit }, params) { |
| | | return new Promise((resolve, reject) => { |
| | | Vue.prototype.$login |
| | | .login(params) |
| | | .then((res) => { |
| | | if (res.ok()) { |
| | | console.log(response); |
| | | this.$storage.set("token", response.data.token); |
| | | this.$storage.setJson("accountInFor", this.form); |
| | | let token = this.$storage.get("token"); |
| | | commit("login", tmp); |
| | | resolve(res); |
| | | } else { |
| | | reject(res); |
| | | } |
| | | }) |
| | | .catch((err) => { |
| | | reject(err); |
| | | }); |
| | | }); |
| | | }, |
| | | logout({ commit }) { |
| | | commit("logout"); |
| | | uni.reLaunch({ |
| | | url: "/pages/login/login", |
| | | }); |
| | | }, |
| | | }, |
| | | getters: { |
| | | user: (state) => { |
| | | if (state.user) { |
| | | return state.user; |
| | | } |
| | | // return Vue.prototype.$cache.get('_userInfo') |
| | | }, |
| | | }, |
| | | }; |