ExpressServices.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\ExpressService;
  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. /**
  32. * 构造方法
  33. * ExpressServices constructor.
  34. * @param ExpressDao $dao
  35. */
  36. public function __construct(ExpressDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 获取物流信息
  42. * @param array $where
  43. * @return array
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public function getExpressList(array $where)
  49. {
  50. [$page, $limit] = $this->getPageValue();
  51. $list = $this->dao->getExpressList($where, '*', $page, $limit);
  52. $count = $this->dao->count($where);
  53. return compact('list', 'count');
  54. }
  55. public function apiExpressList()
  56. {
  57. return $this->dao->getExpressList([], '*', 0, 0);
  58. }
  59. /**
  60. * 物流表单
  61. * @param array $formData
  62. * @return mixed
  63. * @throws \FormBuilder\Exception\FormBuilderException
  64. */
  65. public function createExpressForm(array $formData = [])
  66. {
  67. if (isset($formData['partner_id']) && $formData['partner_id'] == 1) $field[] = Form::input('account', '月结账号', $formData['account'] ?? '');
  68. if (isset($formData['partner_key']) && $formData['partner_key'] == 1) $field[] = Form::input('key', '月结密码', $formData['key'] ?? '');
  69. if (isset($formData['net']) && $formData['net'] == 1) $field[] = Form::input('net_name', '取件网点', $formData['net_name'] ?? '');
  70. $field[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0));
  71. $field[] = Form::radio('is_show', '是否启用', $formData['is_show'] ?? 1)->options([['value' => 0, 'label' => '隐藏'], ['value' => 1, 'label' => '启用']]);
  72. return $field;
  73. }
  74. /**
  75. * 创建物流信息表单获取
  76. * @return array
  77. * @throws \FormBuilder\Exception\FormBuilderException
  78. */
  79. public function createForm()
  80. {
  81. return create_form('添加物流公司', $this->createExpressForm(), $this->url('/freight/express'));
  82. }
  83. /**
  84. * 修改物流信息表单获取
  85. * @param int $id
  86. * @return array
  87. * @throws \FormBuilder\Exception\FormBuilderException
  88. */
  89. public function updateForm(int $id)
  90. {
  91. $express = $this->dao->get($id);
  92. if (!$express) {
  93. throw new AdminException('查询数据失败,无法修改');
  94. }
  95. return create_form('编辑物流公司', $this->createExpressForm($express->toArray()), $this->url('/freight/express/' . $id), 'PUT');
  96. }
  97. /**
  98. * 平台获取快递
  99. * @return array|mixed
  100. */
  101. public function getPlatExpress()
  102. {
  103. /** @var ServeServices $expressService */
  104. $expressService = app()->make(ServeServices::class);
  105. /** @var CacheService $cacheService */
  106. $cacheService = app()->make(CacheService::class);
  107. $data = [];
  108. if ($list = $cacheService::get($this->_cacheKey)) {
  109. $data = json_decode($list, true);
  110. } else {
  111. $list = $expressService->express()->express(0, 0, 1000);
  112. if (isset($list['data'])) {
  113. $cacheService->set($this->_cacheKey, json_encode($list['data']), 3600);
  114. $data = $list['data'];
  115. }
  116. }
  117. return $data;
  118. }
  119. /**
  120. * 获取物流信息组合成新的数组返回
  121. * @param array $where
  122. * @return array
  123. */
  124. public function express(array $where = [], string $k = 'id')
  125. {
  126. $list = $this->expressList($where);
  127. $data = [];
  128. if ($list) {
  129. foreach ($list as $k => $v) {
  130. $data[$k]['id'] = $v['id'];
  131. $data[$k]['value'] = $v['name'];
  132. $data[$k]['code'] = $v['code'];
  133. }
  134. }
  135. return $data;
  136. }
  137. /**
  138. * 获取物流信息组合成新的数组返回
  139. * @param array $where
  140. * @return array
  141. */
  142. public function expressSelectForm(array $where = [])
  143. {
  144. $list = $this->expressList();
  145. //$list = $this->dao->getExpress($where, 'name', 'id');
  146. $data = [];
  147. foreach ($list as $key => $value) {
  148. $data[] = ['label' => $value['name'], 'value' => $value['id']];
  149. }
  150. return $data;
  151. }
  152. public function expressList($where = [])
  153. {
  154. if(empty($where)) $where = ['is_show' => 1];
  155. return $this->dao->getExpressList($where, 'id,name,code,partner_id,partner_key,net,account,key,net_name', 0, 0);
  156. }
  157. /**
  158. * 物流公司查询
  159. * @param string $cacheName
  160. * @param string $expressNum
  161. * @param string|null $com
  162. * @return array
  163. */
  164. public function query(string $cacheName, string $expressNum, string $com = null)
  165. {
  166. $resultData = CacheService::get($cacheName, null);
  167. if ($resultData === null || !is_array($resultData)) {
  168. $data = [];
  169. $cacheTime = 0;
  170. switch ((int)sys_config('logistics_type')) {
  171. case 1:
  172. /** @var ServeServices $services */
  173. $services = app()->make(ServeServices::class);
  174. $result = $services->express()->query($expressNum, $com);
  175. if (isset($result['ischeck']) && $result['ischeck'] == 1) {
  176. $cacheTime = 0;
  177. } else {
  178. $cacheTime = 1800;
  179. }
  180. foreach (isset($result['content']) ? $result['content'] : [] as $item) {
  181. $data[] = ['time' => $item['time'], 'status' => $item['status']];
  182. }
  183. break;
  184. case 2:
  185. $result = ExpressService::query($expressNum);
  186. if (is_array($result) &&
  187. isset($result['result']) &&
  188. isset($result['result']['deliverystatus']) &&
  189. $result['result']['deliverystatus'] >= 3)
  190. $cacheTime = 0;
  191. else
  192. $cacheTime = 1800;
  193. $data = $result['result']['list'] ?? [];
  194. break;
  195. }
  196. CacheService::set($cacheName, $data, $cacheTime);
  197. return $data;
  198. }
  199. return $resultData;
  200. }
  201. /**
  202. * 同步物流公司
  203. * @return bool
  204. */
  205. public function syncExpress()
  206. {
  207. if (CacheService::get('sync_express')) {
  208. return true;
  209. }
  210. $expressList = $this->getPlatExpress();
  211. $data = $data_all = [];
  212. $selfExpress = $this->dao->getExpress([], 'id,code', 'id');
  213. $codes = [];
  214. if ($selfExpress) {
  215. $codes = array_column($selfExpress, 'code');
  216. }
  217. foreach ($expressList as $express) {
  218. if (!in_array($express['code'], $codes)) {
  219. $data['name'] = $express['name'] ?? '';
  220. $data['code'] = $express['code'] ?? '';
  221. $data['partner_id'] = $express['partner_id'] ?? '';
  222. $data['partner_key'] = $express['partner_key'] ?? '';
  223. $data['net'] = $express['net'] ?? '';
  224. $data['is_show'] = 1;
  225. $data['status'] = 0;
  226. if ($express['partner_id'] == 0 && $express['partner_key'] == 0 && $express['net'] == 0) {
  227. $data['status'] = 1;
  228. }
  229. $data_all[] = $data;
  230. }
  231. }
  232. if ($data_all) {
  233. $this->dao->saveAll($data_all);
  234. }
  235. CacheService::set('sync_express', 1, 3600);
  236. return true;
  237. }
  238. /** 查询单个快递公司
  239. * @param array $where
  240. * @return array|\think\Model|null
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. */
  245. public function getOneByWhere(array $where)
  246. {
  247. return $this->dao->getOne($where);
  248. }
  249. }