123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/10/13
- * Time: 14:17
- */
- namespace App\Support;
- use App\Log;
- use App\Models\Redis\RedisModel;
- use App\Models\TbAccountInfo;
- use App\Service\HttpService;
- class TaoBaoApi
- {
- private static $accountId = 3265370068;
- private static $httpRequestUrl = 'http://gw.api.taobao.com/router/rest';
- private static $httpsRequestUrl = 'https://eco.taobao.com/router/rest';
- //生成签名
- private static function createSign($appSecret, $paramArr)
- {
- $sign = $appSecret;
- # 根据参数名称排序
- ksort($paramArr);
- # 把所有参数名和参数值串在一起
- foreach ($paramArr as $key => $val) {
- if ($key != '' && $val != '') {
- $sign .= $key.$val;
- }
- }
- # 参数值前后拼接secret
- $sign.=$appSecret;
- # 使用MD5加密,并把二进制转化为大写的十六进制
- $sign = strtoupper(md5($sign));
- return $sign;
- }
- // 获取基础配置信息
- private static function getConf($accountId)
- {
- # 优先从redis中获取
- $redisKey = 'yhq-smallapp::tb_account';
- $value = RedisModel::get($redisKey);
- if(empty($value)){
- $conf = TbAccountInfo::query()
- ->select(['account_id', 'tb_nickname', 'site_id', 'site_name', 'app_key', 'secret', 'pid'])
- ->where('account_id', $accountId)
- ->where('enable', 1)
- ->first();
- if(empty($conf)) {
- return [];
- } else {
- $conf = json_decode(json_encode($conf), 1);
- RedisModel::set($redisKey, json_encode($conf, 256));
- RedisModel::expire($redisKey, 600);
- return $conf;
- }
- } else {
- $conf = json_decode($value, 1);
- return $conf;
- }
- }
- public static function createRequest($method, $paramsArr)
- {
- $conf = self::getConf(self::$accountId);
- if(empty($conf)){
- Log::error('createRequest', [
- 'err_msg' => '获取配置信息失败',
- 'account_id' => self::$accountId,
- ], 'taobao-api');
- return false;
- }
- $paramsArr['app_key'] = $conf['app_key'];
- $paramsArr['method'] = $method;
- $paramsArr['format'] = 'json';
- $paramsArr['v'] = '2.0';
- $paramsArr['sign_method'] = 'md5';
- $paramsArr['timestamp'] = date('Y-m-d H:i:s');
- // 生成签名
- $sign = self::createSign($conf['secret'], $paramsArr);
- $paramsArr['sign'] = $sign;
- $requestUrl = self::$httpRequestUrl.'?'.http_build_query($paramsArr);
- // 访问服务
- $result = HttpService::httpGet($requestUrl);
- $result = json_decode($result, 1);
- $apiDesc = config('taobao');
- Log::info($method, [
- 'desc' => isset($apiDesc[$method]) ? $apiDesc[$method] : '',
- 'params' => $paramsArr,
- 'response' => $result,
- ], 'taobao-api');
- return $result;
- }
- public static function fieldMapping()
- {
- return [
- 'item_id' => 'item_id',
- 'title' => 'item_title',
- 'short_title' => 'item_short_title',
- 'item_description' => 'item_desc',
- 'reserve_price' => 'item_price',
- 'volume' => 'item_sale',
- 'pict_url' => 'item_pic',
- 'zk_final_price' => 'zk_final_price',
- 'user_type' => 'shop_type',
- 'coupon_share_url' => 'coupon_url',
- 'coupon_amount' => 'coupon_money',
- 'coupon_info' => 'coupon_info',
- 'input_num_iid' => 'item_id',
- 'small_images' => 'taobao_image',
- 'item_url' => 'item_url',
- 'url' => 'url',
- ];
- }
- }
|