123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- <?php
- namespace App\Http\Utils;
- use App\Exceptions\CommonException;
- use App\Http\Enum\ErrorEnum;
- use App\Http\Enum\PlatformTypeEnum;
- use App\Http\Utils\Pdd\DuoDuoKeUtil;
- use App\Models\PlatformSidModel;
- use App\Models\WebSiteModel;
- use App\Models\WechatAccountModel;
- use Illuminate\Support\Facades\DB;
- use Tool\ShanTaoTool\HttpCurl;
- use Tool\ShanTaoTool\QiWeiTool;
- class BaseUtil
- {
-
- public static function listenSql()
- {
- DB::listen(function ($sql) {
- $i = 0;
- $bindings = $sql->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);
-
- LoggerFactoryUtil::addSqlMessage($rawSql);
- });
- }
-
- 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");
- }
-
- public static function getCacheFromRedis($key)
- {
-
- $redis = app("redis");
- return $redis->get($key);
- }
-
- public static function setRedisCache($key, $val, $ttl)
- {
-
- $redis = app("redis");
- if($ttl==0){
- return $redis->set($key,$val);
- }else{
- return $redis->setex($key,$ttl,$val);
- }
- }
-
- public static function setRedisLock($lokKey, $ttl)
- {
-
- $redis = app("redis")->client();
- $flag = $redis->set($lokKey,1,["nx","ex"=>$ttl]);
- if(!$flag){
- throw new CommonException(ErrorEnum::ERROR_REPEAT);
- }
- }
-
- public static function generateMeiTuanSid($userId)
- {
- return "meituanSpread:".$userId;
- }
-
- public static function generateJutuiKeSid($userId)
- {
- return "jtkupstream".$userId;
- }
-
- 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;
- }
- }
-
- 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"];
- }
-
- 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);
- }
-
- 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);
- $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
- ];
- }
-
- 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;
- }
-
- public static function getUserId()
- {
- return request()->header("user_id");
- }
-
- 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;
- }
-
- public static function getOrderNumber($userId)
- {
- return date("YmdHis").$userId.random_int(10000,99999);
- }
- }
|