Skip to content

Commit c37c1d0

Browse files
committed
消息页面
1 parent 60a1720 commit c37c1d0

File tree

20 files changed

+391
-135
lines changed

20 files changed

+391
-135
lines changed

.babelrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
"node": true
88
}
99
}],
10-
"stage-2"
10+
"stage-2",
11+
"es2015"
1112
],
1213
"plugins": [
1314
"transform-runtime"
1415
],
1516
"env": {
1617
"test": {
17-
"presets": ["env", "stage-2"],
18+
"presets": ["env", "stage-3"],
1819
"plugins": ["istanbul"]
1920
}
2021
}

a.sql

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
SELECT a.id, a.time,b.face AS imgUrl, (
2+
3+
SELECT COUNT( * )
4+
FROM message_user c
5+
WHERE c.is_read =0
6+
AND c.to_user = a.to_user
7+
AND c.from_user = a.from_user
8+
) AS unread, (
9+
10+
SELECT d.message
11+
FROM message_user d
12+
WHERE d.to_user = a.to_user
13+
AND d.from_user = a.from_user
14+
ORDER BY TIME DESC
15+
LIMIT 1
16+
) AS message,(
17+
select beizhu from friend e
18+
where e.other_user_id=a.from_user
19+
) AS from_user
20+
21+
FROM message_user a
22+
LEFT JOIN user_detail b ON a.from_user = b.user_id
23+
AND a.to_user =1
24+
GROUP BY from_user;
25+
26+
27+
28+
SELECT a.id, a.unread, c.group_name AS from_user, c.group_avator AS imgUrl, (
29+
30+
SELECT d.message
31+
FROM message_group d
32+
WHERE d.to_group = a.group_id
33+
ORDER BY TIME DESC
34+
LIMIT 1
35+
) AS message, (
36+
37+
SELECT e.time
38+
FROM message_group e
39+
WHERE e.to_group = a.group_id
40+
ORDER BY TIME DESC
41+
LIMIT 1
42+
) AS time
43+
FROM group_user a
44+
RIGHT JOIN message_group b ON b.to_group = a.group_id
45+
AND a.user_id =1
46+
LEFT JOIN groups c ON c.id = a.group_id
47+
GROUP BY a.group_id
48+
49+
50+
51+
52+
53+
54+
55+
SELECT a.id, a.unread, c.group_name AS from_user, c.group_avator AS imgUrl, (
56+
57+
SELECT d.message,d.time
58+
FROM message_group d
59+
WHERE d.to_group = a.group_id
60+
ORDER BY TIME DESC
61+
LIMIT 1
62+
) as message time,
63+
FROM group_user a
64+
RIGHT JOIN message_group b ON b.to_group = a.group_id
65+
AND a.user_id =1
66+
LEFT JOIN groups c ON c.id = a.group_id
67+
GROUP BY a.group_id

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"dependencies": {
1414
"axios": "^0.16.2",
15+
"body-parser": "^1.17.2",
1516
"cookie-parser": "^1.4.3",
1617
"crypto-js": "^3.1.9-1",
1718
"express-session": "^1.15.3",
@@ -22,7 +23,6 @@
2223
"socket.io": "^2.0.3",
2324
"urllib": "^2.22.0",
2425
"vue": "^2.3.3",
25-
"vue-axios": "^2.0.2",
2626
"vue-router": "^2.3.1",
2727
"vueg": "^1.2.45",
2828
"vuex": "^2.3.1"
@@ -39,7 +39,6 @@
3939
"babel-preset-stage-2": "^6.22.0",
4040
"babel-register": "^6.22.0",
4141
"babel-runtime": "^6.23.0",
42-
"body-parser": "^1.17.2",
4342
"chalk": "^1.1.3",
4443
"connect-history-api-fallback": "^1.3.0",
4544
"copy-webpack-plugin": "^4.0.1",

server/controller/login/index.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class Login {
1212
const {
1313
user,
1414
pwd
15-
} = req.body
15+
} = req.params
1616

1717
let type //填写的类型
1818
if (/^[\d]{8,10}$/.test(user)) { //如果填写的是qq号
@@ -45,19 +45,24 @@ export default class Login {
4545
}
4646

4747
//检查密码
48-
const pwdIsValid = await this.checkPwd(user, pwd, field)
49-
if (pwdIsValid == 0) { //如果密码错误则直接返回,否则保存登陆信息
48+
const userId = await this.checkPwd(user, pwd, field)
49+
if (userId == 0) { //如果密码错误则直接返回,否则保存登陆信息
5050
return {
5151
"code": 0,
5252
"message": "密码错误,请检查"
5353
}
5454
}
5555

56-
//更新最后登陆时间
56+
//更新用户后登陆时间
5757
await this.updateLoginTime(user, field)
5858

59-
//保存登陆信息
60-
const data = await this.saveSession(req, user, type)
59+
//保存session
60+
let data = await this.saveSession(req, user, type)
61+
62+
data.isLogin = 1 // 将登录态置为1
63+
data.userId = userId //取到用户的id
64+
65+
//返回数据
6166
return {
6267
"code": 1,
6368
"message": "登陆成功",
@@ -72,7 +77,7 @@ export default class Login {
7277
* @return {[Promise]} [description]
7378
*/
7479
static async checkUser(user, field) {
75-
const sql = `select * from user where ${field} = ? limit 1`
80+
const sql = `select id from user where ${field} = ? limit 1`
7681
const rows = await query(sql, user).catch((err) => {
7782
console.log(err)
7883
})
@@ -87,11 +92,11 @@ export default class Login {
8792
* @return {[type]} [description]
8893
*/
8994
static async checkPwd(user, pwd, field) {
90-
const sql = `select * from user where ${field} = ? and pwd= ? limit 1 `
95+
const sql = `select id from user where ${field} = ? and pwd= ? limit 1 `
9196
const rows = await query(sql, [user, pwd]).catch((err) => {
9297
console.log(err)
9398
})
94-
return rows.length > 0 ? 1 : 0
99+
return rows.length > 0 ? rows[0].id : 0
95100
}
96101

97102
/**
@@ -120,7 +125,7 @@ export default class Login {
120125
*/
121126
static async saveSession(req, user, type) {
122127
let data
123-
//如果是qq号
128+
//如果是qq号
124129
if (type == 1) {
125130
req.session.qq = user
126131
data = {
@@ -133,7 +138,7 @@ export default class Login {
133138
"type": "phone",
134139
"value": user
135140
}
136-
} else if (type == 3){
141+
} else if (type == 3) {
137142
req.session.email = user
138143
data = {
139144
"type": "email",
@@ -142,4 +147,4 @@ export default class Login {
142147
}
143148
return data
144149
}
145-
}
150+
}

server/controller/message/index.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import query from '../../common/mysql/db'
2+
3+
export default class Messge {
4+
5+
/**
6+
* [getAllMessage 获取某个用户的首页的所有消息]
7+
* @param {[type]} userId [description]
8+
* @return {[type]} [description]
9+
*/
10+
static async getAllMessage(userId) {
11+
//先获取私聊消息
12+
const singleMessage = await this.getSingleMessage(userId)
13+
//再获取群聊消息
14+
const groupMessage = await this.getGroupMessage(userId)
15+
16+
//本应再合并,排序,但是为了减缓服务器的压力,所以让前台去做了
17+
return {
18+
code: 1,
19+
data: {
20+
singleMessage,
21+
groupMessage
22+
}
23+
}
24+
}
25+
26+
/**
27+
* [getSingleMessage 获取私聊消息]
28+
* @param {[type]} userId [用户的id]
29+
* @return {[type]} [description]
30+
*/
31+
static async getSingleMessage(userId) {
32+
const sql = `
33+
SELECT a.id, a.time, b.face AS imgUrl, (
34+
35+
SELECT COUNT( * )
36+
FROM message_user c
37+
WHERE c.is_read =0
38+
AND c.to_user = a.to_user
39+
AND c.from_user = a.from_user
40+
) AS unread, (
41+
42+
SELECT d.message
43+
FROM message_user d
44+
WHERE d.to_user = a.to_user
45+
AND d.from_user = a.from_user
46+
ORDER BY TIME DESC
47+
LIMIT 1
48+
) AS message, (
49+
50+
SELECT beizhu
51+
FROM friend e
52+
WHERE e.other_user_id = a.from_user
53+
) AS from_user
54+
FROM message_user a
55+
LEFT JOIN user_detail b ON a.from_user = b.user_id
56+
AND a.to_user =${userId}
57+
GROUP BY from_user`
58+
59+
const rows = await query(sql, [userId]).catch((err) => {
60+
console.log(err)
61+
})
62+
return rows
63+
}
64+
65+
/**
66+
* [getGroupMessage description]
67+
* @param {[type]} userId [description]
68+
* @return {[type]} [description]
69+
*/
70+
static async getGroupMessage(userId) {
71+
const sql = `
72+
SELECT a.id, a.unread, c.group_name AS from_user, c.group_avator AS imgUrl, (
73+
74+
SELECT d.message
75+
FROM message_group d
76+
WHERE d.to_group = a.group_id
77+
ORDER BY TIME DESC
78+
LIMIT 1
79+
) AS message, (
80+
81+
SELECT e.time
82+
FROM message_group e
83+
WHERE e.to_group = a.group_id
84+
ORDER BY TIME DESC
85+
LIMIT 1
86+
) AS time
87+
FROM group_user a
88+
RIGHT JOIN message_group b ON b.to_group = a.group_id
89+
AND a.user_id =${userId}
90+
LEFT JOIN groups c ON c.id = a.group_id
91+
GROUP BY a.group_id`
92+
93+
const rows = await query(sql, [userId]).catch((err) => {
94+
console.log(err)
95+
})
96+
return rows
97+
}
98+
}

server/router.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ import express from 'express'
22

33
import login from './controller/login'
44
import register from './controller/register'
5+
import message from './controller/message'
56

67
const router = express.Router()
78

9+
// RESTFul API
10+
// GET /tickets # 获取ticket列表
11+
// GET /tickets/12 # 查看某个具体的ticket
12+
// POST /tickets # 新建一个ticket
13+
// PUT /tickets/12 # 更新ticket 12.
14+
// DELETE /tickets/12 #删除ticekt 12
15+
816
router
9-
.post('/login', async(req, res) => { // 登录
10-
let result = await login.login(req, res)
17+
.get('/login/:user/:pwd', async(req, res) => { // 登录
18+
const result = await login.login(req, res)
1119
res.json(result)
1220
})
1321
.post('/register/:type', async(req, res) => { //注册
@@ -23,5 +31,11 @@ router
2331
}
2432
res.json(result)
2533
})
34+
.get('/message/all/:userId', async(req, res) => { //获取某个用户首页的所有消息
35+
const userId = req.params.userId
36+
const result = await message.getAllMessage(userId)
37+
res.json(result)
38+
})
39+
40+
module.exports = router
2641

27-
module.exports = router

src/api/login.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import encrypt from '@/common/js/encrypt'
22
import {
3-
request_post
3+
request_get
44
} from '@/common/js/request'
55

6-
//验证登陆
7-
export function checkLogin(data) {
8-
const url = '/api/login'
9-
//加密登录
10-
data.pwd = encrypt(data.pwd)
6+
/**
7+
* [checkLogin 验证登录]
8+
* @param {[type]} options.user [description]
9+
* @param {[type]} options.pwd [description]
10+
* @return {[type]} [description]
11+
*/
12+
export function checkLogin({user,pwd}) {
13+
pwd = encrypt(pwd) //加密登录
14+
const url = `/api/login/${user}/${pwd}`
1115

12-
return request_post(url, data)
16+
return request_get(url)
1317
}

src/api/message.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {
2+
request_get
3+
} from '@/common/js/request'
4+
5+
/**]
6+
7+
* [getAllMessage 获取某个用户的首页的所有消息 * @param {[type]} userId [用户id]
8+
* @return {[type]} [消息]
9+
*/
10+
export function get_all_message(userId) {
11+
const url = `/api/message/all/${userId}`
12+
return request_get(url)
13+
}

src/base/Scroll/Scroll.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default {
3030
//初始化滚动条
3131
setTimeout(()=>{
3232
this.initScroll()
33-
},0)
33+
},200)
3434
},
3535
methods:{
3636
initScroll(){

src/common/js/encrypt.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import hmacSHA512 from 'crypto-js/hmac-sha512'
22

3-
// 密码加密
3+
/**
4+
* [加密]
5+
* @param {[string]} pwd [密码]
6+
* @return {[string]} [加密后的密码]
7+
*/
48
export default (pwd) => {
5-
return hmacSHA512(pwd, 'my-qq').toString().substr(20, 30)
6-
}
9+
return hmacSHA512(pwd, 'my-qq').toString().substr(20, 30)
10+
}

0 commit comments

Comments
 (0)