Authored by 王水玲

极验证

... ... @@ -6,12 +6,34 @@ module.exports=function(app) {
app.get("/logout","common.Login",function (req) {
delete req.session.user;
});
app.get("/login/captcha", "common_geeCaptcha", function(data, req, res) {
if (data.code === 500) {
return;
}
if (!data.success) {
req.session.fallback = true;
return res.json({
code: 501,
data: data
});
} else {
// 正常模式
req.session.fallback = false;
return res.send({
code: 200,
data: data
});
}
});
app.post("/login", "common_login", function (login, req, res){
if (login.code == 200) {
if (req.session.gray) {
// 登录成功后,将phpsession保存下来
var gray_cookie = req.session.gray.replace(/[\[|\]]/g, '').split(';');
gray_cookie.forEach(function (n) {
console.log(n);
var item = n.split("=");
... ...
var request = require('request');
var _ = require('lodash');
var fs = require('fs');
var Geetest = require('gt3-sdk');
var Iaccount = global.Register.system;
var pcGeetest = new Geetest({
geetest_id: '4bf7f911856073c1347247667790e7c4',
geetest_key: '8d41283971d071216f1cef3d190d6b44'
});
var Utils = {
createrMenus: function (data) {
... ... @@ -76,30 +81,73 @@ module.exports = {
var userInfo = {},result = { code: 400, message: "登录失败" },isSuccess=false;
var user = req.body.user||req.query.user;
var password = req.body.password||req.query.password;
var captcha = req.body.captcha || req.query.captcha;
var verifyCode = captcha.split(',');
var errcallback = function (err) {
return callback(null, result);
}
common.Ilogin(user, password).allRight().sessionAsync(user, password).done(function (IloginRes,allRes,sesRes) {
var userData = IloginRes.data, cookie = sesRes.caseless.dict["set-cookie"];
userInfo = { auth: userData, uid: userData.pid, name: userData.truename, role: userData.roleName, right: {},shopList:[],brands:[]};
if(userData.data_authority&&userData.data_authority.brand&&userData.data_authority.brand.list){
userInfo.brands=userData.data_authority.brand.is_all?"0":Object.keys(userData.data_authority.brand.list);
}
allRes.data.forEach(function (data) {
if (data.platform_id == Iaccount.WEBSITE) {
userInfo.right[data.path] = true;
}
if (!captcha) {
return callback(null, {
message: '图形验证失败',
code: 405
});
common.getResourceByPid(userData.pid, userData.role_id).done(function (getRRes) {
userInfo.menu = Utils.createrMenus(getRRes.data);
req.session.user = userInfo;
if (cookie && cookie.length > 0) {
req.session.gray = cookie[0];
result = { code: 200, message: "登录成功" ,data:req.session.user};
}
return callback(null, result);
},errcallback);
}, errcallback);
}
pcGeetest.validate(req.session.fallback, {
geetest_challenge: verifyCode[0],
geetest_validate: verifyCode[1],
geetest_seccode: verifyCode[2]
}, function(err, success) {
if (err) {
return callback(null, {
message: '网络失败',
code: 405
});
} else if (!success) {
return callback(null, {
message: '图形验证失败',
code: 405
});
} else {
common.Ilogin(user, password).allRight().sessionAsync(user, password).done(function (IloginRes,allRes,sesRes) {
var userData = IloginRes.data, cookie = sesRes.caseless.dict["set-cookie"];
userInfo = { auth: userData, uid: userData.pid, name: userData.truename, role: userData.roleName, right: {},shopList:[],brands:[]};
if(userData.data_authority&&userData.data_authority.brand&&userData.data_authority.brand.list){
userInfo.brands=userData.data_authority.brand.is_all?"0":Object.keys(userData.data_authority.brand.list);
}
allRes.data.forEach(function (data) {
if (data.platform_id == Iaccount.WEBSITE) {
userInfo.right[data.path] = true;
}
});
common.getResourceByPid(userData.pid, userData.role_id).done(function (getRRes) {
userInfo.menu = Utils.createrMenus(getRRes.data);
req.session.user = userInfo;
if (cookie && cookie.length > 0) {
req.session.gray = cookie[0];
result = { code: 200, message: "登录成功" ,data:req.session.user};
}
return callback(null, result);
},errcallback);
}, errcallback);
}
});
},
geeCaptcha: function(req, callback, common) {
return pcGeetest.register({
client_type: 'web'
}, function(err, data) {
if (err) {
return callback(null, {
code: 500,
message: "极验证注册失败"
});
}
callback(null, data);
});
},
isUsedMenuAuth:{
title:'检查有没有权限',
... ...
... ... @@ -13,7 +13,7 @@
<div class="panel-body">
<h3 class="text-center mb5">Yoho!Buy运营平台</h3>
<div class="mb30"></div>
<form action="/login" method="post">
<form action="/login" method="post" id="loginForm">
<div class="input-group mb15">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" name="user" id="user" class="form-control" placeholder="用户名" autocomplete="off" >
... ... @@ -24,15 +24,19 @@
<input type="password" name="password" id="password" class="form-control" placeholder="密码" autocomplete="off" >
</div>
<div class="clearfix">
<input type="hidden" id="captcha" name="captcha">
<div id="img-check-main"></div>
<!-- <div class="clearfix">
<div class="pull-right">
<button type="submit" class="btn btn-success">登录<i class="fa fa-angle-right ml5"></i></button>
</div>
</div>
</div> -->
</form>
</div>
</div>
</section>
<script src="http://static.geetest.com/static/tools/gt.js"></script>
</body>
</html>
\ No newline at end of file
... ...
This diff could not be displayed because it is too large.
... ... @@ -15,7 +15,6 @@
"connect-multiparty": "^2.0.0",
"connect-redis": "^3.0.2",
"cookie-parser": "^1.4.1",
"dev": "^0.1.3",
"express": "^4.13.4",
"express-session": "^1.13.0",
"glob": "^7.0.3",
... ... @@ -34,9 +33,11 @@
"babel-plugin-transform-runtime": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-runtime": "^6.9.0",
"crypto-js": "^3.1.9-1",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.8.5",
"gt3-sdk": "^2.0.0",
"gulp": "^3.9.1",
"gulp-autoprefixer": "^3.1.0",
"gulp-clean": "^0.3.2",
... ... @@ -62,4 +63,4 @@
"vue-style-loader": "^1.0.0",
"webpack": "^1.13.0"
}
}
\ No newline at end of file
}
... ...
let href = window.location.href + '';
let $ = require('jquery');
let $captcha = $('#captcha');
if(href) {
let index = href.indexOf('?code');
if(index > 0) {
... ... @@ -8,6 +11,68 @@ if(href) {
alert("用户名或者密码错误,请确认!");
}
if(code === 405) {
alert('图形验证失败');
}
window.location.href = "/login";
}
}
\ No newline at end of file
}
var captcha = {
init: function() {
var that = this;
$.ajax({
type: 'GET',
dataType: 'json',
url: 'login/captcha',
success: function (result) {
if (result.code === 500) {
alert('验证码加载异常');
window.location.reload(true);
return;
}
initGeetest && initGeetest({ // eslint-disable-line
gt: result.data.gt,
challenge: result.data.challenge,
width: '100%',
product: 'float', // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
new_captcha: result.data.new_captcha,
offline: !result.data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
}, that.initCallback);
},
error: function (rs) {
console.log(rs)
}
});
},
initCallback: function(captchaObj) {
captchaObj.onSuccess(function() {
var validate = captchaObj.getValidate();
var result = [
validate.geetest_challenge,
validate.geetest_validate,
validate.geetest_seccode
];
$captcha.val(result.join(','));
$('#loginForm').submit();
});
captchaObj.onError(function() {
$captcha.val('');
});
captchaObj.onClose(function() {
$captcha.val('');
});
captchaObj.appendTo(document.getElementById('img-check-main'));
}
}
$(function() {
captcha.init();
});
\ No newline at end of file
... ...
... ... @@ -601,4 +601,8 @@ ul {
text-overflow : ellipsis;
white-space : nowrap;
width : 250px;
}
#img-check-main {
margin-bottom: 15px;
}
\ No newline at end of file
... ...
This diff could not be displayed because it is too large.