fwdb.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "fwdb.h"
  2. #include <iostream>
  3. fwdb::fwdb()
  4. {
  5. mpthread = new std::thread(&fwdb::threaddb,this);
  6. }
  7. fwdb::~fwdb()
  8. {
  9. mbthreadrun = false;
  10. mpthread->join();
  11. }
  12. void fwdb::OpenDataBase()
  13. {
  14. mstrdbpath = "fw.db";
  15. if(QSqlDatabase::contains("sqliteadc"))
  16. mdatabase = QSqlDatabase::database("sqliteadc");
  17. else
  18. mdatabase = QSqlDatabase::addDatabase("QSQLITE","sqliteadc");
  19. // mdatabase = QSqlDatabase::addDatabase("QSQLITE");
  20. // if(QSqlDatabase::contains("sqlite"))
  21. // mdatabase = QSqlDatabase::database("sqlite");
  22. // else
  23. // mdatabase = QSqlDatabase::addDatabase("QSQLITE","sqlite");
  24. mdatabase.setDatabaseName(mstrdbpath.data());
  25. if (!mdatabase.open())
  26. {
  27. qDebug("Error: Failed to connect database. error is %s ",mdatabase.lastError());
  28. return;
  29. }
  30. else
  31. {
  32. qDebug("Succeed to connect database.");
  33. }
  34. QSqlQuery sql_query(mdatabase);
  35. bool bHaveTable = true;
  36. //查询数据
  37. sql_query.exec("select count(*) from sqlite_master where type='table' and name = 'accountdata'");
  38. if(!sql_query.exec())
  39. {
  40. qDebug("%s",sql_query.lastError());
  41. return;
  42. }
  43. else
  44. {
  45. if(sql_query.next())
  46. {
  47. int count = sql_query.value(0).toInt();
  48. // qDebug("count %d ",count);
  49. if(count < 1)bHaveTable = false;
  50. }
  51. }
  52. if(bHaveTable == false)
  53. {
  54. if(!sql_query.exec("create table accountdata(id INTEGER primary key AUTOINCREMENT,username TEXT, password TEXT,"
  55. "phone TEXT, passneedchange BOOL, passerrorcount INTEGER,"
  56. "uservalidtime DATETIME, usercreatetime DATETIME,lastloginerrortime DATETIME)"))
  57. {
  58. qDebug() << "Error: Fail to create table."<< sql_query.lastError();
  59. return;
  60. }
  61. // if(!sql_query.exec("CREATE INDEX index_vehid ON groupdata (vehid)"))
  62. // {
  63. // qDebug() << "Error: Fail to create index_vehid."<< sql_query.lastError();
  64. // return;
  65. // }
  66. // if(!sql_query.exec("CREATE INDEX index_recvtime ON groupdata (recvtime)"))
  67. // {
  68. // qDebug() << "Error: Fail to create index_recvtime."<< sql_query.lastError();
  69. // return;
  70. // }
  71. // if(!sql_query.exec("CREATE INDEX index_recordid ON groupdata (recordid)"))
  72. // {
  73. // qDebug() << "Error: Fail to create index_recordid."<< sql_query.lastError();
  74. // return;
  75. // }
  76. char strsen[1000];
  77. QDateTime now = QDateTime::currentDateTime();
  78. QString strnow = now.toString("yyyy-MM-dd hh:mm:ss");
  79. snprintf(strsen,1000,"INSERT INTO accountdata(username,password,passerrorcount,uservalidtime,usercreatetime,lastloginerrortime)"
  80. " VALUES(\"%s\",\"%s\",%d,\"%s\",\"%s\",\"%s\")",
  81. "admin","adc",0,"2099-12-31 23:59:59",strnow.toLatin1().data(),"1970-1-1 1:00:00");
  82. sql_query.exec(strsen);
  83. qDebug("Create Table groupdata successfully.");
  84. }
  85. }
  86. void fwdb::threaddb()
  87. {
  88. OpenDataBase();
  89. }
  90. int fwdb::CheckAuth(std::string strusername, std::string strpassword ,std::string & strerrorcode)
  91. {
  92. int nrtn = 0;
  93. mmutexdb.lock();
  94. QSqlQuery query(mdatabase);
  95. char strsen[1000];
  96. snprintf(strsen,1000,"select * from accountdata where((accountdata.username = \"%s\"))",
  97. strusername.data(),strpassword.data());
  98. query.exec(strsen);
  99. if(!query.exec(strsen))
  100. {
  101. std::cout<<query.lastError().text().toLatin1().data()<<std::endl;
  102. strerrorcode = "SQL Error.";
  103. mmutexdb.unlock();
  104. return 0;
  105. }
  106. else
  107. {
  108. if(query.next())
  109. {
  110. int nerrorcount = query.value("passerrorcount").toInt();
  111. QString strerrortime = query.value("lastloginerrortime").toString();
  112. QDateTime dtlasterr = QDateTime::fromString(strerrortime,"yyyy-MM-dd hh:mm:ss");
  113. int64_t nseclasterr = dtlasterr.toSecsSinceEpoch();
  114. int64_t nsecnow = QDateTime::currentSecsSinceEpoch();
  115. int64_t ndiff = nsecnow - nseclasterr ;
  116. if((nerrorcount >= 5) &&(ndiff < 1800))
  117. {
  118. strerrorcode = "PassWord Error 5 times, Please Wait 30 minutes.";
  119. mmutexdb.unlock();
  120. return 0;
  121. }
  122. }
  123. }
  124. snprintf(strsen,1000,"select * from accountdata where((accountdata.username = \"%s\") &(accountdata.password = \"%s\"))",
  125. strusername.data(),strpassword.data());
  126. query.exec(strsen);
  127. if(!query.exec(strsen))
  128. {
  129. std::cout<<query.lastError().text().toLatin1().data()<<std::endl;
  130. strerrorcode = "SQL Error.";
  131. mmutexdb.unlock();
  132. return 0;
  133. }
  134. else
  135. {
  136. if(query.next())
  137. {
  138. nrtn = 1;
  139. std::cout<<" auth ok. "<<std::endl;
  140. QString strvalidtime = query.value("uservalidtime").toString();
  141. QDateTime dtvalid = QDateTime::fromString(strvalidtime,"yyyy-MM-dd hh:mm:ss");
  142. int64_t nsecvalid = dtvalid.toSecsSinceEpoch();
  143. int64_t nsecnow = QDateTime::currentSecsSinceEpoch();
  144. int nerrorcount = query.value("passerrorcount").toInt();
  145. int64_t ndiff = nsecvalid - nsecnow;
  146. if(ndiff <0)
  147. {
  148. strerrorcode = "Account Expire.";
  149. mmutexdb.unlock();
  150. return 0;
  151. }
  152. if(nerrorcount != 0)
  153. {
  154. snprintf(strsen,1000,"update accountdata set passerrorcount = %d where((accountdata.username = \"%s\") )",
  155. 0, strusername.data());
  156. query.exec(strsen);
  157. if(!query.exec(strsen))
  158. {
  159. std::cout<<query.lastError().text().toLatin1().data()<<std::endl;
  160. }
  161. }
  162. // int id = query.value(0).toInt();
  163. // qint64 recordid = query.value(1).toLongLong();
  164. // mql_xvectorrecordtime.push_back(recordid);
  165. // std::string strvehid = query.value(3).toString().toStdString();
  166. }
  167. else
  168. {
  169. snprintf(strsen,1000,"select * from accountdata where((accountdata.username = \"%s\") )",
  170. strusername.data());
  171. query.exec(strsen);
  172. if(!query.exec(strsen))
  173. {
  174. std::cout<<query.lastError().text().data()<<std::endl;
  175. }
  176. else
  177. {
  178. if(query.next())
  179. {
  180. int nerrorcount = 0;
  181. nerrorcount = query.value(5).toInt();
  182. snprintf(strsen,1000,"update accountdata set lastloginerrortime = \"%s\",passerrorcount = %d where(accountdata.username = \"%s\" )",
  183. QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss").toLatin1().data(),
  184. nerrorcount+1, strusername.data());
  185. query.exec(strsen);
  186. if(!query.exec(strsen))
  187. {
  188. std::cout<<query.lastError().text().toLatin1().data()<<std::endl;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. mmutexdb.unlock();
  195. return nrtn;
  196. }