remotepackconfig.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "remotepackconfig.h"
  2. #include "globalinfo.h"
  3. #include "handle/utils/cachefile.h"
  4. #include <QJsonDocument>
  5. RemotePackConfig::RemotePackConfig(QObject *parent) : QObject(parent)
  6. {
  7. connect(&_request,&HttpRequest::result,this,&RemotePackConfig::result);
  8. parsefUNS.insert("A1000100",[this](const QJsonObject & obj){this->_config.jitai = this->getConfigStringList(obj);});
  9. parsefUNS.insert("A1000101",[this](const QJsonObject & obj){this->_config.dangchegongbanci = this->getConfigStringList(obj);});
  10. parsefUNS.insert("A1000102",[this](const QJsonObject & obj){this->_config.daBaoGongBanCi = this->getConfigStringList(obj);});
  11. parsefUNS.insert("A1000103",[this](const QJsonObject & obj){this->_config.xiangzhong = this->getConfigStringDouble(obj);});
  12. parsefUNS.insert("A1000104",[this](const QJsonObject & obj){this->_config.tongZhong = this->getConfigStringDouble(obj);});
  13. parsefUNS.insert("A1000105",[this](const QJsonObject & obj){this->_config.guanse = this->getConfigStringList(obj);});
  14. parsefUNS.insert("A1000106",[this](const QJsonObject & obj){this->_config.nianxiang = this->getConfigStringList(obj);});
  15. parsefUNS.insert("A1000107",[this](const QJsonObject & obj){this->_config.zhixiang = this->getConfigStringList(obj);});
  16. parsefUNS.insert("A1000108",[this](const QJsonObject & obj){this->_config.mashu = this->getConfigStringInt(obj);});
  17. parsefUNS.insert("A1000109",[this](const QJsonObject & obj){this->_config.cheZhong = this->getConfigStringList(obj);});
  18. parsefUNS.insert("A1000110",[this](const QJsonObject & obj){this->_config.gangHao = this->getConfigStringList(obj);});
  19. parsefUNS.insert("A1000111",[this](const QJsonObject & obj){this->_config.xiangHaoXianShiGuiZe = this->getConfigInt(obj);});
  20. parsefUNS.insert("A1000112",[this](const QJsonObject & obj){this->_config.daBaoRuku = this->getConfigBool(obj);});
  21. parsefUNS.insert("A1000113",[this](const QJsonObject & obj){this->_config.daBaoRiQiXianShiFangShi = this->getConfigInt(obj);});
  22. parsefUNS.insert("A2000200",[this](const QJsonObject & obj){this->_config.ziDongDushu = this->getConfigBool(obj);});
  23. parsefUNS.insert("A2000201",[this](const QJsonObject & obj){this->_config.buDingZhongDaBaoXianzhi = this->getConfigBool(obj);});
  24. parsefUNS.insert("A2000202",[this](const QJsonObject & obj){this->_config.dingZhongDaBaoXianZhiKaiQi = this->getConfigBool(obj);});
  25. parsefUNS.insert("A2000203",[this](const QJsonObject & obj){this->_config.jiTaiBangDingMaDan = this->getConfigBool(obj);});
  26. parsefUNS.insert("A2000204",[this](const QJsonObject & obj){this->_config.morenTongZhongGUDing = this->getConfigBool(obj);});
  27. parsefUNS.insert("A2000205",[this](const QJsonObject & obj){this->_config.morenXiandhiMaoZhong = this->getConfigBool(obj);});
  28. parsefUNS.insert("A2000206",[this](const QJsonObject & obj){this->_config.morenDaYinRuKuMadan = this->getConfigBool(obj);});
  29. parsefUNS.insert("A2000207",[this](const QJsonObject & obj){this->_config.morenBudingZhongMaoZhongXianZhi = this->getConfigBool(obj);});
  30. parsefUNS.insert("A2000208",[this](const QJsonObject & obj){this->_config.daBaoZhuangLeiXingGuiZeKaiQi = this->getConfigBool(obj);});
  31. parsefUNS.insert("A2000209",[this](const QJsonObject & obj){this->_config.daBaoZhuangMaoZhongXianZhiKaiQi = this->getConfigBool(obj);});
  32. parsefUNS.insert("A2000210",[this](const QJsonObject & obj){this->_config.ziDongDaBaoKaiQi = this->getConfigBool(obj);});
  33. parsefUNS.insert("A2000211",[this](const QJsonObject & obj){this->_config.pingBanZiDongTongBuShuJu = this->getConfigBool(obj);});
  34. parsefUNS.insert("A2000212",[this](const QJsonObject & obj){
  35. auto jingdu = this->daBaoJingDu(obj);
  36. this->_config.chengZhongJIngDu = jingdu.first;
  37. this->_config.chengZhongJingDuMode = jingdu.second;
  38. });
  39. parsefUNS.insert("A2000213",[this](const QJsonObject & obj){this->_config.buDingZhongTongShuFanWei = this->getConfigpAIR(obj);});
  40. parsefUNS.insert("A2000214",[this](const QJsonObject & obj){this->_config.dingZhongTongShuFanwei = this->getConfigpAIR(obj);});
  41. parsefUNS.insert("A2000215",[this](const QJsonObject & obj){this->_config.morenRuKuMaDanFenShu = this->getConfigInt(obj);});
  42. parsefUNS.insert("A2000216",[this](const QJsonObject & obj){this->_config.buDingZhongXianZhiFanWei = this->getConfigpAIR(obj);});
  43. parsefUNS.insert("A2000217",[this](const QJsonObject & obj){this->_config.daBaoZhuangDaYinXiangDanFenShu = this->getConfigInt(obj);});
  44. parsefUNS.insert("A2000218",[this](const QJsonObject & obj){this->_config.daBaoZhuangMaoZhongFanWei = this->getConfigInt(obj);});
  45. parsefUNS.insert("A2000219",[this](const QJsonObject & obj){this->_config.ziDongDaBaoXiangZhongFanWei = this->getConfigInt(obj);});
  46. parsefUNS.insert("A2000220",[this](const QJsonObject & obj){this->_config.zidongDaBaoMaoZhongFanWei = this->getConfigInt(obj);});
  47. parsefUNS.insert("A3000300",[this](const QJsonObject & obj){this->_config.morenDaYinHuaMaDan = this->getConfigInt(obj);});
  48. parsefUNS.insert("A3000301",[this](const QJsonObject & obj){this->_config.morenXiaoShouXianShiJinE = this->getConfigInt(obj);});
  49. parsefUNS.insert("A3000302",[this](const QJsonObject & obj){this->_config.morenDayinXiaoShouMinXidan = this->getConfigInt(obj);});
  50. parsefUNS.insert("A3000303",[this](const QJsonObject & obj){this->_config.morenDaYinXIaoShouDan = this->getConfigInt(obj);});
  51. parsefUNS.insert("A3000304",[this](const QJsonObject & obj){this->_config.morenXianShiQianKuan = this->getConfigInt(obj);});
  52. parsefUNS.insert("A3000305",[this](const QJsonObject & obj){this->_config.xiaoShouEJingDu = this->getConfigInt(obj);});
  53. parsefUNS.insert("A4000400",[this](const QJsonObject & obj){this->_config.diys = this->getDiys(obj);});
  54. //TODO:http://doc.vanlai.net:3001/web/#/1?page_id=57 补全剩下
  55. //http://erp.itwsw.cn/#/packageCon
  56. }
  57. QList<DIYPackConfig> RemotePackConfig::getDiys(const QJsonObject & data)
  58. {
  59. // TODO:
  60. QJsonArray value_array = data["value"].toArray();
  61. DIYPackConfig DTYConfig;
  62. QList<DIYPackConfig> DTYConfigList;
  63. for(int i = 0;i<value_array.size();i++){
  64. QJsonObject obj = value_array.at(i).toObject();
  65. DTYConfig.type = obj["packaging_type"].toInt();
  66. DTYConfig.danDianXiaXian = obj["danding_lower_limit"].toInt();
  67. DTYConfig.danDianShangXian = obj["danding_upper_limit"].toInt();
  68. DTYConfig.jingZhongXiaXian = obj["net_weight_lower_limit"].toInt();
  69. DTYConfig.jingZhongShangXian = obj["net_weight_upper_limit"].toInt();
  70. DTYConfigList.append(DTYConfig);
  71. }
  72. return DTYConfigList;
  73. }
  74. QList<POYPackConfig> RemotePackConfig::getPoys(const QJsonObject & data)
  75. {
  76. // TODO:
  77. QJsonArray value_array = data["value"].toArray();
  78. POYPackConfig POYConfig;
  79. QList<POYPackConfig> POYConfigList;
  80. for(int i = 0;i<value_array.size();i++){
  81. QJsonObject obj = value_array.at(i).toObject();
  82. POYConfig.type = obj["packaging_type"].toInt();
  83. POYConfig.jingZhongXiaXian = obj["net_weight_lower_limit"].toInt();
  84. POYConfig.jingZhongShangXian = obj["net_weight_upper_limit"].toInt();
  85. POYConfigList.append(POYConfig);
  86. }
  87. return POYConfigList;
  88. }
  89. void RemotePackConfig::refConfig()
  90. {
  91. getDdengji = false;
  92. GlobalInfo * glo = GlobalInfo::this_();
  93. if(glo->isNoLine){
  94. CacheFile f;
  95. auto dt = f.readFile("config.cache");
  96. auto obj = QJsonDocument::fromJson(dt).object();
  97. result(200,obj);
  98. getDdengji = true;
  99. dt = f.readFile("dengjiList.cache");
  100. obj = QJsonDocument::fromJson(dt).object();
  101. result(200,obj);
  102. }
  103. // _aid = QString::number(body["user_id"].toInt());
  104. // _token = body["access_token"].toString();
  105. _request.setUserToken(QString::number( glo->user().accountId) ,glo->user().acessToken);
  106. QString url = glo->config().baseUrl+"/v1/configuration";
  107. _request.get(url);
  108. }
  109. void RemotePackConfig::result(int code, const QJsonObject & body)
  110. {
  111. int ecode = body.value("code").toInt(404);
  112. if(ecode != 200){
  113. emit configUpdateError(ecode,body.value("message").toString(""));
  114. return;
  115. }
  116. if(!getDdengji){
  117. CacheFile f;
  118. f.writeFile("config.cache",QJsonDocument(body).toJson());
  119. QJsonArray object = body.value("data").toArray();
  120. for(int i = 0; i < object.size(); ++ i){
  121. QJsonObject obj = object.at(i).toObject();
  122. QString key = obj.value("key").toString();
  123. auto handle = parsefUNS.value(key,[](const QJsonObject &){});
  124. handle(obj);
  125. }
  126. emit configUpdate(_config);
  127. if(!GlobalInfo::this_()->isNoLine){refDengJiList();}
  128. } else {
  129. // TODO: 解析等级列表
  130. CacheFile f;
  131. f.writeFile("dengjiList.cache",QJsonDocument(body).toJson());
  132. QJsonArray object = body.value("data").toArray();
  133. DengJiConfigItem dengJiConfig;
  134. for(int i = 0; i<object.size();i++){
  135. QJsonObject obj = object.at(i).toObject();
  136. dengJiConfig.id = obj.value("level_id").toInt();
  137. dengJiConfig.value = obj.value("name").toString();
  138. dengJiConfig.isEnable = obj.value("is_disable").toBool();
  139. dengji.append(dengJiConfig);
  140. }
  141. emit dengJIConfigUp(dengji);
  142. }
  143. }
  144. void RemotePackConfig::refDengJiList()
  145. {
  146. getDdengji = true;
  147. // todo: http://doc.vanlai.net:3001/web/#/1?page_id=35
  148. QString url = GlobalInfo::this_()->config().baseUrl+"/v1/level/list";
  149. _request.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken);
  150. _request.get(url);
  151. }
  152. bool RemotePackConfig::getConfigBool(const QJsonObject & data)
  153. {
  154. QJsonArray value_array = data.value("value").toArray();
  155. int vSize = value_array.size();
  156. QString select;
  157. for(int i = 0; i < vSize; ++i){
  158. QJsonValue value_content = value_array.at(i);
  159. if(value_content.isObject()){
  160. QJsonObject value_object = value_content.toObject();
  161. if(value_object.value("selected").toBool(false)){
  162. select = value_object.value("name").toString();
  163. }
  164. }
  165. }
  166. // select = select.trimmed();
  167. if(select.contains("是") || select.contains("yes",Qt::CaseInsensitive))
  168. return true;
  169. return false;
  170. }
  171. QStringList RemotePackConfig::getConfigStringList(const QJsonObject & data)
  172. {
  173. QJsonArray value_array = data.value("value").toArray();
  174. QStringList list;
  175. int vSize = value_array.size();
  176. for(int i = 0; i < vSize; ++i){
  177. QJsonValue value_content = value_array.at(i);
  178. if(value_content.isObject()){
  179. QJsonObject value_object = value_content.toObject();
  180. list.append(value_object.value("value").toString());
  181. }
  182. }
  183. return list;
  184. }
  185. int RemotePackConfig::getConfigInt(const QJsonObject & data)
  186. {
  187. QJsonArray value_array = data.value("value").toArray();
  188. int list;
  189. int vSize = value_array.size();
  190. for(int i = 0; i < vSize; ++i){
  191. QJsonValue value_content = value_array.at(i);
  192. if(value_content.isObject()){
  193. QJsonObject value_object = value_content.toObject();
  194. if(value_content.isObject()){
  195. QJsonObject value_object = value_content.toObject();
  196. if(value_object.value("selected").toBool(false)){
  197. list = value_object.value("value").toInt();
  198. }
  199. }
  200. }
  201. }
  202. return list;
  203. }
  204. QPair<int,int> RemotePackConfig::getConfigpAIR(const QJsonObject & data)
  205. {
  206. QJsonArray value_array = data.value("value").toArray();
  207. // qDebug()<<value_array;
  208. QPair<int,int> list;
  209. int vSize = value_array.size();
  210. bool frist = true;
  211. for(int i = 0; i < vSize; ++i){
  212. QJsonObject value_object = value_array.at(i).toObject();
  213. // if(value_object.value("selected").toBool(false)){
  214. if(frist) {
  215. list.first = value_object.value("value").toInt();
  216. // qDebug()<<"不定重范围"<<value_object.value("value").toInt();
  217. frist = false;
  218. } else {
  219. list.second = value_object.value("value").toInt();
  220. // qDebug()<<"不定重范围"<<value_object.value("value").toInt();
  221. }
  222. // }
  223. }
  224. return list;
  225. }
  226. QPair<int,int> RemotePackConfig::daBaoJingDu(const QJsonObject & data)
  227. {
  228. QJsonArray value_array = data.value("value").toArray();
  229. QPair<int,int> list;
  230. int vSize = value_array.size();
  231. bool frist = true;
  232. for(int i = 0; i < vSize; ++i){
  233. QJsonValue value_content = value_array.at(i);
  234. auto ary = value_content.toArray();
  235. for(int j = 0; j < ary.size(); ++j){
  236. QJsonObject value_object = ary.at(j).toObject();
  237. if(value_object.value("selected").toBool(false)){
  238. if(frist) {
  239. list.first = value_object.value("value").toInt();
  240. frist = false;
  241. } else {
  242. list.second = value_object.value("value").toInt();
  243. }
  244. }
  245. }
  246. }
  247. return list;
  248. }
  249. QList<double> RemotePackConfig::getConfigStringDouble(const QJsonObject & data)
  250. {
  251. QJsonArray value_array = data.value("value").toArray();
  252. QList<double> list;
  253. int vSize = value_array.size();
  254. for(int i = 0; i < vSize; ++i){
  255. QJsonValue value_content = value_array.at(i);
  256. if(value_content.isObject()){
  257. QJsonObject value_object = value_content.toObject();
  258. list.append(value_object.value("value").toDouble());
  259. }
  260. }
  261. return list;
  262. }
  263. QList<int> RemotePackConfig::getConfigStringInt(const QJsonObject & data)
  264. {
  265. QJsonArray value_array = data.value("value").toArray();
  266. QList<int> list;
  267. int vSize = value_array.size();
  268. for(int i = 0; i < vSize; ++i){
  269. QJsonValue value_content = value_array.at(i);
  270. if(value_content.isObject()){
  271. QJsonObject value_object = value_content.toObject();
  272. list.append(value_object.value("value").toInt());
  273. }
  274. }
  275. return list;
  276. }