SupplyService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. 'getGoodsDetail'=> '/api_v2/Goods/getGoodsDetail', // 商品详情
  33. 'getGoodsSku'=> '/api_v2/Goods/getSkuDetail', // sku
  34. 'getFreight'=> '/api_v2/order/getFreight', // 运费
  35. 'orderSubmit'=> '/api_v2/order/submit', // 下单
  36. 'getOrderDetail'=> '/api_v2/order/getOrderDetail', // 订单详情
  37. 'getOrderTrack'=> '/api_v2/order/track', // 物流信息
  38. 'getAfterCan'=> '/api_v2/After/isCanAfter', // 查询是否可售后
  39. 'getAfterStatus'=> '/api_v2/After/afterStatus', // 查询售后信息
  40. 'applyAfterCancel'=> '/api_v2/After/cancel_apply', // 取消售后
  41. 'applyAfter'=> '/api_v2/After/applyAfterSales', // 申请售后
  42. ];
  43. /**
  44. * 构造函数
  45. * @author laravel开发员
  46. * @since 2020/11/11
  47. * ConfigService constructor.
  48. */
  49. public function __construct()
  50. {
  51. $this->model = new GoodsModel();
  52. self::$apiUrl = ConfigService::make()->getConfigByCode('supply_api_url','https://www.douhuomall.com');
  53. self::$supplyMobile = ConfigService::make()->getConfigByCode('supply_mobile','19354894149');
  54. self::$appId = ConfigService::make()->getConfigByCode('supply_app_id','YS1ef14c155555dce4bb');
  55. self::$appSecret = ConfigService::make()->getConfigByCode('supply_app_secret','2cbbf72408cb906ec039105d9ff9b365');
  56. }
  57. /**
  58. * 静态入口
  59. * @return SmsService|static|null
  60. */
  61. public static function make()
  62. {
  63. if (!self::$instance) {
  64. self::$instance = new static();
  65. }
  66. return self::$instance;
  67. }
  68. /**
  69. * 获取授权TOKEN
  70. * @return bool
  71. */
  72. public function getToken()
  73. {
  74. if(empty(self::$appId) || empty(self::$supplyMobile) || empty(self::$appSecret)){
  75. $this->error = 2702;
  76. return false;
  77. }
  78. $cacheKey = "caches:supply:token_".self::$appId.'_'.self::$supplyMobile;
  79. $data = RedisService::get($cacheKey);
  80. $accessToken = isset($data['access_token'])? $data['access_token'] : '';
  81. $expireAt = isset($result['expire_time'])? $result['expire_time'] :'';
  82. if(empty($accessToken) || $expireAt <= date('Y-m-d H:i:s')){
  83. $url = self::$apiUrl.self::$apiUrls['getToken'];
  84. $timestamp = time();
  85. $params = [
  86. 'app_id'=> self::$appId,
  87. 'sign'=> $this->makeSignature($timestamp),
  88. 'mobile'=> self::$supplyMobile,
  89. 'timestamp'=> $timestamp,
  90. ];
  91. $result = httpRequest($url, $params, 'post','', 5);
  92. RedisService::set("caches:supply:token_result_".self::$appId, ['url'=>$url,'params'=>$params,'result'=>$result], 600);
  93. $result = isset($result['result'])? $result['result'] : [];
  94. $accessToken = isset($result['access_token'])? $result['access_token'] :'';
  95. $expireAt = isset($result['expire_time'])? $result['expire_time'] :'';
  96. if(empty($accessToken) || $expireAt <= date('Y-m-d H:i:s')){
  97. $this->error = 2701;
  98. return false;
  99. }
  100. RedisService::set($cacheKey, $result, 3600);
  101. }
  102. self::$accessToken = $accessToken;
  103. return true;
  104. }
  105. /**
  106. * 签名
  107. * @param int $timestamp
  108. * @return string
  109. */
  110. public function makeSignature($timestamp=0)
  111. {
  112. $timestamp = $timestamp>0? $timestamp : time();
  113. $str = self::$appId.self::$supplyMobile.$timestamp.self::$appSecret;
  114. var_dump($str);
  115. return md5($str);
  116. }
  117. /**
  118. * 获取接口数据
  119. * @param string $apiName 接口名称
  120. * @param array $params 接口参数
  121. * @param string $requestType 请求方式
  122. * @param int $timeout 超时时间/秒
  123. * @return array|false|mixed|string
  124. */
  125. public function getApiData($apiName, $params=[], $requestType='post', $timeout=5)
  126. {
  127. $url = isset(self::$apiUrls[$apiName])?self::$apiUrls[$apiName] : '';
  128. if(empty($url)){
  129. $this->error = 1044;
  130. return false;
  131. }
  132. if(!$this->getToken()){
  133. $this->error = 1046;
  134. return false;
  135. }
  136. $params['access_token'] = self::$accessToken;
  137. $url = self::$apiUrl.$url;
  138. RedisService::set("caches:supply:".self::$appId.'_'.self::$supplyMobile.":{$apiName}_request",['url'=>$url,'params'=> $params], 600);
  139. $result = httpRequest($url,$params,$requestType,'',$timeout);
  140. RedisService::set("caches:supply:".self::$appId.'_'.self::$supplyMobile.":{$apiName}_result",['url'=>$url,'params'=> $params,'result'=>$result], 600);
  141. $errCode = isset($result['error_code'])? $result['error_code'] : '';
  142. $errMsg = isset($result['error_msg'])? $result['error_msg'] : '';
  143. $result = isset($result['result'])? $result['result'] : [];
  144. if($errCode == 0){
  145. return $result;
  146. }else{
  147. $this->error = $errMsg? $errMsg : 1045;
  148. return false;
  149. }
  150. }
  151. }