load.js
4.12 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
/*
* @Descripttion:
* @version: 1.0.0
* @Author: limin
* @Date: 2021-07-15 15:46:53
* @LastEditors: huyue
* @LastEditTime: 2023-10-17 15:49:47
* @FilePath: \account\js\load.js
*/
;(function load() {
loadLayConfig()
loadConfig()
loadHeaderFooter()
connect()
})()
/**
* 跳转方法 TODO
*/
function go(module, page, params, blank) {
var _p = !params || layui.$.isEmptyObject(params) ? '' : genurl(params)
var href = '/html/' + module + '/' + page + '.html'
if (blank) {
window.open(href, '_blank')
} else {
window.location.href = _p ? href + '?' + _p : href
}
}
function loadLayConfig() {
layui
.config({
base: '/js/'
})
.extend({
xmSelect: 'xm-select'
})
}
function loadHeaderFooter() {
layui.use(['store'], function () {
var isExternal = isAppend()
var store = layui.store
if (isExternal) {
//运营平台外部系统
append()
} else {
//嵌入运营平台 或者是登录页面
if (qs('sessionId')) {
// SCRM 系统
store.set('accountInfo', {
merchantAccountLevel: qs('accountLevel'),
merchantId: qs('merchantId'),
model: qs('merchantModel') || 1
})
store.set('token', qs('sessionId'))
} else {
// 运营平台
if (isLoginPage()) {
//如果是登陆页面 直接返回 登录成功会设置token
return
}
var t = qs('t') //运营平台子系统从 父系统的iframe src获取 TGT.
if (t) {
store.set('token', t)
}
}
}
})
}
function append() {
var $ = layui.$
var header = document.createElement('header')
var footer = document.createElement('footer')
$(header).load('/html/components/header.html')
$(footer).load('/html/components/footer.html')
$('main').before(header).after(footer)
}
function isAppend() {
return !isLoginPage() && self == top
}
function isLoginPage() {
var _href = location.href.split('/')
var _path = _href.slice(_href.length - 1, _href.length).toString()
return _path.split('?')[0] === 'login.html'
}
function loadConfig() {
layui.use(['store'], function () {
var store = layui.store
if (store.getEx('external', true)) {
return
}
var ajax = layui.$.ajax
ajax({
// url: 'https://oss-config.test.glsx.com.cn/mock/5be17454f31545347559d499/config/glsx-oss-account-config',
url: 'https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=glsx-oss-account-config',
async: false,
success: function (res) {
store.setEx('external', res.external, 10 * 60 * 1000)
}
})
})
}
function goLogin() {
var store = layui.store
if (store.get('accountInfo')) {
$messenger.emit('route-to', { path: '/login' })
} else {
if (!isAppend()) return
layui.use(['store'], function () {
layui.store.remove('external')
layui.store.remove('token')
layui.store.remove('user')
window.location.href = '/login.html'
})
}
}
function genurl(param, key, encode) {
// if (!param || layui.$.isEmptyObject(param)) return ''
var params = []
var t = typeof param
if (t == 'string' || t == 'number' || t == 'boolean') {
params.push(key + '=' + (encode == null || encode ? encodeURIComponent(param) : param))
} else {
for (var i in param) {
var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i)
params.push(genurl(param[i], k, encode))
}
}
return params.join('&')
}
function qs(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
var r = window.location.search.substr(1).match(reg)
if (r != null) {
return decodeURI(r[2])
// return unescape(r[2])
}
return null
}
function connect() {
layui.use(['postmate'], function () {
var Postmate = layui.postmate
new Postmate.Model({
height: () => {
console.log('clientHeight', document.body.clientHeight, 'document.height ', document.height || document.body.offsetHeight)
return document.height || document.body.offsetHeight
},
push: data => {
console.log(data)
}
}).then(parent => (window.$messenger = parent))
})
}