app.js
5.79 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* @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, util } from 'utils'
import { Http } from 'glsx-common'
import { Notify } from 'vant'
const state = {
pageMeta: '',
keepAlives: [],
lastPath: ''
}
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
}, [])
},
SET_LAST_PATH(state, path) {
state.lastPath = path
}
}
const actions = {
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)
},
pushKeep({ commit }, name) {
commit('PUSH_KEEP', name)
},
removeKeep({ commit }, names) {
commit('REMOVE_KEEP', names)
},
login({ dispatch }) {
let [redirectUrl] = window.location.href.split('?')
console.log(redirectUrl)
// 以下是 本地调试
if (process.env.NODE_ENV == 'development') {
const code = getParams('code')
if (code) {
const { corpId } = util.getLocalStorage('dj_scrm_corpId')
ssoApi
.login({
code,
corpId
})
.then(({ code, data, message }) => {
let route = state.lastPath == 'coupon' ? '' : state.lastPath || ''
route = route.replace('.', '/')
if (code == '0') {
if (data.loginList.length) {
if (data.loginList.length > 1) {
let userInfo = data.loginList[0]
let { merchantId, accountName } = userInfo
let loginAccount = accountName
ssoApi
.selectAccount({
loginAccount,
merchantId,
loginToken: data.loginToken,
loginSource: 'WW',
loginType: 3
})
.then(res => {
util.setLocalStorage('dj_scrm_userInfo', res.data)
util.setSessionId(res.data.sessionId)
dispatch('registerHttp', res.data.sessionId)
state.showWindow = true
window.location.replace(`${redirectUrl}#/${route}`)
})
} else {
let [userInfo] = data.loginList
util.setLocalStorage('dj_scrm_userInfo', userInfo)
util.setSessionId(userInfo.sessionId)
dispatch('registerHttp', userInfo.sessionId)
// 只有一个账号 history.back()
console.log(`${redirectUrl}${route}`)
state.showWindow = true
window.location.replace(`${redirectUrl}#/${route}`)
}
} else {
/**没有账号 */
Notify('没有账号')
// window.location.href = `${url}common/nothing`
}
} else {
/**账号过期 (暂时没有字段判断是否过期) */
Notify(message)
// window.location.href = `${url}common/overdue`
}
})
} else {
util.showLoading()
let { appid } = getParams('corpId') ? getParams('corpId') : '' //从链接获取
let dj_scrm = util.getLocalStorage('dj_scrm_corpId')
if (!appid) appid = (dj_scrm && dj_scrm.corpId) || ''
util.setLocalStorage('dj_scrm_corpId', { corpId: appid })
// 'ww657a2d4ae89a4424'
const wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize'
window.location.href = `${wxUrl}?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`
}
} else {
const storage_corpId = util.getLocalStorage('dj_scrm_corpId')
const { baseURL } = window.$fetchConfig.request
let url = `${baseURL}/dj-scrm-wx-work-main/#/`
window.location.href = `${url}?redirect_uri=${redirectUrl}&corpId=${storage_corpId.corpId}`
}
},
navigation({ dispatch }) {
let { sessionId, corpId, reportType } = getParams()
const storage_corpId = util.getLocalStorage('dj_scrm_corpId')
if (!corpId) corpId = storage_corpId && storage_corpId.corpId
if (corpId && storage_corpId) {
if (corpId != storage_corpId.corpId) {
util.removeLocalStorage('dj_scrm_userInfo')
util.clearSessionId()
}
}
util.setLocalStorage('dj_scrm_corpId', { corpId })
if (reportType) util.setLocalStorage('reportType', { reportType })
const storage_sessionId = util.getSessionId()
if (sessionId || storage_sessionId) {
state.showWindow = true
util.setSessionId(sessionId || storage_sessionId)
dispatch('registerHttp', sessionId || storage_sessionId)
} else {
util.showLoading()
dispatch('registerHttp')
dispatch('login')
return
}
state.showWindow = true
},
registerHttp({ }, sessionId = '') {
window.$HTTP = new Http({ ...window.$fetchConfig, alert: Notify, headers: { token: sessionId } })
}
}
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
}