forked from hua1995116/webchat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
huayifeng
committed
Oct 4, 2017
1 parent
d9c3b26
commit a6f08db
Showing
86 changed files
with
2,638 additions
and
1,435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,5 @@ Network Trash Folder | |
Temporary Items | ||
.apdisk | ||
|
||
node_modules | ||
node_modules | ||
dist |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
var User = require('../models/user') | ||
var Message = require('../models/message') | ||
var superagent = require('superagent') | ||
var fs = require('fs') | ||
var multiparty = require('multiparty'); | ||
var util = require('util') | ||
module.exports = function (app) { | ||
app.use(function (req, res, next) { | ||
var _user = req.session.user | ||
|
||
app.locals.user = _user | ||
|
||
next() | ||
}) | ||
app.post('/file/uploadimg', function (req, res, next) { | ||
// console.log(util.inspect(req.body, { showHidden: true, depth: null })) | ||
// console.log(util.inspect(req.header, { showHidden: true, depth: null })) | ||
// //生成multiparty对象,并配置上传目标路径 | ||
var form = new multiparty.Form() | ||
// //设置编辑 | ||
form.encoding = 'utf-8' | ||
// //设置文件存储路径 | ||
form.uploadDir = "./static/files/" | ||
// //设置单文件大小限制 | ||
form.maxFilesSize = 2 * 1024 * 1024 | ||
// form.maxFields = 1000; 设置所以文件的大小总和 | ||
// 上传完成后处理 | ||
form.parse(req, function (err, fields, files) { | ||
console.log(fields) | ||
var filesTmp = JSON.stringify(files, null, 2) | ||
console.log(filesTmp) | ||
if (err) { | ||
console.log('parse error: ' + err) | ||
res.json({ | ||
errno: 1 | ||
}) | ||
} else { | ||
var inputFile = files.file[0]; | ||
var uploadedPath = inputFile.path | ||
var array = inputFile.originalFilename.split('.') | ||
var imgtype = array[array.length - 1] | ||
var realPath = './dist/static/files/' + new Date().getTime() + '.' + imgtype | ||
var dstPath = './static/files/' + new Date().getTime() + '.' + imgtype | ||
//重命名为真实文件名 | ||
fs.rename(uploadedPath, realPath, function (err) { | ||
if (err) { | ||
console.log('rename error: ' + err) | ||
res.json({ | ||
errno: 1 | ||
}) | ||
} else { | ||
var mess = { | ||
username: fields.username, | ||
src: fields.src, | ||
img: dstPath, | ||
roomid: fields.roomid, | ||
time: fields.time | ||
} | ||
var message = new Message(mess) | ||
message.save(function (err, mess) { | ||
if (err) { | ||
console.log(err) | ||
} | ||
console.log(mess) | ||
}) | ||
console.log('rename ok') | ||
res.json({ | ||
errno: 0 | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
}) | ||
// 注册 | ||
app.post('/user/signup', function (req, res) { | ||
var _user = req.body | ||
console.log(_user) | ||
User.findOne({name: _user.name}, function (err, user) { | ||
if (err) { | ||
console.log(err) | ||
} | ||
if (user) { | ||
res.json({ | ||
errno: 1, | ||
data: '用户名已存在' | ||
}) | ||
} else { | ||
var user = new User(_user) | ||
user.save(function (err, user) { | ||
if (err) { | ||
console.log(err) | ||
} | ||
res.json({ | ||
errno: 0, | ||
data: '注册成功' | ||
}) | ||
}) | ||
} | ||
}) | ||
}), | ||
// 登录 | ||
app.post('/user/signin', function (req, res) { | ||
console.log(req.body) | ||
var _user = req.body | ||
var name = _user.name | ||
var password = _user.password | ||
console.log(password) | ||
User.findOne({name: name}, function (err, user) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
console.log(user) | ||
if (!user) { | ||
res.json({ | ||
errno: 1, | ||
data: '用户不存在' | ||
}) | ||
} else { | ||
if (!!password) { | ||
user.comparePassword(password, function (err, isMatch) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
if (isMatch) { | ||
req.session.user = user; | ||
console.log('success'); | ||
res.json({ | ||
errno: 0, | ||
data: '登录成功', | ||
name: name, | ||
src: user.src | ||
}) | ||
} else { | ||
res.json({ | ||
errno: 1, | ||
data: '密码不正确' | ||
}) | ||
console.log('password is not meached'); | ||
} | ||
}) | ||
} else { | ||
res.json({ | ||
errno: 1, | ||
data: '登录失败' | ||
}) | ||
} | ||
} | ||
|
||
}) | ||
}), | ||
|
||
// 信息 | ||
app.get('/message', function (req, res) { | ||
var id = req.query.roomid | ||
Message.find({roomid: id}).sort({"time": -1}).limit(80).exec(function (err, message) { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
res.json({ | ||
errno: 0, | ||
data: message.reverse() | ||
}) | ||
} | ||
}) | ||
}), | ||
// 获取历史记录 | ||
app.get('/history/message', function (req, res) { | ||
var id = req.query.roomid | ||
var current = req.query.current | ||
if (!id || !current) { | ||
res.json({ | ||
errno: 1 | ||
}) | ||
} | ||
var message = { | ||
errno: 0, | ||
data: {}, | ||
total: 0, | ||
current: current | ||
} | ||
var task1 = new Promise(function(resolve, reject) { | ||
var skip = parseInt((current - 1) * 40) | ||
Message.find({roomid: id}).skip(skip).limit(40).exec(function (err, data) { | ||
if (err) { | ||
console.log(err) | ||
return reject() | ||
} else { | ||
message.data = data | ||
return resolve() | ||
} | ||
}) | ||
}) | ||
var task2 = new Promise(function(resolve, reject) { | ||
Message.find({roomid: id}).count().exec(function (err, data) { | ||
if (err) { | ||
console.log(err) | ||
return reject() | ||
} else { | ||
message.total = data | ||
return resolve() | ||
} | ||
}) | ||
}) | ||
Promise.all([task1, task2]).then(() => { | ||
res.json({ | ||
data: message | ||
}) | ||
}) | ||
}), | ||
// 机器人消息 | ||
app.get('/robotapi', function (req, res) { | ||
var response = res | ||
var info = req.query.info | ||
var userid = req.query.id | ||
var key = 'fde7f8d0b3c9471cbf787ea0fb0ca043' | ||
superagent.post('http://www.tuling123.com/openapi/api') | ||
.send({info, userid, key}) | ||
.end((err, res) => { | ||
if (err) { | ||
console.log(err) | ||
} | ||
response.json({ | ||
data: res.text | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,inital-scale=1,maximum-scale=1,user-scalable=no"><title>vuepro</title><link rel=stylesheet href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic"><link rel=stylesheet href="https://fonts.googleapis.com/icon?family=Material+Icons"><link rel=stylesheet href=./static/css/reset.css><link href=/static/css/app.d816c7a04040b6d603ec425806ff1b31.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.18208aa543ad45c6c6f3.js></script><script type=text/javascript src=/static/js/vendor.91c767c1206a30887a6b.js></script><script type=text/javascript src=/static/js/app.e1cd7c7fd20db59e2770.js></script></body></html> |
Oops, something went wrong.