ExpressDetail.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\models\common;
  8. use app\models\Option;
  9. use Cyy\Express\Exceptions\TrackingException;
  10. use Cyy\Express\Trackers\TrackerInterface;
  11. use Cyy\Express\Waybill;
  12. use yii\base\Model;
  13. class ExpressDetail extends Model
  14. {
  15. public $express_no;
  16. public $express;
  17. public $receive_mobile;
  18. public $store_id;
  19. public $status_text = [
  20. 1 => '?',
  21. 2 => '运输中',
  22. 3 => '已签收',
  23. 4 => '问题件',
  24. ];
  25. public function rules()
  26. {
  27. return [
  28. [['express', 'express_no'], 'trim'],
  29. [['express_no', 'express', 'store_id'], 'required'],
  30. [['receive_mobile'], 'safe'],
  31. ];
  32. }
  33. /**
  34. * @return array
  35. * @throws | array
  36. */
  37. public function search()
  38. {
  39. if (!$this->validate()) {
  40. return [
  41. 'code' => 1,
  42. 'msg' => $this->getErrorSummary(false)[0]
  43. ];
  44. }
  45. $express_datail_data = cache()->get('express_detail_' . $this->express_no);
  46. if (!$express_datail_data) {
  47. $express_datail_data = $this->getData();
  48. \Yii::$app->cache->set('express_detail_' . $this->express_no, $express_datail_data, 28800);
  49. }
  50. return $express_datail_data;
  51. }
  52. /**
  53. * @param $name
  54. * @return bool|mixed
  55. */
  56. private function transExpressName($name)
  57. {
  58. if (!$name) {
  59. return false;
  60. }
  61. $append_list = [
  62. '快递',
  63. '快运',
  64. '物流',
  65. '速运',
  66. '速递',
  67. ];
  68. foreach ($append_list as $append) {
  69. $name = str_replace($append, '', $name);
  70. }
  71. $name_map_list = [
  72. '邮政快递包裹' => '邮政',
  73. '邮政包裹信件' => '邮政',
  74. ];
  75. if (isset($name_map_list[$name])) {
  76. $name = $name_map_list[$name];
  77. }
  78. if (!empty($name['邮政包裹'])) {
  79. $name = "邮政";
  80. }
  81. return $name;
  82. }
  83. /**
  84. * @return array
  85. * @throws \yii\base\InvalidConfigException | array
  86. */
  87. private function getData()
  88. {
  89. /**@var array $status_map 定义在 Cyy\Express\Status */
  90. $status_map = [
  91. -1 => '已揽件',
  92. 0 => '已揽件',
  93. 1 => '已发出',
  94. 2 => '在途中',
  95. 3 => '派件中',
  96. 4 => '已签收',
  97. 5 => '已自取',
  98. 6 => '问题件',
  99. 7 => '已退回',
  100. 8 => '已退签',
  101. 9 => '暂无轨迹',
  102. 10 => '单号或快递公司代码错误',
  103. ];
  104. /** @var Waybill $wb */
  105. $wb = \Yii::createObject([
  106. 'class' => 'Cyy\Express\Waybill',
  107. 'id' => $this->express_no,
  108. 'express' => $this->transExpressName($this->express),
  109. ]);
  110. $tracker_class_list = [
  111. // 'Cyy\Express\Trackers\Kuaidi100',
  112. 'app\utils\Express\Trackers\Kuaidiniao',
  113. 'app\utils\Express\Trackers\Alicloud',
  114. // 'Cyy\Express\Trackers\Kuaidiwang',
  115. ];
  116. $str = '';
  117. foreach ($tracker_class_list as $index => $tracker_class) {
  118. $class_args = [
  119. 'class' => $tracker_class,
  120. ];
  121. if ($tracker_class == 'app\utils\Express\Trackers\Kuaidiniao') {
  122. list($EBusinessID, $AppKey, $kdniao_api_free) = $this->getKuaidiniaoConfig();
  123. if(empty($EBusinessID)){
  124. continue;
  125. }
  126. $class_args['EBusinessID'] = $EBusinessID;
  127. $class_args['AppKey'] = $AppKey;
  128. $class_args['freeApi'] = $kdniao_api_free;
  129. $class_args['receive_mobile'] = $this->receive_mobile;
  130. }
  131. if ($tracker_class == 'app\utils\Express\Trackers\Alicloud') {
  132. list($AppCode) = $this->getAlicloudExpressConfig();
  133. if(empty($AppCode)){
  134. continue;
  135. }
  136. $class_args['AppCode'] = $AppCode;
  137. $class_args['receive_mobile'] = $this->receive_mobile;
  138. }
  139. /** @var TrackerInterface $tracker */
  140. $tracker = \Yii::createObject($class_args);
  141. try {
  142. $list = $wb->getTraces($tracker)->toArray();
  143. if (!is_array($list)) {
  144. continue;
  145. }
  146. foreach ($list as &$item) {
  147. $item['datetime'] = $item['time'];
  148. $item['detail'] = $item['desc'];
  149. unset($item['time']);
  150. unset($item['desc']);
  151. }
  152. } catch (TrackingException $ex) {
  153. $str .= ' ' . $ex->getMessage();
  154. continue;
  155. }
  156. if (isset($status_map[$wb->status])) {
  157. $status_text = $status_map[$wb->status];
  158. } else {
  159. continue;
  160. }
  161. return [
  162. 'code' => 0,
  163. 'data' => [
  164. 'list' => $list,
  165. 'status' => $wb->status,
  166. 'status_text' => $status_text,
  167. ],
  168. ];
  169. }
  170. return [
  171. 'code' => 1,
  172. 'msg' => '未查询到物流信息。' . $str,
  173. ];
  174. }
  175. /**
  176. * @return array
  177. */
  178. private function getKuaidiniaoConfig()
  179. {
  180. $kdniao_mch_id = Option::get('kdniao_mch_id', $this->store_id, 'store')['value'];
  181. $kdniao_api_key = Option::get('kdniao_api_key', $this->store_id, 'store')['value'];
  182. $kdniao_api_free = Option::get('kdniao_api_free', $this->store_id, 'store', 1)['value'];
  183. if (!$kdniao_mch_id || !$kdniao_api_key) {
  184. return ['', ''];
  185. }
  186. return [$kdniao_mch_id, $kdniao_api_key, $kdniao_api_free];
  187. }
  188. private function getAlicloudExpressConfig()
  189. {
  190. $app_code = Option::get('ali_express_app_code', $this->store_id, 'store', '')['value'];
  191. $app_code = Option::get('ali_express_app_code', $this->store_id, 'pay', $app_code)['value'];
  192. if (!$app_code) {
  193. return [''];
  194. }
  195. return [$app_code];
  196. }
  197. }