123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\GoodsModel;
- /**
- * 供应链服务管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services
- */
- class SupplyService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- protected static $apiUrl = '';
- protected static $appId = '';
- protected static $appSecret = '';
- protected static $supplyMobile = '';
- protected static $accessToken = '';
- protected static $apiUrls = [
- 'getToken'=> '/api_v2/noAuth/getAccessToken',
- 'getGoodsStock'=> '/api_v2/Goods/stock', //
- 'getGoodsList'=> '/api_v2/Goods/getGoodsList', // 商品列表
- 'getGoodsDetail'=> '/api_v2/Goods/getGoodsDetail', // 商品详情
- 'getGoodsSku'=> '/api_v2/Goods/getSkuDetail', // sku
- 'getFreight'=> '/api_v2/order/getFreight', // 运费
- 'orderSubmit'=> '/api_v2/order/submit', // 下单
- 'getOrderDetail'=> '/api_v2/order/getOrderDetail', // 订单详情
- 'getOrderTrack'=> '/api_v2/order/track', // 物流信息
- 'getAfterCan'=> '/api_v2/After/isCanAfter', // 查询是否可售后
- 'getAfterStatus'=> '/api_v2/After/afterStatus', // 查询售后信息
- 'applyAfterCancel'=> '/api_v2/After/cancel_apply', // 取消售后
- 'applyAfter'=> '/api_v2/After/applyAfterSales', // 申请售后
- ];
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * ConfigService constructor.
- */
- public function __construct()
- {
- $this->model = new GoodsModel();
- self::$apiUrl = ConfigService::make()->getConfigByCode('supply_api_url','https://www.douhuomall.com');
- self::$supplyMobile = ConfigService::make()->getConfigByCode('supply_mobile','19354894149');
- self::$appId = ConfigService::make()->getConfigByCode('supply_app_id','YS1ef14c155555dce4bb');
- self::$appSecret = ConfigService::make()->getConfigByCode('supply_app_secret','2cbbf72408cb906ec039105d9ff9b365');
- }
- /**
- * 静态入口
- * @return SmsService|static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取授权TOKEN
- * @return bool
- */
- public function getToken()
- {
- if(empty(self::$appId) || empty(self::$supplyMobile) || empty(self::$appSecret)){
- $this->error = 2702;
- return false;
- }
- $cacheKey = "caches:supply:token_".self::$appId.'_'.self::$supplyMobile;
- $data = RedisService::get($cacheKey);
- $accessToken = isset($data['access_token'])? $data['access_token'] : '';
- $expireAt = isset($result['expire_time'])? $result['expire_time'] :'';
- if(empty($accessToken) || $expireAt <= date('Y-m-d H:i:s')){
- $url = self::$apiUrl.self::$apiUrls['getToken'];
- $timestamp = time();
- $params = [
- 'app_id'=> self::$appId,
- 'sign'=> $this->makeSignature($timestamp),
- 'mobile'=> self::$supplyMobile,
- 'timestamp'=> $timestamp,
- ];
- $result = httpRequest($url, $params, 'post','', 5);
- RedisService::set("caches:supply:token_result_".self::$appId, ['url'=>$url,'params'=>$params,'result'=>$result], 600);
- $result = isset($result['result'])? $result['result'] : [];
- $accessToken = isset($result['access_token'])? $result['access_token'] :'';
- $expireAt = isset($result['expire_time'])? $result['expire_time'] :'';
- if(empty($accessToken) || $expireAt <= date('Y-m-d H:i:s')){
- $this->error = 2701;
- return false;
- }
- RedisService::set($cacheKey, $result, 3600);
- }
- self::$accessToken = $accessToken;
- return true;
- }
- /**
- * 签名
- * @param int $timestamp
- * @return string
- */
- public function makeSignature($timestamp=0)
- {
- $timestamp = $timestamp>0? $timestamp : time();
- $str = self::$appId.self::$supplyMobile.$timestamp.self::$appSecret;
- var_dump($str);
- return md5($str);
- }
- /**
- * 获取接口数据
- * @param string $apiName 接口名称
- * @param array $params 接口参数
- * @param string $requestType 请求方式
- * @param int $timeout 超时时间/秒
- * @return array|false|mixed|string
- */
- public function getApiData($apiName, $params=[], $requestType='post', $timeout=5)
- {
- $url = isset(self::$apiUrls[$apiName])?self::$apiUrls[$apiName] : '';
- if(empty($url)){
- $this->error = 1044;
- return false;
- }
- if(!$this->getToken()){
- $this->error = 1046;
- return false;
- }
- $params['access_token'] = self::$accessToken;
- $url = self::$apiUrl.$url;
- RedisService::set("caches:supply:".self::$appId.'_'.self::$supplyMobile.":{$apiName}_request",['url'=>$url,'params'=> $params], 600);
- $result = httpRequest($url,$params,$requestType,'',$timeout);
- RedisService::set("caches:supply:".self::$appId.'_'.self::$supplyMobile.":{$apiName}_result",['url'=>$url,'params'=> $params,'result'=>$result], 600);
- $errCode = isset($result['error_code'])? $result['error_code'] : '';
- $errMsg = isset($result['error_msg'])? $result['error_msg'] : '';
- $result = isset($result['result'])? $result['result'] : [];
- if($errCode == 0){
- return $result;
- }else{
- $this->error = $errMsg? $errMsg : 1045;
- return false;
- }
- }
- }
|