Skip to content

Commit

Permalink
进行2.0重构
Browse files Browse the repository at this point in the history
  • Loading branch information
huayifeng committed Oct 4, 2017
1 parent d9c3b26 commit a6f08db
Show file tree
Hide file tree
Showing 86 changed files with 2,638 additions and 1,435 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ Network Trash Folder
Temporary Items
.apdisk

node_modules
node_modules
dist
797 changes: 468 additions & 329 deletions .idea/workspace.xml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ created() {
const that = this
this.socket = io.connect('http://qiufengh.com:8081')
this.socket.on('message', function(obj) {
that.$store.commit('addroomdetailinfos', obj)
that.$store.commit('addRoomDetailInfos', obj)
window.scrollTo(0, 900000)
})
this.socket.on('logout', function (obj) {
that.$store.commit('setusers', obj)
that.$store.commit('setUsers', obj)
})
},
```
Expand Down
17 changes: 13 additions & 4 deletions build/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,25 @@ io.on('connection', function (socket) {
console.log(obj.name + '加入了' + obj.roomid)
})
socket.on('logout',function (obj) {
delete global.users[obj.roomid][obj.name]
console.log(obj.name + '退出了' + obj.roomid)
io.to(obj.roomid).emit('logout', global.users[obj.roomid])
try{
const is = Object.hasOwnProperty.call(global.users[obj.roomid], obj.name)
if (is) {
delete global.users[obj.roomid][obj.name]
console.log(obj.name + '退出了' + obj.roomid)
io.to(obj.roomid).emit('logout', global.users[obj.roomid])
socket.leave(obj.roomid)
}
} catch (e) {
console.log(e)
}
})

socket.on('disconnect', function () {
if (global.users[socket.room]) {
if (global.users[socket.room] && global.users[socket.room].length > 0) {
delete global.users[socket.room][socket.name]
// 用户监听用退出聊天室
console.log(socket.name + '退出了' + socket.room)
socket.leave(obj.roomid)
io.to(socket.room).emit('logout', global.users[socket.room])
}
})
Expand Down
2 changes: 1 addition & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
},
dev: {
env: require('./dev.env'),
port: 8081,
port: 9090,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
Expand Down
229 changes: 229 additions & 0 deletions config/routes-build.js
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
})
})
})
}
51 changes: 41 additions & 10 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ module.exports = function (app) {
username: fields.username,
src: fields.src,
img: dstPath,
roomid: fields.roomid
roomid: fields.roomid,
time: fields.time
}
var message = new Message(mess)
message.save(function (err, mess) {
Expand Down Expand Up @@ -166,15 +167,45 @@ module.exports = function (app) {
// 获取历史记录
app.get('/history/message', function (req, res) {
var id = req.query.roomid
Message.find({roomid: id}).exec(function (err, messsage) {
if (err) {
console.log(err)
} else {
res.json({
errno: 0,
data: messsage
})
}
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
})
})
}),
// 机器人消息
Expand Down
1 change: 1 addition & 0 deletions dist/index.html
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>
Loading

0 comments on commit a6f08db

Please sign in to comment.