67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
|
|
import http from "./http";
|
||
|
|
import Vue from "vue";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 用户登录信息
|
||
|
|
*/
|
||
|
|
export const user = {
|
||
|
|
state: () => ({}),
|
||
|
|
mutations: {
|
||
|
|
setUser(state, user) {
|
||
|
|
for (const key in user) {
|
||
|
|
Vue.set(state, key, user[key])
|
||
|
|
}
|
||
|
|
},
|
||
|
|
},
|
||
|
|
actions: {
|
||
|
|
getUserInfo({commit}) {
|
||
|
|
//获取企业微信后台账号信息
|
||
|
|
//党员认证状态 partyStatusForWX:0、未认证 1、认证中 2、已认证
|
||
|
|
return http.post("/app/appwechatuser/check").then(res => {
|
||
|
|
if (res?.code == 0) {
|
||
|
|
commit('setUser', res.data)
|
||
|
|
return Promise.all([])
|
||
|
|
}
|
||
|
|
})
|
||
|
|
},
|
||
|
|
getCode({dispatch}, count = 0) {
|
||
|
|
if (count > 3) {
|
||
|
|
return Promise.reject("无法获取code")
|
||
|
|
} else return new Promise((resolve, reject) => {
|
||
|
|
uni.login({
|
||
|
|
success: res => {
|
||
|
|
if (res?.code) {
|
||
|
|
resolve(res.code);
|
||
|
|
} else {
|
||
|
|
reject(res);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
fail: () => resolve(dispatch("getCode", ++count))
|
||
|
|
})
|
||
|
|
})
|
||
|
|
},
|
||
|
|
getToken({commit}, code) {
|
||
|
|
if (code) {
|
||
|
|
return http.post("/auth/wechat-con/token", {code}, {
|
||
|
|
headers: {"Authorization": "Basic d2VjaGF0OndlY2hhdA=="},
|
||
|
|
withoutToken: true
|
||
|
|
}).then(res => {
|
||
|
|
if (res?.access_token) {
|
||
|
|
const token = [res?.token_type, res?.access_token].join(" ").trim()
|
||
|
|
commit("setToken", token)
|
||
|
|
return token
|
||
|
|
} else {
|
||
|
|
uni.showToast({title: res?.msg})
|
||
|
|
return Promise.resolve(res?.msg)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
} else return Promise.reject("缺少登录code")
|
||
|
|
},
|
||
|
|
autoLogin({dispatch}, count = 0) {
|
||
|
|
if (count <= 3) {
|
||
|
|
return dispatch("getCode").then(code => dispatch("getToken", code).catch(() => dispatch("autoLogin", ++count)))
|
||
|
|
} else return Promise.reject("登录失败,请联系管理员")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|