SupplyService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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', // 申请售后
  44. 'getSkuDetail'=> '/api/Goods/getSkuDetail', // 申请售后
  45. ];
  46. /**
  47. * 构造函数
  48. * @author laravel开发员
  49. * @since 2020/11/11
  50. * ConfigService constructor.
  51. */
  52. public function __construct()
  53. {
  54. $this->model = new GoodsModel();
  55. self::$apiUrl = ConfigService::make()->getConfigByCode('supply_api_url','https://www.douhuomall.com');
  56. self::$supplyMobile = ConfigService::make()->getConfigByCode('supply_mobile','19354894149');
  57. self::$appId = ConfigService::make()->getConfigByCode('supply_app_id','YS1ef14c155555dce4bb');
  58. self::$appSecret = ConfigService::make()->getConfigByCode('supply_app_secret','2cbbf72408cb906ec039105d9ff9b365');
  59. }
  60. /**
  61. * 静态入口
  62. * @return SmsService|static|null
  63. */
  64. public static function make()
  65. {
  66. if (!self::$instance) {
  67. self::$instance = new static();
  68. }
  69. return self::$instance;
  70. }
  71. /**
  72. * 获取授权TOKEN
  73. * @return bool
  74. */
  75. public function getToken()
  76. {
  77. if(empty(self::$appId) || empty(self::$supplyMobile) || empty(self::$appSecret)){
  78. $this->error = 2702;
  79. return false;
  80. }
  81. $cacheKey = "caches:supply:token_".self::$appId.'_'.self::$supplyMobile;
  82. $data = RedisService::get($cacheKey);
  83. $accessToken = isset($data['access_token'])? $data['access_token'] : '';
  84. $expireTime = isset($result['expire_date_time'])? $result['expire_date_time'] : 0;
  85. if(empty($accessToken) || $expireTime <= time()){
  86. $url = self::$apiUrl.self::$apiUrls['getToken'];
  87. $timestamp = time();
  88. $params = [
  89. 'app_id'=> self::$appId,
  90. 'sign'=> $this->makeSignature($timestamp),
  91. 'mobile'=> self::$supplyMobile,
  92. 'timestamp'=> $timestamp,
  93. ];
  94. $result = httpRequest($url, $params, 'post','', 5);
  95. RedisService::set("caches:supply:token_result_".self::$appId, ['url'=>$url,'params'=>$params,'result'=>$result], 600);
  96. $result = isset($result['result'])? $result['result'] : [];
  97. $accessToken = isset($result['access_token'])? $result['access_token'] :'';
  98. $expireTime = isset($result['expire_date_time'])? $result['expire_date_time'] : 0;
  99. if(empty($accessToken) || $expireTime <= time()){
  100. $this->error = 2701;
  101. return false;
  102. }
  103. RedisService::set($cacheKey, $result, 3*3600);
  104. }
  105. self::$accessToken = $accessToken;
  106. return true;
  107. }
  108. /**
  109. * 签名
  110. * @param int $timestamp
  111. * @return string
  112. */
  113. public function makeSignature($timestamp=0)
  114. {
  115. $timestamp = $timestamp>0? $timestamp : time();
  116. $str = self::$appId.self::$supplyMobile.$timestamp.self::$appSecret;
  117. //var_dump($str);
  118. return md5($str);
  119. }
  120. /**
  121. * 获取接口数据
  122. * @param string $apiName 接口名称
  123. * @param array $params 接口参数
  124. * @param string $requestType 请求方式
  125. * @param int $timeout 超时时间/秒
  126. * @return array|false|mixed|string
  127. */
  128. public function getApiData($apiName, $params=[], $requestType='post', $timeout=5)
  129. {
  130. $url = isset(self::$apiUrls[$apiName])?self::$apiUrls[$apiName] : '';
  131. if(empty($url)){
  132. $this->error = 1044;
  133. return false;
  134. }
  135. if(!$this->getToken()){
  136. $this->error = 1046;
  137. return false;
  138. }
  139. $params['access_token'] = self::$accessToken;
  140. $url = self::$apiUrl.$url;
  141. RedisService::set("caches:supply:".self::$appId.'_'.self::$supplyMobile.":{$apiName}_request",['url'=>$url,'params'=> $params], 600);
  142. $result = httpRequest($url,$params,$requestType,'',$timeout);
  143. RedisService::set("caches:supply:".self::$appId.'_'.self::$supplyMobile.":{$apiName}_result",['url'=>$url,'params'=> $params,'result'=>$result], 600);
  144. $errCode = isset($result['error_code'])? $result['error_code'] : '';
  145. $errMsg = isset($result['error_msg'])? $result['error_msg'] : '';
  146. $result = isset($result['result'])? $result['result'] : [];
  147. if($errCode == 0){
  148. return $result;
  149. }else{
  150. $this->error = $errMsg? $errMsg : 1045;
  151. return false;
  152. }
  153. }
  154. }