Files
dvcp_v2_wxcp_app/src/store/index.js

180 lines
5.6 KiB
JavaScript
Raw Normal View History

2021-11-15 10:29:05 +08:00
import Vue from 'vue'
import Vuex from 'vuex'
import perState from 'vuex-persistedstate'
import http from '../common/axios'
2021-11-15 15:53:44 +08:00
import CryptoJS from '../utils/crypto-js'
2022-06-10 17:15:56 +08:00
import * as modules from "../common/modules";
2021-11-15 10:29:05 +08:00
Vue.use(Vuex)
2021-11-23 16:07:39 +08:00
let agentSignURL = "", apiList = []
2021-11-15 10:29:05 +08:00
const store = new Vuex.Store({
state: {
token: "",
openUser: {},
2022-05-30 18:46:49 +08:00
config: {},
apps: []
2021-11-15 10:29:05 +08:00
},
mutations: {
2022-05-30 18:46:49 +08:00
setApps(state, apps) {
state.apps = apps
},
2021-11-15 10:29:05 +08:00
login(state, token) {
state.token = token
},
logout(state) {
state.token = ""
state.openUser = {}
2022-06-10 16:24:29 +08:00
state.user = {}
2021-11-15 10:29:05 +08:00
},
setOpenUser(state, user) {
state.openUser = user
},
getConfig(state, params) {
2021-11-23 16:07:39 +08:00
state.config = params
2021-11-15 10:29:05 +08:00
},
bindAccount(state, params) {
//具备解决二次登录,绑定手机,token等问题
// http.post("/admin/user/cpBindByPhone", params, {
http.post("/auth/wechatcp/token", {customerLogin: 1, ...params}, {
headers: {
Authorization: "Basic d2VjaGF0OndlY2hhdA=="
},
withoutToken: true
}).then(res => {
if (res?.access_token) {
params?.then(res)
} else alert(res)
}).catch(err => alert(err))
},
redirectCode(state, url = location.href) {
let REDIRECT_URI = encodeURIComponent(url),
2022-06-09 10:24:21 +08:00
corpid = state.config.corpid
2021-11-15 10:29:05 +08:00
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'
2022-01-27 16:18:43 +08:00
.replace(/CORPID/g, cid)
.replace(/REDIRECT_URI/g, REDIRECT_URI)
2021-11-15 10:29:05 +08:00
}
if (corpid) {
redirectTo(corpid)
} else {
store.dispatch("agentSign").then(() => {
2022-06-09 10:24:21 +08:00
corpid = state.config.corpid
2021-11-15 10:29:05 +08:00
redirectTo(corpid)
})
}
},
},
actions: {
2021-11-15 15:53:44 +08:00
getToken(state, params) {
const encryptByDES = password => {
2021-11-25 16:02:53 +08:00
let isIos = uni.getSystemInfoSync().system.toUpperCase == 'ios'
2021-11-15 15:53:44 +08:00
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();
}
}
2022-05-31 09:55:33 +08:00
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'})
2021-11-15 10:29:05 +08:00
})
},
getCode(store, url) {
2021-11-23 16:07:39 +08:00
if (store.state.config?.corpid) {
2021-11-15 10:29:05 +08:00
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, {
params: {code}, withoutToken: true
}).then(res => {
if (res?.code == 0) {
state.commit("setOpenUser", res.data)
}
})
} else {
return http.post("/app/wxcp/wxuser/getUserInfoByToken")
2022-01-27 16:18:43 +08:00
.then(res => {
if (res?.code == 0) {
state.commit("setOpenUser", res.data)
}
})
2021-11-15 10:29:05 +08:00
}
},
2021-11-23 16:07:39 +08:00
agentSign(state, params) {
let url = window.location.href
2021-11-15 10:29:05 +08:00
if (agentSignURL == url) {
return Promise.resolve()
} else {
agentSignURL = url
2022-05-18 14:12:43 +08:00
let action = "/app/wxcp/portal/agentSign"
2022-02-09 15:11:33 +08:00
if (sessionStorage.getItem("prj")?.indexOf("saas") > -1) {
2022-02-09 10:35:29 +08:00
params = {...params, corpId: "ww596787bb70f08288"}
2022-05-18 14:13:02 +08:00
action = "/app/wxcptp/portal/agentSign"
2022-02-09 10:35:29 +08:00
}
state.commit("getConfig", {})
2022-05-18 14:12:43 +08:00
return http.post(action, null, {
2022-03-17 11:37:46 +08:00
withoutToken: true,
2022-01-27 16:31:59 +08:00
params: {...params, url}
2021-11-15 10:29:05 +08:00
}).then(res => {
if (res?.data) {
2021-11-23 16:07:39 +08:00
let config = {
...params,
2021-11-15 10:29:05 +08:00
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,
}
2021-11-23 16:07:39 +08:00
state.commit("getConfig", config)
return config
2021-11-15 10:29:05 +08:00
}
}).catch(err => {
console.error(err)
})
}
},
},
2022-06-10 17:15:56 +08:00
modules,
2021-11-15 10:29:05 +08:00
plugins: [perState({
storage: {
getItem: key => uni.getStorageSync(key),
setItem: (key, value) => uni.setStorageSync(key, value),
removeItem: key => uni.removeStorageSync(key)
}
})]
})
export default store