MeituanLianmengUtilV1.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Http\Utils\Meituan;
  3. use App\Exceptions\CommonException;
  4. use App\Http\Bean\Util\Meituan\OrderListParamBean;
  5. use App\Http\Enum\ErrorEnum;
  6. use App\Http\Utils\BaseUtil;
  7. use App\Http\Utils\LoggerFactoryUtil;
  8. use Tool\ShanTaoTool\HttpCurl;
  9. class MeituanLianmengUtilV1 extends BaseUtil
  10. {
  11. /**
  12. * 生成sign
  13. * @param $params array 参数
  14. * @return string
  15. */
  16. public static function generateSign($params)
  17. {
  18. $secret = env("MEITUAN_LIANMENG_SECRET");
  19. unset($params["sign"]);
  20. ksort($params);
  21. $str = $secret; // $secret为分配的密钥
  22. foreach($params as $key => $value) {
  23. $str .= $key . $value;
  24. }
  25. $str .= $secret;
  26. $sign = md5($str);
  27. return $sign;
  28. }
  29. /**
  30. * @param $actId int 活动id,可以在联盟活动列表中查看获取
  31. * @param $sid string 推广位ID
  32. * @param $linkType int 链接类型(1、h5链接2、deeplink(唤起)链接3、中间页唤起链接4、微信小程序唤起路径)
  33. * @param $appkey string appkey
  34. * @param $secret string appkey
  35. * @return mixed
  36. */
  37. public static function generateLink($actId,$sid,$linkType)
  38. {
  39. $appkey = env("MEITUAN_LIANMENG_KEY");
  40. $url = "https://openapi.meituan.com/api/generateLink";
  41. $params = [
  42. "actId"=>$actId,
  43. "appkey"=>$appkey,
  44. "sid"=>$sid,
  45. "linkType"=>$linkType,
  46. "sign"=>"",
  47. "shortLink"=>1//0表示获取长链,1表示获取短链
  48. ];
  49. $sign = self::generateSign($params);
  50. $params["sign"] = $sign;
  51. $res = HttpCurl::getCurl($url,$params);
  52. if($res["status"]!=0){
  53. $instance = new LoggerFactoryUtil(MeituanLianmengUtilV1::class);
  54. $instance->info("美团发挥结果:".json_encode($res));
  55. throw new CommonException(ErrorEnum::ERROR_SYSTEM);
  56. }
  57. return $res["data"];
  58. }
  59. /**
  60. * 获取订单列表接口
  61. * @param OrderListParamBean $orderListParamBean
  62. */
  63. public static function orderList(OrderListParamBean $orderListParamBean)
  64. {
  65. $url = "https://openapi.meituan.com/api/orderList";
  66. $params = [
  67. "appkey"=>$orderListParamBean->getAppkey(),
  68. "ts"=>time(),
  69. "businessLine"=>$orderListParamBean->getType(),
  70. "sign"=>"",
  71. "startTime"=>$orderListParamBean->getStartTime(),
  72. "endTime"=>$orderListParamBean->getEndTime(),
  73. "page"=>$orderListParamBean->getPage(),
  74. "limit"=>$orderListParamBean->getLimit(),
  75. "queryTimeType"=>$orderListParamBean->getQueryTimeType()
  76. ];
  77. $sign = self::generateSign($params);
  78. $params["sign"] = $sign;
  79. $res = HttpCurl::getCurl($url,$params);
  80. return $res;
  81. }
  82. /**
  83. * 生成小程序二维码
  84. * @param $sid string 推广为ID
  85. * @param $actId int 活动ID
  86. */
  87. public static function miniCode($sid,$actId)
  88. {
  89. $appkey = env("MEITUAN_LIANMENG_KEY");
  90. $url = "https://openapi.meituan.com/api/miniCode";
  91. $params = [
  92. "sid"=>$sid,
  93. "actId"=>$actId,
  94. "appKey"=>$appKey,
  95. "sign"=>""
  96. ];
  97. $sign = self::generateSign($params);
  98. $params["sign"] = $sign;
  99. $res = HttpCurl::getCurl($url,$params);
  100. return $res;
  101. }
  102. /**
  103. * 根据订单编号获取单个订单
  104. * @param $orderNumber string 订单编号
  105. * @param $key string 媒体appKey
  106. * @param $type string 订单类型
  107. */
  108. public static function queryOrderByOrderNumber($orderNumber,$type,$acId)
  109. {
  110. $appkey = env("MEITUAN_LIANMENG_KEY");
  111. $url = "https://openapi.meituan.com/api/order";
  112. $params = [
  113. "appkey"=>$appkey,
  114. "orderId"=>$orderNumber,
  115. "full"=>1,
  116. "businessLine"=>$type,
  117. "sign"=>"",
  118. "acId"=>$acId
  119. ];
  120. $sign = self::generateSign($params);
  121. $params["sign"] = $sign;
  122. $res = HttpCurl::getCurl($url,$params);
  123. return $res;
  124. }
  125. /**
  126. * 获取美团推广链接
  127. * @param $userId string 用户ID
  128. * @param $spreadId string 推广位
  129. * @param $actId string 活动ID
  130. * @param $linkType string 链接类型
  131. */
  132. public static function getMeituanWaimaiUrl($userId, $spreadId, $actId, $linkType)
  133. {
  134. $key = "meituanUrl:".$userId."linktype:".$linkType."actid:".$actId;
  135. $val = self::getCacheFromRedis($key);
  136. if($val){
  137. return $val;
  138. }
  139. //不存在则从官方获取
  140. $val = self::generateLink($actId,$spreadId,$linkType);
  141. self::setRedisCache($key,$val,0);
  142. return $val;
  143. }
  144. }