packinfoseed2server.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/product/pending";
  29. _requst.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken);
  30. _requst.post(url,_current->toObject());
  31. }
  32. }
  33. void PackInfoSeed2Server::sendList(QList<QSharedPointer<PackInfo>> & infos)
  34. {
  35. if(_manger == nullptr) return;
  36. _queue.append(infos);
  37. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  38. }
  39. void PackInfoSeed2Server::result(int code, const QJsonObject & body)
  40. {
  41. qDebug()<<body;
  42. if(code != 200 || body.isEmpty()){
  43. _current.clear();
  44. QTimer::singleShot(500,this,&PackInfoSeed2Server::doSend);
  45. _netErrorCount ++;
  46. return;
  47. }
  48. auto error = body.value("code").toInt();
  49. auto message = body.value("message").toString();
  50. if(error == 200 || message.indexOf("已存在") >= 0){ // TODO: fix error
  51. _queue.dequeue();
  52. _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync);
  53. _current.clear();
  54. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  55. _netErrorCount = 0;
  56. _oneSyncError = 0;
  57. } else {
  58. _oneSyncError ++;
  59. if(_oneSyncError > 3){
  60. pqWarning(log) << "sync error,and to skip: data : \n" << QJsonDocument(_current->toObject()).toJson() <<
  61. "\n error: \n " << QJsonDocument(body).toJson();
  62. _oneSyncError = 0;
  63. _queue.dequeue();
  64. }
  65. _current.clear();
  66. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  67. }
  68. }