WeiPinHuiUtil.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\Utils\WeiPinHui;
  3. use App\Http\Utils\BaseUtil;
  4. class WeiPinHuiUtil extends BaseUtil
  5. {
  6. /**
  7. * 生成sign
  8. * @param $params array 参数
  9. * @return string
  10. */
  11. public static function generateSign($params, $req)
  12. {
  13. $secret = config("utilWeiPinHui")["appSecret"];
  14. ksort($params);
  15. $str = "";
  16. foreach ($params as $key => $value) {
  17. $str .= $key . $value;
  18. }
  19. $str .= json_encode($req);
  20. $sign = hash_hmac('md5', $str, $secret);
  21. $sign = strtoupper($sign);
  22. return $sign;
  23. }
  24. /**
  25. * @param $req 请求参数
  26. * @param $serviceName 服务名称
  27. * @param $method 请求方法
  28. * @param string $accessToken
  29. * @return mixed
  30. * @throws CommonException
  31. */
  32. public static function weiPinHuiRequestApi($req = [], $serviceName, $method, $accessToken = "")
  33. {
  34. $appKey = config("utilWeiPinHui")["appKey"];
  35. $domain = config("utilWeiPinHui")["domain"];
  36. $params = [
  37. "service" => $serviceName,
  38. "method" => $method,
  39. "version" => "1.0.0",
  40. "timestamp" => time(),
  41. "format" => "json",
  42. "appKey" => $appKey,
  43. "accessToken" => $accessToken
  44. ];
  45. $params["sign"] = self::generateSign($params, $req);
  46. $domain = $domain . "?" . http_build_query($params);
  47. $res = HttpCurl::postCurl($domain, $req);
  48. $log = new LoggerFactoryUtil(WeiPinHuiUtil::class);
  49. $log->info(json_encode(["params" => $params, "res" => $res]));
  50. if ($res && $res["returnCode"] == 0) {
  51. return $res["result"];
  52. }
  53. throw new CommonException(ErrorEnum::EXCEPTION_ERROR);
  54. }
  55. /**
  56. * 将商品链接转化为联盟链接
  57. * @param $goodLink 商品链接
  58. * @param $accessToken 授权密钥
  59. */
  60. public static function tranferVipLink($goodLink,$pid,$accessToken)
  61. {
  62. $params = [
  63. "urlList"=>[$goodLink],
  64. "requestId"=>time()."",
  65. "chanTag"=>$pid
  66. ];
  67. return self::weiPinHuiRequestApi($params,"com.vip.adp.api.open.service.UnionUrlService","genByVIPUrlWithOauth",$accessToken);
  68. }
  69. /**
  70. * 将商品ID转化为联盟链接
  71. * @param $goodsId 商品ID
  72. * @param $accessToken 授权密钥
  73. * @return mixed
  74. * @throws CommonException
  75. */
  76. public static function transferGoodsId($goodsId,$pid, $accessToken)
  77. {
  78. $params = [
  79. "goodsIdList"=>[$goodsId],
  80. "requestId"=>time()."",
  81. "chanTag"=>$pid
  82. ];
  83. return self::weiPinHuiRequestApi($params,"com.vip.adp.api.open.service.UnionUrlService","genByGoodsIdWithOauth",$accessToken);
  84. }
  85. /**
  86. * 获取联盟商品列表
  87. * @param $params
  88. * @param $acessToken
  89. * @return mixed
  90. * @throws CommonException
  91. */
  92. public static function getGoodsList($params, $accessToken)
  93. {
  94. return self::weiPinHuiRequestApi($params,"com.vip.adp.api.open.service.UnionGoodsService","goodsListWithOauth",$accessToken);
  95. }
  96. /**
  97. * 获取联盟商品详情
  98. * @param $params
  99. * @param $acessToken
  100. * @return mixed
  101. * @throws CommonException
  102. */
  103. public static function getGoodsDetail($params, $accessToken)
  104. {
  105. return self::weiPinHuiRequestApi($params,"com.vip.adp.api.open.service.UnionGoodsService","getByGoodsIdsWithOauth",$accessToken);
  106. }
  107. /**
  108. * 通过搜索获取联盟商品列表
  109. * @param $params
  110. * @param $acessToken
  111. * @return mixed
  112. * @throws CommonException
  113. */
  114. public static function getGoodsListBySearch($params, $accessToken)
  115. {
  116. return self::weiPinHuiRequestApi($params,"com.vip.adp.api.open.service.UnionGoodsService","queryWithOauth",$accessToken);
  117. }
  118. /**
  119. * 获取一级类目
  120. * @return mixed
  121. * @throws CommonException
  122. */
  123. public static function getCategory($accessToken)
  124. {
  125. $params = [
  126. "request"=>[
  127. "parentId"=>0,
  128. "grade"=>1,
  129. "requestId"=>time().""
  130. ]
  131. ];
  132. return self::weiPinHuiRequestApi($params,"com.vip.adp.api.open.service.UnionGoodsService","getCategorys",$accessToken);
  133. }
  134. /**
  135. * 获取订单列表
  136. * @param $params
  137. * @param $accessToken
  138. */
  139. public static function getOrderList($params, $accessToken)
  140. {
  141. return self::weiPinHuiRequestApi($params,"com.vip.adp.api.open.service.UnionOrderService","orderListWithOauth",$accessToken);
  142. }
  143. /**
  144. * 获取维权订单列表
  145. * @param $params
  146. * @param $accessToken
  147. */
  148. public static function getRefundOrderList($params, $accessToken)
  149. {
  150. return self::weiPinHuiRequestApi($params,"com.vip.adp.api.open.service.UnionOrderService","refundOrderListWithOauth",$accessToken);
  151. }
  152. }