BaseUtil.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Http\Utils;
  3. use App\Exceptions\CommonException;
  4. use App\Http\Enum\ErrorEnum;
  5. use App\Http\Enum\PlatformTypeEnum;
  6. use App\Models\PlatformSidModel;
  7. use Illuminate\Support\Facades\DB;
  8. class BaseUtil
  9. {
  10. /**
  11. * sql监听
  12. */
  13. public static function listenSql()
  14. {
  15. DB::listen(function ($sql) {
  16. $i = 0;
  17. $bindings = $sql->bindings;
  18. $rawSql = preg_replace_callback('/\?/', function ($matches) use ($bindings, &$i) {
  19. $item = isset($bindings[$i]) ? $bindings[$i] : $matches[0];
  20. $i++;
  21. return gettype($item) == 'string' ? "'$item'" : $item;
  22. }, $sql->sql);
  23. //记录sql
  24. LoggerFactoryUtil::addSqlMessage($rawSql);
  25. // echo $rawSql, "\n<br /><br />\n";
  26. });
  27. }
  28. /**
  29. * 获取应用编号
  30. */
  31. public static function getAppCodeByQuery()
  32. {
  33. return request()->input("wechat_app_code");
  34. }
  35. /**
  36. * 从redis中获取数据
  37. * @param $key string 键
  38. */
  39. public static function getCacheFromRedis($key)
  40. {
  41. /**
  42. * @var \Redis $redis
  43. */
  44. $redis = app("redis");
  45. return $redis->get($key);
  46. }
  47. /**
  48. * 设置redis数据的过期时间
  49. * @param $key string 键
  50. * @param $val string 值
  51. * @param $ttl int 过期时间
  52. */
  53. public static function setRedisCache($key, $val, $ttl)
  54. {
  55. /**
  56. * @var \Redis $redis
  57. */
  58. $redis = app("redis");
  59. if($ttl==0){
  60. $redis->set($key,$val);
  61. }else{
  62. $redis->setex($key,$ttl,$val);
  63. }
  64. }
  65. /**
  66. * 获取redis锁
  67. * @param $lokKey string 键
  68. * @param $ttl int 过期时间
  69. */
  70. public static function setRedisLock($lokKey, $ttl)
  71. {
  72. /**
  73. * @var \Redis $redis
  74. */
  75. $redis = app("redis");
  76. $flag = $redis->set($lokKey,1,["nx","ex"=>$ttl]);
  77. if(!$flag){
  78. throw new CommonException(ErrorEnum::ERROR_REDIS_LOCK);
  79. }
  80. }
  81. /**
  82. * 生成美团推广位
  83. * @param $userId int 用户ID
  84. */
  85. public static function generateMeiTuanSid($userId)
  86. {
  87. return "meituanSpread:".$userId;
  88. }
  89. /**
  90. * 获取用户的平台推广位
  91. * @param $platformType int 平台
  92. * @param $userId int 用户ID
  93. */
  94. public static function getPlatformUserSpreadId($platformType,$userId,$userSpreadId="")
  95. {
  96. //获取平台推广位
  97. $platformSid = PlatformSidModel::query()
  98. ->where("user_id",$userId)
  99. ->where("platform_type",$platformType)
  100. ->first();
  101. if($platformSid){
  102. //存在则直接返回
  103. return $platformSid->platform_sid;
  104. }else{
  105. switch ($platformType){
  106. case PlatformTypeEnum::PLATFORM_TAOBAO:
  107. //淘宝
  108. break;
  109. case PlatformTypeEnum::PLATFORM_PINGDUODUO:
  110. //拼多多
  111. break;
  112. case PlatformTypeEnum::PLATFORM_JINGDONG:
  113. //京东
  114. break;
  115. case PlatformTypeEnum::PLATFORM_MEITUAN:
  116. //美团
  117. $sid = self::generateMeiTuanSid($userId);
  118. break;
  119. case PlatformTypeEnum::PLATFORM_FANBUTING:
  120. //平台
  121. break;
  122. default:
  123. throw new CommonException(ErrorEnum::ERROR_EXIST_PLATFORM);
  124. }
  125. if(!$userSpreadId){
  126. //不存在用户自传的推广位,则默认和平台的推广位相等
  127. $userSpreadId = $sid;
  128. }
  129. //写入用户的推广位
  130. PlatformSidModel::query()->insert(
  131. [
  132. "user_id"=>$userId,
  133. "user_sid"=>$userSpreadId,
  134. "platform_type"=>$platformType,
  135. "platform_sid"=>$sid,
  136. "created_at"=>date("Y-m-d H:i:s"),
  137. "updated_at"=>date("Y-m-d H:i:s")
  138. ]
  139. );
  140. return $sid;
  141. }
  142. }
  143. }