菜谱项目

VestPublish.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Vest;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. class VestPublish extends Model
  7. {
  8. protected $table = 'vest_publish';
  9. public $timestamps = false;
  10. public static function getFindsInfo()
  11. {
  12. $http=config('constants.HOST_URL');
  13. $publish_info=DB::table('vest_publish')->where('status',1)->get();
  14. if (empty($publish_info)){
  15. return [];
  16. }
  17. $publish_info=json_decode($publish_info,true);
  18. if (count($publish_info)==1){
  19. //判断是图集还是视频
  20. if ($publish_info[0]['img_or_video']==2){
  21. //反串行化publish_info
  22. $publish_info[0]['publish_info']=unserialize($publish_info[0]['publish_info']);
  23. $publish_info[0]['publish_info'][0]=$http.$publish_info[0]['publish_info'][0];
  24. //马甲头像
  25. $publish_info[0]['img']=Vest::get_img_by_id($publish_info[0]['vest_id']);
  26. //视频个数
  27. $publish_info[0]['counts']=1;
  28. //点赞
  29. $datas=self::thumb_up($publish_info[0]['id']);
  30. $publish_info[0]['thumb_up_num']=$datas['thumb_up_num'];
  31. $publish_info[0]['thumb_up_imgs']=$datas['thumb_up_imgs'];
  32. }else{
  33. if (!empty($publish_info[0]['publish_info'])){
  34. $allimgs=unserialize($publish_info[0]['publish_info']);
  35. if (count($allimgs)==1){
  36. unset($publish_info[0]['publish_info']);
  37. $publish_info[0]['publish_info'][]=$http.$allimgs[0];
  38. //马甲头像
  39. $publish_info[0]['img']=Vest::get_img_by_id($publish_info[0]['vest_id']);
  40. //图集个数
  41. $publish_info[0]['counts']=1;
  42. //点赞
  43. $datas=self::thumb_up($publish_info[0]['id']);
  44. $publish_info[0]['thumb_up_num']=$datas['thumb_up_num'];
  45. $publish_info[0]['thumb_up_imgs']=$datas['thumb_up_imgs'];
  46. }else{
  47. unset($publish_info[0]['publish_info']);
  48. $publish_info[0]['publish_info']=self::add_http($allimgs);
  49. //马甲头像
  50. $publish_info[0]['img']=Vest::get_img_by_id($publish_info[0]['vest_id']);
  51. //图集个数
  52. $publish_info[0]['counts']=count($publish_info[0]['publish_info']);
  53. //点赞
  54. $datas=self::thumb_up($publish_info[0]['id']);
  55. $publish_info[0]['thumb_up_num']=$datas['thumb_up_num'];
  56. $publish_info[0]['thumb_up_imgs']=$datas['thumb_up_imgs'];
  57. }
  58. }
  59. }
  60. }else{
  61. foreach ($publish_info as $key=>$info){
  62. //判断是图集还是视频
  63. if ($info['img_or_video']==2){
  64. //反串行化publish_info
  65. $info['publish_info']=unserialize($info['publish_info']);
  66. $info['publish_info'][0]=$http.$info['publish_info'][0];
  67. //马甲头像
  68. $info['img']=Vest::get_img_by_id($info['vest_id']);
  69. //视频个数
  70. $info['counts']=1;
  71. //点赞
  72. $datas=self::thumb_up($publish_info[0]['id']);
  73. $info['thumb_up_num'] =$datas['thumb_up_num'];
  74. $info['thumb_up_imgs']=$datas['thumb_up_imgs'];
  75. $publish_info[$key]=$info;
  76. }else{
  77. if (!empty($info['publish_info'])){
  78. $allimgs=unserialize($info['publish_info']);
  79. if (count($allimgs)==1){
  80. $http=config('constants.HOST_URL');
  81. unset($info['publish_info']);
  82. $info['publish_info'][]=$http.$allimgs[0];
  83. //马甲头像
  84. $info['img']=Vest::get_img_by_id($info['vest_id']);
  85. //图集个数
  86. $info['counts']=1;
  87. //点赞
  88. $datas=self::thumb_up($publish_info[0]['id']);
  89. $info['thumb_up_num'] =$datas['thumb_up_num'];
  90. $info['thumb_up_imgs']=$datas['thumb_up_imgs'];
  91. $publish_info[$key]=$info;
  92. }else{
  93. unset($info['publish_info']);
  94. $info['publish_info']=self::add_http($allimgs);
  95. //马甲头像
  96. $info['img']=Vest::get_img_by_id($info['vest_id']);
  97. //图集个数
  98. $info['counts']=count($info['publish_info']);
  99. //点赞
  100. $datas=self::thumb_up($publish_info[0]['id']);
  101. $info['thumb_up_num'] =$datas['thumb_up_num'];
  102. $info['thumb_up_imgs']=$datas['thumb_up_imgs'];
  103. $publish_info[$key]=$info;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. return $publish_info;
  110. }
  111. /*
  112. * 拼接地址
  113. */
  114. public static function add_http($array){
  115. $http=config('constants.HOST_URL');
  116. $count = count($array);
  117. for($i=0;$i<$count;$i++){
  118. $array[$i] = $http. $array[$i];
  119. }
  120. return $array;
  121. }
  122. /*
  123. * 点赞处理
  124. */
  125. public static function thumb_up($vest_id){
  126. $http=config('constants.HOST_URL');
  127. //左下角共显示多少张马甲头像
  128. $vest_display_num=5;
  129. //点赞数上限
  130. $max_thumb_up_num=2000;
  131. //已启用的马甲 头像数组
  132. $vest_imgs=DB::table('vest')->where('status',1)->pluck('img');
  133. // $user_list=DB::table('collect_vest_publish')->where('status',1)->where('vest_publish_id',$vest_id)->pluck('user_id');
  134. $user_num=DB::table('collect_vest_publish')->where('status',1)->where('vest_publish_id',$vest_id)->count();
  135. // $user_img=DB::table('users')->whereIn('id',$user_list)->pluck('headimgurl');
  136. // $user_img=json_decode(json_encode($user_img),true);
  137. // $user_img=array_filter($user_img);
  138. $vest_imgs=json_decode($vest_imgs,true);
  139. // $vest_imgs=array_merge($user_img,$vest_imgs);
  140. //随机生成图集点赞数
  141. $thumb_up_num=intval(abs(crc32($vest_id)/1000000))+($vest_id%3*100)+$user_num;
  142. //$thumb_up_num=0;//测试数据
  143. //判断点赞数是否为空
  144. if ($thumb_up_num==0){
  145. $datas=array('thumb_up_num'=>$thumb_up_num,'thumb_up_imgs'=>[]);
  146. return $datas;
  147. }
  148. if ($thumb_up_num<=$vest_display_num){
  149. $thumb_up_imgs_keys=array_rand($vest_imgs,$thumb_up_num);
  150. if($thumb_up_num==1){
  151. $thumb_up_imgs[]=$http.$vest_imgs[$thumb_up_imgs_keys];
  152. }else{
  153. foreach($thumb_up_imgs_keys as $thumb_up_imgs_key){
  154. $thumb_up_imgs[]=$http.$vest_imgs[$thumb_up_imgs_key];
  155. }
  156. }
  157. }else{
  158. $thumb_up_imgs_keys=array_rand($vest_imgs,$vest_display_num);
  159. foreach($thumb_up_imgs_keys as $thumb_up_imgs_key){
  160. $thumb_up_imgs[]=$http.$vest_imgs[$thumb_up_imgs_key];
  161. }
  162. }
  163. $datas=array('thumb_up_num'=>$thumb_up_num,'thumb_up_imgs'=>$thumb_up_imgs);
  164. return $datas;
  165. }
  166. }