packinfoseed2server.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "packinfoseed2server.h"
  2. #include <QTimer>
  3. #include <QJsonDocument>
  4. #include "globalinfo.h"
  5. PackInfoSeed2Server::PackInfoSeed2Server(PackInfoManger * manger, QObject *parent, bool autoSync) : QObject(parent),
  6. _manger(manger),_autoSync(autoSync)
  7. {
  8. connect(&_requst,&HttpRequest::result,this,&PackInfoSeed2Server::result);
  9. _netErrorCount = 0;
  10. _oneSyncError = 0;
  11. log = PQ::PQLogManger::this_()->getLog("PackInfoSeed2Server");
  12. }
  13. void PackInfoSeed2Server::send(QSharedPointer<PackInfo> & info)
  14. {
  15. if(_manger == nullptr) return;
  16. _queue.append(info);
  17. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  18. }
  19. void PackInfoSeed2Server::doSend()
  20. {
  21. if(_current.isNull() && !_queue.isEmpty()){
  22. _current = _queue.first();
  23. // TODO:小宋, 发送 _current 到服务器
  24. //docs: http://doc.vanlai.net:3001/web/#/1?page_id=64
  25. QString url = GlobalInfo::this_()->config().baseUrl+"/v1/product/pending";
  26. _requst.post(url,_current->toObject());
  27. }
  28. }
  29. void PackInfoSeed2Server::result(int code, const QJsonObject & body)
  30. {
  31. if(code != 200 || body.isEmpty()){
  32. _current.clear();
  33. QTimer::singleShot(500,this,&PackInfoSeed2Server::doSend);
  34. _netErrorCount ++;
  35. return;
  36. }
  37. auto error = body.value("code").toInt();
  38. auto message = body.value("message").toString();
  39. if(error == 200 || message.indexOf("已存在") >= 0){ // TODO: fix error
  40. _queue.dequeue();
  41. _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync);
  42. _current.clear();
  43. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  44. _netErrorCount = 0;
  45. _oneSyncError = 0;
  46. } else {
  47. _oneSyncError ++;
  48. if(_oneSyncError > 3){
  49. pqWarning(log) << "sync error,and to skip: data : \n" << QJsonDocument(_current->toObject()).toJson() <<
  50. "\n error: \n " << QJsonDocument(body).toJson();
  51. _oneSyncError = 0;
  52. _queue.dequeue();
  53. }
  54. _current.clear();
  55. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  56. }
  57. }