ExpressController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/12/30
  8. * Time: 17:24
  9. */
  10. namespace app\frontend\modules\refund\controllers;
  11. use app\common\models\goods\ReturnAddress;
  12. use app\common\models\refund\ResendExpress;
  13. use Illuminate\Support\Facades\Redis;
  14. use app\common\components\ApiController;
  15. use app\common\exceptions\AppException;
  16. use app\frontend\modules\refund\models\RefundApply;
  17. class ExpressController extends ApiController
  18. {
  19. protected $refundApply;
  20. public function userReturn()
  21. {
  22. $this->refundApply = $refundApply = RefundApply::find(request()->input('refund_id'));
  23. if(!$refundApply){
  24. throw new AppException('未找到该售后信息');
  25. }
  26. if(!$refundApply->returnExpress) {
  27. throw new AppException('未找到该售后快递单号');
  28. }
  29. $cacheKey = 'refundExpressId_'.$refundApply->id.'_' . $refundApply->returnExpress->express_sn;
  30. $result = Redis::get($cacheKey);
  31. if ($result) {
  32. $result = json_decode($result, true);
  33. return $this->successJson('用户发货物流信息Cache', $result);
  34. }
  35. switch ($refundApply->returnExpress->way_id) {
  36. case 1:
  37. if(app('plugins')->isEnabled('jd-take-parts')) {
  38. $dispatch = \Yunshop\JdTakeParts\services\RefundService::receiveTraceGet($refundApply);
  39. break;
  40. }
  41. default:
  42. $dispatch = $this->getLogistics($refundApply->returnExpress);
  43. }
  44. $dispatch['thumb'] = $refundApply->returnExpress->images;
  45. $result = $dispatch;
  46. Redis::setex($cacheKey, 120, json_encode($result));
  47. return $this->successJson('用户发货物流信息',$result);
  48. }
  49. public function resendList()
  50. {
  51. $refundApply = RefundApply::find(request()->input('refund_id'));
  52. if(!$refundApply){
  53. throw new AppException('未找到该售后信息');
  54. }
  55. if($refundApply->hasManyResendExpress->isEmpty()) {
  56. throw new AppException('未找到该售后快递');
  57. }
  58. $dispatch = [];
  59. foreach ($refundApply->hasManyResendExpress as $expressModel) {
  60. $express = $this->getLogistics($expressModel);
  61. if(!empty($express['data'])){
  62. $express['last_express_context'] = $express['data'][count($express['data'])-1]['context'];
  63. }else{
  64. $express['last_express_context'] = '';
  65. }
  66. $express['express_id'] = $expressModel->id;
  67. $express['goods'] = $expressModel->pack_goods;
  68. $express['thumb'] = $expressModel->pack_goods?$expressModel->pack_goods[0]['thumb'] : '';
  69. $express['count'] = count($expressModel->pack_goods);
  70. $dispatch[] = $express;
  71. }
  72. $result = [
  73. 'count' => $refundApply->hasManyResendExpress->count(),
  74. 'data' => $dispatch,
  75. ];
  76. return $this->successJson('list', $result);
  77. }
  78. public function resend()
  79. {
  80. $this->refundApply = $refundApply = RefundApply::find(request()->input('refund_id'));
  81. if(!$refundApply){
  82. throw new AppException('未找到该售后信息');
  83. }
  84. /**
  85. * @var ResendExpress $resendExpress
  86. */
  87. $resendExpress = ResendExpress::find(request()->input('express_id'));
  88. if(!$resendExpress) {
  89. throw new AppException('未找到该售后快递');
  90. }
  91. $cacheKey = 'refundExpressId_'.$refundApply->id.'_' . $resendExpress->id;
  92. $result = Redis::get($cacheKey);
  93. if ($result) {
  94. $result = json_decode($result, true);
  95. return $this->successJson('商户发货物流信息Cache', $result);
  96. }
  97. $dispatch = $this->getLogistics($resendExpress);
  98. $dispatch['goods'] = $resendExpress->pack_goods?: [];
  99. $dispatch['thumb'] = $resendExpress->pack_goods?$resendExpress->pack_goods[0]['thumb'] : '';
  100. // foreach ($refundApply->hasManyResendExpress as $resendExpress) {
  101. // $dispatch = $this->getLogistics($resendExpress);
  102. // $dispatch['goods'] = $resendExpress->pack_goods?: [];
  103. // $dispatch['thumb'] = $resendExpress->pack_goods?$resendExpress->pack_goods[0]['goods_thumb'] : '';
  104. // $result[] = $dispatch;
  105. // }
  106. Redis::setex($cacheKey, 120, json_encode($dispatch));
  107. return $this->successJson('商户发货物流信息', $dispatch);
  108. }
  109. protected function getLogistics($expressModel)
  110. {
  111. //测试
  112. // return $this->testData($expressModel);
  113. $phoneLastFour = '';
  114. if ($expressModel->express_code == 'SF') {
  115. $returnAddress = ReturnAddress::find($this->refundApply->refund_address);
  116. if (empty($returnAddress->mobile)) {
  117. throw new AppException('SF查询物流,联系电话不能为空');
  118. }
  119. $phoneLastFour = substr($returnAddress->mobile,-4);
  120. }
  121. $express = (new \app\common\models\order\Express())->getExpress($expressModel->express_code, $expressModel->express_sn, $phoneLastFour);
  122. $dispatch['express_sn'] = $expressModel->express_sn;
  123. $dispatch['company_name'] = $expressModel->express_company_name;
  124. $dispatch['data'] = $express['data'];
  125. $dispatch['thumb'] = '';
  126. $dispatch['tel'] = $express['tel'];
  127. $dispatch['status_name'] = $express['status_name'];
  128. return $dispatch;
  129. }
  130. protected function testData($expressModel)
  131. {
  132. $dispatch['express_sn'] = $expressModel->express_sn;
  133. $dispatch['company_name'] = $expressModel->express_company_name;
  134. $data = [];
  135. for ($i = rand(8,20);$i > 1; $i--) {
  136. $data[] = [
  137. 'context' => '物流信息到达:'.$i,
  138. 'ftime' => '202111231+'.$i,
  139. ];
  140. }
  141. $dispatch['data'] = $data;
  142. $dispatch['thumb'] = '';
  143. $dispatch['tel'] = '95533';
  144. $dispatch['status_name'] = '测试状态:'.rand(0,100);
  145. return $dispatch;
  146. }
  147. }