OrderGetYundan.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\console\service;
  13. use app\common\library\express\Usps;
  14. use app\common\library\helper;
  15. use app\console\library\Tools;
  16. use app\common\service\BaseService;
  17. use app\console\model\Order as OrderModel;
  18. use think\facade\Log;
  19. /**
  20. * 服务类:会员等级
  21. * Class UserGrade
  22. * @package app\console\service
  23. */
  24. class OrderGetYundan extends BaseService
  25. {
  26. /**
  27. * 设置用户的会员等级
  28. * @param int $storeId
  29. * @return array|bool
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function orderCreateYundans(int $storeId)
  35. {
  36. $Usps = new Usps();
  37. // 请求查询接口
  38. $token = $Usps->getItdidaToken();
  39. if (empty($token)) {
  40. Log::error('无有效的Iddida token');
  41. return false;
  42. }
  43. // 获取所有待发货订单
  44. $list = $this->getUsableList($storeId);
  45. // 遍历等级,根据升级条件 查询满足消费金额的用户列表,并且他的等级小于该等级
  46. $data = [];
  47. $url = 'http://f1.itdida.com/itdida-api/yundans';
  48. $params =
  49. [
  50. "baoGuanFangShi" => 0,
  51. "baoGuoLeiXing" => 1,
  52. "cardType" => 0,
  53. "clientId" => $token,
  54. "danJianList" => [
  55. [
  56. "chang" => 1,
  57. "gao" => 1,
  58. "kuan" => 1,
  59. "origBoardNum" => 0,
  60. "shiZhong" => 0.5,
  61. "stackable" => true
  62. ]
  63. ],
  64. "eori" => "",
  65. "extraFreight" => 0,
  66. "freightPaymentWay" => 0,
  67. "fuShuiJin" => 0,
  68. "guoJia" => "US",
  69. "huoWuTeXing" => "带电",
  70. "ioss" => "",
  71. "jianShu" => 1,//to fix
  72. "kapaiWaybill" => false,
  73. "keHuDanHao" => "",//todo fill
  74. "maiJiaId" => "",//to fill
  75. "needDispatch" => false,
  76. "needValidateAddress" => false,
  77. "pushBackLabelAndInvoice" => false,
  78. "qiTaDanHao1" => "JCX0415047275YQ",
  79. "requiredChangeNo" => false,
  80. "requiredTrackNo" => true,
  81. "requiredZhuanDanFaPiao" => false,
  82. "shenBaoXinXiList" => [
  83. [
  84. "shenBaoBiZhong" => "USD",
  85. "shenBaoDanJia" => 0,//todo fill 申报单价
  86. "shenBaoHaiGuanBianMa" => "",
  87. "shenBaoJinE" => 0,////todo fill 申报金额
  88. "shenBaoPinMing" => "",//todo fill 品名
  89. "shenBaoShuLiang" => 1,
  90. "zhongWenPinMing" => ""//to fill
  91. ],
  92. ],
  93. "shouHuoQuDao" => "美国专线小包普货",
  94. "shouHuoShiZhong" => 0.500,
  95. "shouJianRenChengShi" => "",//to fill收货城市
  96. "shouJianRenDiZhi1" => "",//to fill收货地址
  97. "shouJianRenDiZhi2" => "",
  98. "shouJianRenDiZhi3" => "",
  99. "shouJianRenDianHua" => "",//to fill收货电话
  100. "shouJianRenGongSi" => "",
  101. "shouJianRenXingMing" => "",//to fill收货人姓名
  102. "shouJianRenYouBian" => "",//to fill收货人邮编
  103. "skuText" => "null,null,null",
  104. "synPushBill" => false,
  105. "tradeTerms" => "",
  106. "useReferenceApi" => false,
  107. "wuLiuFangShi" => 2,
  108. "zhouMing" => ""//to fill收货人州名
  109. ];
  110. $headers = [
  111. "Content-type: application/json;charset=utf-8",
  112. //'Content-Length: ' . strlen($data_string)),
  113. "Authorization: {$token}",
  114. "Accept: */*"
  115. ];
  116. foreach ($list as $item) {
  117. $params['keHuDanHao'] = $item['order_no'];
  118. $params['maiJiaId'] = $item['user_id'];
  119. $params['zhouMing'] = $item['address']['region']['code'];
  120. $params['shouJianRenChengShi'] = $item['address']['city'];
  121. $params['shouJianRenYouBian'] = $item['address']['zip_code'];
  122. $params['shouJianRenDianHua'] = $item['address']['phone'];
  123. $params['shouJianRenDiZhi1'] = $item['address']['detail'];
  124. $params['shouJianRenXingMing'] = $item['address']['name'] . ' ' . $item['address']['last_name'];
  125. $goodsList = [];
  126. foreach ($item['goods'] as $good) {
  127. $goodsList[] = [
  128. "shenBaoBiZhong" => "USD",
  129. "shenBaoDanJia" => $good['goods_price'],//todo fill 申报单价
  130. "shenBaoHaiGuanBianMa" => "",
  131. "shenBaoJinE" => $good['total_price'],//todo fill 申报金额
  132. "shenBaoPinMing" => "vape",//$good['goods_name'],//todo fill 品名
  133. "shenBaoShuLiang" => $good['total_num'],
  134. "zhongWenPinMing" => "电子烟"//todo fill
  135. ];
  136. }
  137. $params['shenBaoXinXiList'] = $goodsList;
  138. $resJson = curl_post($url, [$params], $headers);
  139. Log::write('itdida::' . $resJson);
  140. $res = json_decode($resJson, true);
  141. // 遍历整理数据
  142. if (isset($res['data']) && $res['data'][0]['code'] == 200) {
  143. $model = \app\store\model\Order::detail($item['order_id']);
  144. if (!$model->delivery(['express_id' => 10001, 'express_no' => $res['data']['zhuanDanHao']])) {
  145. Log::write('发货失败orderId:' . $item['order_id'],'error');
  146. }
  147. }else{
  148. Log::write('itdida运单失败::orderNo:' . $item['order_no'] . $resJson,'error');
  149. }
  150. sleep(1);
  151. }
  152. // 记录日志
  153. Tools::taskLogs('UserGrade', 'setUserGrade', [
  154. 'storeId' => $storeId,
  155. 'data' => $data
  156. ]);
  157. }
  158. /**
  159. * 获取所有会员等级
  160. * @param int $storeId 商城ID
  161. * @return false|\think\Collection
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. */
  166. private function getUsableList(int $storeId)
  167. {
  168. $orderModel = new OrderModel();
  169. return $orderModel->getUsableList($storeId);
  170. }
  171. }