app.js
5.62 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
/*
* @Author: 'wangzhihui' '1733904062@qq.com'
* @Date: 2026-01-26 12:02:37
* @LastEditors: 'wangzhihui' '1733904062@qq.com'
* @LastEditTime: 2026-03-20 16:50:12
* @FilePath: \wx-notification\src\store\modules\app.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import { util } from 'utils'
import { Http } from 'glsx-common'
import { Notify } from 'vant'
import { ssoApi } from '@/api'
import store from '@/store'
const state = {
showWindow: false,
lastPath: '',
authorizePage: false,
}
import router from '@/router'
const mutations = {
SET_PAGE_META(state, meta) {
state.pageMeta = meta
},
SET_LAST_PATH(state, path) {
state.lastPath = path
},
SET_SHOW_WINDOW(state, showWindow) {
state.showWindow = showWindow
},
SET_AUTHORIZE_PAGE(state, authorizePage) {
state.authorizePage = authorizePage
}
}
const actions = {
setPageMeta({ commit }, meta) {
document.title = meta.title || '企微通知模板'
commit('SET_PAGE_META', meta)
},
registerHttp({ }, sessionId = '') {
let { scrmRequest, djRequest } = window.$fetchConfig
window.$SCRM_HTTP = new Http({ ...scrmRequest, alert: Notify })
window.$HTTP = new Http({ ...djRequest, alert: Notify, headers: { token: sessionId } })
// window.$HTTP = new Http({ ...window.$fetchConfig, alert: Notify, headers: { token: sessionId } })
},
login({ dispatch, commit, state }) {
//获取当前路由路径
const currentPath = window.location.href;
const pathKeywords = ['/department', '/personage', '/company'];
const isTargetPath = pathKeywords.some(keyword => currentPath.includes(keyword));
// const pathTitleMap = {
// '/department': '美团消费部门月度总结',
// '/personage': '美团消费个人月度总结',
// '/company': '美团消费公司月度总结'
// };
// const matchedPath = Object.keys(pathTitleMap).find(path => currentPath.includes(path));
// if (matchedPath) {
// document.title = pathTitleMap[matchedPath];
// }
const pushId = util.getQuery('pushId');
if (isTargetPath && (pushId && (pushId * 1) > 744)) {
try {
let { corpId, agentId } = util.getQuery();
const wwCode = util.getQuery('code');
let { loginUrl } = window.$fetchConfig;
let userInfo = util.getLocalStorage('ww_userInfo') || '';
if (userInfo && userInfo.timestamp) {
//判断用户时间戳是否过期12小时
if (userInfo.timestamp && (Date.now() - userInfo.timestamp) > (1000 * 60 * 1440)) {
console.log('用户登录过期');
} else {
console.log('用户已经登录');
commit('SET_AUTHORIZE_PAGE', true);
return commit('SET_SHOW_WINDOW', true);
}
}
if (wwCode) {
ssoApi.login({ code: wwCode, corpId, getUserInfoFlag: false }).then(res => {
const { code, data, message } = res;
if (code == "0") {
commit('SET_AUTHORIZE_PAGE', true);
commit('SET_SHOW_WINDOW', true);
const hrefUrl = window.location.href;
const newUrl = hrefUrl.replace(/([?&])code=[^&]*/, (match, prefix) => { return prefix === '?' ? '?' : ''; });
window.history.replaceState({}, '', newUrl);
if (data.loginList && data.loginList.length) {
let [item] = data.loginList
if (item.loginList && item.loginList.length) {
let [userInfo] = item.loginList,
{ phone } = userInfo
if (phone) util.setLocalStorage('ww_params_phone', phone)
//获取当前时间戳
userInfo.timestamp = Date.now();
util.setLocalStorage('ww_userInfo', userInfo);
} else throw ''
} else throw ''
} else {
Notify({ message: message })
commit('SET_SHOW_WINDOW', false);
commit('SET_AUTHORIZE_PAGE', false);
}
})
} else {
commit('SET_AUTHORIZE_PAGE', false);
//是否企微环境
const ua = navigator.userAgent.toLowerCase()
if (Boolean(ua.match(/MicroMessenger/i)) && Boolean(ua.match(/wxwork/i))) {
window.location.href = `${loginUrl}?redirect_uri=${encodeURIComponent(window.location.href)}&corpId=${corpId}&agentId=${agentId}&backLoginCode=BACKCODE`
} else {
Notify({ message: '请使用企业微信打开', duration: 72000000 })
}
}
} catch (error) {
if (!error) error = '登录失败或无权限'
if (error) Notify(typeof error === 'string' ? error : error.toString())
}
} else {
commit('SET_AUTHORIZE_PAGE', true);
commit('SET_SHOW_WINDOW', true);
}
},
navigation({ dispatch, commit }, to) {
let { corpId, agentId, phone, name, id } = util.getQuery(),
userInfo = util.getLocalStorage('ww_userInfo')
if (phone) util.setLocalStorage('ww_params_phone', phone)
if (name) util.setLocalStorage('ww_params_name', name)
if (id) util.setLocalStorage('ww_params_id', id)
dispatch('registerHttp')
if (corpId) {
let ww_storage_info = {}
ww_storage_info.corpId = corpId
if (agentId && agentId != 'undefined') ww_storage_info.agentId = agentId
util.setLocalStorage('ww_storage_info', ww_storage_info)
}
if (userInfo) {
commit('SET_SHOW_WINDOW', true)
} else {
util.showLoading()
dispatch('login')
}
}
}
export default {
state,
mutations,
actions
}