Skip to content

Commit 6c4fa63

Browse files
committed
fix(view): 登录和相关配置
1 parent 24a0cbc commit 6c4fa63

File tree

7 files changed

+432
-104
lines changed

7 files changed

+432
-104
lines changed

admin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"axios": "0.18.0",
1919
"element-ui": "2.7.2",
2020
"js-cookie": "2.2.0",
21+
"layui-src": "^2.5.4",
2122
"normalize.css": "7.0.0",
2223
"nprogress": "0.2.0",
2324
"path-to-regexp": "2.4.0",

admin/src/assets/js/gt.js

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
/* initGeetest 1.0.0
2+
* 用于加载id对应的验证码库,并支持宕机模式
3+
* 暴露 initGeetest 进行验证码的初始化
4+
* 一般不需要用户进行修改
5+
*/
6+
let initGeetest = (function (global, factory) {
7+
'use strict'
8+
if (typeof module === 'object' && typeof module.exports === 'object') {
9+
// CommonJS
10+
module.exports = global.document ?
11+
factory(global, true) :
12+
function (w) {
13+
if (!w.document) {
14+
throw new Error('Geetest requires a window with a document')
15+
}
16+
return factory(w)
17+
}
18+
} else {
19+
factory(global)
20+
}
21+
})(typeof window !== 'undefined' ? window : this, function (window, noGlobal) {
22+
'use strict'
23+
if (typeof window === 'undefined') {
24+
throw new Error('Geetest requires browser environment')
25+
}
26+
var document = window.document
27+
var Math = window.Math
28+
var head = document.getElementsByTagName('head')[0]
29+
30+
function _Object(obj) {
31+
this._obj = obj
32+
}
33+
34+
_Object.prototype = {
35+
_each: function (process) {
36+
var _obj = this._obj
37+
for (var k in _obj) {
38+
if (_obj.hasOwnProperty(k)) {
39+
process(k, _obj[k])
40+
}
41+
}
42+
return this
43+
}
44+
}
45+
46+
function Config(config) {
47+
var self = this
48+
new _Object(config)._each(function (key, value) {
49+
self[key] = value
50+
})
51+
}
52+
53+
Config.prototype = {
54+
api_server: 'api.geetest.com',
55+
protocol: 'http://',
56+
type_path: '/gettype.php',
57+
fallback_config: {
58+
slide: {
59+
static_servers: ['static.geetest.com', 'dn-staticdown.qbox.me'],
60+
type: 'slide',
61+
slide: '/static/js/geetest.0.0.0.js'
62+
},
63+
fullpage: {
64+
static_servers: ['static.geetest.com', 'dn-staticdown.qbox.me'],
65+
type: 'fullpage',
66+
fullpage: '/static/js/fullpage.0.0.0.js'
67+
}
68+
},
69+
_get_fallback_config: function () {
70+
var self = this
71+
if (isString(self.type)) {
72+
return self.fallback_config[self.type]
73+
} else if (self.new_captcha) {
74+
return self.fallback_config.fullpage
75+
} else {
76+
return self.fallback_config.slide
77+
}
78+
},
79+
_extend: function (obj) {
80+
var self = this
81+
new _Object(obj)._each(function (key, value) {
82+
self[key] = value
83+
})
84+
}
85+
}
86+
var isNumber = function (value) {
87+
return (typeof value === 'number')
88+
}
89+
var isString = function (value) {
90+
return (typeof value === 'string')
91+
}
92+
var isBoolean = function (value) {
93+
return (typeof value === 'boolean')
94+
}
95+
var isObject = function (value) {
96+
return (typeof value === 'object' && value !== null)
97+
}
98+
var isFunction = function (value) {
99+
return (typeof value === 'function')
100+
}
101+
var callbacks = {}
102+
var status = {}
103+
var random = function () {
104+
return parseInt(Math.random() * 10000) + (new Date()).valueOf()
105+
}
106+
var loadScript = function (url, cb) {
107+
var script = document.createElement('script')
108+
script.charset = 'UTF-8'
109+
script.async = true
110+
script.onerror = function () {
111+
cb(true)
112+
}
113+
var loaded = false
114+
script.onload = script.onreadystatechange = function () {
115+
if (!loaded &&
116+
(!script.readyState ||
117+
'loaded' === script.readyState ||
118+
'complete' === script.readyState)) {
119+
120+
loaded = true
121+
setTimeout(function () {
122+
cb(false)
123+
}, 0)
124+
}
125+
}
126+
script.src = url
127+
head.appendChild(script)
128+
}
129+
var normalizeDomain = function (domain) {
130+
return domain.replace(/^https?:\/\/|\/$/g, '')
131+
}
132+
var normalizePath = function (path) {
133+
path = path.replace(/\/+/g, '/')
134+
if (path.indexOf('/') !== 0) {
135+
path = '/' + path
136+
}
137+
return path
138+
}
139+
var normalizeQuery = function (query) {
140+
if (!query) {
141+
return ''
142+
}
143+
var q = '?'
144+
new _Object(query)._each(function (key, value) {
145+
if (isString(value) || isNumber(value) || isBoolean(value)) {
146+
q = q + encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&'
147+
}
148+
})
149+
if (q === '?') {
150+
q = ''
151+
}
152+
return q.replace(/&$/, '')
153+
}
154+
var makeURL = function (protocol, domain, path, query) {
155+
domain = normalizeDomain(domain)
156+
157+
var url = normalizePath(path) + normalizeQuery(query)
158+
if (domain) {
159+
url = protocol + domain + url
160+
}
161+
162+
return url
163+
}
164+
var load = function (protocol, domains, path, query, cb) {
165+
var tryRequest = function (at) {
166+
167+
var url = makeURL(protocol, domains[at], path, query)
168+
loadScript(url, function (err) {
169+
if (err) {
170+
if (at >= domains.length - 1) {
171+
cb(true)
172+
} else {
173+
tryRequest(at + 1)
174+
}
175+
} else {
176+
cb(false)
177+
}
178+
})
179+
}
180+
tryRequest(0)
181+
}
182+
var jsonp = function (domains, path, config, callback) {
183+
if (isObject(config.getLib)) {
184+
config._extend(config.getLib)
185+
callback(config)
186+
return
187+
}
188+
if (config.offline) {
189+
callback(config._get_fallback_config())
190+
return
191+
}
192+
var cb = 'geetest_' + random()
193+
window[cb] = function (data) {
194+
if (data.status === 'success') {
195+
callback(data.data)
196+
} else if (!data.status) {
197+
callback(data)
198+
} else {
199+
callback(config._get_fallback_config())
200+
}
201+
window[cb] = undefined
202+
try {
203+
delete window[cb]
204+
} catch (e) {
205+
}
206+
}
207+
load(config.protocol, domains, path, {
208+
gt: config.gt,
209+
callback: cb
210+
}, function (err) {
211+
if (err) {
212+
callback(config._get_fallback_config())
213+
}
214+
})
215+
}
216+
var throwError = function (errorType, config) {
217+
var errors = {
218+
networkError: '网络错误'
219+
}
220+
if (typeof config.onError === 'function') {
221+
config.onError(errors[errorType])
222+
} else {
223+
throw new Error(errors[errorType])
224+
}
225+
}
226+
var detect = function () {
227+
return !!window.Geetest
228+
}
229+
if (detect()) {
230+
status.slide = 'loaded'
231+
}
232+
var initGeetest = function (userConfig, callback) {
233+
var config = new Config(userConfig)
234+
if (userConfig.https) {
235+
config.protocol = 'https://'
236+
} else if (!userConfig.protocol) {
237+
config.protocol = window.location.protocol + '//'
238+
}
239+
jsonp([config.api_server || config.apiserver], config.type_path, config, function (newConfig) {
240+
var type = newConfig.type
241+
var init = function () {
242+
config._extend(newConfig)
243+
callback(new window.Geetest(config))
244+
}
245+
callbacks[type] = callbacks[type] || []
246+
var s = status[type] || 'init'
247+
if (s === 'init') {
248+
status[type] = 'loading'
249+
callbacks[type].push(init)
250+
load(config.protocol, newConfig.static_servers || newConfig.domains, newConfig[type] || newConfig.path, null, function (err) {
251+
if (err) {
252+
status[type] = 'fail'
253+
throwError('networkError', config)
254+
} else {
255+
status[type] = 'loaded'
256+
var cbs = callbacks[type]
257+
for (var i = 0, len = cbs.length; i < len; i = i + 1) {
258+
var cb = cbs[i]
259+
if (isFunction(cb)) {
260+
cb()
261+
}
262+
}
263+
callbacks[type] = []
264+
}
265+
})
266+
} else if (s === 'loaded') {
267+
init()
268+
} else if (s === 'fail') {
269+
throwError('networkError', config)
270+
} else if (s === 'loading') {
271+
callbacks[type].push(init)
272+
}
273+
})
274+
}
275+
window.initGeetest = initGeetest
276+
return initGeetest
277+
})
278+
279+
export default initGeetest

admin/src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'element-ui/lib/theme-chalk/index.css'
77
import locale from 'element-ui/lib/locale/lang/en' // lang i18n
88

99
import '@/styles/index.scss' // global css
10+
// 引入 layUI css
11+
import 'layui-src/dist/css/layui.css'
1012

1113
import App from './App'
1214
import store from './store'

admin/src/permission.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import getPageTitle from '@/utils/get-page-title'
88

99
NProgress.configure({ showSpinner: false }) // NProgress Configuration
1010

11-
const whiteList = ['/login'] // no redirect whitelist
11+
const whiteList = ['/login', '/index', '/line', '/home', '/404', '/', '', '/echarts', '/md', '/out', '/excel', '/upload'] // 不重定向白名单
1212

1313
router.beforeEach(async(to, from, next) => {
1414
// start progress bar

admin/src/utils/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Cookies from 'js-cookie'
22

3-
const TokenKey = 'vue_admin_template_token'
3+
const TokenKey = 'Admin-Token'
44

55
export function getToken() {
66
return Cookies.get(TokenKey)

admin/src/utils/request.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ service.interceptors.request.use(
1919
// let each request carry token
2020
// ['X-Token'] is a custom headers key
2121
// please modify it according to the actual situation
22-
config.headers['X-Token'] = getToken()
22+
config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
2323
}
2424
return config
2525
},
@@ -53,14 +53,18 @@ service.interceptors.response.use(
5353
duration: 5 * 1000
5454
})
5555

56-
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
57-
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
56+
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; 1200: 登录超时
57+
if (res.code === 50008 || res.code === 50012 || res.code === 50014 || res.code === 1200) {
5858
// to re-login
59-
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
60-
confirmButtonText: 'Re-Login',
61-
cancelButtonText: 'Cancel',
62-
type: 'warning'
63-
}).then(() => {
59+
MessageBox.confirm(
60+
'你已被登出,可以取消继续留在该页面,或者重新登录',
61+
'确定登出',
62+
{
63+
confirmButtonText: '重新登录',
64+
cancelButtonText: '取消',
65+
type: 'warning'
66+
}
67+
).then(() => {
6468
store.dispatch('user/resetToken').then(() => {
6569
location.reload()
6670
})

0 commit comments

Comments
 (0)