1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
| 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')
| },
| },
| };
|
|