整合企微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

@@ -18,7 +18,7 @@ export default {
onPageNotFound() {
},
computed: {
...mapState(['token', 'user']),
...mapState(['user']),
},
methods: {
...mapMutations(['initWaterMarker', 'logout', 'getConfig', 'setApps']),

View File

@@ -23,7 +23,7 @@ export default {
}
},
computed: {
...mapState(['user', 'token']),
...mapState(['user']),
showDetail() {
return !!this.$route.query?.id && this.access
},
@@ -40,11 +40,11 @@ export default {
},
methods: {
...mapActions(['getCode', 'getToken']),
checkAccess() {
let {corpId, code, suiteId, id} = this.$route.query
checkAccess(review) {
let {code, suiteId, id} = this.$route.query
if (this.isPreview) {
this.access = true
} else if (!!this.token) {
} else if (!!this.user.token) {
this.$http.post("/app/appquestionnairetemplate/commitCheck", null, {
params: {id}
}).then(res => {
@@ -57,12 +57,10 @@ export default {
this.err = err
})
} else if (code) {
this.getToken({code, corpId, suiteId, isAppForm: true}).then(() => {
let {query, path, hash} = this.$route
delete query.code
this.root.goto({query, path, hash})
})
} else this.getCode()
this.getToken({code, suiteId}).then(() => this.checkAccess(true))
} else if (!review) this.getCode()
else this.err = "登录失败!"
},
},
created() {

View File

@@ -71,7 +71,7 @@ export default {
name: "formDetail",
inject: {root: {}},
computed: {
...mapState(['openUser', 'token']),
...mapState(['openUser']),
isExam() {
return this.form?.type == 1
},

View File

@@ -16,7 +16,7 @@ export default {
name: "AppCreditPoints",
appName: "我的积分",
computed: {
...mapState(['user', 'token'])
...mapState(['user'])
},
components: {SysUserIntegral, userRank, familyRank},
data() {

View File

@@ -105,7 +105,7 @@ export default {
name: "userRank",
appName: "个人积分",
computed: {
...mapState(['user', 'token'])
...mapState(['user'])
},
props: {
showDetail: { //true 积分明细 false积分排行

View File

@@ -27,14 +27,13 @@
</div>
</template>
<script>
import {mapActions, mapState} from 'vuex'
import {mapState} from 'vuex'
export default {
name: "sysUserIntegral",
appName: "员工积分",
computed: {
...mapState(['user', 'token']),
...mapActions(['selectEnterpriseContact'])
...mapState(['user'])
},
data() {
return {

View File

@@ -105,7 +105,7 @@ export default {
name: "userRank",
appName: "个人积分",
computed: {
...mapState(['user', 'token'])
...mapState(['user'])
},
props: {
showDetail: { //true 积分明细 false积分排行

View File

@@ -25,9 +25,6 @@ import { mapState, mapActions } from 'vuex'
export default {
name: 'newsDetail',
computed: {
...mapState(['token'])
},
data() {
return {
isShowComment: false,

View File

@@ -79,7 +79,7 @@ export default {
name: "AppOnlineAnswer",
appName: "党史题库",
computed: {
...mapState(["user", "token"]),
...mapState(["user"]),
},
data() {
return {

View File

@@ -70,7 +70,7 @@ export default {
name: "AppPartyHistoryEducation",
appName: "党员学习",
computed: {
...mapState(["user", "token"]),
...mapState(["user"]),
},
data() {
return {

View File

@@ -16,7 +16,7 @@ import { mapState } from "vuex";
export default {
name: "home",
computed: {
...mapState(["user", "token"]),
...mapState(["user"]),
},
data() {
return {

View File

@@ -19,7 +19,7 @@ import { mapState } from "vuex";
export default {
name: "home",
computed: {
...mapState(["user", "token"]),
...mapState(["user"]),
},
data() {
return {

View File

@@ -12,9 +12,6 @@
import { mapState } from "vuex";
export default {
name: "home",
computed: {
...mapState(["user", "token"]),
},
data() {
return {
tabList: ['全部', '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],

View File

@@ -18,9 +18,6 @@
import { mapState } from "vuex";
export default {
name: "home",
computed: {
...mapState(["user", "token"]),
},
data() {
return {
id: '',

View File

@@ -22,7 +22,7 @@ import { mapState } from "vuex";
export default {
name: "home",
computed: {
...mapState(["user", "token"]),
...mapState(["user"]),
},
data() {
return {

View File

@@ -34,7 +34,7 @@ import { mapState } from "vuex";
export default {
name: "home",
computed: {
...mapState(["user", "token"]),
...mapState(["user"]),
},
data() {
return {

View File

@@ -14,7 +14,7 @@ instance.interceptors.request.use(config => {
} else if (/\/project\/police\//.test(location.pathname) || config.module == 'hnjc' || module == "hnjc") {
config.baseURL = '/hnjc'
config.url = config.url.replace(/(app|auth|admin)\//, "api/")
} else if (!/^App/.test(module)) {
} else if (!/^App/.test(module) && module) {
config.baseURL = `/${module}`
if (['xaxc'].includes(module)) {
config.url = config.url.replace(/(app|auth|admin)\//, "api/")

View File

@@ -6,7 +6,7 @@ const instance = axios.create({
})
const getToken = () => {
let vuex = uni.getStorageSync("vuex")
return !!vuex ? JSON.parse(vuex).token : null
return !!vuex ? JSON.parse(vuex).user.token : null
}
const source = axios.CancelToken.source();
instance.interceptors.request.use(config => {

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(() => {

View File

@@ -31,7 +31,7 @@ export default {
limit: {default: 1000}
},
computed: {
...mapState(['token']),
...mapState(['user']),
wordCount() {
return this.content?.length || 0
},
@@ -39,7 +39,7 @@ export default {
return this.content || this.placeholder
},
isLogin() {
return !!this.token
return !!this.user.token
},
},
data() {

View File

@@ -58,7 +58,6 @@ export default {
sourceType: {default: () => ['album', 'camera']}
},
computed: {
...mapState(['token']),
errorImage() {
return this.$cdn + 'file.png'
},

View File

@@ -35,7 +35,7 @@ const app = new Vue({
...App
});
let params = {}
if (/saas/.test(location.pathname) > -1) {//上架版
if (/saas/.test(location.pathname)) {//上架版
params = {action: "/app/wxcptp/portal/agentSign", corpId: "ww596787bb70f08288"}
}
if (/AppCountryAlbum/.test(location.pathname)) {//乡村相册版

View File

@@ -25,7 +25,7 @@
},
"devServer": {
"disableHostCheck": true,
"port": "10323",
"port": "80",
"open": true,
"proxy": {
"/lan": {

View File

@@ -64,14 +64,11 @@ export default {
module = 'wangge'
}
this.setModule(module)
this.getToken({...this.form, module, corpId}).then(token => {
if (token) {
this.login(token)
this.getToken({...this.form, module, corpId}).then(() => {
if (module != 'AppCountryAlbum') {
this.getAccount({module})
}
this.target ? uni.reLaunch({url: this.target}) : uni.navigateBack({})
}
}).catch(() => 0)
}
})
@@ -93,14 +90,11 @@ export default {
module = 'wangge'
}
let {code} = this.$route.query
this.getToken({code}).then(token => {
if (token) {
this.login(token)
this.getToken({code}).then(() => {
if (module != 'AppCountryAlbum') {
this.getAccount({module})
}
this.target ? uni.reLaunch({url: this.target}) : uni.navigateBack({})
}
}).catch(err => {
this.err = err
})

View File

@@ -248,7 +248,7 @@ export default {
},
toSessionDetail(id, signStatus) {
uni.navigateTo({
url: `../threeSessions/threeSessionsDetail?id=${id}&signStatus=${signStatus}&token=${uni.getStorageSync("token")}`
url: `../threeSessions/threeSessionsDetail?id=${id}&signStatus=${signStatus}&token=${this.user.token}`
})
},
leave() {

View File

@@ -13,6 +13,8 @@
</div>
</template>
<script>
import {mapState} from "vuex"
export default {
data() {
return {
@@ -24,10 +26,12 @@ export default {
isSign: true
};
},
computed: {
...mapState(['user'])
},
onShow() {
if (uni.getStorageSync('token')) {
if (this.user.token) {
this.meetingId = this.$mp.query.scene
// this.meetingId = 'c29aacb54f074c14ab93532e384a6daa'
this.getUserInfo()
} else {
uni.showModal({

View File

@@ -54,6 +54,7 @@
</template>
<script>
import {mapState} from "vuex"
export default {
name: "fillLog",
data() {
@@ -78,6 +79,7 @@ export default {
};
},
computed: {
...mapState(['user']),
user() {
return uni.getStorageSync("userInfo");
},
@@ -133,6 +135,7 @@ export default {
return;
}
}
const {token} = this.user
uni.chooseImage({
// 从相册选择或相机拍摄
sourceType: that.sourceType[that.sourceTypeIndex],
@@ -144,11 +147,6 @@ export default {
// console.log(res.tempFilePaths)
for (let i = 0; i < that.tempFilePaths.length; i++) {
let str = "";
let token = uni.getStorageSync("token");
// console.log('token',token)
let params = {
token: token,
};
// url String 是 开发者服务器 url
// files Aarry 否 需要上传的文件列表。使用 files 时filePath 和 name 不生效。 5+App
// filePath String 是 要上传文件资源的路径。

View File

@@ -101,6 +101,8 @@
</div>
</template>
<script>
import {mapState} from "vuex"
export default {
data() {
return {
@@ -122,10 +124,10 @@ export default {
user: {}
};
},
computed: {
...mapState(['user'])
},
onLoad(options) {
if (!uni.getStorageSync("token")) {
uni.setStorageSync("token", options.token)
}
this.$dict.load('postStatus', 'meetingClassification', 'topicClassification').then(() => {
this.id = options.id
this.signStatus = options.signStatus
@@ -147,8 +149,7 @@ export default {
uni.navigateTo({url: `./ylinkMeetingRoom?meetingNum=${this.data.onlineNum}&pwd=${this.data.onlinePwd}`})
},
copy() {
var token = uni.getStorageSync('token')
uni.setClipboardData({data: `https://gdpartyh5.cunwuyun.cn/pages/party/threeSessions/threeSessionsDetail?id=${this.id}&signStatus=${this.signStatus}&token=${token}`});
uni.setClipboardData({data: `https://gdpartyh5.cunwuyun.cn/pages/party/threeSessions/threeSessionsDetail?id=${this.id}&signStatus=${this.signStatus}&token=${this.user.token}`});
},
signMeeting() {
this.$http.post(`/app/appthreemeetinguser/signByMeetingIdAndUserIdForWX?meetingId=${this.data.id}&userId=${this.user.id}`, null, {}).then(res => {
@@ -333,7 +334,7 @@ export default {
}
.sign-end {
padding-left: 150 rpx;
padding-left: 150px;
}
.btn-bottom {
@@ -360,10 +361,9 @@ export default {
}
.detail-info {
padding: 16px 0 8px 32px;
border-bottom: 2px solid #D8DDE6;
background-color: #D40A05;
padding-bottom: 80px;
padding: 16px 0 80px 32px;
position: relative;
.info-title {

View File

@@ -23,7 +23,7 @@ export default {
}
},
computed: {
...mapState(['user', 'token']),
...mapState(['user']),
showDetail() {
return !!this.$route.query?.id && this.access
},
@@ -45,7 +45,7 @@ export default {
if (this.isPreview) {
this.access = true
} else if (this.access) {
} else if (!!this.token) {
} else if (!!this.user.token) {
this.$http.post("/app/appquestionnairetemplate/commitCheck", null, {
params: {id}
}).then(res => {

View File

@@ -71,7 +71,7 @@ export default {
name: "formDetail",
inject: {root: {}},
computed: {
...mapState(['openUser', 'token']),
...mapState(['openUser']),
isExam() {
return this.form?.type == 1
},

View File

@@ -7,7 +7,6 @@ import * as modules from "../common/modules";
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
token: "",
openUser: {},
apps: [],
module: ""
@@ -19,11 +18,7 @@ const store = new Vuex.Store({
setModule(state, mod) {
state.module = mod
},
login(state, token) {
state.token = token
},
logout(state) {
state.token = ""
state.openUser = {}
state.user = {}
},