This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
databaseutil.cpp
213 lines (202 loc) · 6.33 KB
/
databaseutil.cpp
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
#include "databaseutil.h"
DataBaseUtil::DataBaseUtil(){
}
//DataBaseUtil::DataBaseUtil(const char * name)
//{
// qF = new File(name);
// //fileStream()
//}
DataBaseUtil::~DataBaseUtil(){
qF->close();
}
int DataBaseUtil::openReadFile(){
if(!qF->open(QIODevice::ReadOnly|QIODevice::Text)){
qDebug()<<"open read file error!";
return -1;
}
// QTextStream qTextStream(qF);
// qTS = &qTextStream;
return 0;
}
int DataBaseUtil::openWriteFile(){
if(!qF->open(QIODevice::WriteOnly|QIODevice::Text)){
qDebug()<<"open write file error!";
return -1;
}
QFile q;
//QTextStream
return 0;
}
QString DataBaseUtil::searchInfo(QString email){
QFile UserFile("userFile.txt");
if(!UserFile.open(QIODevice::ReadOnly|QIODevice::Text)){
qDebug()<<"open login file fail!";
return "";
}
QTextStream txtOutput(&UserFile);
QJsonParseError *error = new QJsonParseError;
QByteArray user;
QJsonArray infoArr;
while(!txtOutput.atEnd()){
user = txtOutput.readLine().toLatin1();
user = QByteArray::fromBase64(user);
infoArr = QJsonDocument::fromJson(user,error).array();
if(infoArr.at(2).toString()==email){
UserFile.close();
return user;
}
}
UserFile.close();
return "";
}
bool DataBaseUtil::searchUuid(QString email){
QFile loginFile("loginFile.txt");
if(!loginFile.open(QIODevice::ReadOnly|QIODevice::Text)){
qDebug()<<"open login file fail!";
return false;
}
QTextStream txtOutput(&loginFile);
QJsonParseError *error=new QJsonParseError;
QByteArray user;
QJsonArray infoArr;
while(!txtOutput.atEnd()){
user = txtOutput.readLine().toLatin1();
user = QByteArray::fromBase64(user);
infoArr = QJsonDocument::fromJson(user,error).array();
if(infoArr.at(0).toString()==email){
loginFile.close();
return true;
}
}
loginFile.close();
return false;
}
QString DataBaseUtil::searchUuid(QString email, QString password){
QFile loginFile("loginFile.txt");
if(!loginFile.open(QIODevice::ReadOnly|QIODevice::Text)){
qDebug()<<"open login file fail!";
return "";
}
QTextStream txtOutput(&loginFile);
QJsonParseError *error=new QJsonParseError;
QByteArray user;
QJsonArray infoArr;
while(!txtOutput.atEnd()){
user = txtOutput.readLine().toLatin1();
user = QByteArray::fromBase64(user);
infoArr = QJsonDocument::fromJson(user,error).array();
if(infoArr.at(0).toString()==email){
if(infoArr.at(1).toString()==password){
loginFile.close();
return infoArr.at(2).toString();
}else{
loginFile.close();
return "";
}
}
}
loginFile.close();
return "";
}
QString DataBaseUtil::searchPassword(QString Uuid){
QFile loginFile("loginFile.txt");
if(!loginFile.open(QIODevice::ReadOnly|QIODevice::Text)){
qDebug()<<"open login file fail!";
return "";
}
QTextStream txtOutput(&loginFile);
QJsonParseError *error=new QJsonParseError;
QByteArray user;
QJsonArray infoArr;
while(!txtOutput.atEnd()){
user = txtOutput.readLine().toLatin1();
user = QByteArray::fromBase64(user);
infoArr = QJsonDocument::fromJson(user,error).array();
if(infoArr.at(2).toString()==Uuid){
loginFile.close();
return infoArr.at(1).toString();
}
}
loginFile.close();
return "";
}
QString DataBaseUtil::getAllUsersName(QString Uuid){
//获取全部用户的信息,用#分隔本人姓名,用;分隔用户,以昵称{Uuid}形式保存
QFile UserFile("userFile.txt");
if(!UserFile.open(QIODevice::ReadOnly|QIODevice::Text)){
qDebug()<<"open user File fail!";
return "";
}
QTextStream txtOutput(&UserFile);
QJsonParseError *error = new QJsonParseError;
QByteArray userInfo;
QJsonArray infoarr;
QString myName;
QStringList strlist;
while(!txtOutput.atEnd()){
userInfo = txtOutput.readLine().toLatin1();
userInfo = QByteArray::fromBase64(userInfo);
infoarr = QJsonDocument::fromJson(userInfo,error).array();
if(infoarr.at(0).toString()==Uuid){
myName = infoarr.at(1).toString();
continue;
}
QString info = infoarr.at(1).toString() + infoarr.at(0).toString();
strlist.append(info);
}
QString res = strlist.join(";");
res.append("#");
res.append(myName);
return res;
}
vector<QString> DataBaseUtil::offline;
int DataBaseUtil::writeAtEnd(QString context){
offline.push_back(context);
QFile writeFile("history2.txt");
if(!writeFile.open(QIODevice::Append|QIODevice::Text|QIODevice::WriteOnly)){
qDebug()<<"open history fail!";
return -1;
}
QTextStream txtInput(&writeFile);
QByteArray text = context.toLatin1();
text = text.toBase64();
txtInput<<text<<endl;
return 0;
}
void DataBaseUtil::saveRegister(QJsonArray arr){
QString Uuid = QUuid::createUuid().toString();
QFile writeHis("loginFile.txt");
if(!writeHis.open(QIODevice::Append|QIODevice::Text|QIODevice::WriteOnly)){
qDebug()<<"open history fail!";
return;
}
QTextStream txtInHis(&writeHis);
QJsonArray loginarr;
loginarr.insert(0,arr.at(2).toString());
loginarr.insert(1,arr.at(3).toString());
loginarr.insert(2,Uuid);
QJsonDocument doc;doc.setArray(loginarr);
QString login = doc.toJson(QJsonDocument::Compact);
QByteArray text = login.toLatin1();
text = text.toBase64();
txtInHis<<text<<endl;
writeHis.close();
QFile writeUser("userFile.txt");
if(!writeUser.open(QIODevice::Append|QIODevice::Text|QIODevice::WriteOnly)){
qDebug()<<"open history fail!";
return;
}
QTextStream txtInUser(&writeUser);
QJsonArray Userarr;
Userarr.insert(0,Uuid);
Userarr.insert(1,arr.at(1).toString());
Userarr.insert(2,arr.at(2).toString());
Userarr.insert(3,arr.at(4).toString());
Userarr.insert(4,arr.at(5).toString());
QJsonDocument doc1;doc1.setArray(Userarr);
QString User = doc1.toJson(QJsonDocument::Compact);
text = User.toLatin1();
text = text.toBase64();
txtInUser<<text<<endl;
writeUser.close();
}