This repository was archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdb-utils.js
More file actions
104 lines (95 loc) · 3.17 KB
/
Copy pathdb-utils.js
File metadata and controls
104 lines (95 loc) · 3.17 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
const mysql = require('mysql');
const db = mysql.createConnection({
host: 'localhost',
user: 'kys',
password: 'kys',
database: 'kaiyuanshe'
});
db.connect((err) => {
if (err) {
throw err;
}
console.log("Connected to Kaiyuanshe DB");
});
global.db = db;
exports.get_user = function (nick_name, func) {
if (nick_name.slice(0, 1) == "@") {
nick_name = nick_name.slice(1);
}
let query = "select * from users where `nick_name`='" + nick_name + "'";
console.log(query);
db.query(query, (err, result) => {
if (!err) {
func(result[0]);
} else {
func(null);
}
});
}
exports.update_user_status = function (wechat_id, user_status, func) {
let query = "update `users` set `status`='" + user_status + "' where `wechat_id`='" + wechat_id + "'";
db.query(query, (err, result) => {
if (func) { func(); }
});
}
exports.update_user_position = function (wechat_id, position, func) {
let query = "update `users` set `position`='" + position + "' where `wechat_id`='" + wechat_id + "'";
db.query(query, (err, result) => {
if (func) { func(); }
});
}
exports.save_wechat_friend = async function (user) {
var wechat_id = user.id;
var nick_name = await user.name();
console.log(wechat_id + "," + nick_name);
db.query("SET NAMES utf8mb4", (err, result) => {
db.query("select * from wechat_friends where wechat_id='"+wechat_id+"'", (err1, result) => {
if(result.length==0){
db.query("insert into `wechat_friends` (wechat_id,nick_name) values ('" + wechat_id + "','" + nick_name + "')", (err2, result2) => {
if (err2) {
console.log(err2);
}
});
}
});
});
}
const MessageType = ["Unknown", "Attachment", "Audio", "Contact", "Emoticon", "Image", "Text", "Video", "Url"];
exports.save_msg = async function (msg) {
var fields = "`type`,";
var values = "'" + MessageType[msg.type()-1] + "',";
var room = await msg.room();
if (room) {
var room_id = room.id;
var room_topic = await room.topic();
fields = fields + "`room_id`,`room_topic`,";
room_topic = room_topic.replace(/\'/g, "\\\'");
values = values + "'" + room_id + "','" + room_topic + "',";
}
var from = await msg.from();
if (from) {
var from_user_id = from.id;
var from_user_name = await from.name();
fields = fields + "`from_user_id`,`from_user_name`,";
values = values + "'" + from_user_id + "','" + from_user_name + "',";
}
var mention_list = await msg.mention();
if (mention_list) {
var mention_id_list = "";
var mention_name_list = "";
mention_list.forEach(async function (item, index) {
mention_id_list = mention_id_list + item.id + ",";
mention_name_list = mention_name_list + await item.name() + ",";
});
fields = fields + "`mention_id_list`,`mention_name_list`,";
values = values + "'" + mention_id_list + "','" + mention_name_list + "',";
}
text = await msg.text();
text = text.replace(/\'/g,"\\\'");
fields = fields + "`text`,`create_at`";
values = values + "'" + text + "',CURRENT_TIMESTAMP";
sql = "INSERT INTO `messages` (" + fields + ") VALUES (" + values + ")";
db.query("SET NAMES utf8mb4", (err, result) => {
db.query(sql);
});
}