ExpressServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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\other;
  12. use app\dao\other\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\other
  23. * @mixin ExpressDao
  24. */
  25. class ExpressServices extends BaseServices
  26. {
  27. public $_cacheKey = "plat_express_list";
  28. //物流查询物流公司code
  29. public $express_code = [
  30. 'yunda' => 'yunda',
  31. 'yundakuaiyun' => 'yunda56',
  32. 'ems' => 'EMS',
  33. 'youzhengguonei' => 'chinapost',
  34. 'huitongkuaidi' => 'HTKY',
  35. 'baishiwuliu' => 'BSKY',
  36. 'shentong' => 'STO',
  37. 'jd' => 'JD',
  38. 'zhongtong' => 'ZTO',
  39. 'zhongtongkuaiyun' => 'ZTO56',
  40. ];
  41. /**
  42. * 构造方法
  43. * ExpressServices constructor.
  44. * @param ExpressDao $dao
  45. */
  46. public function __construct(ExpressDao $dao)
  47. {
  48. $this->dao = $dao;
  49. }
  50. /**
  51. * 获取物流信息
  52. * @param array $where
  53. * @return array
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function getExpressList(array $where)
  59. {
  60. [$page, $limit] = $this->getPageValue();
  61. $list = $this->dao->getExpressList($where, '*', $page, $limit);
  62. $count = $this->dao->count($where);
  63. return compact('list', 'count');
  64. }
  65. public function apiExpressList()
  66. {
  67. return $this->dao->getExpressList([], '*', 0, 0);
  68. }
  69. /**
  70. * 物流表单
  71. * @param array $formData
  72. * @return mixed
  73. * @throws \FormBuilder\Exception\FormBuilderException
  74. */
  75. public function createExpressForm(array $formData = [])
  76. {
  77. if (isset($formData['partner_id']) && $formData['partner_id'] == 1) $field[] = Form::input('account', '月结账号', $formData['account'] ?? '');
  78. if (isset($formData['partner_key']) && $formData['partner_key'] == 1) $field[] = Form::input('key', '月结密码', $formData['key'] ?? '');
  79. if (isset($formData['net']) && $formData['net'] == 1) $field[] = Form::input('net_name', '取件网点', $formData['net_name'] ?? '');
  80. if (isset($formData['check_man']) && $formData['check_man'] == 1) $field[] = Form::input('courier_name', '承载快递员名', $formData['courier_name'] ?? '')->required();
  81. if (isset($formData['partner_name']) && $formData['partner_name'] == 1) $field[] = Form::input('customer_name', '客户账户名称', $formData['customer_name'] ?? '')->required();
  82. if (isset($formData['is_code']) && $formData['is_code'] == 1) $field[] = Form::input('code_name', '电子面单承载编号', $formData['code_name'] ?? '')->required();
  83. $field[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->min(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('查询数据失败,无法修改');
  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['id']];
  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. switch ((int)sys_config('logistics_type')) {
  183. case 1://一号通
  184. /** @var ServeServices $services */
  185. $services = app()->make(ServeServices::class);
  186. $result = $services->express()->query($expressNum, $com);
  187. if (isset($result['ischeck']) && $result['ischeck'] == 1) {
  188. $cacheTime = 0;
  189. } else {
  190. $cacheTime = 1800;
  191. }
  192. foreach (isset($result['content']) ? $result['content'] : [] as $item) {
  193. $data[] = ['time' => $item['time'], 'status' => $item['status']];
  194. }
  195. break;
  196. case 2://阿里云
  197. $result = ExpressService::query($expressNum);
  198. if (is_array($result) &&
  199. isset($result['result']) &&
  200. isset($result['result']['deliverystatus']) &&
  201. $result['result']['deliverystatus'] >= 3)
  202. $cacheTime = 0;
  203. else
  204. $cacheTime = 1800;
  205. $data = $result['result']['list'] ?? [];
  206. break;
  207. }
  208. CacheService::set($cacheName, $data, $cacheTime);
  209. return $data;
  210. }
  211. return $resultData;
  212. }
  213. /**
  214. * 同步物流公司
  215. * @return bool
  216. */
  217. public function syncExpress()
  218. {
  219. if (CacheService::get('sync_express')) {
  220. return true;
  221. }
  222. $expressList = $this->getPlatExpress();
  223. $data = $data_all = [];
  224. $selfExpress = $this->dao->getExpress([], 'id,code,status,account,key,net_name,courier_name,customer_name,code_name', 'id');
  225. $codes = [];
  226. if ($selfExpress) {
  227. $codes = array_column($selfExpress, 'code');
  228. $selfExpress = array_combine($codes, $selfExpress);
  229. }
  230. foreach ($expressList as $express) {
  231. $data = [];
  232. $data['partner_id'] = $express['partner_id'] ?? 0;
  233. $data['partner_key'] = $express['partner_key'] ?? 0;
  234. $data['net'] = $express['net'] ?? 0;
  235. $data['check_man'] = $express['check_man'] ?? 0;
  236. $data['partner_name'] = $express['partner_name'] ?? 0;
  237. $data['is_code'] = $express['is_code'] ?? 0;
  238. if (!in_array($express['code'], $codes)) {
  239. $data['name'] = $express['name'] ?? '';
  240. $data['code'] = $express['code'] ?? '';
  241. $data['is_show'] = 1;
  242. $data['status'] = 0;
  243. if ($express['partner_id'] == 0 && $express['partner_key'] == 0 && $express['net'] == 0 && $express['check_man'] == 0 && $express['partner_name'] == 0 && $express['is_code'] == 0) {
  244. $data['status'] = 1;
  245. }
  246. $data_all[] = $data;
  247. } else {
  248. if (isset($selfExpress[$express['code']]['id']) && $selfExpress[$express['code']]['id']) {
  249. 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'])) {
  250. $data['status'] = 0;
  251. } else {
  252. $data['status'] = 1;
  253. }
  254. $this->dao->update($selfExpress[$express['code']]['id'], $data, 'id');
  255. }
  256. }
  257. }
  258. if ($data_all) {
  259. $this->dao->saveAll($data_all);
  260. }
  261. CacheService::set('sync_express', 1, 3600);
  262. return true;
  263. }
  264. /**
  265. * 查询单个快递公司
  266. * @param array $where
  267. * @return array|\think\Model|null
  268. * @throws \think\db\exception\DataNotFoundException
  269. * @throws \think\db\exception\DbException
  270. * @throws \think\db\exception\ModelNotFoundException
  271. */
  272. public function getOneByWhere(array $where)
  273. {
  274. return $this->dao->getOne($where);
  275. }
  276. }