整合企微token变量

This commit is contained in:
aixianling
2022-07-19 18:06:22 +08:00
parent 36852052ec
commit eda5128d4b
31 changed files with 170 additions and 207 deletions

View File

@@ -3,123 +3,16 @@ import Vue from "vue";
import CryptoJS from "./crypto-js";
/**
* 登录方法
*/
export const config = {
state: () => ({}),
mutations: {
getConfig(state, params) {
for (const key in params) {
Vue.set(state, key, params[key])
}
}
},
actions: {
agentSign({state, commit}, params) {
//授权jssdk在url上使用,并获取corpId
let url = window.location.href
if (state.agentSignURL == url) {
return Promise.resolve()
} else {
commit("getConfig", {agentSignURL: url})
let action = "/app/wxcp/portal/agentSign"
if (!!params?.action) {
action = params.action
delete params.action
} else if (!!params?.suiteId) {
action = "/app/wxcptp/portal/agentSign"
}
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,
agentSignURL: url
}
commit("getConfig", config)
return config
}
}).catch(err => {
console.error(err)
})
}
},
getCode({state, dispatch}, tryAgentSign = false) {
let {corpid: corpId, suiteId, agentid: agentId} = state, url = location.href, REDIRECT_URI = encodeURIComponent(url), scope = "snsapi_privateinfo"
if (/\/AppForm\//.test(location.pathname)) {
scope = "snsapi_base"
} else if (suiteId) {
corpId = suiteId
scope = "snsapi_privateinfo"
}
return new Promise((resolve, reject) => {
if (corpId && scope) {
let oauthURL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&agentid=AGENTID#wechat_redirect'
.replace(/APPID/g, corpId)
.replace(/REDIRECT_URI/g, REDIRECT_URI)
.replace(/SCOPE/g, scope)
.replace(/AGENTID/g, agentId)
console.log(oauthURL)
location.replace(oauthURL)
} else if (!tryAgentSign) {
dispatch("agentSign", {corpId, suiteId}).then(() => dispatch("getCode", true)).then(() => resolve())
} else reject("URL缺少必要参数(corpId)")
})
},
getToken({state, commit, dispatch}, 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, code} = params, action = "/auth/oauth/token"
if (!!code) {
action = "/auth/wechatcp/token"
}
return http.post(action, params, {
withoutToken: true,
module,
params: {
...params, grant_type: 'password',
password: encryptByDES(params.password)
},
headers: {
Authorization: "Basic d2VjaGF0OndlY2hhdA=="
}
}).then(res => {
if (res?.access_token) {
return [res?.token_type, res?.access_token].join(" ").trim()
} else return Promise.reject(res.msg)
})
}
}
}
/**
* 用户登录信息
* 用户登录信息和方法
*/
export const user = {
state: () => ({}),
state: () => ({
token: ""
}),
mutations: {
setToken(state, token) {
state.token = token
},
setUser(state, user) {
for (const key in user) {
Vue.set(state, key, user[key])
@@ -205,6 +98,44 @@ export const user = {
}
},
actions: {
getToken({commit, dispatch}, 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, code} = params, action = "/auth/oauth/token"
if (!!code) {
action = "/auth/wechatcp/token"
}
return http.post(action, params, {
withoutToken: true,
module,
params: {
...params, grant_type: 'password',
password: encryptByDES(params.password)
},
headers: {
Authorization: "Basic d2VjaGF0OndlY2hhdA=="
}
}).then(res => {
if (res?.access_token) {
const token = [res?.token_type, res?.access_token].join(" ").trim()
commit("setToken", token)
return token
} else return Promise.reject(res.msg)
})
},
getAccount({dispatch, commit}, config) {
//获取企业微信后台账号信息
return http.post("/admin/user/detail-phone", null, config).then(res => {
@@ -228,7 +159,7 @@ export const user = {
/**
* 企微jssdk功能
*/
let apiList = []
let timer = {}
export const wxwork = {
state: () => ({
agentSignURL: "",
@@ -247,33 +178,87 @@ export const wxwork = {
},
},
actions: {
injectJWeixin({commit, dispatch, rootState}, ops) {
const inject = (jsApiList, config = rootState.config) => new Promise((resolve, reject) => {
setTimeout(() => {
let sdk = wx?.agentConfig ? wx : jWeixin
agentSign({state, commit}, params) {
//授权jssdk在url上使用,并获取corpId
let url = window.location.href
if (state.agentSignURL == url && state.config.corpId) {
return Promise.resolve()
} else {
commit("setAgentSignURL", url)
commit("setApiList", [])
let action = "/app/wxcp/portal/agentSign"
if (!!params?.action) {
action = params.action
delete params.action
} else if (!!params?.suiteId) {
action = "/app/wxcptp/portal/agentSign"
}
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
}
commit("setConfig", config)
return config
}
}).catch(err => {
console.error(err)
})
}
},
getCode({state, dispatch}, tryAgentSign = false) {
let {corpid: corpId, suiteId, agentid: agentId} = state.config, url = location.href, REDIRECT_URI = encodeURIComponent(url), scope = "snsapi_privateinfo"
if (/\/AppForm/.test(location.pathname)) {
scope = "snsapi_base"
} else if (suiteId) {
corpId = suiteId
scope = "snsapi_privateinfo"
}
return new Promise((resolve, reject) => {
if (corpId && scope) {
let oauthURL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&agentid=AGENTID#wechat_redirect'
.replace(/APPID/g, corpId)
.replace(/REDIRECT_URI/g, REDIRECT_URI)
.replace(/SCOPE/g, scope)
.replace(/AGENTID/g, agentId)
location.replace(oauthURL)
} else if (!tryAgentSign && corpId) {
dispatch("agentSign", {corpId, suiteId}).then(() => dispatch("getCode", true)).then(() => resolve())
} else reject("URL缺少必要参数(corpId)")
})
},
injectJWeixin({state, commit, rootState}, apis) {
const inject = (jsApiList) => new Promise((resolve, reject) => {
jsApiList = jsApiList || []
if (timer.injectJWeixin) {//节流设置,50ms内的多次请求合并到一处
clearTimeout(timer.injectJWeixin)
jsApiList = [...new Set([...state.apiList, ...jsApiList])]
commit("setApiList", jsApiList)
}
timer.injectJWeixin = setTimeout(() => {
const sdk = wx?.agentConfig ? wx : jWeixin
sdk?.agentConfig({
...config, jsApiList,
...state.config, jsApiList,
success: res => resolve(res),
fail: err => {
console.error(err)
reject(err)
}
})
}, 500)
})
return new Promise((resolve, reject) => {
let {config: {corpId, suiteId}} = rootState
dispatch("agentSign", {corpId, suiteId}).then(config => {
if (typeof ops == "object") {
ops?.map(api => {
if (!apiList?.includes(api)) apiList.push(api)
})
} else {
if (!apiList?.includes(ops)) apiList.push(ops)
}
inject(apiList, config).then(r => resolve(r)).catch(err => reject(err))
})
}, 50)
})
return inject(apis)
},
wxInvoke(state, op) {
setTimeout(() => {