Subject.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\cache;
  6. /**
  7. * 投票
  8. */
  9. class Subject extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. //进行中的唯一一个投票活动
  14. public function info(){
  15. $find = Db::name('vote_subject')->where('status',1)->order('id desc')->find();
  16. if(!$find){
  17. $this->error('没有进行中的投票活动');
  18. }
  19. $find = info_domain_image($find,['video_file']);
  20. $this->success(1,$find);
  21. }
  22. //选手列表
  23. public function playerlist(){
  24. $subject_id = Db::name('vote_subject')->where('status',1)->order('id desc')->value('id');
  25. if(!$subject_id){
  26. $this->success(1,[]);
  27. }
  28. $map = [
  29. 'subject_id' => $subject_id,
  30. 'status' => 1,
  31. ];
  32. //搜索
  33. $keyword = input('keyword','');
  34. if($keyword){
  35. $map['title|suozaidanwei|tuijiangonghui'] = ['LIKE','%'.$keyword.'%'];
  36. }
  37. $lists = Db::name('vote_player')
  38. ->where($map)->order('votes desc,id desc')->autopage()->select();
  39. //当前用户手里有票,按钮:投票
  40. //用户手里没有票,但是还能答题,按钮:答题加票
  41. //以上两者都没有,按钮:投票
  42. foreach($lists as $key => $val){
  43. $val['video_thumb'] = $val['video_file'].'?x-oss-process=video/snapshot,t_0,m_fast,f_jpg';
  44. $val['button'] = 'vote';
  45. $val['button'] = 'answer';
  46. }
  47. //$lists = $this->mingci($subject_id,$lists);
  48. $this->success(1,$lists);
  49. }
  50. //选手排行榜
  51. public function playerlist_votes(){
  52. $subject_id = Db::name('vote_subject')->where('status',1)->order('id desc')->value('id');
  53. if(!$subject_id){
  54. $this->success(1,[]);
  55. }
  56. $map = [
  57. 'subject_id' => $subject_id,
  58. 'status' => 1,
  59. ];
  60. $lists = Db::name('vote_player')
  61. ->where($map)->order('votes desc,id desc')->autopage()->select();
  62. $lists = $this->mingci($subject_id,$lists);
  63. $this->success(1,$lists);
  64. }
  65. //oss视频截贞 https://www.alibabacloud.com/help/zh/oss/user-guide/video-snapshots?spm=a2c63.p38356.0.0.6aaa4b78aOSpJH
  66. //使用fast模式截取视频7s处的内容,输出为JPG格式的图片,宽度为800,高度为600。
  67. //处理后的URL为:https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/video.mp4?x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast
  68. //选手详情
  69. public function playerinfo(){
  70. $player_id = input('player_id');
  71. if(!$player_id){
  72. $this->error();
  73. }
  74. $map = [
  75. 'p.id' => $player_id,
  76. ];
  77. $info = Db::name('vote_player')->alias('p')
  78. ->field('p.id,p.mobile,p.video_file,p.votes,p.renqi,p.subject_id')
  79. ->where($map)->find();
  80. $info = info_domain_image($info,['video_file']);
  81. //名次
  82. $players = Db::name('vote_player')->where(['subject_id'=>$info['subject_id'],'status'=>1])->order('votes desc')->column('id,votes');
  83. $id_arr = array_keys($players);
  84. $mingci = 1; //名次
  85. $mingci = array_search($info['id'],$id_arr);
  86. $mingci++;
  87. $info['mingci'] = $mingci;
  88. $this->success(1,$info);
  89. }
  90. //获得我的名次,票数导向
  91. private function mingci($subject_id,$list){
  92. if(empty($list)){
  93. return $list;
  94. }
  95. //准备数据
  96. $players = Db::name('vote_player')->where(['subject_id'=>$subject_id,'status'=>1])->order('votes desc,id desc')->column('id,votes');
  97. $id_arr = array_keys($players);
  98. foreach($list as $key => $val)
  99. {
  100. $mingci = 1; //名次
  101. $mingci = array_search($val['id'],$id_arr);
  102. $mingci++;
  103. //赋值名次
  104. $list[$key]['mingci'] = $mingci;
  105. }
  106. return $list;
  107. }
  108. }