'?', 2 => '运输中', 3 => '已签收', 4 => '问题件', ]; public function rules() { return [ [['express', 'express_no'], 'trim'], [['express_no', 'express', 'store_id'], 'required'], [['receive_mobile'], 'safe'], ]; } /** * @return array * @throws | array */ public function search() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } $express_datail_data = cache()->get('express_detail_' . $this->express_no); if (!$express_datail_data) { $express_datail_data = $this->getData(); \Yii::$app->cache->set('express_detail_' . $this->express_no, $express_datail_data, 28800); } return $express_datail_data; } /** * @param $name * @return bool|mixed */ private function transExpressName($name) { if (!$name) { return false; } $append_list = [ '快递', '快运', '物流', '速运', '速递', ]; foreach ($append_list as $append) { $name = str_replace($append, '', $name); } $name_map_list = [ '邮政快递包裹' => '邮政', '邮政包裹信件' => '邮政', ]; if (isset($name_map_list[$name])) { $name = $name_map_list[$name]; } if (!empty($name['邮政包裹'])) { $name = "邮政"; } return $name; } /** * @return array * @throws \yii\base\InvalidConfigException | array */ private function getData() { /**@var array $status_map 定义在 Cyy\Express\Status */ $status_map = [ -1 => '已揽件', 0 => '已揽件', 1 => '已发出', 2 => '在途中', 3 => '派件中', 4 => '已签收', 5 => '已自取', 6 => '问题件', 7 => '已退回', 8 => '已退签', 9 => '暂无轨迹', 10 => '单号或快递公司代码错误', ]; /** @var Waybill $wb */ $wb = \Yii::createObject([ 'class' => 'Cyy\Express\Waybill', 'id' => $this->express_no, 'express' => $this->transExpressName($this->express), ]); $tracker_class_list = [ // 'Cyy\Express\Trackers\Kuaidi100', 'app\utils\Express\Trackers\Kuaidiniao', 'app\utils\Express\Trackers\Alicloud', // 'Cyy\Express\Trackers\Kuaidiwang', ]; $str = ''; foreach ($tracker_class_list as $index => $tracker_class) { $class_args = [ 'class' => $tracker_class, ]; if ($tracker_class == 'app\utils\Express\Trackers\Kuaidiniao') { list($EBusinessID, $AppKey, $kdniao_api_free) = $this->getKuaidiniaoConfig(); if(empty($EBusinessID)){ continue; } $class_args['EBusinessID'] = $EBusinessID; $class_args['AppKey'] = $AppKey; $class_args['freeApi'] = $kdniao_api_free; $class_args['receive_mobile'] = $this->receive_mobile; } if ($tracker_class == 'app\utils\Express\Trackers\Alicloud') { list($AppCode) = $this->getAlicloudExpressConfig(); if(empty($AppCode)){ continue; } $class_args['AppCode'] = $AppCode; $class_args['receive_mobile'] = $this->receive_mobile; } /** @var TrackerInterface $tracker */ $tracker = \Yii::createObject($class_args); try { $list = $wb->getTraces($tracker)->toArray(); if (!is_array($list)) { continue; } foreach ($list as &$item) { $item['datetime'] = $item['time']; $item['detail'] = $item['desc']; unset($item['time']); unset($item['desc']); } } catch (TrackingException $ex) { $str .= ' ' . $ex->getMessage(); continue; } if (isset($status_map[$wb->status])) { $status_text = $status_map[$wb->status]; } else { continue; } return [ 'code' => 0, 'data' => [ 'list' => $list, 'status' => $wb->status, 'status_text' => $status_text, ], ]; } return [ 'code' => 1, 'msg' => '未查询到物流信息。' . $str, ]; } /** * @return array */ private function getKuaidiniaoConfig() { $kdniao_mch_id = Option::get('kdniao_mch_id', $this->store_id, 'store')['value']; $kdniao_api_key = Option::get('kdniao_api_key', $this->store_id, 'store')['value']; $kdniao_api_free = Option::get('kdniao_api_free', $this->store_id, 'store', 1)['value']; if (!$kdniao_mch_id || !$kdniao_api_key) { return ['', '']; } return [$kdniao_mch_id, $kdniao_api_key, $kdniao_api_free]; } private function getAlicloudExpressConfig() { $app_code = Option::get('ali_express_app_code', $this->store_id, 'store', '')['value']; $app_code = Option::get('ali_express_app_code', $this->store_id, 'pay', $app_code)['value']; if (!$app_code) { return ['']; } return [$app_code]; } }