// +---------------------------------------------------------------------- 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); } public function goodsList() { } }