产品库增加企微端登录,登录修复

This commit is contained in:
aixianling
2022-06-13 19:47:49 +08:00
parent dcf0271990
commit ff0414cb79
2 changed files with 40 additions and 11 deletions

View File

@@ -10,7 +10,7 @@ export const config = {
mutations: {
getConfig(state, params) {
for (const key in params) {
Vue.set(state, key, user[key])
Vue.set(state, key, params[key])
}
},
clearConfig(state) {
@@ -26,7 +26,6 @@ export const config = {
if (state.agentSignURL == url) {
return Promise.resolve()
} else {
commit("clearConfig")
commit("getConfig", {agentSignURL: url})
let action = "/app/wxcp/portal/agentSign"
if (!!params?.action) {
@@ -50,6 +49,7 @@ export const config = {
...res.data,
agentSignURL: url
}
if (!!state.corpid && res.data.corpid != state.corpid) commit('clearConfig')
commit("getConfig", config)
return config
}
@@ -59,18 +59,19 @@ export const config = {
}
},
getCode({state, dispatch}, tryAgentSign = false) {
let {corpId} = state, url = location.href, REDIRECT_URI = encodeURIComponent(url), scope = "snsapi_base"
let {corpid: corpId} = state, url = location.href, REDIRECT_URI = encodeURIComponent(url), scope = "snsapi_base"
if (/\/AppForm\//.test(location.search)) {
scope = "snsapi_userinfo"
}
return new Promise((resolve, reject) => {
if (corpId && scope) {
location.replace('https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE#wechat_redirect'
.replace(/APPID/g, cid)
let oauthURL = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE#wechat_redirect'
.replace(/APPID/g, corpId)
.replace(/REDIRECT_URI/g, REDIRECT_URI)
.replace(/SCOPE/g, scope))
.replace(/SCOPE/g, scope)
location.replace(oauthURL)
} else if (!tryAgentSign) {
dispatch("agentSign").then(() => dispatch("getCode", true))
dispatch("agentSign").then(() => dispatch("getCode", true)).then(() => resolve())
} else reject("URL缺少必要参数(corpId)")
})
},
@@ -104,7 +105,7 @@ export const config = {
}).then(res => {
if (res?.access_token) {
return [res?.token_type, res?.access_token].join(" ").trim()
}
} else return Promise.reject(res.msg)
}).catch(err => {
uni.showToast({title: err, icon: 'none'})
})

View File

@@ -46,7 +46,7 @@ export default {
}
},
methods: {
...mapActions(['getToken','getAccount']),
...mapActions(['getToken', 'getAccount', 'getCode']),
...mapMutations(['login', 'logout']),
handleLogin() {
this.$refs.loginForm.validate(v => {
@@ -61,7 +61,7 @@ export default {
}
this.getToken({...this.form, module, corpId}).then(token => {
this.login(token)
if( module != 'AppCountryAlbum'){
if (module != 'AppCountryAlbum') {
this.getAccount({module})
}
this.target ? uni.reLaunch({url: this.target}) : uni.navigateBack({})
@@ -74,7 +74,35 @@ export default {
}
},
onShow() {
this.handleLogout()
if (/wxwork/.test(navigator.userAgent)) {
//在企微端
if (this.$route.query.code) {
let {name: module, libPath} = this.currentApp,
corpId = 'ww596787bb70f08288'
if (/\/project\/police\//.test(libPath)) {
module = 'hnjc'
} else if (/\/project\/beta\//.test(libPath)) {
corpId = 'ww2a667717a70164f1'
module = 'wangge'
}
let {code} = this.$route.query
this.getToken({code}).then(token => {
this.login(token)
if (module != 'AppCountryAlbum') {
this.getAccount({module})
}
this.target ? uni.reLaunch({url: this.target}) : uni.navigateBack({})
}).catch(err => {
this.err = err
})
} else {
this.getCode().catch(err => {
this.err = err
})
}
} else {
this.handleLogout()
}
},
mounted() {
this.$refs.loginForm.setRules(this.rules)