app.js
4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* @Descripttion:
* @version:
* @Author: limin
* @Date: 2021-06-07 14:12:32
* @LastEditors: limin
* @LastEditTime: 2021-07-06 12:02:21
*/
import { ssoApi } from '@/api'
import { store } from 'utils'
import util from '@/utils/util'
const state = {
pageMeta: '',
keepAlives: [],
}
const mutations = {
SET_PAGE_META(state, meta) {
state.pageMeta = meta
},
PUSH_KEEP(state, name) {
if (!state.keepAlives.includes(name)) {
state.keepAlives.push(name)
}
},
REMOVE_KEEP(state, names) {
state.keepAlives = state.keepAlives.reduce((arr, name) => {
if (!names.includes(name)) {
return arr.push(name)
}
return arr
}, [])
},
}
const actions = {
// 设置name
setUserInfo({ commit }, user) {
commit('SET_USER_INFO', user)
store.set(store.consts.USER_KEY, user)
},
setPageMeta({ commit }, meta) {
document.title = meta.title
commit('SET_PAGE_META', meta)
},
setLoginToken({ commit }, val) {
commit('SET_LOGIN_TOKEN', val)
},
setAccountList({ commit }, list) {
commit('SET_ACCOUNT_LIST', list)
},
setSessionId({ commit }, id) {
commit('SET_SESSION_ID', id)
},
pushKeep({ commit }, name) {
commit('PUSH_KEEP', name)
},
removeKeep({ commit }, names) {
commit('REMOVE_KEEP', names)
},
login() {
let [redirectUrl] = window.location.href.split('?')
const code = getParams('code')
if (code) {
const corpId = util.getLocalStorage('dj_scrm_corpId')
ssoApi.login({
code,
corpId
}).then(({ code, data, message }) => {
let url = `${window.$baseUrl}/dj-scrm-wx-work-main/#/`
let route = state.lastPath == 'home' ? '' : state.lastPath
state.showWindow = true
if (code == '0') {
if (data.loginList.length) {
if (data.loginList.length > 1) {
util.setLocalStorage('dj_scrm_loginToken', data.loginToken)
util.setLocalStorage('dj_scrm_accountList', data.loginList)
let redirect_uri = encodeURI(`${redirectUrl}${route}`)
window.location.replace(`${url}common/accountList?redirect_uri=${redirect_uri}`)
} else {
let [userInfo] = data.loginList
util.setLocalStorage('dj_scrm_userInfo', userInfo)
util.setLocalStorage('dj_scrm_session_id', userInfo.sessionId)
// 只有一个账号 history.back()
console.log(`${redirectUrl}${route}`)
window.location.replace(`${redirectUrl}${route}`)
}
} else {
/**没有账号 */
window.location.replace(`${url}common/nothing`)
}
} else {
/**账号过期 (暂时没有字段判断是否过期) */
window.vm.showToast(message)
window.location.replace(`${url}common/overdue`)
}
})
} else {
util.showLoading()
let corpId = getParams('corpId') //从链接获取
if (!corpId) corpId = 'ww7ee69c648ae1b7e6'
util.setLocalStorage('dj_scrm_corpId', corpId)
// 'ww657a2d4ae89a4424'
const wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize'
window.location.replace(`${wxUrl}?appid=${corpId}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`)
}
},
navigation({ dispatch }) {
util.showLoading()
const sessionId = getParams('sessionId')
const storage_sessionId = util.getLocalStorage('dj_scrm_session_id')
if (sessionId || storage_sessionId) {
state.showWindow = true
util.setLocalStorage('dj_scrm_session_id', sessionId || storage_sessionId)
} else {
dispatch('login')
return
}
state.showWindow = true
}
}
const getParams = name => {
let [url, paramsList] = window.location.href.split('?')
if (!paramsList) return name ? null : {}
let param = paramsList.split('&')
let obj = {}
param.map(e => {
let [key, value] = e.split('=')
obj[key] = value
})
return name ? obj[name] : obj
}
export default {
state,
mutations,
actions
}