mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
added ApiAdmin添加登录验证
This commit is contained in:
parent
40eff4e80e
commit
caacd73425
@ -16,6 +16,13 @@ class LoginController extends BaseController {
|
||||
public function login() {
|
||||
$pass = user_md5(I('post.password'));
|
||||
$user = I('post.username');
|
||||
|
||||
$challenge = I('post.geetest_challenge');
|
||||
$validate = I('post.geetest_validate');
|
||||
if(!$challenge || md5($challenge) != $validate){
|
||||
$this->ajaxError('请先通过验证!');
|
||||
}
|
||||
|
||||
$userInfo = D('ApiUser')->where(array('username' => $user, 'password' => $pass))->find();
|
||||
if (!empty($userInfo)) {
|
||||
if ($userInfo['status']) {
|
||||
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/**
|
||||
* 工具类控制器,不受权限等控制
|
||||
* @since 2017/06/23 创建
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Admin\Controller;
|
||||
|
||||
|
||||
use Think\Controller;
|
||||
|
||||
class VerificationController extends Controller {
|
||||
|
||||
private $gt_captcha_id = 'YourID';
|
||||
private $gt_private_key = 'YourKey';
|
||||
|
||||
public function gt(){
|
||||
$rnd1 = md5(rand(0, 100));
|
||||
$rnd2 = md5(rand(0, 100));
|
||||
$challenge = $rnd1 . substr($rnd2, 0, 2);
|
||||
$result = array(
|
||||
'success' => 0,
|
||||
'gt' => $this->gt_captcha_id,
|
||||
'challenge' => $challenge,
|
||||
'new_captcha'=>1
|
||||
);
|
||||
$this->ajaxReturn($result);
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
@ -52,6 +51,33 @@
|
||||
.login-box .version{
|
||||
font-size: 12px;
|
||||
}
|
||||
.inp {
|
||||
border: 1px solid gray;
|
||||
padding: 0 10px;
|
||||
width: 200px;
|
||||
height: 30px;
|
||||
font-size: 18px;
|
||||
}
|
||||
.btn {
|
||||
border: 1px solid gray;
|
||||
width: 100px;
|
||||
height: 30px;
|
||||
font-size: 18px;
|
||||
cursor: pointer;
|
||||
}
|
||||
#embed-captcha {
|
||||
width: 300px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.show {
|
||||
display: block;
|
||||
}
|
||||
.hide {
|
||||
display: none;
|
||||
}
|
||||
#notice {
|
||||
color: red;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body class="login-body">
|
||||
@ -73,6 +99,13 @@
|
||||
<input type="password" name="password" required class="layui-input" lay-verify="password" placeholder="请输入密码"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-input-inline">
|
||||
<div id="embed-captcha"></div>
|
||||
<p id="wait" class="show">正在加载验证码......</p>
|
||||
<p id="notice" class="hide">请先完成验证</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<button type="reset" class="layui-btn btn-reset layui-btn-danger" >重置</button>
|
||||
<button type="button" class="layui-btn btn-submit" lay-submit="" lay-filter="sub">立即登录</button>
|
||||
@ -80,6 +113,8 @@
|
||||
</form>
|
||||
</div>
|
||||
<script type="text/javascript" src="__PUBLIC__/layui/layui.js"></script>
|
||||
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
|
||||
<script src="__PUBLIC__/js/gt.js"></script>
|
||||
<script type="text/javascript">
|
||||
layui.use(['form', 'layer'], function () {
|
||||
var $ = layui.jquery,form = layui.form(),layer = layui.layer;
|
||||
@ -114,5 +149,38 @@
|
||||
})
|
||||
|
||||
</script>
|
||||
<script type="text/javascript">
|
||||
var handlerEmbed = function (captchaObj) {
|
||||
$("#embed-submit").click(function (e) {
|
||||
var validate = captchaObj.getValidate();
|
||||
if (!validate) {
|
||||
$("#notice")[0].className = "show";
|
||||
setTimeout(function () {
|
||||
$("#notice")[0].className = "hide";
|
||||
}, 2000);
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
captchaObj.appendTo("#embed-captcha");
|
||||
captchaObj.onReady(function () {
|
||||
$("#wait")[0].className = "hide";
|
||||
});
|
||||
};
|
||||
$.ajax({
|
||||
url: "{:U('Verification/gt')}?t=" + (new Date()).getTime(),
|
||||
type: "get",
|
||||
dataType: "json",
|
||||
success: function (data) {
|
||||
console.log(data);
|
||||
initGeetest({
|
||||
gt: data.gt,
|
||||
challenge: data.challenge,
|
||||
new_captcha: data.new_captcha,
|
||||
product: "float",
|
||||
offline: !data.success
|
||||
}, handlerEmbed);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
277
Public/js/gt.js
Normal file
277
Public/js/gt.js
Normal file
@ -0,0 +1,277 @@
|
||||
/* initGeetest 1.0.0
|
||||
* 用于加载id对应的验证码库,并支持宕机模式
|
||||
* 暴露 initGeetest 进行验证码的初始化
|
||||
* 一般不需要用户进行修改
|
||||
*/
|
||||
(function (global, factory) {
|
||||
"use strict";
|
||||
if (typeof module === "object" && typeof module.exports === "object") {
|
||||
// CommonJS
|
||||
module.exports = global.document ?
|
||||
factory(global, true) :
|
||||
function (w) {
|
||||
if (!w.document) {
|
||||
throw new Error("Geetest requires a window with a document");
|
||||
}
|
||||
return factory(w);
|
||||
};
|
||||
} else {
|
||||
factory(global);
|
||||
}
|
||||
})(typeof window !== "undefined" ? window : this, function (window, noGlobal) {
|
||||
"use strict";
|
||||
if (typeof window === 'undefined') {
|
||||
throw new Error('Geetest requires browser environment');
|
||||
}
|
||||
var document = window.document;
|
||||
var Math = window.Math;
|
||||
var head = document.getElementsByTagName("head")[0];
|
||||
|
||||
function _Object(obj) {
|
||||
this._obj = obj;
|
||||
}
|
||||
|
||||
_Object.prototype = {
|
||||
_each: function (process) {
|
||||
var _obj = this._obj;
|
||||
for (var k in _obj) {
|
||||
if (_obj.hasOwnProperty(k)) {
|
||||
process(k, _obj[k]);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
};
|
||||
function Config(config) {
|
||||
var self = this;
|
||||
new _Object(config)._each(function (key, value) {
|
||||
self[key] = value;
|
||||
});
|
||||
}
|
||||
|
||||
Config.prototype = {
|
||||
api_server: 'api.geetest.com',
|
||||
protocol: 'http://',
|
||||
type_path: '/gettype.php',
|
||||
fallback_config: {
|
||||
slide: {
|
||||
static_servers: ["static.geetest.com", "dn-staticdown.qbox.me"],
|
||||
type: 'slide',
|
||||
slide: '/static/js/geetest.0.0.0.js'
|
||||
},
|
||||
fullpage: {
|
||||
static_servers: ["static.geetest.com", "dn-staticdown.qbox.me"],
|
||||
type: 'fullpage',
|
||||
fullpage: '/static/js/fullpage.0.0.0.js'
|
||||
}
|
||||
},
|
||||
_get_fallback_config: function () {
|
||||
var self = this;
|
||||
if (isString(self.type)) {
|
||||
return self.fallback_config[self.type];
|
||||
} else if (self.new_captcha) {
|
||||
return self.fallback_config.fullpage;
|
||||
} else {
|
||||
return self.fallback_config.slide;
|
||||
}
|
||||
},
|
||||
_extend: function (obj) {
|
||||
var self = this;
|
||||
new _Object(obj)._each(function (key, value) {
|
||||
self[key] = value;
|
||||
})
|
||||
}
|
||||
};
|
||||
var isNumber = function (value) {
|
||||
return (typeof value === 'number');
|
||||
};
|
||||
var isString = function (value) {
|
||||
return (typeof value === 'string');
|
||||
};
|
||||
var isBoolean = function (value) {
|
||||
return (typeof value === 'boolean');
|
||||
};
|
||||
var isObject = function (value) {
|
||||
return (typeof value === 'object' && value !== null);
|
||||
};
|
||||
var isFunction = function (value) {
|
||||
return (typeof value === 'function');
|
||||
};
|
||||
var callbacks = {};
|
||||
var status = {};
|
||||
var random = function () {
|
||||
return parseInt(Math.random() * 10000) + (new Date()).valueOf();
|
||||
};
|
||||
var loadScript = function (url, cb) {
|
||||
var script = document.createElement("script");
|
||||
script.charset = "UTF-8";
|
||||
script.async = true;
|
||||
script.onerror = function () {
|
||||
cb(true);
|
||||
};
|
||||
var loaded = false;
|
||||
script.onload = script.onreadystatechange = function () {
|
||||
if (!loaded &&
|
||||
(!script.readyState ||
|
||||
"loaded" === script.readyState ||
|
||||
"complete" === script.readyState)) {
|
||||
|
||||
loaded = true;
|
||||
setTimeout(function () {
|
||||
cb(false);
|
||||
}, 0);
|
||||
}
|
||||
};
|
||||
script.src = url;
|
||||
head.appendChild(script);
|
||||
};
|
||||
var normalizeDomain = function (domain) {
|
||||
return domain.replace(/^https?:\/\/|\/$/g, '');
|
||||
};
|
||||
var normalizePath = function (path) {
|
||||
path = path.replace(/\/+/g, '/');
|
||||
if (path.indexOf('/') !== 0) {
|
||||
path = '/' + path;
|
||||
}
|
||||
return path;
|
||||
};
|
||||
var normalizeQuery = function (query) {
|
||||
if (!query) {
|
||||
return '';
|
||||
}
|
||||
var q = '?';
|
||||
new _Object(query)._each(function (key, value) {
|
||||
if (isString(value) || isNumber(value) || isBoolean(value)) {
|
||||
q = q + encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&';
|
||||
}
|
||||
});
|
||||
if (q === '?') {
|
||||
q = '';
|
||||
}
|
||||
return q.replace(/&$/, '');
|
||||
};
|
||||
var makeURL = function (protocol, domain, path, query) {
|
||||
domain = normalizeDomain(domain);
|
||||
|
||||
var url = normalizePath(path) + normalizeQuery(query);
|
||||
if (domain) {
|
||||
url = protocol + domain + url;
|
||||
}
|
||||
|
||||
return url;
|
||||
};
|
||||
var load = function (protocol, domains, path, query, cb) {
|
||||
var tryRequest = function (at) {
|
||||
|
||||
var url = makeURL(protocol, domains[at], path, query);
|
||||
loadScript(url, function (err) {
|
||||
if (err) {
|
||||
if (at >= domains.length - 1) {
|
||||
cb(true);
|
||||
} else {
|
||||
tryRequest(at + 1);
|
||||
}
|
||||
} else {
|
||||
cb(false);
|
||||
}
|
||||
});
|
||||
};
|
||||
tryRequest(0);
|
||||
};
|
||||
var jsonp = function (domains, path, config, callback) {
|
||||
if (isObject(config.getLib)) {
|
||||
config._extend(config.getLib);
|
||||
callback(config);
|
||||
return;
|
||||
}
|
||||
if (config.offline) {
|
||||
callback(config._get_fallback_config());
|
||||
return;
|
||||
}
|
||||
var cb = "geetest_" + random();
|
||||
window[cb] = function (data) {
|
||||
if (data.status === 'success') {
|
||||
callback(data.data);
|
||||
} else if (!data.status) {
|
||||
callback(data);
|
||||
} else {
|
||||
callback(config._get_fallback_config());
|
||||
}
|
||||
window[cb] = undefined;
|
||||
try {
|
||||
delete window[cb];
|
||||
} catch (e) {
|
||||
}
|
||||
};
|
||||
load(config.protocol, domains, path, {
|
||||
gt: config.gt,
|
||||
callback: cb
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
callback(config._get_fallback_config());
|
||||
}
|
||||
});
|
||||
};
|
||||
var throwError = function (errorType, config) {
|
||||
var errors = {
|
||||
networkError: '网络错误'
|
||||
};
|
||||
if (typeof config.onError === 'function') {
|
||||
config.onError(errors[errorType]);
|
||||
} else {
|
||||
throw new Error(errors[errorType]);
|
||||
}
|
||||
};
|
||||
var detect = function () {
|
||||
return !!window.Geetest;
|
||||
};
|
||||
if (detect()) {
|
||||
status.slide = "loaded";
|
||||
}
|
||||
var initGeetest = function (userConfig, callback) {
|
||||
var config = new Config(userConfig);
|
||||
if (userConfig.https) {
|
||||
config.protocol = 'https://';
|
||||
} else if (!userConfig.protocol) {
|
||||
config.protocol = window.location.protocol + '//';
|
||||
}
|
||||
jsonp([config.api_server || config.apiserver], config.type_path, config, function (newConfig) {
|
||||
var type = newConfig.type;
|
||||
var init = function () {
|
||||
config._extend(newConfig);
|
||||
callback(new window.Geetest(config));
|
||||
};
|
||||
callbacks[type] = callbacks[type] || [];
|
||||
var s = status[type] || 'init';
|
||||
if (s === 'init') {
|
||||
status[type] = 'loading';
|
||||
callbacks[type].push(init);
|
||||
load(config.protocol, newConfig.static_servers || newConfig.domains, newConfig[type] || newConfig.path, null, function (err) {
|
||||
if (err) {
|
||||
status[type] = 'fail';
|
||||
throwError('networkError', config);
|
||||
} else {
|
||||
status[type] = 'loaded';
|
||||
var cbs = callbacks[type];
|
||||
for (var i = 0, len = cbs.length; i < len; i = i + 1) {
|
||||
var cb = cbs[i];
|
||||
if (isFunction(cb)) {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
callbacks[type] = [];
|
||||
}
|
||||
});
|
||||
} else if (s === "loaded") {
|
||||
init();
|
||||
} else if (s === "fail") {
|
||||
throwError('networkError', config);
|
||||
} else if (s === "loading") {
|
||||
callbacks[type].push(init);
|
||||
}
|
||||
});
|
||||
};
|
||||
window.initGeetest = initGeetest;
|
||||
return initGeetest;
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user