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

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

View File

@@ -46,7 +46,7 @@ export default {
} }
}, },
methods: { methods: {
...mapActions(['getToken','getAccount']), ...mapActions(['getToken', 'getAccount', 'getCode']),
...mapMutations(['login', 'logout']), ...mapMutations(['login', 'logout']),
handleLogin() { handleLogin() {
this.$refs.loginForm.validate(v => { this.$refs.loginForm.validate(v => {
@@ -61,7 +61,7 @@ export default {
} }
this.getToken({...this.form, module, corpId}).then(token => { this.getToken({...this.form, module, corpId}).then(token => {
this.login(token) this.login(token)
if( module != 'AppCountryAlbum'){ if (module != 'AppCountryAlbum') {
this.getAccount({module}) this.getAccount({module})
} }
this.target ? uni.reLaunch({url: this.target}) : uni.navigateBack({}) this.target ? uni.reLaunch({url: this.target}) : uni.navigateBack({})
@@ -74,7 +74,35 @@ export default {
} }
}, },
onShow() { 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() { mounted() {
this.$refs.loginForm.setRules(this.rules) this.$refs.loginForm.setRules(this.rules)