ExpressService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\model\store\order\StoreOrder;
  13. use app\common\repositories\store\shipping\ExpressRepository;
  14. use think\exception\ValidateException;
  15. use think\facade\Cache;
  16. use think\facade\Config;
  17. class ExpressService
  18. {
  19. const API = 'https://wuliu.market.alicloudapi.com/kdi';
  20. public static function query($no, $type = '',$phone = '')
  21. {
  22. $express = systemConfig('crmeb_serve_express') ?:2;
  23. if($express == 2){
  24. //一号通
  25. return self::serve($no, $type,$phone);
  26. } else {
  27. //阿里云
  28. return self::ali($no, $type);
  29. }
  30. }
  31. /**
  32. * 一号通查询
  33. * @param $no
  34. * @param $type
  35. * @return array
  36. * @author Qinii
  37. * @day 8/28/21
  38. */
  39. public static function serve($no,$type,$phone)
  40. {
  41. $res = app()->make(CrmebServeServices::class)->express()->query($no,'',$phone);
  42. if (!$res) $res = app()->make(CrmebServeServices::class)->express()->query($no,$type,$phone);
  43. $cacheTime = 1800;
  44. if(!empty($res)){
  45. if($res['status'] == 3){
  46. $cacheTime = 0;
  47. }
  48. }
  49. $list = $res['content'] ?? [];
  50. return compact('cacheTime','list');
  51. }
  52. /**
  53. * 阿里云查询
  54. * @param $no
  55. * @param $re
  56. * @return array|bool
  57. * @author Qinii
  58. * @day 8/28/21
  59. */
  60. public static function ali($no, $re)
  61. {
  62. //阿里云
  63. $appCode = systemConfig('express_app_code');
  64. if (!$appCode) return false;
  65. $type = '';
  66. $res = HttpService::getRequest(self::API, compact('no', 'type'), ['Authorization:APPCODE ' . $appCode]);
  67. if (!$res) throw new ValidateException('未查询到快递信息,请确认单号及余额是否充足');
  68. $result = json_decode($res, true) ?: null;
  69. if(!is_null($result) && $result['status'] != 200){
  70. if (in_array($result['status'],[201,203,204,207,205])){
  71. throw new ValidateException($result['msg']);
  72. }
  73. }
  74. $cacheTime = 1800;
  75. if (is_array($result) && isset($result['result']) && isset($result['result']['deliverystatus']) && $result['result']['deliverystatus'] >= 3){
  76. $cacheTime = 0;
  77. }
  78. $list = $result['result']['list'] ?? [];
  79. return compact('cacheTime','list');
  80. }
  81. /**
  82. *
  83. * @param $sn 快递号
  84. * @param $name 快递公司
  85. * @param $phone 收货人手机号
  86. * @return array|bool|mixed
  87. * @author Qinii
  88. * @day 8/16/21
  89. */
  90. public static function express($sn,$name,$phone)
  91. {
  92. $com = app()->make(ExpressRepository::class)->getSearch(['name' => $name])->value('code');
  93. $key = 'express_' . $com.'_'.$sn;
  94. $has = Cache::has($key);
  95. if ($has) {
  96. $result = Cache::get($key);
  97. } else {
  98. $suffix = '';
  99. $is_shunfeng = strtoupper(substr($sn,0,2));
  100. if ($is_shunfeng == 'SF') {
  101. $suffix = ':'.substr($phone,7);
  102. }
  103. if ($name == '中通快递') {
  104. $suffix = ':'.substr($phone,7);
  105. }
  106. $result = self::query($sn.$suffix, $com, $phone);
  107. if(!empty($result['list'])){
  108. Cache::set($key, $result['list'], $result['cacheTime']);
  109. $result = $result['list'];
  110. }
  111. }
  112. return $result ?? [];
  113. }
  114. }