ExpressServices.php 9.1 KB

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