SupplyService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\GoodsModel;
  13. /**
  14. * 供应链服务管理-服务类
  15. * @author laravel开发员
  16. * @since 2020/11/11
  17. * @package App\Services
  18. */
  19. class SupplyService extends BaseService
  20. {
  21. // 静态对象
  22. protected static $instance = null;
  23. protected static $apiUrl = '';
  24. protected static $appId = '';
  25. protected static $appSecret = '';
  26. protected static $supplyMobile = '';
  27. protected static $accessToken = '';
  28. protected static $apiUrls = [
  29. 'getToken'=> '/api_v2/noAuth/getAccessToken',
  30. 'getGoodsStock'=> '/api_v2/Goods/stock', //
  31. 'getGoodsList'=> '/api_v2/Goods/getGoodsList', // 商品列表
  32. 'getGoodsCategory'=> '/api/category/get_list', // 商品分类
  33. 'getGoodsDetail'=> '/api_v2/Goods/getGoodsDetail', // 商品详情
  34. 'getGoodsSku'=> '/api_v2/Goods/getSkuDetail', // sku
  35. 'getFreight'=> '/api_v2/order/getFreight', // 运费
  36. 'orderSubmit'=> '/api_v2/order/submit', // 下单
  37. 'getOrderDetail'=> '/api_v2/order/getOrderDetail', // 订单详情
  38. 'getOrderTrack'=> '/api_v2/order/track', // 物流信息
  39. 'getAfterCan'=> '/api_v2/After/isCanAfter', // 查询是否可售后
  40. 'getAfterStatus'=> '/api_v2/After/afterStatus', // 查询售后信息
  41. 'applyAfterCancel'=> '/api_v2/After/cancel_apply', // 取消售后
  42. 'applyAfter'=> '/api_v2/After/applyAfterSales', // 申请售后
  43. 'getSkuUpdate'=> '/api/MessagePool/getSkuUpdate', // 更新SKU
  44. 'getSkuDetail'=> '/api/Goods/getSkuDetail', // SKU详情
  45. 'getRegion'=> '/api/regions/get_region_code', // 获取区域
  46. ];
  47. /**
  48. * 构造函数
  49. * @author laravel开发员
  50. * @since 2020/11/11
  51. * ConfigService constructor.
  52. */
  53. public function __construct()
  54. {
  55. $this->model = new GoodsModel();
  56. self::$apiUrl = ConfigService::make()->getConfigByCode('supply_api_url','https://www.douhuomall.com');
  57. self::$supplyMobile = ConfigService::make()->getConfigByCode('supply_mobile','19354894149');
  58. self::$appId = ConfigService::make()->getConfigByCode('supply_app_id','YS1ef14c155555dce4bb');
  59. self::$appSecret = ConfigService::make()->getConfigByCode('supply_app_secret','2cbbf72408cb906ec039105d9ff9b365');
  60. }
  61. /**
  62. * 静态入口
  63. * @return SmsService|static|null
  64. */
  65. public static function make()
  66. {
  67. if (!self::$instance) {
  68. self::$instance = new static();
  69. }
  70. return self::$instance;
  71. }
  72. /**
  73. * 获取授权TOKEN
  74. * @return bool
  75. */
  76. public function getToken()
  77. {
  78. if(empty(self::$appId) || empty(self::$supplyMobile) || empty(self::$appSecret)){
  79. $this->error = 2702;
  80. return false;
  81. }
  82. $cacheKey = "caches:supply:token_".self::$appId.'_'.self::$supplyMobile;
  83. $data = RedisService::get($cacheKey);
  84. $accessToken = isset($data['access_token'])? $data['access_token'] : '';
  85. $expireTime = isset($data['expire_date_time'])? $data['expire_date_time'] : 0;
  86. if(empty($accessToken) || $expireTime <= time()){
  87. $url = self::$apiUrl.self::$apiUrls['getToken'];
  88. $timestamp = time();
  89. $params = [
  90. 'app_id'=> self::$appId,
  91. 'sign'=> $this->makeSignature($timestamp),
  92. 'mobile'=> self::$supplyMobile,
  93. 'timestamp'=> $timestamp,
  94. ];
  95. $result = httpRequest($url, $params, 'post','', 5);
  96. RedisService::set("caches:supply:token_result_".self::$appId, ['url'=>$url,'params'=>$params,'result'=>$result], 600);
  97. $result = isset($result['result'])? $result['result'] : [];
  98. $accessToken = isset($result['access_token'])? $result['access_token'] :'';
  99. $expireTime = isset($result['expire_date_time'])? $result['expire_date_time'] : 0;
  100. if(empty($accessToken) || $expireTime <= time()){
  101. $this->error = 2701;
  102. return false;
  103. }
  104. RedisService::set($cacheKey, $result, 3*3600);
  105. }
  106. self::$accessToken = $accessToken;
  107. return true;
  108. }
  109. /**
  110. * 签名
  111. * @param int $timestamp
  112. * @return string
  113. */
  114. public function makeSignature($timestamp=0)
  115. {
  116. $timestamp = $timestamp>0? $timestamp : time();
  117. $str = self::$appId.self::$supplyMobile.$timestamp.self::$appSecret;
  118. //var_dump($str);
  119. return md5($str);
  120. }
  121. /**
  122. * 获取接口数据
  123. * @param string $apiName 接口名称
  124. * @param array $params 接口参数
  125. * @param string $requestType 请求方式
  126. * @param int $timeout 超时时间/秒
  127. * @return array|false|mixed|string
  128. */
  129. public function getApiData($apiName, $params=[], $requestType='post', $timeout=5)
  130. {
  131. $url = isset(self::$apiUrls[$apiName])?self::$apiUrls[$apiName] : '';
  132. if(empty($url)){
  133. $this->error = 1044;
  134. return false;
  135. }
  136. if(!$this->getToken()){
  137. $this->error = 1046;
  138. return false;
  139. }
  140. $params['access_token'] = self::$accessToken;
  141. $url = self::$apiUrl.$url;
  142. RedisService::set("caches:supply:".self::$appId.'_'.self::$supplyMobile.":{$apiName}_request_".date('His'),['url'=>$url,'params'=> $params], 600);
  143. $result = httpRequest($url,$params,$requestType,'',$timeout);
  144. RedisService::set("caches:supply:".self::$appId.'_'.self::$supplyMobile.":{$apiName}_result_".date('His'),['url'=>$url,'params'=> $params,'result'=>$result], 600);
  145. $errCode = isset($result['error_code'])? $result['error_code'] : '';
  146. $errMsg = isset($result['error_msg'])? $result['error_msg'] : '';
  147. $result = isset($result['result'])? $result['result'] : [];
  148. if($errCode == 0){
  149. return $result? $result : true;
  150. }else{
  151. $this->error = $errCode==2009? '11'.$errCode : ($errMsg? $errMsg : 1045);
  152. return false;
  153. }
  154. }
  155. }