优惠券小程序

TaoBaoApi.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/10/13
  6. * Time: 14:17
  7. */
  8. namespace App\Support;
  9. use App\Log;
  10. use App\Models\Redis\RedisModel;
  11. use App\Models\TbAccountInfo;
  12. use App\Service\HttpService;
  13. class TaoBaoApi
  14. {
  15. private static $accountId = 3265370068;
  16. private static $httpRequestUrl = 'http://gw.api.taobao.com/router/rest';
  17. private static $httpsRequestUrl = 'https://eco.taobao.com/router/rest';
  18. //生成签名
  19. private static function createSign($appSecret, $paramArr)
  20. {
  21. $sign = $appSecret;
  22. # 根据参数名称排序
  23. ksort($paramArr);
  24. # 把所有参数名和参数值串在一起
  25. foreach ($paramArr as $key => $val) {
  26. if ($key != '' && $val != '') {
  27. $sign .= $key.$val;
  28. }
  29. }
  30. # 参数值前后拼接secret
  31. $sign.=$appSecret;
  32. # 使用MD5加密,并把二进制转化为大写的十六进制
  33. $sign = strtoupper(md5($sign));
  34. return $sign;
  35. }
  36. // 获取基础配置信息
  37. private static function getConf($accountId)
  38. {
  39. # 优先从redis中获取
  40. $redisKey = 'yhq-smallapp::tb_account';
  41. $value = RedisModel::get($redisKey);
  42. if(empty($value)){
  43. $conf = TbAccountInfo::query()
  44. ->select(['account_id', 'tb_nickname', 'site_id', 'site_name', 'app_key', 'secret', 'pid'])
  45. ->where('account_id', $accountId)
  46. ->where('enable', 1)
  47. ->first();
  48. if(empty($conf)) {
  49. return [];
  50. } else {
  51. $conf = json_decode(json_encode($conf), 1);
  52. RedisModel::set($redisKey, json_encode($conf, 256));
  53. RedisModel::expire($redisKey, 600);
  54. return $conf;
  55. }
  56. } else {
  57. $conf = json_decode($value, 1);
  58. return $conf;
  59. }
  60. }
  61. public static function createRequest($method, $paramsArr)
  62. {
  63. $conf = self::getConf(self::$accountId);
  64. if(empty($conf)){
  65. Log::error('createRequest', [
  66. 'err_msg' => '获取配置信息失败',
  67. 'account_id' => self::$accountId,
  68. ], 'taobao-api');
  69. return false;
  70. }
  71. $paramsArr['app_key'] = $conf['app_key'];
  72. $paramsArr['method'] = $method;
  73. $paramsArr['format'] = 'json';
  74. $paramsArr['v'] = '2.0';
  75. $paramsArr['sign_method'] = 'md5';
  76. $paramsArr['timestamp'] = date('Y-m-d H:i:s');
  77. // 生成签名
  78. $sign = self::createSign($conf['secret'], $paramsArr);
  79. $paramsArr['sign'] = $sign;
  80. $requestUrl = self::$httpRequestUrl.'?'.http_build_query($paramsArr);
  81. // 访问服务
  82. $result = HttpService::httpGet($requestUrl);
  83. $result = json_decode($result, 1);
  84. $apiDesc = config('taobao');
  85. Log::info($method, [
  86. 'desc' => isset($apiDesc[$method]) ? $apiDesc[$method] : '',
  87. 'params' => $paramsArr,
  88. 'response' => $result,
  89. ], 'taobao-api');
  90. return $result;
  91. }
  92. public static function fieldMapping()
  93. {
  94. return [
  95. 'item_id' => 'item_id',
  96. 'title' => 'item_title',
  97. 'short_title' => 'item_short_title',
  98. 'item_description' => 'item_desc',
  99. 'reserve_price' => 'item_price',
  100. 'volume' => 'item_sale',
  101. 'pict_url' => 'item_pic',
  102. 'zk_final_price' => 'zk_final_price',
  103. 'user_type' => 'shop_type',
  104. 'coupon_share_url' => 'coupon_url',
  105. 'coupon_amount' => 'coupon_money',
  106. 'coupon_info' => 'coupon_info',
  107. 'input_num_iid' => 'item_id',
  108. 'small_images' => 'taobao_image',
  109. 'item_url' => 'item_url',
  110. 'url' => 'url',
  111. ];
  112. }
  113. }