ExpressService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace ln\services;
  3. use app\common\repositories\store\shipping\ExpressRepository;
  4. use think\facade\Cache;
  5. class ExpressService
  6. {
  7. const API = 'https://wuliu.market.alicloudapi.com/kdi';
  8. public static function query($no, $type = '')
  9. {
  10. $express = systemConfig('crmeb_serve_express');
  11. $cacheTime = 0;
  12. if($express == 2){
  13. $espressData = app()->make(ExpressRepository::class)->getWhere(['name' => $type]);
  14. //一号通
  15. $res = app()->make(CrmebServeServices::class)->express()->query($no,$espressData->code);
  16. if(!empty($res)){
  17. if($res['status'] == 3){
  18. $cacheTime = 1800;
  19. }else{
  20. $cacheTime = 0;
  21. }
  22. }
  23. $list = $res['content'] ?? [];
  24. } else {
  25. //阿里云
  26. $appCode = systemConfig('express_app_code');
  27. if (!$appCode) return false;
  28. $type = '';
  29. $res = HttpService::getRequest(self::API, compact('no', 'type'), ['Authorization:APPCODE ' . $appCode]);
  30. $result = json_decode($res, true) ?: null;
  31. if($result['status'] != 200){
  32. if (
  33. is_array($result) &&
  34. isset($result['result']) &&
  35. isset($result['result']['deliverystatus']) &&
  36. $result['result']['deliverystatus'] >= 3
  37. ){
  38. $cacheTime = 0;
  39. } else {
  40. $cacheTime = 1800;
  41. }
  42. }
  43. $list = $result['result']['list'] ?? [];
  44. }
  45. return compact('cacheTime','list');
  46. }
  47. public static function express($delivery_id,$com)
  48. {
  49. if (Cache::has('express_' . $delivery_id)) {
  50. $result = Cache::get('express_' . $delivery_id);
  51. } else {
  52. $result = self::query($delivery_id,$com);
  53. if(!empty($result)){
  54. Cache::set('express_' . $delivery_id, $result['list'], $result['cacheTime']);
  55. $result = $result['list'];
  56. }
  57. }
  58. return $result ?? [];
  59. }
  60. }