user.js
2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_USER = 'user';
class UserModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
getUser(mobile) {
return mysqlCli.query(`select * from ${TABLE_USER}
where user_phone=:mobile`, {
mobile
});
}
createUser({name, mobile, avatar}) {
name = name || '';
mobile = mobile || '';
avatar = avatar || '';
return mysqlCli.insert(
`insert into ${TABLE_USER}
(user_phone, user_name, user_avatar) values (:mobile, :name, :avatar);`, {
name,
mobile,
avatar
});
}
/**
* 创建微信用户
* @param union_id
* @param name
* @param avatar
* @param is_wechat
* @returns {*}
*/
createWechatUser(union_id, name, avatar, is_wechat) {
let params = {
mobile: '0',
name: name,
avatar: avatar,
union_id: union_id,
is_wechat: is_wechat || 1
};
let sqlStr = `INSERT INTO ${TABLE_USER}
(user_phone, user_name, user_avatar, union_id, is_wechat_user) VALUES
(:mobile, :name, :avatar, :union_id, :is_wechat);`;
return mysqlCli.insert(sqlStr, params);
}
/**
* 根据union_id获取用户信息
* @param union_id
* @returns {*}
*/
getWechatUser(union_id) {
let sqlStr = `SELECT * FROM ${TABLE_USER}
WHERE union_id = :union_id
AND is_wechat_user = 1;`;
return mysqlCli.query(sqlStr, {
union_id
});
}
/**
* 根据union_id更新用户昵称和头像
* @param union_id
* @param name
* @param avatar
* @returns {*}
*/
modifyWechatUser(union_id, name, avatar) {
let sqlStr = `UPDATE ${TABLE_USER} SET
user_name = :name, user_avatar = :avatar
WHERE union_id = :union_id;`;
return mysqlCli.update(sqlStr, {
name,
avatar,
union_id
});
}
}
module.exports = UserModel;