fileupload.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "fileupload.h"
  2. #include "fileprepare.h"
  3. #include "wsnumchk.h"
  4. #include "respsubminpnum.h"
  5. #include "resppackmerge.h"
  6. #include "respchkuserfeestatus.h"
  7. #include "resppacktran.h"
  8. #include "filesplitter.h"
  9. #include "sncdatabase.h"
  10. #include "numberreg.h"
  11. FileUpload::FileUpload(QString fileName, QString fileType,QString submitWay, QString memo,QString host,QString port, QString userHash)
  12. {
  13. this->fileName = fileName;
  14. this->fileType = fileType;
  15. this->submitWay = submitWay;
  16. this->memo = memo;
  17. this->host = host;
  18. this->port = port;
  19. this->userHash = userHash;
  20. this->stop = false;
  21. this->isPackTran = true;
  22. }
  23. void FileUpload::abort()
  24. {
  25. this->stop = true;
  26. }
  27. FileUpload::~FileUpload()
  28. {
  29. qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
  30. <<"FormNumcheck.FileUpload Destroyed.";
  31. }
  32. void FileUpload::run()
  33. {
  34. if(isPackTran)
  35. submitByPack();
  36. else
  37. submit();
  38. emit uploadFinished();
  39. }
  40. void FileUpload::submit()
  41. {
  42. emit msgUpdated("正在进行预处理,请稍候","status");
  43. bool ismulti;
  44. if(submitWay=="1")
  45. {
  46. ismulti = false;
  47. }
  48. else
  49. {
  50. ismulti = true;
  51. }
  52. FilePrepare filePrepare(this->fileName,this->fileType,ismulti);
  53. if(!filePrepare.prepare())
  54. {
  55. emit msgUpdated(filePrepare.getRtnMsg(),"fail");
  56. return ;
  57. }
  58. emit msgUpdated(QString::number(filePrepare.getCount()),"numbercount");
  59. if(!checkFee(filePrepare.getCount()))
  60. return;
  61. emit msgUpdated("正在提交,请稍候","status");
  62. WSNumChk ws(this->host,this->port);
  63. QString json = ws.submitPnumFile(filePrepare.getLastUploadFileName(),this->fileType,submitWay,this->memo,this->userHash);
  64. if(json.isEmpty())
  65. {
  66. emit msgUpdated("网络无法连接,请稍候再试!","fail");
  67. }
  68. else
  69. {
  70. RespSubminPNum response(json);
  71. if(response.getRtnCode()==2)
  72. {
  73. emit msgUpdated("上传成功,号码全部接收","succeed");
  74. emit msgUpdated(response.getRtnMemo(),"detailok");
  75. }
  76. else if(response.getRtnCode()==1)
  77. {
  78. emit msgUpdated("上传成功,文件中含有部分非法数据","succeed");
  79. emit msgUpdated(response.getRtnMemo(),"detailwarning");
  80. }
  81. else if(response.getRtnCode()==-1)
  82. {
  83. emit msgUpdated("登录超时,请重新登录","fail");
  84. }
  85. else
  86. {
  87. emit msgUpdated(response.getRtnMemo(),"fail");
  88. }
  89. }
  90. }
  91. void FileUpload::submitByPack()
  92. {
  93. emit msgUpdated("正在进行预处理,请稍候","status");
  94. bool ismulti;
  95. if(submitWay=="1")
  96. {
  97. ismulti = false;
  98. }
  99. else
  100. {
  101. ismulti = true;
  102. }
  103. FilePrepare filePrepare(this->fileName,this->fileType,ismulti);
  104. if(!filePrepare.prepare())
  105. {
  106. emit msgUpdated(filePrepare.getRtnMsg(),"fail");
  107. return ;
  108. }
  109. qDebug()<<"step1";
  110. emit msgUpdated(QString::number(filePrepare.getCount()),"numbercount");
  111. if(stop)
  112. return ;
  113. if(!checkFee(filePrepare.getCount()))
  114. return;
  115. FileSplitter splitter(filePrepare.getLastUploadFileName(),"");
  116. QStringList list = splitter.getSplitResult();
  117. emit msgUpdated("正在提交,请稍候","status");
  118. WSNumChk ws(this->host,this->port);
  119. bool allupload = true;
  120. QString packName;
  121. for(int i=0;i<list.size();i++)
  122. {
  123. if(stop)
  124. return ;
  125. packName = list.at(i);
  126. QString json = ws.PackTran(packName,this->userHash);
  127. RespPackTran response(json);
  128. if(response.getRtnCode()==1)
  129. {
  130. QFile::remove(packName);
  131. }
  132. else
  133. {
  134. allupload = false;
  135. }
  136. emit msgUpdated(QString::number(3+90*(i+1)/list.size()),"progress");
  137. }
  138. if(allupload)
  139. {
  140. splitter.cleanup();
  141. emit msgUpdated(QString::number(94),"progress");
  142. QString json = ws.PackMerge(packName,this->userHash);
  143. emit msgUpdated(QString::number(95),"progress");
  144. RespPackMerge response(json);
  145. if(response.getRtnCode()==1)
  146. {
  147. emit msgUpdated(QString::number(96),"progress");
  148. json = ws.submitPnumFile(filePrepare.getLastUploadFileName(),response.getFileHash(),fileType,submitWay,memo,userHash);
  149. RespSubminPNum response(json);
  150. if(response.getRtnCode()==2)
  151. {
  152. emit msgUpdated("上传成功,号码全部接收。","succeed");
  153. emit msgUpdated(response.getRtnMemo(),"detailok");
  154. }
  155. else if(response.getRtnCode()==1)
  156. {
  157. emit msgUpdated("上传成功,文件中含有部分非法数据。","succeed");
  158. emit msgUpdated(response.getRtnMemo(),"detailwarning");
  159. }
  160. else if(response.getRtnCode()==-1)
  161. {
  162. emit msgUpdated("登录超时,请重新登录","fail");
  163. }
  164. else
  165. {
  166. emit msgUpdated(response.getRtnMemo(),"fail");
  167. }
  168. }
  169. else
  170. {
  171. emit msgUpdated(response.getRtnMemo(),"fail");
  172. }
  173. }
  174. else
  175. {
  176. emit msgUpdated("未能提交,请再次尝试上传(支持断点续传)","fail");
  177. }
  178. emit msgUpdated(QString::number(100),"progress");
  179. }
  180. bool FileUpload::checkFee(int numCount)
  181. {
  182. WSNumChk client(this->host,this->port);
  183. emit msgUpdated("正在检测是否有足够的余额","status");
  184. QString json = client.chkUserFeeStatus(QString::number(numCount),this->userHash);
  185. bool pass = false;
  186. if(!json.isEmpty()){
  187. RespChkUserFeeStatus response(json);
  188. if(response.getRtnCode()==1)
  189. {
  190. pass = true;
  191. }
  192. else if(response.getRtnCode()==-1)
  193. {
  194. emit msgUpdated("登录超时,请重新登录!","fail");
  195. }
  196. else
  197. {
  198. emit msgUpdated(response.getRtnMemo(),"fail");
  199. }
  200. }
  201. else
  202. {
  203. emit msgUpdated("网络无法连接,请稍候再试!","fail");
  204. }
  205. return pass;
  206. }