123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace App\Http\Utils\Meituan;
- use App\Exceptions\CommonException;
- use App\Http\Bean\Util\Meituan\OrderListParamBean;
- use App\Http\Enum\ErrorEnum;
- use App\Http\Utils\BaseUtil;
- use App\Http\Utils\LoggerFactoryUtil;
- use Tool\ShanTaoTool\HttpCurl;
- class MeituanLianmengUtilV1 extends BaseUtil
- {
- /**
- * 生成sign
- * @param $params array 参数
- * @return string
- */
- public static function generateSign($params)
- {
- $secret = env("MEITUAN_LIANMENG_SECRET");
- unset($params["sign"]);
- ksort($params);
- $str = $secret; // $secret为分配的密钥
- foreach($params as $key => $value) {
- $str .= $key . $value;
- }
- $str .= $secret;
- $sign = md5($str);
- return $sign;
- }
- /**
- * @param $actId int 活动id,可以在联盟活动列表中查看获取
- * @param $sid string 推广位ID
- * @param $linkType int 链接类型(1、h5链接2、deeplink(唤起)链接3、中间页唤起链接4、微信小程序唤起路径)
- * @param $appkey string appkey
- * @param $secret string appkey
- * @return mixed
- */
- public static function generateLink($actId,$sid,$linkType)
- {
- $appkey = env("MEITUAN_LIANMENG_KEY");
- $url = "https://openapi.meituan.com/api/generateLink";
- $params = [
- "actId"=>$actId,
- "appkey"=>$appkey,
- "sid"=>$sid,
- "linkType"=>$linkType,
- "sign"=>"",
- "shortLink"=>1//0表示获取长链,1表示获取短链
- ];
- $sign = self::generateSign($params);
- $params["sign"] = $sign;
- $res = HttpCurl::getCurl($url,$params);
- if($res["status"]!=0){
- $instance = new LoggerFactoryUtil(MeituanLianmengUtilV1::class);
- $instance->info("美团发挥结果:".json_encode($res));
- throw new CommonException(ErrorEnum::ERROR_SYSTEM);
- }
- return $res["data"];
- }
- /**
- * 获取订单列表接口
- * @param OrderListParamBean $orderListParamBean
- */
- public static function orderList(OrderListParamBean $orderListParamBean)
- {
- $url = "https://openapi.meituan.com/api/orderList";
- $params = [
- "appkey"=>$orderListParamBean->getAppkey(),
- "ts"=>time(),
- "businessLine"=>$orderListParamBean->getType(),
- "sign"=>"",
- "startTime"=>$orderListParamBean->getStartTime(),
- "endTime"=>$orderListParamBean->getEndTime(),
- "page"=>$orderListParamBean->getPage(),
- "limit"=>$orderListParamBean->getLimit(),
- "queryTimeType"=>$orderListParamBean->getQueryTimeType()
- ];
- $sign = self::generateSign($params);
- $params["sign"] = $sign;
- $res = HttpCurl::getCurl($url,$params);
- return $res;
- }
- /**
- * 生成小程序二维码
- * @param $sid string 推广为ID
- * @param $actId int 活动ID
- */
- public static function miniCode($sid,$actId)
- {
- $appkey = env("MEITUAN_LIANMENG_KEY");
- $url = "https://openapi.meituan.com/api/miniCode";
- $params = [
- "sid"=>$sid,
- "actId"=>$actId,
- "appKey"=>$appKey,
- "sign"=>""
- ];
- $sign = self::generateSign($params);
- $params["sign"] = $sign;
- $res = HttpCurl::getCurl($url,$params);
- return $res;
- }
- /**
- * 根据订单编号获取单个订单
- * @param $orderNumber string 订单编号
- * @param $key string 媒体appKey
- * @param $type string 订单类型
- */
- public static function queryOrderByOrderNumber($orderNumber,$type,$acId)
- {
- $appkey = env("MEITUAN_LIANMENG_KEY");
- $url = "https://openapi.meituan.com/api/order";
- $params = [
- "appkey"=>$appkey,
- "orderId"=>$orderNumber,
- "full"=>1,
- "businessLine"=>$type,
- "sign"=>"",
- "acId"=>$acId
- ];
- $sign = self::generateSign($params);
- $params["sign"] = $sign;
- $res = HttpCurl::getCurl($url,$params);
- return $res;
- }
- /**
- * 获取美团推广链接
- * @param $userId string 用户ID
- * @param $spreadId string 推广位
- * @param $actId string 活动ID
- * @param $linkType string 链接类型
- */
- public static function getMeituanWaimaiUrl($userId, $spreadId, $actId, $linkType)
- {
- $key = "meituanUrl:".$userId."linktype:".$linkType."actid:".$actId;
- $val = self::getCacheFromRedis($key);
- $instance = new LoggerFactoryUtil(MeituanLianmengUtilV1::class);
- if($val){
- $instance->info("从缓存中获取美团链接");
- return $val;
- }
- //不存在则从官方获取
- $val = self::generateLink($actId,$spreadId,$linkType);
- $instance->info("从接口中获取美团链接");
- self::setRedisCache($key,$val,0);
- return $val;
- }
- }
|