vuex模块化 登录,oauth,agentSign模块

This commit is contained in:
aixianling
2022-06-13 17:21:40 +08:00
parent 0b157621ca
commit 4520a74627
4 changed files with 144 additions and 153 deletions

View File

@@ -2,16 +2,13 @@ import Vue from 'vue'
import Vuex from 'vuex'
import perState from 'vuex-persistedstate'
import http from '../common/axios'
import CryptoJS from '../utils/crypto-js'
import * as modules from "../common/modules";
Vue.use(Vuex)
let agentSignURL = "", apiList = []
const store = new Vuex.Store({
state: {
token: "",
openUser: {},
config: {},
apps: []
},
mutations: {
@@ -29,9 +26,6 @@ const store = new Vuex.Store({
setOpenUser(state, user) {
state.openUser = user
},
getConfig(state, params) {
state.config = params
},
bindAccount(state, params) {
//具备解决二次登录,绑定手机,token等问题
// http.post("/admin/user/cpBindByPhone", params, {
@@ -45,72 +39,9 @@ const store = new Vuex.Store({
params?.then(res)
} else alert(res)
}).catch(err => alert(err))
},
redirectCode(state, url = location.href) {
let REDIRECT_URI = encodeURIComponent(url),
corpid = state.config.corpid
const redirectTo = cid => {
location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base#wechat_redirect'
.replace(/CORPID/g, cid)
.replace(/REDIRECT_URI/g, REDIRECT_URI)
}
if (corpid) {
redirectTo(corpid)
} else {
store.dispatch("agentSign").then(() => {
corpid = state.config.corpid
redirectTo(corpid)
})
}
},
}
},
actions: {
getToken(state, params) {
const encryptByDES = password => {
let isIos = uni.getSystemInfoSync().system.toUpperCase == 'ios'
let key = "thanks,villcloud"
let iv = CryptoJS.enc.Latin1.parse(key)
let encrypted = CryptoJS.AES.encrypt(password, iv, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
})
if (isIos) {
return encodeURIComponent(encrypted.toString());
} else {
return encrypted.toString();
}
}
let {module} = params
return http.post("/auth/oauth/token", null, {
withoutToken: true,
module,
params: {
...params, grant_type: 'password',
password: encryptByDES(params.password)
},
headers: {
Authorization: "Basic d2VjaGF0OndlY2hhdA=="
}
}).then(res => {
if (res?.access_token) {
state.commit("login", [res?.token_type, res?.access_token].join(" ").trim())
return module != 'AppCountryAlbum' && state.dispatch("getAccount", {module})
}
}).catch(err => {
uni.showToast({title: err, icon: 'none'})
})
},
getCode(store, url) {
if (store.state.config?.corpid) {
store.commit('redirectCode', url)
} else {
store.dispatch('agentSign').then(() => {
store.commit('redirectCode', url)
})
}
},
getUserInfo(state, code) {
if (code) {
return http.post("/app/wxcp/portal/getUserInfo", null, {
@@ -129,42 +60,6 @@ const store = new Vuex.Store({
})
}
},
agentSign(state, params) {
let url = window.location.href
if (agentSignURL == url) {
return Promise.resolve()
} else {
agentSignURL = url
let action = "/app/wxcp/portal/agentSign"
if (sessionStorage.getItem("prj")?.indexOf("saas") > -1) {
params = {...params, corpId: "ww596787bb70f08288"}
action = "/app/wxcptp/portal/agentSign"
}
state.commit("getConfig", {})
return http.post(action, null, {
withoutToken: true,
params: {...params, url}
}).then(res => {
if (res?.data) {
let config = {
...params,
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来若要查看传入的参数可以在pc端打开参数信息会通过log打出仅在pc端时才会打印。
beta: true,// 必须这么写否则wx.invoke调用形式的jsapi会有问题
corpid: res.data.corpid, // 必填企业微信的corpid必须与当前登录的企业一致
agentid: res.data.agentId, // 必填企业微信的应用id e.g. 1000247
timestamp: Number(res.data.timestamp), // 必填,生成签名的时间戳
nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
signature: res.data.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
...res.data,
}
state.commit("getConfig", config)
return config
}
}).catch(err => {
console.error(err)
})
}
},
},
modules,
plugins: [perState({