Live.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use Redis;
  5. /**
  6. * 模型
  7. */
  8. class Live extends Model
  9. {
  10. // 开启自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. /**
  15. * 根据liveid 获取 live 信息
  16. */
  17. public function getLiveInfoByLiveId($live_id,$type_id,$is_recommend,$all) {
  18. if(!$live_id) return false;
  19. // 判断派对是否存在
  20. $redis = new Redis();
  21. $redisconfig = config("redis");
  22. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  23. if(is_array($live_id)) {
  24. $redisLiveInfo = [];
  25. foreach($live_id as $k => $v) {
  26. $getredisLiveInfo = $redis->get('live_'.$v);
  27. if(!$getredisLiveInfo) {
  28. // redis 中数据丢失 (每天凌晨四点更新数据到数据库做备份用)
  29. $where = [];
  30. $where["a.id"] = $v;
  31. if($all != 1) {
  32. $type_id > 0 && $where["live_type"] = $type_id;
  33. $is_recommend == 1 && $where["is_recommend"] = 1;
  34. }
  35. $getredisLiveInfo = $this->alias("a")
  36. ->field("a.*,b.id as live_type,b.name as live_type_name,u.nickname,u.avatar,at.name as anchor_type_name")
  37. ->join("hx_live_type b","a.live_type = b.id","left")
  38. ->join("hx_user u","a.user_id = u.id","left")
  39. ->join("hx_user_anchor an","a.user_id = an.user_id","left")
  40. ->join("hx_user_anchor_type at","an.type_id = an.type_id")
  41. ->where($where)->find();
  42. $getredisLiveInfo && $redis->set('live_'.$live_id,json_encode($getredisLiveInfo));
  43. // $redisLiveInfo[] = $getredisLiveInfo;
  44. $foreachData = $getredisLiveInfo;
  45. } else {
  46. $foreachData = json_decode($getredisLiveInfo,true);
  47. }
  48. if(!$foreachData) {
  49. return [];
  50. }
  51. if(($is_recommend == 1 && $foreachData["is_recommend"] == 1) || ($type_id == $foreachData["live_type"]) || ($all == 1)) {
  52. $redisLiveInfo[] = [
  53. "id" => $foreachData["id"],
  54. "live_id" => $foreachData["live_id"],
  55. "live_notice" => $foreachData["live_notice"],
  56. "avatar" => $foreachData["avatar"],
  57. "live_hot" => $foreachData["live_hot"],
  58. "live_type_name" => $foreachData["live_type_name"],
  59. ];
  60. }
  61. }
  62. } else {
  63. $data = $redis->get('live_'.$live_id);
  64. if(!$data) {
  65. // redis 中数据丢失 (每天凌晨四点更新数据到数据库做备份用)
  66. $data = $this->where(["live_id"=>$live_id])->find();
  67. $redis->set('live_'.$live_id,json_encode($data));
  68. } else {
  69. $data = json_decode($data,true);
  70. }
  71. if(($is_recommend == 1 && $data["is_recommend"] == 1) || ($type_id == $data["live_type"]) || ($all == 1)) {
  72. $redisLiveInfo = [
  73. "id" => $data["id"],
  74. "live_id" => $data["live_id"],
  75. "live_notice" => $data["live_notice"],
  76. "avatar" => $data["avatar"],
  77. "live_hot" => $data["live_hot"],
  78. "live_type_name" => $data["live_type_name"],
  79. ];
  80. }
  81. }
  82. return $redisLiveInfo;
  83. }
  84. }