bindings; $rawSql = preg_replace_callback('/\?/', function ($matches) use ($bindings, &$i) { $item = isset($bindings[$i]) ? $bindings[$i] : $matches[0]; $i++; return gettype($item) == 'string' ? "'$item'" : $item; }, $sql->sql); //记录sql LoggerFactoryUtil::addSqlMessage($rawSql); // echo $rawSql, "\n

\n"; }); } /** * 统一企微报警 * @param $params * @param $response * @param $funcname * @throws \Tool\ShanTaoTool\Exception\QiWeiException */ public static function sendBaoJing($params,$response,$funcname) { $param = [ "功能"=>$funcname, "请求参数"=>$params, "用户ID"=>"", "相应信息"=>$response, "信息时间"=>date("Y-m-d H:i:s"), "环境"=>env("ENV_NAME") ]; QiWeiTool::sendMessageToBaoJing(json_encode($param,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); } /** * 获取应用编号 */ public static function getAppCodeByQuery() { return request()->input("wechat_app_code"); } /** * 从redis中获取数据 * @param $key string 键 */ public static function getCacheFromRedis($key) { /** * @var \Redis $redis */ $redis = app("redis"); return $redis->get($key); } /** * 设置redis数据的过期时间 * @param $key string 键 * @param $val string 值 * @param $ttl int 过期时间 */ public static function setRedisCache($key, $val, $ttl) { /** * @var \Redis $redis */ $redis = app("redis"); if($ttl==0){ return $redis->set($key,$val); }else{ return $redis->setex($key,$ttl,$val); } } /** * 获取redis锁 * @param $lokKey string 键 * @param $ttl int 过期时间 */ public static function setRedisLock($lokKey, $ttl) { /** * @var \Redis $redis */ $redis = app("redis")->client(); $flag = $redis->set($lokKey,1,["nx","ex"=>$ttl]); if(!$flag){ throw new CommonException(ErrorEnum::ERROR_REPEAT); } } /** * 生成美团推广位 * @param $userId int 用户ID */ public static function generateMeiTuanSid($userId) { return "meituanSpread:".$userId; } /** * 生成聚推客推广位 * @param $userId */ public static function generateJutuiKeSid($userId) { return "jtkupstream".$userId; } /** * 获取用户的平台推广位 * @param $platformType int 平台 * @param $userId int 用户ID */ public static function getPlatformUserSpreadId($platformType,$userId,$userSpreadId="") { //获取平台推广位 $platformSid = PlatformSidModel::query() ->where("user_id",$userId) ->where("platform_type",$platformType) ->first(); if($platformSid){ //存在则直接返回 return $platformSid->platform_sid; }else{ switch ($platformType){ case PlatformTypeEnum::PLATFORM_TAOBAO: //淘宝 break; case PlatformTypeEnum::PLATFORM_PINGDUODUO: //拼多多 $res = DuoDuoKeUtil::goodsPidGenerate(1,["用户:".$userId]); $instance = new LoggerFactoryUtil(BaseUtil::class); $instance->info("拼多多返回数据:".json_encode($res)); $sid = $res["p_id_generate_response"]["p_id_list"][0]["p_id"]; break; case PlatformTypeEnum::PLATFORM_JINGDONG: //京东 break; case PlatformTypeEnum::PLATFORM_MEITUAN: //美团 $sid = self::generateMeiTuanSid($userId); break; case PlatformTypeEnum::PLATFORM_FANBUTING: //平台(饭不停) break; case PlatformTypeEnum::PLATFORM_JUTUIKE: //聚推客 $sid = self::generateJutuiKeSid($userId); break; default: throw new CommonException(ErrorEnum::ERROR_EXIST_PLATFORM); } if(!$userSpreadId){ //不存在用户自传的推广位,则默认和平台的推广位相等 $userSpreadId = $sid; } //写入用户的推广位 PlatformSidModel::query()->insert( [ "user_id"=>$userId, "user_sid"=>$userSpreadId, "platform_type"=>$platformType, "platform_sid"=>$sid, "created_at"=>date("Y-m-d H:i:s"), "updated_at"=>date("Y-m-d H:i:s") ] ); return $sid; } } /** * 根据app ID和secret获取accesstoken * @param $appId * @param $appSecret */ public static function getAccessToken($appId, $appSecret) { $key = $appId."accesstoken"; $val = self::getCacheFromRedis($key); if($val){ return $val; } //不存在则获取 $params = [ "grant_type"=>"client_credential", "appid"=>$appId, "secret"=>$appSecret ]; $res = HttpCurl::getCurl("https://api.weixin.qq.com/cgi-bin/token",$params); if(isset($res["errcode"])){ $instance = new LoggerFactoryUtil(BaseUtil::class); $instance->info("微信返回信息:".json_encode($res)); throw new CommonException(ErrorEnum::ERROR_ACCOUNT_ACCESS_TOKEN); } self::setRedisCache($key,$res["access_token"],7100); return $res["access_token"]; } /** * 获取jsapi_ticket * @param $appId * @param $appSecret */ public static function getJsapiTicket($appId, $appSecret) { $key = $appId."getJsapiTicket"; $val = self::getCacheFromRedis($key); if($val){ return $val; } $accessToken = self::getAccessToken($appId,$appSecret); $params = [ "access_token"=>$accessToken, "type"=>"jsapi" ]; $res = HttpCurl::getCurl("https://api.weixin.qq.com/cgi-bin/ticket/getticket",$params); if($res["errcode"]==0){ self::setRedisCache($key,$res["ticket"],7000); return $res["ticket"]; } throw new CommonException(ErrorEnum::ERROR_ACCOUNT_ACCESS_TOKEN); } /** * 获取公众号支付参数校验数据 * @param $appId * @param $appSecret */ public static function getJsapiSign($appId, $appSecret,$url) { $jsapi_ticket = self::getJsapiTicket($appId,$appSecret); $instance = new LoggerFactoryUtil(BaseUtil::class); $instance->info("jsapiticket:".$jsapi_ticket); $noncestr = uniqid(); $timestamp = time(); $instance->info("url:".$url); // $url = substr($url,0,strpos($url,"#")); $string = "jsapi_ticket=".$jsapi_ticket."&noncestr=$noncestr"."×tamp=".$timestamp."&url=".$url; $instance->info("签名字符串:".$string); $signature = sha1($string); return [ "appId"=>$appId, "timestamp"=>$timestamp, "nonceStr"=>$noncestr, "signature"=>$signature ]; } /** * 根据网址获取微信公众号 * @param $host string 网址 */ public static function getWechatAccountByHost($host) { $webSite = WebSiteModel::query()->where("web_site_host",$host)->first(); if(!$webSite){ throw new CommonException(ErrorEnum::ERROR_EXIST_WEB_SITE); } $wechatAccount = WechatAccountModel::query()->find($webSite->wechat_account_id); if(!$wechatAccount){ throw new CommonException(ErrorEnum::ERROR_ACCOUNT_EXIST); } return $wechatAccount; } /** * 获取用户ID */ public static function getUserId() { return request()->header("user_id"); } /** * 从url中获取get参数,以数组形式返回 * @param $url */ public static function getParamsByUrl($url) { $flag = preg_match("/(.*)\?(.*)/",$url,$match); $tmp = []; if($flag){ $arrs = explode("&",$match[2]); foreach ($arrs as $arr){ $t = explode("=",$arr); $tmp[$t[0]] = $t[1]; } $tmp["url"]=$match[1]; } return $tmp; } /** * 获取订单编号 * @param $userId */ public static function getOrderNumber($userId) { return date("YmdHis").$userId.random_int(10000,99999); } }