packinfoseed2server.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. //docs: http://doc.vanlai.net:3001/web/#/1?page_id=64
  24. QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/product/pending";
  25. _requst.post(url,_current->toObject());
  26. }
  27. }
  28. void PackInfoSeed2Server::result(int code, const QJsonObject & body)
  29. {
  30. if(code != 200 || body.isEmpty()){
  31. _current.clear();
  32. QTimer::singleShot(500,this,&PackInfoSeed2Server::doSend);
  33. _netErrorCount ++;
  34. return;
  35. }
  36. auto error = body.value("code").toInt();
  37. auto message = body.value("message").toString();
  38. if(error == 200 || message.indexOf("已存在") >= 0){ // TODO: fix error
  39. _queue.dequeue();
  40. _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync);
  41. _current.clear();
  42. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  43. _netErrorCount = 0;
  44. _oneSyncError = 0;
  45. } else {
  46. _oneSyncError ++;
  47. if(_oneSyncError > 3){
  48. pqWarning(log) << "sync error,and to skip: data : \n" << QJsonDocument(_current->toObject()).toJson() <<
  49. "\n error: \n " << QJsonDocument(body).toJson();
  50. _oneSyncError = 0;
  51. _queue.dequeue();
  52. }
  53. _current.clear();
  54. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  55. }
  56. }