-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser.cpp
More file actions
429 lines (389 loc) · 11.5 KB
/
user.cpp
File metadata and controls
429 lines (389 loc) · 11.5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "user.h"
QString User::encryptPassword()
{
QString password=hashedPassword;
QString uuid=account;
//以uuid为密钥rc4加密password
QString result;
for (int i = 0; i < password.size(); ++i) {
ushort xor_result = static_cast<ushort>(password[i].unicode()) ^ static_cast<ushort>(uuid[i % uuid.size()].unicode());
result.append(QChar(xor_result));
}
return result;
}
User::User(const QString &username, const QString &account, const QString &password, QObject *parent)
: QObject(parent), username(username), account(account), hashedPassword(password)
{
apiRequest = new ApiRequest(this);
apiRequest->setBaseUrl("https://example.com");
channel = new MessageChannel(this);
}
User::User(const QString &account, const QString &password, QObject *parent)
: QObject(parent), account(account), hashedPassword(password)
{
apiRequest = new ApiRequest(this);
apiRequest->setBaseUrl("https://example.com");
channel = new MessageChannel(this);
}
User::User(const User &user)
{
username = user.username;
account = user.account;
hashedPassword = user.hashedPassword;
avatarpath = user.avatarpath;
isLogin = user.isLogin;
apiRequest = user.apiRequest;
//apiRequest->setBaseUrl("https://syncapi.snakekiss.com");
channel = new MessageChannel(this);
}
bool User::enroll()
{
QString postData = QString("username=%1&email=%2&password=%3").arg(username).arg(account).arg(hashedPassword);
ApiResponse response = apiRequest->post("/register", postData.toUtf8());
if (response.isSuccess())
{
isLogin = true;
return true;
}
else
{
emit channel->message(response.message, "Error");
return false;
}
}
bool User::enroll(const QString &filePath)
{
// 读取文件并转换为base64字符串
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "open file failed!";
return false;
}
QByteArray byteArray = file.readAll();
QString avatar = byteArray.toBase64();
QFileInfo fileInfo(filePath);
QString extension = fileInfo.suffix().toLower();
if (extension == "png")
{
avatar.prepend("data:image/png;base64,");
}
else if (extension == "jpg" || extension == "jpeg")
{
avatar.prepend("data:image/jpeg;base64,");
}
else if (extension == "gif")
{
avatar.prepend("data:image/gif;base64,");
}
else
{
return false; // 图片格式不支持
}
// post时保留字符串中的加号
// avatar.replace("+","%2B");
// qDebug()<<avatar;
QString postData = QString("username=%1&email=%2&password=%3&avatar=%4").arg(username).arg(account).arg(hashedPassword).arg(avatar);
ApiResponse response = apiRequest->post("/register", postData.toUtf8());
if (response.isSuccess())
{
return true;
}
else
{
emit channel->message(response.message, "Error");
return false;
}
}
bool User::login()
{
QString postData = QString("email=%1&password=%2").arg(account).arg(hashedPassword);
ApiResponse response = apiRequest->post("/login", postData.toUtf8());
if (response.isSuccess())
{
isLogin = true;
username = response.getData().value("username").toString();
}
else if (response.getCode() == 403)
{
isLogin = false;
}
avatarpath = response.getData().value("avatar_url").toString();
qDebug() << "login avatar:" << avatarpath;
if (response.isSuccess())
{
return true;
}
else
{
emit channel->message(response.message, "Error");
return false;
}
}
bool User::forgetPassword()
{
// 找回密码功能
return true;
}
bool User::updateAvatar(const QString &filePath)
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "open file failed!";
return false;
}
QByteArray byteArray = file.readAll();
QString avatar = byteArray.toBase64();
QFileInfo fileInfo(filePath);
QString extension = fileInfo.suffix().toLower();
if (extension == "png")
{
avatar.prepend("data:image/png;base64,");
}
else if (extension == "jpg" || extension == "jpeg")
{
avatar.prepend("data:image/jpeg;base64,");
}
else if (extension == "gif")
{
avatar.prepend("data:image/gif;base64,");
}
else
{
return false; // 图片格式不支持
}
// post时保留字符串中的加号
// avatar.replace("+","%2B");
// qDebug()<<avatar;
QString postData = QString("avatar=%1").arg(avatar);
ApiResponse response = apiRequest->post("/updateAvatar", postData.toUtf8());
avatarpath = response.getData().value("avatar_url").toString();
qDebug() << "update avatar:" << avatarpath;
emit updateAvatarResponse(response.getCode(), response.getData(), response.getMessage());
if (response.isSuccess())
{
return true;
}
else
{
emit channel->message(response.message, "Error");
return false;
}
}
bool User::deleteAccount()
{
ApiResponse response = apiRequest->get("/deleteAccount");
return response.isSuccess();
}
bool User::addTask(const QString &localDir, const QString &s3Dir, int syncType, int usedSize, int totalSize)
{
// 添加云端task
QString postData = QString("localDir=%1&s3Dir=%2&syncType=%3&usedSize=%4&totalSize=%5")
.arg(localDir)
.arg(s3Dir)
.arg(syncType)
.arg(usedSize)
.arg(totalSize);
ApiResponse response = apiRequest->post("/addTask", postData.toUtf8());
emit addTaskResponse(response.getCode(), response.getData(), response.getMessage());
return response.isSuccess();
}
QVector<SyncTask> User::getTask()
{
// 获取云端task
ApiResponse response = apiRequest->get("/tasks");
QJsonArray tasksArray = response.getDatav().toArray();
QVector<SyncTask> tasks;
for (int i = 0; i < tasksArray.size(); ++i)
{
QJsonObject taskObj = tasksArray[i].toObject();
SyncTask task(taskObj["localDir"].toString(),
taskObj["s3Dir"].toString(),
taskObj["syncType"].toInt());
// taskObj["usedSize"].toDouble(),
// taskObj["totalSize"].toDouble()
tasks.push_back(task);
}
return tasks;
}
TaskToken User::getTaskToken(int id)
{
ApiResponse response = apiRequest->get(QString("/getTaskToken?task_id=%1").arg(id));
if (response.isSuccess())
{
TaskToken tasktoken(response.getData());
return tasktoken;
}
else
{
QString jsonString = R"({"code": 666, "message": "get token error!", "data": "Nooo!"})";
QByteArray jsonData = jsonString.toUtf8();
QJsonDocument document = QJsonDocument::fromJson(jsonData);
QJsonObject jsonObject = document.object();
TaskToken errortasktoken(jsonObject);
return errortasktoken;
}
}
TaskToken User::getTaskTokenByRemote(QString s3Dir)
{
ApiResponse response = apiRequest->get(QString("/getTaskTokenByS3Dir?s3Dir=%1").arg(s3Dir));
if (response.isSuccess())
{
TaskToken tasktoken(response.getData());
return tasktoken;
}
else
{
QString jsonString = R"({"code": 666, "message": "get tokenRemote error!", "data": "Nooo!"})";
QByteArray jsonData = jsonString.toUtf8();
QJsonDocument document = QJsonDocument::fromJson(jsonData);
QJsonObject jsonObject = document.object();
TaskToken errortasktoken(jsonObject);
return errortasktoken;
}
}
TaskToken User::getUnifiedTaskToken()
{
ApiResponse response = apiRequest->get("/getTaskUnifiedToken");
if (response.isSuccess())
{
TaskToken tasktoken(response.getData());
return tasktoken;
}
else
{
QString jsonString = R"({"code": 666, "message": "get unified token error!", "data": "Nooo!"})";
QByteArray jsonData = jsonString.toUtf8();
QJsonDocument document = QJsonDocument::fromJson(jsonData);
QJsonObject jsonObject = document.object();
TaskToken errortasktoken(jsonObject);
return errortasktoken;
}
}
bool User::logout()
{
ApiResponse response = apiRequest->get("/logout");
if (response.isSuccess())
{
return true;
}
else
{
emit channel->message(response.message, "Error");
return false;
}
}
QString User::getUsername()
{
return username;
}
QString User::getEmail()
{
return account;
}
QString User::gethashedPassword()
{
return hashedPassword;
}
QString User::getAvatarPath()
{
return avatarpath;
}
QString User::getUserHash() const
{
QByteArray hash = QCryptographicHash::hash(account.toUtf8(), QCryptographicHash::Sha1);
return hash.toHex();
}
/*QString User::getSession()
bool User::getisLogin()
{
// return session;
}*/
bool User::updateUser()
{
QString postData = QString("username=%1&password=%2").arg(this->username).arg(this->hashedPassword);
ApiResponse response = apiRequest->post("/updateInfo", postData.toUtf8());
if (response.isSuccess())
{
return true;
}
else
{
emit channel->message(response.message, "Error");
return false;
}
}
COSConfig User::getS3Config()
{
ApiResponse response = apiRequest->get("/s3Config");
COSConfig config;
if (response.isSuccess())
{
config.bucketName=response.getData()["bucket"].toString();
config.region=response.getData()["region"].toString();
config.appId=response.getData()["appid"].toString();
config.allowPrefix=response.getData()["allowPrefix"].toString();
}
else
{
emit channel->message(response.message, "Error");
return config;
}
return config;
}
QVector<QString> User::getS3Dirs()
{
ApiResponse response = apiRequest->get("/tasks");
QVector<QString> s3Dirs;
// 检查API响应是否成功
if (!response.isSuccess())
{
qDebug() << "API request failed with message:" << response.message;
return s3Dirs; // 返回空的s3Dirs
}
if(!response.getDatav().isArray())
{
qDebug() << "'data' is not an array";
return s3Dirs; // 返回空的s3Dirs
}
// 提取data数组
QJsonArray tasksArray = response.getDatav().toArray();
// 检查数组是否为空
if (tasksArray.isEmpty())
{
qDebug() << "Tasks array is empty";
return s3Dirs; // 返回空的s3Dirs
}
QSet<QString> s3DirsSet; // 使用QSet去重
// 遍历data数组中的每个任务对象
for (const QJsonValue& taskValue : tasksArray)
{
// 检查每个元素是否是对象
if (!taskValue.isObject())
{
qDebug() << "Task is not an object";
continue; // 跳过非对象元素
}
QJsonObject taskObject = taskValue.toObject();
// 检查是否存在"s3Dir"键
if (!taskObject.contains("s3Dir"))
{
qDebug() << "No 's3Dir' key in task object";
continue; // 跳过没有"s3Dir"键的对象
}
// 提取s3Dir键的值
QString s3Dir = taskObject["s3Dir"].toString();
// 检查提取的值是否为空
if (s3Dir.isEmpty())
{
qDebug() << "s3Dir is empty";
continue; // 跳过空的s3Dir
}
// 将提取的s3Dir路径添加到s3DirsSet中去重
s3DirsSet.insert(s3Dir);
}
// 将QSet转换回QVector
s3Dirs = QVector<QString>(s3DirsSet.begin(), s3DirsSet.end());
return s3Dirs;
}