ExpressService.php 2.8 KB

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