Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Necis Zhang committed Jul 2, 2018
1 parent 7ccaa3a commit 9823c07
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 68 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Created by .ignore support plugin (hsz.mobi)
.idea
back-end/node_modules
back-end/node_modules
httpd.conf
ssl.conf
back-end/register/
10 changes: 10 additions & 0 deletions back-end/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ router.get('/', function(req, res) {
});
});

router.post('/check', function(req, res) {
Site.check(req, res).then((result) => {
res.json({
code: 200,
msg: "check return",
data: result
});
});
});

router.post('/register', function(req, res) {
Site.register(req, res).then((error) => {
if (error) {
Expand Down
1 change: 1 addition & 0 deletions back-end/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"apiPort": "5000",
"db": "mongodb://127.0.0.1:27017/faceregister",
"delay": 1000,
"apiFace":"http://localhost:3000",
"scheduleTime":"0 0 9 * * *",
"options": {
"headless": true,
Expand Down
51 changes: 50 additions & 1 deletion back-end/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const moment = require('moment');
const http = require('http');
const urltil = require('url');

class Utils {
static getMoment() {
Expand All @@ -19,9 +21,56 @@ class Utils {
}
}

static getNow(){
static getNow() {
return moment().toISOString(true)
}

static requestPost (url, data) {
let self = this;
return new Promise((resolve, reject) => {
//解析 url 地址
let urlData = urltil.parse(url);
//设置 https.request options 传入的参数对象
let options = {
//目标主机地址
hostname: urlData.hostname,
port:urlData.port,
//目标地址
path: urlData.path,
//请求方法
method: 'POST',
//头部协议
headers: {
// 'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': 'application/json',
// 'Content-Type': 'multipart/form-data',
'content-length': Buffer.byteLength(data, 'utf-8')
}
};
let req = http.request(options, (res) => {
let buffer = [],
result = '';
//用于监听 data 事件 接收数据
res.on('data', (data) => {
buffer.push(data);
});
//用于监听 end 事件 完成数据的接收
res.on('end', () => {
result = Buffer.concat(buffer).toString('utf-8');
resolve(result);
})
})
//监听错误事件
.on('error', (err) => {
console.log(err);
reject(err);
});
//传入数据
// console.log(data);
req.write(data);
req.end();
});
}
}

module.exports = Utils;
83 changes: 71 additions & 12 deletions back-end/website/datas/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,88 @@ const mongoose = require('mongoose');
const db = require("../db.js");
const schema = require('../models/schema.js');
const Config = require('../../config.json');
const fs=require('fs');
const fs = require('fs');

const model = mongoose.model('User', schema);
const {
requestPost
} = require('../../utils/utils.js');

module.exports.register = function(req, res) {
return new Promise(function(resolve, reject) {
var exist = req.body.exist;
var base64Data = req.body.img.replace(/^data:image\/jpeg;base64,/, "");
var binaryData = new Buffer(base64Data, 'base64').toString('binary');
var img='./register/'+req.body.openid+'.jpg';
fs.writeFile(img, binaryData, 'binary', function(err) {
if (err) {
console.log(err);
}
});
//存储数据到mongoogdb
let json = {

let pic = {
openid: req.body.openid,
name: req.body.name,
tel: req.body.tel,
img: '/register/'+req.body.openid+'.jpg'
img: base64Data,
exist: exist
}

let url = Config.apiFace + '/tencent/newperson';

requestPost(url, JSON.stringify(pic)).then(function(data) {
let result=JSON.parse(data);
if (result.code == 0) {
var img = './register/' + req.body.openid + '.jpg';
fs.writeFile(img, binaryData, 'binary', function(err) {
if (err) {
console.log(err);
}
});
if (exist == 'false') {
//存储数据到mongoogdb
let json = {
openid: req.body.openid,
name: req.body.name,
tel: req.body.tel,
img: '/register/' + req.body.openid + '.jpg'
}
model.create(json, function(error) {
resolve(error);
});
} else {
//更新
var conditions = {
openid: req.body.openid
};

var update = {
$set: {
name: req.body.name,
tel: req.body.tel,
img: '/register/' + req.body.openid + '.jpg'
}
};
model.update(conditions, update, function(error) {
resolve(error);
});
}
}else{
resolve(result);
}
})


});
}

module.exports.check = function(req, res) {
return new Promise(function(resolve, reject) {
//查找数据
let json = {
openid: req.body.openid
}
model.create(json, function(error) {
resolve(error);
model.findOne(json, function(error, result) {
if (error) {
console.log(error);
resolve(error);
} else {
resolve(result);
}
});
});
}
4 changes: 4 additions & 0 deletions back-end/website/models/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const schema = new Schema({
createTime: {
type: String,
default: getNow
},
updateTime: {
type: String,
default: getNow
}
});

Expand Down
14 changes: 9 additions & 5 deletions front-end/register/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

var openid = getUrlParameterByName('openid');
function getUrlParameterByName(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'),
results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
if(!getUrlParameterByName('openid')){
window.location.href="http://wechat.mynecis.cn/wx/wx/oauth/openid?url="+window.location.origin+window.location.pathname;
if (!openid) {
window.location.href = "https://wechat.mynecis.cn/wx/wx/oauth/openid?url=" + window.location.origin + window.location.pathname;
}
</script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
</head>

<body>
Expand Down Expand Up @@ -71,14 +73,16 @@
电话:
</div>
<div class="right">
<input id="tel" type="phone" placeholder="请填写电话">
<input id="tel" type="phone" placeholder="请填写电话" maxlength="11">
</div>
</div>
<div class="text-center">
<button id="submit" class="btn btn-default btn-success submit">提交</button>
</div>
<div id="check" class="title" style="display: none;color:red;">
你已经注册过!重新上传可以更换
</div>
</div>

<div class="top-box">
<img src="images/003.gif" alt="">
</div>
Expand Down
55 changes: 41 additions & 14 deletions front-end/register/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* Created by necis on 2018/6/26.
*/
var api = {
"upload": "http://localhost:5000/register"
"register": "http://localhost:5000/register",
"check": "http://localhost:5000/check"
}
var Mobile = {
Android: function() {
Expand Down Expand Up @@ -36,8 +37,27 @@ var Mobile = {
};

$(function() {
var exist = false;
$.ajax({
url: api.check,
type: 'POST',
data: {
openid: openid,
},
success: function(e) {
console.log(e);
if (e.data) {
console.log('注册过');
exist = true;
$("#check").show();
}
},
error: function() {
alert('错误的网路环境!请联系管理人员!');
}
});

var img;
var openid="abc";
var IS_IOS = Mobile.iOS();

var $page, $canvas, $handler, $confirm;
Expand Down Expand Up @@ -92,7 +112,7 @@ $(function() {
initImage(image, faceCallback, canvas, $confirm, $handler)
};
image.src = base64;
img=base64;
img = base64;
$photo.val('');
$profilepopup.show();
};
Expand All @@ -111,7 +131,7 @@ $(function() {
var name = $("#name").val();
var tel = $("#tel").val();
var id = openid;
if(!img){
if (!img) {
alert("请上传图片");
return false;
}
Expand All @@ -124,29 +144,36 @@ $(function() {
alert("请填写电话");
return false;
}
var json={
openid: id,
name: name,
tel: tel,
img: base64
};

console.log(json);

$.ajax({
url: api.upload,
url: api.register,
type: 'POST',
data: {
openid: openid,
name: name,
tel: tel,
img: base64
img: base64,
exist: exist
},
beforeSend: function() {
$(".top-box").show();
},
success: function(e) {
$(".top-box").hide();
if(e.code==200){
console.log('注册成功');
$("#name").val('');
$("#tel").val('');
img=null;
base64=null;
$photo.val('');
$confirmfaceimg.attr("src",'');
exist=true;
$("#check").show();
alert("注册成功");
}else{
alert("请上传正确的人脸");
}
},
error: function() {
$(".top-box").hide();
Expand Down
Loading

0 comments on commit 9823c07

Please sign in to comment.