2022-02-14 17:25:54 +08:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
import Vuex from 'vuex'
|
|
|
|
|
import perState from 'vuex-persistedstate'
|
|
|
|
|
import http from "../utils/axios";
|
|
|
|
|
|
|
|
|
|
Vue.use(Vuex)
|
|
|
|
|
|
|
|
|
|
const store = new Vuex.Store({
|
|
|
|
|
state: {
|
|
|
|
|
/**
|
|
|
|
|
* 是否需要强制登录
|
|
|
|
|
*/
|
|
|
|
|
forcedLogin: false,
|
|
|
|
|
token: "",
|
|
|
|
|
userName: "",
|
|
|
|
|
user: {
|
|
|
|
|
username: "",
|
|
|
|
|
avatarUrl: "",
|
|
|
|
|
nickName: "",
|
|
|
|
|
gender: "",
|
|
|
|
|
token: "",
|
|
|
|
|
phoneNumber: ''
|
|
|
|
|
},
|
|
|
|
|
global: {
|
|
|
|
|
areaId: ""
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mutations: {
|
|
|
|
|
getToken(state, params) {
|
|
|
|
|
if (params?.code) {
|
|
|
|
|
http.instance.post("/auth/wechat-con/token", params, {
|
|
|
|
|
headers: {"Authorization": "Basic d2VjaGF0OndlY2hhdA=="},
|
|
|
|
|
withoutToken: true
|
|
|
|
|
}).then(res => {
|
|
|
|
|
if (res?.access_token) {
|
2022-03-22 13:46:42 +08:00
|
|
|
store.commit("setToken", res.token_type + ' ' + res.access_token)
|
|
|
|
|
params?.then?.(true)
|
2022-02-14 17:25:54 +08:00
|
|
|
} else {
|
|
|
|
|
uni.showToast({title: res?.msg})
|
2022-03-22 13:46:42 +08:00
|
|
|
params?.then?.(false)
|
2022-02-14 17:25:54 +08:00
|
|
|
}
|
2022-03-22 13:46:42 +08:00
|
|
|
}).finally(err => console.error(err))
|
2022-02-14 17:25:54 +08:00
|
|
|
} else params?.then(false)
|
|
|
|
|
},
|
2022-03-22 13:46:42 +08:00
|
|
|
setToken(state, token) {
|
|
|
|
|
state.token = token
|
|
|
|
|
},
|
|
|
|
|
setUserInfo(state, user) {
|
|
|
|
|
state.user = user
|
|
|
|
|
},
|
2022-02-14 17:25:54 +08:00
|
|
|
getUserInfo(state, cb) {
|
|
|
|
|
http.instance.post("/app/appwechatuser/check").then(res => {
|
|
|
|
|
if (res?.data) {
|
2022-03-22 13:46:42 +08:00
|
|
|
store.commit("setUserInfo", res.data)
|
2022-02-14 17:25:54 +08:00
|
|
|
cb && cb(true)
|
|
|
|
|
} else {
|
|
|
|
|
cb && cb(false)
|
|
|
|
|
}
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.error(err)
|
|
|
|
|
cb && cb(false)
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
login(state, ploy) {
|
|
|
|
|
state.userName = ploy.userName || '新用户';
|
|
|
|
|
state.token = ploy.token
|
|
|
|
|
},
|
|
|
|
|
logout(state) {
|
|
|
|
|
state.userName = "";
|
|
|
|
|
state.token = ""
|
|
|
|
|
uni.setStorageSync('userInfo', {})
|
|
|
|
|
uni.setStorageSync('vuex', {})
|
|
|
|
|
state.user = {
|
|
|
|
|
username: "",
|
|
|
|
|
avatarUrl: "",
|
|
|
|
|
nickName: "",
|
|
|
|
|
gender: "",
|
|
|
|
|
token: "",
|
|
|
|
|
phoneNumber: ''
|
|
|
|
|
}
|
|
|
|
|
uni.showToast({title: `登录失效,请重新登录`, duration: 2000, icon: 'none'})
|
|
|
|
|
},
|
|
|
|
|
setUser(state, info) {
|
|
|
|
|
state.user = info
|
|
|
|
|
},
|
|
|
|
|
getUser(state, ploy) {
|
|
|
|
|
state.user.nickName = ploy.nickName;
|
|
|
|
|
state.user.avatarUrl = ploy.avatarUrl;
|
|
|
|
|
state.user.gender = ploy.gender;
|
|
|
|
|
},
|
|
|
|
|
getUserPhoneNumber(state, ploy) {
|
|
|
|
|
state.user.phoneNumber = ploy.phoneNumber;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
plugins: [perState({
|
|
|
|
|
storage: {
|
|
|
|
|
getItem: key => uni.getStorageSync(key),
|
|
|
|
|
setItem: (key, value) => uni.setStorageSync(key, value),
|
|
|
|
|
removeItem: key => uni.removeStorageSync(key)
|
|
|
|
|
}
|
|
|
|
|
})]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default store
|