Subject.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\cache;
  6. use app\utils\RedisKeyEnum;
  7. use app\utils\RedisUtil;
  8. /**
  9. * 投票
  10. */
  11. class Subject extends Api
  12. {
  13. protected $noNeedLogin = ['*'];
  14. protected $noNeedRight = ['*'];
  15. //进行中的唯一一个投票活动
  16. public function info(){
  17. $find = Db::name('vote_subject')->where('id',1)->find();
  18. $find['image'] = localpath_to_netpath($find['image']);
  19. if(!$find){
  20. $this->error('没有进行中的投票活动');
  21. }
  22. $this->success(1,$find);
  23. }
  24. //作品页,选手列表
  25. public function playerlist(){
  26. $subject_id = 1;
  27. $map = [
  28. 'subject_id' => $subject_id,
  29. 'status' => 1,
  30. ];
  31. //搜索
  32. $keyword = input('keyword','');
  33. if($keyword){
  34. $map['title|suozaidanwei|tuijiangonghui'] = ['LIKE','%'.$keyword.'%'];
  35. }
  36. $lists = Db::name('vote_player')
  37. ->where($map)->order('votes desc,id desc')->autopage()->select(); //选手不多,看看不加分页了,直接缓存10秒
  38. // $button = 'answer';
  39. //当前用户手里有票,按钮:投票
  40. //用户手里没有票,但是今天还能答题,按钮:答题加票
  41. /*$user_votes = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('votes');
  42. $user_question_count = Db::name('user_question_log')->where('user_id',$this->auth->id)->whereTime('createtime','today')->count('id');
  43. $exam_times_user_eday = config('site.exam_times_user_eday');
  44. if($user_votes == 0 && $user_question_count <= $exam_times_user_eday){
  45. $button = 'answer';
  46. }*/
  47. //以上两者都没有,按钮:投票
  48. foreach($lists as $key => $val){
  49. $val['video_file'] = localpath_to_netpath($val['video_file']);
  50. $val['video_thumb'] = '';
  51. if(!empty($val['video_file'])){
  52. $val['video_thumb'] = $val['video_file'].'?x-oss-process=video/snapshot,t_0,m_fast,f_jpg';
  53. }
  54. // $val['button'] = $button;
  55. $lists[$key] = $val;
  56. }
  57. //$lists = $this->mingci($subject_id,$lists);
  58. $this->success(1,$lists);
  59. }
  60. //选手详情
  61. public function playerinfo(){
  62. $player_id = input('player_id','');
  63. if(!$player_id){
  64. $this->error();
  65. }
  66. $map = [
  67. 'id' => $player_id,
  68. ];
  69. $info = Db::name('vote_player')->where($map)->find();
  70. $info['video_thumb'] = '';
  71. if(!empty($info['video_file'])){
  72. $info['video_file'] = localpath_to_netpath($info['video_file']);
  73. $info['video_thumb'] = $info['video_file'].'?x-oss-process=video/snapshot,t_0,m_fast,f_jpg';
  74. }
  75. //获取播放地址和秒数
  76. $videoVod = new \app\common\library\Video();
  77. $video_PlayInfo = $videoVod->getPlayInfo($info['vodid']);
  78. $video_PlayInfo = $video_PlayInfo->PlayInfoList->PlayInfo;
  79. $info['video_file'] = $video_PlayInfo[0]->PlayURL;
  80. $info['video_seconds'] = ceil($video_PlayInfo[0]->Duration);
  81. //名次
  82. /*$players = Db::name('vote_player')->where(['subject_id'=>$info['subject_id'],'status'=>1])->order('votes desc,id 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. public function getuserinfo(){
  92. $info = $this->auth->getUserinfo();
  93. //今天,投了几票
  94. $today_record = RedisUtil::getInstance(RedisKeyEnum::VOTE_RECORD.date('Y-m-d').':'.$this->auth->id)->get();
  95. //今天,免费的的票
  96. $gift_votes = config('site.gift_votes_user_eday');
  97. //今天,用户答对的次数,也就是答题获得的票数
  98. $question_vote = RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$this->auth->id)->get();
  99. //基础票数
  100. $jcps = $gift_votes - $today_record;
  101. if($jcps < 0){
  102. $jcps = 0;
  103. }
  104. //答题票数
  105. $dtps = $question_vote - ( ($today_record - $gift_votes) <= 0 ? 0 : ($today_record - $gift_votes) );
  106. if($dtps < 0){
  107. $dtps = 0;
  108. }
  109. //今日投票记录
  110. $player_ids = Db::name('vote_record')->where('user_id',$this->auth->id)->where('createdate',strtotime(date('Y-m-d')))->column('player_id');
  111. $map = [
  112. 'status' => 1,
  113. 'id' => ['IN',$player_ids]
  114. ];
  115. $lists = Db::name('vote_player')->field('id,title,votes')
  116. ->where($map)->order('votes desc,id desc')->select();
  117. //
  118. $result = [
  119. 'info' => $info,
  120. 'jcps' => $jcps,
  121. 'dtps' => $dtps,
  122. 'lists' => $lists,
  123. ];
  124. $this->success(1,$result);
  125. }
  126. //选手票数排行榜,按票
  127. public function playerlist_votes(){
  128. $subject_id = 1;
  129. $map = [
  130. 'subject_id' => $subject_id,
  131. 'status' => 1,
  132. ];
  133. $lists = Db::name('vote_player')->field('id,title,votes')
  134. ->where($map)->order('votes desc,id desc')->autopage()->select();
  135. $lists = $this->mingci_vote($subject_id,$lists);
  136. $this->success(1,$lists);
  137. }
  138. //选手票数排行榜,按分
  139. public function playerlist_score(){
  140. $subject_id = 1;
  141. $map = [
  142. 'subject_id' => $subject_id,
  143. 'status' => 1,
  144. ];
  145. $lists = Db::name('vote_player')->field('id,title,score')
  146. ->where($map)->order('score desc,id desc')->autopage()->select();
  147. $lists = $this->mingci_score($subject_id,$lists);
  148. $this->success(1,$lists);
  149. }
  150. //oss视频截贞 https://www.alibabacloud.com/help/zh/oss/user-guide/video-snapshots?spm=a2c63.p38356.0.0.6aaa4b78aOSpJH
  151. //使用fast模式截取视频7s处的内容,输出为JPG格式的图片,宽度为800,高度为600。
  152. //处理后的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
  153. //获得我的名次,票数导向
  154. private function mingci_vote($subject_id,$list){
  155. if(empty($list)){
  156. return $list;
  157. }
  158. //准备数据
  159. $players = Db::name('vote_player')->where(['subject_id'=>$subject_id,'status'=>1])->order('votes desc,id desc')->column('id,votes');
  160. $id_arr = array_keys($players);
  161. foreach($list as $key => $val)
  162. {
  163. $mingci = 1; //名次
  164. $mingci = array_search($val['id'],$id_arr);
  165. $mingci++;
  166. //赋值名次
  167. $list[$key]['mingci'] = $mingci;
  168. }
  169. return $list;
  170. }
  171. //获得我的名次,分数导向
  172. private function mingci_score($subject_id,$list){
  173. if(empty($list)){
  174. return $list;
  175. }
  176. //准备数据
  177. $players = Db::name('vote_player')->where(['subject_id'=>$subject_id,'status'=>1])->order('score desc,id desc')->column('id,score');
  178. $id_arr = array_keys($players);
  179. foreach($list as $key => $val)
  180. {
  181. $mingci = 1; //名次
  182. $mingci = array_search($val['id'],$id_arr);
  183. $mingci++;
  184. //赋值名次
  185. $list[$key]['mingci'] = $mingci;
  186. }
  187. return $list;
  188. }
  189. }