123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace App\Http\Utils;
- use App\Exceptions\CommonException;
- use App\Http\Enum\ErrorEnum;
- use App\Http\Enum\PlatformTypeEnum;
- use App\Models\PlatformSidModel;
- use Illuminate\Support\Facades\DB;
- class BaseUtil
- {
- /**
- * sql监听
- */
- 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);
- //记录sql
- LoggerFactoryUtil::addSqlMessage($rawSql);
- // echo $rawSql, "\n<br /><br />\n";
- });
- }
- /**
- * 获取应用编号
- */
- 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){
- $redis->set($key,$val);
- }else{
- $redis->setex($key,$ttl,$val);
- }
- }
- /**
- * 获取redis锁
- * @param $lokKey string 键
- * @param $ttl int 过期时间
- */
- public static function setRedisLock($lokKey, $ttl)
- {
- /**
- * @var \Redis $redis
- */
- $redis = app("redis");
- $flag = $redis->set($lokKey,1,["nx","ex"=>$ttl]);
- if(!$flag){
- throw new CommonException(ErrorEnum::ERROR_REDIS_LOCK);
- }
- }
- /**
- * 生成美团推广位
- * @param $userId int 用户ID
- */
- public static function generateMeiTuanSid($userId)
- {
- return "meituanSpread:".$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:
- //拼多多
- break;
- case PlatformTypeEnum::PLATFORM_JINGDONG:
- //京东
- break;
- case PlatformTypeEnum::PLATFORM_MEITUAN:
- //美团
- $sid = self::generateMeiTuanSid($userId);
- break;
- case PlatformTypeEnum::PLATFORM_FANBUTING:
- //平台
- 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;
- }
- }
- }
|