ExpressServices.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\storage\AliyunExpress;
  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'] ?? '');
  83. $field[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->precision(0);
  84. $field[] = Form::radio('is_show', '是否启用', $formData['is_show'] ?? 1)->options([['value' => 0, 'label' => '隐藏'], ['value' => 1, 'label' => '启用']]);
  85. return $field;
  86. }
  87. /**
  88. * 创建物流信息表单获取
  89. * @return array
  90. * @throws \FormBuilder\Exception\FormBuilderException
  91. */
  92. public function createForm()
  93. {
  94. return create_form('添加物流公司', $this->createExpressForm(), $this->url('/freight/express'));
  95. }
  96. /**
  97. * 修改物流信息表单获取
  98. * @param int $id
  99. * @return array
  100. * @throws \FormBuilder\Exception\FormBuilderException
  101. */
  102. public function updateForm(int $id)
  103. {
  104. $express = $this->dao->get($id);
  105. if (!$express) {
  106. throw new AdminException(100026);
  107. }
  108. return create_form('编辑物流公司', $this->createExpressForm($express->toArray()), $this->url('/freight/express/' . $id), 'PUT');
  109. }
  110. /**
  111. * 平台获取快递
  112. * @return array|mixed
  113. */
  114. public function getPlatExpress()
  115. {
  116. /** @var ServeServices $expressService */
  117. $expressService = app()->make(ServeServices::class);
  118. /** @var CacheService $cacheService */
  119. $cacheService = app()->make(CacheService::class);
  120. $data = [];
  121. if ($list = $cacheService::get($this->_cacheKey)) {
  122. $data = json_decode($list, true);
  123. } else {
  124. $list = $expressService->express()->express(0, 0, 1000);
  125. if (isset($list['data'])) {
  126. $cacheService->set($this->_cacheKey, json_encode($list['data']), 3600);
  127. $data = $list['data'];
  128. }
  129. }
  130. return $data;
  131. }
  132. /**
  133. * 获取物流信息组合成新的数组返回
  134. * @param array $where
  135. * @return array
  136. */
  137. public function express(array $where = [], string $k = 'id')
  138. {
  139. $list = $this->expressList($where);
  140. $data = [];
  141. if ($list) {
  142. foreach ($list as $k => $v) {
  143. $data[$k]['id'] = $v['id'];
  144. $data[$k]['value'] = $v['name'];
  145. $data[$k]['code'] = $v['code'];
  146. }
  147. }
  148. return $data;
  149. }
  150. /**
  151. * 获取物流信息组合成新的数组返回
  152. * @param array $where
  153. * @return array
  154. */
  155. public function expressSelectForm(array $where = [])
  156. {
  157. $list = $this->expressList();
  158. //$list = $this->dao->getExpress($where, 'name', 'id');
  159. $data = [];
  160. foreach ($list as $key => $value) {
  161. $data[] = ['label' => $value['name'], 'value' => $value['code']];
  162. }
  163. return $data;
  164. }
  165. public function expressList($where = [])
  166. {
  167. if (empty($where)) $where = ['is_show' => 1];
  168. return $this->dao->getExpressList($where, 'id,name,code,partner_id,partner_key,net,account,key,net_name', 0, 0);
  169. }
  170. /**
  171. * 物流公司查询
  172. * @param string $cacheName
  173. * @param string $expressNum
  174. * @param string|null $com
  175. * @return array
  176. */
  177. public function query(string $cacheName, string $expressNum, string $com = null)
  178. {
  179. $resultData = CacheService::get($cacheName, null);
  180. if ($resultData === null || !is_array($resultData)) {
  181. $data = [];
  182. $cacheTime = 0;
  183. switch ((int)sys_config('logistics_type')) {
  184. case 1:
  185. /** @var ServeServices $services */
  186. $services = app()->make(ServeServices::class);
  187. $result = $services->express()->query($expressNum, $com);
  188. if (isset($result['ischeck']) && $result['ischeck'] == 1) {
  189. $cacheTime = 0;
  190. } else {
  191. $cacheTime = 1800;
  192. }
  193. foreach ($result['content'] ?? [] as $item) {
  194. $data[] = ['time' => $item['time'], 'status' => $item['status']];
  195. }
  196. break;
  197. case 2:
  198. /** @var AliyunExpress $services */
  199. $services = app()->make(AliyunExpress::class);
  200. $result = $services->query($expressNum, $com);
  201. if (is_array($result) &&
  202. isset($result['result']) &&
  203. isset($result['result']['deliverystatus']) &&
  204. $result['result']['deliverystatus'] >= 3)
  205. $cacheTime = 0;
  206. else
  207. $cacheTime = 1800;
  208. $data = $result['result']['list'] ?? [];
  209. break;
  210. }
  211. CacheService::set($cacheName, $data, $cacheTime);
  212. return $data;
  213. }
  214. return $resultData;
  215. }
  216. /**
  217. * 同步物流公司
  218. * @return bool
  219. */
  220. public function syncExpress()
  221. {
  222. if (CacheService::get('sync_express')) {
  223. return true;
  224. }
  225. $expressList = $this->getPlatExpress();
  226. $data = $data_all = [];
  227. $selfExpress = $this->dao->getExpress([], 'id,code', 'id');
  228. $codes = [];
  229. if ($selfExpress) {
  230. $codes = array_column($selfExpress, 'code');
  231. }
  232. foreach ($expressList as $express) {
  233. if (!in_array($express['code'], $codes)) {
  234. $data['name'] = $express['name'] ?? '';
  235. $data['code'] = $express['code'] ?? '';
  236. $data['partner_id'] = $express['partner_id'] ?? '';
  237. $data['partner_key'] = $express['partner_key'] ?? '';
  238. $data['net'] = $express['net'] ?? '';
  239. $data['is_show'] = 1;
  240. $data['status'] = 0;
  241. if ($express['partner_id'] == 0 && $express['partner_key'] == 0 && $express['net'] == 0) {
  242. $data['status'] = 1;
  243. }
  244. $data_all[] = $data;
  245. }
  246. }
  247. if ($data_all) {
  248. $this->dao->saveAll($data_all);
  249. }
  250. CacheService::set('sync_express', 1, 3600);
  251. return true;
  252. }
  253. /** 查询单个快递公司
  254. * @param array $where
  255. * @return array|\think\Model|null
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\DbException
  258. * @throws \think\db\exception\ModelNotFoundException
  259. */
  260. public function getOneByWhere(array $where)
  261. {
  262. return $this->dao->getOne($where);
  263. }
  264. }