ExpressServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\shipping;
  12. use app\dao\shipping\ExpressDao;
  13. use app\services\BaseServices;
  14. use app\services\serve\ServeServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\CacheService;
  17. use crmeb\services\express\Express;
  18. use crmeb\services\FormBuilder as Form;
  19. /**
  20. * 物流数据
  21. * Class ExpressServices
  22. * @package app\services\shipping
  23. * @method save(array $data) 保存数据
  24. * @method get(int $id, ?array $field = []) 获取数据
  25. * @method delete(int $id, ?string $key = null) 删除数据
  26. * @method update($id, array $data, ?string $key = null) 修改数据
  27. */
  28. class ExpressServices extends BaseServices
  29. {
  30. public $_cacheKey = "plat_express_list";
  31. //物流查询物流公司code
  32. public $express_code = [
  33. 'yunda' => 'yunda',
  34. 'yundakuaiyun' => 'yunda56',
  35. 'ems' => 'EMS',
  36. 'youzhengguonei' => 'chinapost',
  37. 'huitongkuaidi' => 'HTKY',
  38. 'baishiwuliu' => 'BSKY',
  39. 'shentong' => 'STO',
  40. 'jd' => 'JD',
  41. 'zhongtong' => 'ZTO',
  42. 'zhongtongkuaiyun' => 'ZTO56',
  43. ];
  44. /**
  45. * 构造方法
  46. * ExpressServices constructor.
  47. * @param ExpressDao $dao
  48. */
  49. public function __construct(ExpressDao $dao)
  50. {
  51. $this->dao = $dao;
  52. }
  53. /**
  54. * 获取物流信息
  55. * @param array $where
  56. * @return array
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function getExpressList(array $where)
  62. {
  63. [$page, $limit] = $this->getPageValue();
  64. $list = $this->dao->getExpressList($where, '*', $page, $limit);
  65. $count = $this->dao->count($where);
  66. return compact('list', 'count');
  67. }
  68. public function apiExpressList()
  69. {
  70. return $this->dao->getExpressList([], '*', 0, 0);
  71. }
  72. /**
  73. * 物流表单
  74. * @param array $formData
  75. * @return mixed
  76. * @throws \FormBuilder\Exception\FormBuilderException
  77. */
  78. public function createExpressForm(array $formData = [])
  79. {
  80. if (isset($formData['partner_id']) && $formData['partner_id'] == 1) $field[] = Form::input('account', '月结账号', $formData['account'] ?? '');
  81. if (isset($formData['partner_key']) && $formData['partner_key'] == 1) $field[] = Form::input('key', '月结密码', $formData['key'] ?? '');
  82. if (isset($formData['net']) && $formData['net'] == 1) $field[] = Form::input('net_name', '取件网点', $formData['net_name'] ?? '')->required();
  83. if (isset($formData['check_man']) && $formData['check_man'] == 1) $field[] = Form::input('courier_name', '承载快递员名', $formData['courier_name'] ?? '')->required();
  84. if (isset($formData['partner_name']) && $formData['partner_name'] == 1) $field[] = Form::input('customer_name', '客户账户名称', $formData['customer_name'] ?? '')->required();
  85. if (isset($formData['is_code']) && $formData['is_code'] == 1) $field[] = Form::input('code_name', '电子面单承载编号', $formData['code_name'] ?? '')->required();
  86. $field[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->precision(0);
  87. $field[] = Form::radio('is_show', '是否启用', $formData['is_show'] ?? 1)->options([['value' => 0, 'label' => '隐藏'], ['value' => 1, 'label' => '启用']]);
  88. return $field;
  89. }
  90. /**
  91. * 创建物流信息表单获取
  92. * @return array
  93. * @throws \FormBuilder\Exception\FormBuilderException
  94. */
  95. public function createForm()
  96. {
  97. return create_form('添加物流公司', $this->createExpressForm(), $this->url('/freight/express'));
  98. }
  99. /**
  100. * 修改物流信息表单获取
  101. * @param int $id
  102. * @return array
  103. * @throws \FormBuilder\Exception\FormBuilderException
  104. */
  105. public function updateForm(int $id)
  106. {
  107. $express = $this->dao->get($id);
  108. if (!$express) {
  109. throw new AdminException(100026);
  110. }
  111. return create_form('编辑物流公司', $this->createExpressForm($express->toArray()), $this->url('/freight/express/' . $id), 'PUT');
  112. }
  113. /**
  114. * 平台获取快递
  115. * @return array|mixed
  116. */
  117. public function getPlatExpress()
  118. {
  119. /** @var ServeServices $expressService */
  120. $expressService = app()->make(ServeServices::class);
  121. /** @var CacheService $cacheService */
  122. $cacheService = app()->make(CacheService::class);
  123. $data = [];
  124. if ($list = $cacheService::get($this->_cacheKey)) {
  125. $data = json_decode($list, true);
  126. } else {
  127. $list = $expressService->express()->express(0, 0, 1000);
  128. if (isset($list['data'])) {
  129. $cacheService->set($this->_cacheKey, json_encode($list['data']), 3600);
  130. $data = $list['data'];
  131. }
  132. }
  133. return $data;
  134. }
  135. /**
  136. * 获取物流信息组合成新的数组返回
  137. * @param array $where
  138. * @return array
  139. */
  140. public function express(array $where = [], string $k = 'id')
  141. {
  142. $list = $this->expressList($where);
  143. $data = [];
  144. if ($list) {
  145. foreach ($list as $k => $v) {
  146. $data[$k]['id'] = $v['id'];
  147. $data[$k]['value'] = $v['name'];
  148. $data[$k]['code'] = $v['code'];
  149. }
  150. }
  151. return $data;
  152. }
  153. /**
  154. * 获取物流信息组合成新的数组返回
  155. * @param array $where
  156. * @return array
  157. */
  158. public function expressSelectForm(array $where = [])
  159. {
  160. $list = $this->expressList();
  161. //$list = $this->dao->getExpress($where, 'name', 'id');
  162. $data = [];
  163. foreach ($list as $key => $value) {
  164. $data[] = ['label' => $value['name'], 'value' => $value['code']];
  165. }
  166. return $data;
  167. }
  168. public function expressList($where = [])
  169. {
  170. if (empty($where)) $where = ['is_show' => 1];
  171. return $this->dao->getExpressList($where, 'id,name,code,partner_id,partner_key,net,account,key,net_name', 0, 0);
  172. }
  173. /**
  174. * 物流公司查询
  175. * @param string $cacheName
  176. * @param string $expressNum
  177. * @param string|null $com
  178. * @return array
  179. */
  180. public function query(string $cacheName, string $expressNum, string $com = null, $phone = '')
  181. {
  182. $resultData = CacheService::get($cacheName, null);
  183. if ($resultData === null || !is_array($resultData)) {
  184. $data = [];
  185. $cacheTime = 0;
  186. switch ((int)sys_config('logistics_type')) {
  187. case 1:
  188. /** @var ServeServices $services */
  189. $services = app()->make(ServeServices::class);
  190. $result = $services->express()->query($expressNum, $com, $phone);
  191. if (isset($result['ischeck']) && $result['ischeck'] == 1) {
  192. $cacheTime = 0;
  193. } else {
  194. $cacheTime = 1800;
  195. }
  196. foreach ($result['content'] ?? [] as $item) {
  197. $data[] = ['time' => $item['time'], 'status' => $item['status']];
  198. }
  199. break;
  200. case 2:
  201. /** @var Express $services */
  202. $services = app()->make(Express::class, ['aliyun_express']);
  203. $result = $services->query($expressNum, '', sys_config('system_express_app_code'));
  204. if (is_array($result) &&
  205. isset($result['result']) &&
  206. isset($result['result']['deliverystatus']) &&
  207. $result['result']['deliverystatus'] >= 3)
  208. $cacheTime = 0;
  209. else
  210. $cacheTime = 1800;
  211. $data = $result['result']['list'] ?? [];
  212. break;
  213. }
  214. CacheService::set($cacheName, $data, $cacheTime);
  215. return $data;
  216. }
  217. return $resultData;
  218. }
  219. /**
  220. * 同步物流公司
  221. * @return bool
  222. */
  223. public function syncExpress()
  224. {
  225. if (CacheService::get('sync_express')) {
  226. return true;
  227. }
  228. $expressList = $this->getPlatExpress();
  229. $data = $data_all = [];
  230. $selfExpress = $this->dao->getExpress([], 'id,code', 'id');
  231. $codes = [];
  232. if ($selfExpress) {
  233. $codes = array_column($selfExpress, 'code');
  234. }
  235. foreach ($expressList as $express) {
  236. if (!in_array($express['code'], $codes)) {
  237. $data['name'] = $express['name'] ?? '';
  238. $data['code'] = $express['code'] ?? '';
  239. $data['partner_id'] = $express['partner_id'] ?? '';
  240. $data['partner_key'] = $express['partner_key'] ?? '';
  241. $data['check_man'] = $express['check_man'] ?? '';
  242. $data['partner_name'] = $express['partner_name'] ?? '';
  243. $data['is_code'] = $express['is_code'] ?? '';
  244. $data['net'] = $express['net'] ?? '';
  245. $data['is_show'] = 0;
  246. $data['status'] = 0;
  247. if ($express['partner_id'] == 0 && $express['partner_key'] == 0 && $express['net'] == 0 && $express['check_man'] == 0 && $express['partner_name'] == 0 && $express['is_code'] == 0) {
  248. $data['status'] = 1;
  249. }
  250. $data_all[] = $data;
  251. }
  252. }
  253. if ($data_all) {
  254. $this->dao->saveAll($data_all);
  255. }
  256. CacheService::set('sync_express', 1, 3600);
  257. return true;
  258. }
  259. /** 查询单个快递公司
  260. * @param array $where
  261. * @return array|\think\Model|null
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\DbException
  264. * @throws \think\db\exception\ModelNotFoundException
  265. */
  266. public function getOneByWhere(array $where)
  267. {
  268. return $this->dao->getOne($where);
  269. }
  270. }