packinfoseed2server.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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()){
  22. if(_queue.isEmpty()) {
  23. emit syncEnd();
  24. return;
  25. }
  26. _current = _queue.first();
  27. //docs: http://doc.vanlai.net:3001/web/#/1?page_id=64
  28. // if(_current->autoRuku == true){
  29. // QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/product/directly/into";
  30. // _requst.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken);
  31. // _requst.post(url,_current->toObject());
  32. // return;
  33. // }
  34. QString url = GlobalInfo::this_()->config()->baseUrl+"/v2/product/pending";
  35. _requst.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken);
  36. _requst.post(url,_current->toObject());
  37. }
  38. }
  39. void PackInfoSeed2Server::sendList(QList<QSharedPointer<PackInfo>> & infos)
  40. {
  41. if(_manger == nullptr) return;
  42. _queue.append(infos);
  43. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  44. }
  45. void PackInfoSeed2Server::result(int code, const QJsonObject & body)
  46. {
  47. qDebug()<<body;
  48. if(code != 200 || body.isEmpty()){
  49. _current.clear();
  50. QTimer::singleShot(500,this,&PackInfoSeed2Server::doSend);
  51. _netErrorCount ++;
  52. return;
  53. }
  54. auto error = body.value("code").toInt();
  55. auto message = body.value("message").toString();
  56. if(error == 200 || message.indexOf("已存在") >= 0){ // TODO: fix error
  57. _queue.dequeue();
  58. _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync);
  59. // int product_id = body.value("data").toObject().value("product_id").toInt();
  60. _current.clear();
  61. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  62. _netErrorCount = 0;
  63. _oneSyncError = 0;
  64. }else {
  65. _oneSyncError ++;
  66. if(_oneSyncError > 3){
  67. pqWarning(log) << "sync error,and to skip: data : \n" << QJsonDocument(_current->toObject()).toJson() <<
  68. "\n error: \n " << QJsonDocument(body).toJson();
  69. _oneSyncError = 0;
  70. _queue.dequeue();
  71. }
  72. _current.clear();
  73. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  74. }
  75. }