123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\other;
- use app\dao\other\ExpressDao;
- use app\services\BaseServices;
- use app\services\serve\ServeServices;
- use crmeb\exceptions\AdminException;
- use crmeb\services\CacheService;
- use crmeb\services\ExpressService;
- use crmeb\services\FormBuilder as Form;
- /**
- * 物流数据
- * Class ExpressServices
- * @package app\services\other
- * @mixin ExpressDao
- */
- class ExpressServices extends BaseServices
- {
- public $_cacheKey = "plat_express_list";
- //物流查询物流公司code
- public $express_code = [
- 'yunda' => 'yunda',
- 'yundakuaiyun' => 'yunda56',
- 'ems' => 'EMS',
- 'youzhengguonei' => 'chinapost',
- 'huitongkuaidi' => 'HTKY',
- 'baishiwuliu' => 'BSKY',
- 'shentong' => 'STO',
- 'jd' => 'JD',
- 'zhongtong' => 'ZTO',
- 'zhongtongkuaiyun' => 'ZTO56',
- ];
- /**
- * 构造方法
- * ExpressServices constructor.
- * @param ExpressDao $dao
- */
- public function __construct(ExpressDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 获取物流信息
- * @param array $where
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getExpressList(array $where)
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getExpressList($where, '*', $page, $limit);
- $count = $this->dao->count($where);
- return compact('list', 'count');
- }
- public function apiExpressList()
- {
- return $this->dao->getExpressList([], '*', 0, 0);
- }
- /**
- * 物流表单
- * @param array $formData
- * @return mixed
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function createExpressForm(array $formData = [])
- {
- if (isset($formData['partner_id']) && $formData['partner_id'] == 1) $field[] = Form::input('account', '月结账号', $formData['account'] ?? '');
- if (isset($formData['partner_key']) && $formData['partner_key'] == 1) $field[] = Form::input('key', '月结密码', $formData['key'] ?? '');
- if (isset($formData['net']) && $formData['net'] == 1) $field[] = Form::input('net_name', '取件网点', $formData['net_name'] ?? '');
- if (isset($formData['check_man']) && $formData['check_man'] == 1) $field[] = Form::input('courier_name', '承载快递员名', $formData['courier_name'] ?? '')->required();
- if (isset($formData['partner_name']) && $formData['partner_name'] == 1) $field[] = Form::input('customer_name', '客户账户名称', $formData['customer_name'] ?? '')->required();
- if (isset($formData['is_code']) && $formData['is_code'] == 1) $field[] = Form::input('code_name', '电子面单承载编号', $formData['code_name'] ?? '')->required();
- $field[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->min(0);
- $field[] = Form::radio('is_show', '是否启用', $formData['is_show'] ?? 1)->options([['value' => 0, 'label' => '隐藏'], ['value' => 1, 'label' => '启用']]);
- return $field;
- }
- /**
- * 创建物流信息表单获取
- * @return array
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function createForm()
- {
- return create_form('添加物流公司', $this->createExpressForm(), $this->url('/freight/express'));
- }
- /**
- * 修改物流信息表单获取
- * @param int $id
- * @return array
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function updateForm(int $id)
- {
- $express = $this->dao->get($id);
- if (!$express) {
- throw new AdminException('查询数据失败,无法修改');
- }
- return create_form('编辑物流公司', $this->createExpressForm($express->toArray()), $this->url('/freight/express/' . $id), 'PUT');
- }
- /**
- * 平台获取快递
- * @return array|mixed
- */
- public function getPlatExpress()
- {
- /** @var ServeServices $expressService */
- $expressService = app()->make(ServeServices::class);
- /** @var CacheService $cacheService */
- $cacheService = app()->make(CacheService::class);
- $data = [];
- if ($list = $cacheService::get($this->_cacheKey)) {
- $data = json_decode($list, true);
- } else {
- $list = $expressService->express()->express(0, 0, 1000);
- if (isset($list['data'])) {
- $cacheService->set($this->_cacheKey, json_encode($list['data']), 3600);
- $data = $list['data'];
- }
- }
- return $data;
- }
- /**
- * 获取物流信息组合成新的数组返回
- * @param array $where
- * @return array
- */
- public function express(array $where = [], string $k = 'id')
- {
- $list = $this->expressList($where);
- $data = [];
- if ($list) {
- foreach ($list as $k => $v) {
- $data[$k]['id'] = $v['id'];
- $data[$k]['value'] = $v['name'];
- $data[$k]['code'] = $v['code'];
- }
- }
- return $data;
- }
- /**
- * 获取物流信息组合成新的数组返回
- * @param array $where
- * @return array
- */
- public function expressSelectForm(array $where = [])
- {
- $list = $this->expressList();
- //$list = $this->dao->getExpress($where, 'name', 'id');
- $data = [];
- foreach ($list as $key => $value) {
- $data[] = ['label' => $value['name'], 'value' => $value['id']];
- }
- return $data;
- }
- public function expressList($where = [])
- {
- if (empty($where)) $where = ['is_show' => 1];
- return $this->dao->getExpressList($where, 'id,name,code,partner_id,partner_key,net,account,key,net_name', 0, 0);
- }
- /**
- * 物流公司查询
- * @param string $cacheName
- * @param string $expressNum
- * @param string|null $com
- * @return array
- */
- public function query(string $cacheName, string $expressNum, string $com = null)
- {
- $resultData = CacheService::get($cacheName, null);
- if ($resultData === null || !is_array($resultData)) {
- $data = [];
- switch ((int)sys_config('logistics_type')) {
- case 1://一号通
- /** @var ServeServices $services */
- $services = app()->make(ServeServices::class);
- $result = $services->express()->query($expressNum, $com);
- if (isset($result['ischeck']) && $result['ischeck'] == 1) {
- $cacheTime = 0;
- } else {
- $cacheTime = 1800;
- }
- foreach (isset($result['content']) ? $result['content'] : [] as $item) {
- $data[] = ['time' => $item['time'], 'status' => $item['status']];
- }
- break;
- case 2://阿里云
- $result = ExpressService::query($expressNum);
- if (is_array($result) &&
- isset($result['result']) &&
- isset($result['result']['deliverystatus']) &&
- $result['result']['deliverystatus'] >= 3)
- $cacheTime = 0;
- else
- $cacheTime = 1800;
- $data = $result['result']['list'] ?? [];
- break;
- }
- CacheService::set($cacheName, $data, $cacheTime);
- return $data;
- }
- return $resultData;
- }
- /**
- * 同步物流公司
- * @return bool
- */
- public function syncExpress()
- {
- if (CacheService::get('sync_express')) {
- return true;
- }
- $expressList = $this->getPlatExpress();
- $data = $data_all = [];
- $selfExpress = $this->dao->getExpress([], 'id,code,status,account,key,net_name,courier_name,customer_name,code_name', 'id');
- $codes = [];
- if ($selfExpress) {
- $codes = array_column($selfExpress, 'code');
- $selfExpress = array_combine($codes, $selfExpress);
- }
- foreach ($expressList as $express) {
- $data = [];
- $data['partner_id'] = $express['partner_id'] ?? 0;
- $data['partner_key'] = $express['partner_key'] ?? 0;
- $data['net'] = $express['net'] ?? 0;
- $data['check_man'] = $express['check_man'] ?? 0;
- $data['partner_name'] = $express['partner_name'] ?? 0;
- $data['is_code'] = $express['is_code'] ?? 0;
- if (!in_array($express['code'], $codes)) {
- $data['name'] = $express['name'] ?? '';
- $data['code'] = $express['code'] ?? '';
- $data['is_show'] = 1;
- $data['status'] = 0;
- if ($express['partner_id'] == 0 && $express['partner_key'] == 0 && $express['net'] == 0 && $express['check_man'] == 0 && $express['partner_name'] == 0 && $express['is_code'] == 0) {
- $data['status'] = 1;
- }
- $data_all[] = $data;
- } else {
- if (isset($selfExpress[$express['code']]['id']) && $selfExpress[$express['code']]['id']) {
- if (($express['partner_id'] == 1 && !$selfExpress[$express['code']]['account']) || ($express['partner_key'] == 1 && !$selfExpress[$express['code']]['key']) || ($express['net'] == 1 && !$selfExpress[$express['code']]['net_name']) || ($express['check_man'] == 1 && !$selfExpress[$express['code']]['courier_name']) || ($express['partner_name'] == 1 && !$selfExpress[$express['code']]['customer_name']) || ($express['is_code'] == 1 && !$selfExpress[$express['code']]['code_name'])) {
- $data['status'] = 0;
- } else {
- $data['status'] = 1;
- }
- $this->dao->update($selfExpress[$express['code']]['id'], $data, 'id');
- }
- }
- }
- if ($data_all) {
- $this->dao->saveAll($data_all);
- }
- CacheService::set('sync_express', 1, 3600);
- return true;
- }
- /**
- * 查询单个快递公司
- * @param array $where
- * @return array|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getOneByWhere(array $where)
- {
- return $this->dao->getOne($where);
- }
- }
|