JingDongUtil.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Http\Utils\JingDong;
  3. use App\Exceptions\CommonException;
  4. use App\Http\Utils\BaseUtil;
  5. use App\Http\Utils\LoggerFactoryUtil;
  6. use Tool\ShanTaoTool\HttpCurl;
  7. class JingDongUtil extends BaseUtil
  8. {
  9. /**
  10. * 生成sign
  11. * @param $params array 参数
  12. * @return string
  13. */
  14. public static function generateSign($params)
  15. {
  16. $secret = config("utilJingDong")["appSecret"];
  17. ksort($params);
  18. $str = $secret; // $secret为分配的密钥
  19. foreach ($params as $key => $value) {
  20. $str .= $key . $value;
  21. }
  22. $str .= $secret;
  23. $sign = md5($str);
  24. $sign = strtoupper($sign);
  25. return $sign;
  26. }
  27. /**
  28. * @param $req 请求参数
  29. * @param $method 请求接口
  30. * @param $token
  31. * @return mixed
  32. * @throws CommonException
  33. */
  34. public static function jDRequestApi($req, $method, $token='')
  35. {
  36. $appKey = config("utilJingDong")["appKey"];
  37. $domain = config("utilJingDong")["domain"];
  38. $params = [
  39. "method" => $method,
  40. "app_key" => $appKey,
  41. "timestamp" => date("Y-m-d H:i:s"),
  42. "format" => "json",
  43. "v" => "1.0",
  44. "sign_method" => "md5",
  45. "360buy_param_json" => count($req)==0?"{}":json_encode($req)
  46. ];
  47. if($token){
  48. $params["access_token"] = $token;
  49. }
  50. $params["sign"] = self::generateSign($params);
  51. $domain = $domain . "?" . http_build_query($params);
  52. $res = HttpCurl::postCurl($domain);
  53. $arr = explode(".",$method);
  54. $nextKey = $arr[count($arr)-1]."Result";
  55. $responseKey = str_replace(".","_",$method)."_responce";
  56. $log = new LoggerFactoryUtil(JingDongUtil::class);
  57. $log->info(json_encode(["params" => $params, "res" => $res]));
  58. if(!isset($res[$responseKey])){
  59. throw new CommonException(ErrorEnum::EXCEPTION_ERROR);
  60. }
  61. $res = $res[$responseKey][$nextKey];
  62. $res = json_decode($res,true);
  63. if ($res["code"] == 200) {
  64. return $res;
  65. }
  66. dd($res);
  67. throw new CommonException([500,$res["message"]]);
  68. }
  69. /**
  70. * 获取分类
  71. */
  72. public static function getCategory()
  73. {
  74. // $params = [
  75. // "req"=>[
  76. // "parentId"=>0,
  77. // "grade"=>0
  78. // ]
  79. // ];
  80. //
  81. // return self::jDRequestApi($params,"jd.union.open.category.goods.get");
  82. return [
  83. [
  84. "id"=>1,
  85. "name"=>"好券商品"
  86. ],
  87. [
  88. "id"=>2,
  89. "name"=>"精选卖场"
  90. ],
  91. [
  92. "id"=>10,
  93. "name"=>"9.9包邮"
  94. ],
  95. [
  96. "id"=>15,
  97. "name"=>"京东配送"
  98. ],
  99. [
  100. "id"=>32,
  101. "name"=>"京东好物"
  102. ],
  103. [
  104. "id"=>22,
  105. "name"=>"实时热销榜"
  106. ],
  107. [
  108. "id"=>33,
  109. "name"=>"京东秒杀"
  110. ],
  111. [
  112. "id"=>34,
  113. "name"=>"拼购商品"
  114. ],
  115. ];
  116. }
  117. /**
  118. * 根据商品分类获取商品列表
  119. * @param $cateId
  120. */
  121. public static function getGoodsListByCategory($cateId,$page)
  122. {
  123. $params = [
  124. "goodsReqDTO"=>[
  125. "cid1"=>$cateId,
  126. "pageIndex"=>$page
  127. ]
  128. ];
  129. return self::jDRequestApi($params,"jd.union.open.goods.query");
  130. }
  131. /**
  132. * 获取商品详情页
  133. * @param $goodsId
  134. */
  135. public static function getGoodsDetail($goodsId)
  136. {
  137. $params = [
  138. "goodsReqDTO"=>[
  139. "skuIds"=>[$goodsId]
  140. ]
  141. ];
  142. return self::jDRequestApi($params,"jd.union.open.goods.query");
  143. }
  144. /**
  145. * 创建联盟推广位
  146. */
  147. public static function createdPid()
  148. {
  149. //目前默认创建平台的后续会根据用户的进行修改
  150. $oauth = UserOauthModel::query()
  151. ->where("api_user_id",0)
  152. ->where("platform_type",PlatformTypeEnum::JINGDOND)
  153. ->first();
  154. $params = [
  155. "positionReq"=>[
  156. "unionId"=>$oauth["lianmeng_id"],
  157. "key"=>$oauth["lianmeng_key"],
  158. "unionType"=>3,
  159. "type"=>4,
  160. "spaceNameList"=>["pid"]
  161. ]
  162. ];
  163. return self::jDRequestApi($params,"jd.union.open.position.create");
  164. }
  165. /**
  166. * 根据商品ID进行转链
  167. * @param $goodsId
  168. */
  169. public static function transferGoods($goodsId,$pid)
  170. {
  171. $params = [
  172. "goodsReqDTO"=>[
  173. "skuIds"=>[$goodsId]
  174. ]
  175. ];
  176. $data = self::jDRequestApi($params,"jd.union.open.goods.query")["data"][0];
  177. //目前默认创建平台的后续会根据用户的进行修改
  178. $oauth = UserOauthModel::query()
  179. ->where("api_user_id",0)
  180. ->where("platform_type",PlatformTypeEnum::JINGDOND)
  181. ->first();
  182. $arr = [
  183. "promotionCodeReq"=>[
  184. "materialId"=>$data["materialUrl"],
  185. // "subUnionId"=>"ceshi",
  186. "positionId"=>$pid,
  187. "unionId"=>$oauth["lianmeng_id"],
  188. "chainType"=>3
  189. ]
  190. ];
  191. return self::jDRequestApi($arr,"jd.union.open.promotion.byunionid.get");
  192. }
  193. /**
  194. * 获取京东联盟订单列表
  195. * @return mixed
  196. * @throws CommonException
  197. */
  198. public static function getOrderList()
  199. {
  200. //目前默认创建平台的后续会根据用户的进行修改
  201. $oauth = UserOauthModel::query()
  202. ->where("api_user_id",0)
  203. ->where("platform_type",PlatformTypeEnum::JINGDOND)
  204. ->first();
  205. $params = [
  206. "orderReq"=>[
  207. "pageIndex"=>1,
  208. "pageSize"=>20,
  209. "type"=>1,
  210. "startTime"=>"2021-12-22 14:00:00",
  211. "endTime"=>"2021-12-22 14:30:00",
  212. "childUnionId"=>1001268394,
  213. // "childUnionId"=>$oauth["lianmeng_id"],
  214. // "key"=>"ece3b6ab1c8b87a7fa80247b92da4441abcd2d8e862e22e4d374ef1cfbd4fb0373cf264e3f54b391",
  215. ]
  216. ];
  217. return self::jDRequestApi($params,"jd.union.open.order.row.query");
  218. }
  219. }