user.js
3.58 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'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
});
}
async updateUserMobileByUnionId(union_id, phone, force) {
let strFind = `SELECT user_phone, union_id
FROM ${TABLE_USER}
WHERE user_phone = :phone AND is_wechat_user = 1`;
let findData = await mysqlCli.query(strFind, {phone});
if (findData && findData.length > 0 && !force) {
if (findData[0].union_id === union_id) {
return {
code: 202,
message: '该手机号已存在!',
data: findData
};
} else {
return {
code: 203,
message: '该手机号已绑定其他微信号!',
data: findData
};
}
} else {
let strUpdate = `UPDATE ${TABLE_USER} SET user_phone = :phone
WHERE union_id = :union_id`;
let result = await mysqlCli.update(strUpdate, {phone, union_id});
if (result) {
return {
code: 200,
message: '手机号码保存成功!',
data: result
};
} else {
return {
code: 204,
message: '您的微信号不存在!',
data: result
};
}
}
}
}
module.exports = UserModel;