Subject.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. //选手列表
  37. $lists = Db::name('vote_player')
  38. ->where($map)->order('votes desc,id desc')->autopage()->select();
  39. foreach($lists as $key => $val){
  40. $val['video_file'] = localpath_to_netpath($val['video_file']);
  41. $val['video_thumb'] = '';
  42. if(!empty($val['video_file'])){
  43. $val['video_thumb'] = $val['video_file'].'?x-oss-process=video/snapshot,t_0,m_fast,f_jpg';
  44. }
  45. $lists[$key] = $val;
  46. }
  47. //$lists = $this->mingci($subject_id,$lists);
  48. //今日剩余票数
  49. $today_my_vote = 0;
  50. if($this->auth->isLogin()){
  51. //今天,投了几票
  52. $today_record = RedisUtil::getInstance(RedisKeyEnum::VOTE_RECORD.date('Y-m-d').':'.$this->auth->id)->get();
  53. //今天,免费的的票
  54. $gift_votes = config('site.gift_votes_user_eday');
  55. //今天,用户答对的次数,也就是答题获得的票数
  56. $question_vote = RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$this->auth->id)->get();
  57. //今日剩余票数
  58. $today_my_vote = $gift_votes + $question_vote - $today_record;
  59. }
  60. //
  61. $result = [
  62. 'today_my_vote' => $today_my_vote,
  63. 'players' => $lists,
  64. ];
  65. $this->success(1,$result);
  66. }
  67. //选手详情
  68. public function playerinfo(){
  69. $player_id = input('player_id','');
  70. if(!$player_id){
  71. $this->error();
  72. }
  73. $map = [
  74. 'id' => $player_id,
  75. ];
  76. $info = Db::name('vote_player')->where($map)->find();
  77. $info['video_thumb'] = '';
  78. if(!empty($info['video_file'])){
  79. $info['video_file'] = localpath_to_netpath($info['video_file']);
  80. $info['video_thumb'] = $info['video_file'].'?x-oss-process=video/snapshot,t_0,m_fast,f_jpg';
  81. }
  82. //获取播放地址和秒数
  83. $videoVod = new \app\common\library\Video();
  84. $video_PlayInfo = $videoVod->getPlayInfo($info['vodid']);
  85. $video_PlayInfo = $video_PlayInfo->PlayInfoList->PlayInfo;
  86. $info['video_file'] = $video_PlayInfo[0]->PlayURL;
  87. $info['video_seconds'] = ceil($video_PlayInfo[0]->Duration);
  88. //名次
  89. /*$players = Db::name('vote_player')->where(['subject_id'=>$info['subject_id'],'status'=>1])->order('votes desc,id desc')->column('id,votes');
  90. $id_arr = array_keys($players);
  91. $mingci = 1; //名次
  92. $mingci = array_search($info['id'],$id_arr);
  93. $mingci++;
  94. $info['mingci'] = $mingci;*/
  95. $this->success(1,$info);
  96. }
  97. //用户详细资料
  98. public function getuserinfo(){
  99. $info = $this->auth->getUserinfo();
  100. //今天,投了几票
  101. $today_record = RedisUtil::getInstance(RedisKeyEnum::VOTE_RECORD.date('Y-m-d').':'.$this->auth->id)->get();
  102. //今天,免费的的票
  103. $gift_votes = config('site.gift_votes_user_eday');
  104. //今天,用户答对的次数,也就是答题获得的票数
  105. $question_vote = RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$this->auth->id)->get();
  106. //基础票数
  107. $jcps = $gift_votes - $today_record;
  108. if($jcps < 0){
  109. $jcps = 0;
  110. }
  111. //答题票数
  112. $dtps = $question_vote - ( ($today_record - $gift_votes) <= 0 ? 0 : ($today_record - $gift_votes) );
  113. if($dtps < 0){
  114. $dtps = 0;
  115. }
  116. //今日投票记录
  117. $player_ids = Db::name('vote_record')->where('user_id',$this->auth->id)->where('createdate',strtotime(date('Y-m-d')))->column('player_id');
  118. $map = [
  119. 'status' => 1,
  120. 'id' => ['IN',$player_ids]
  121. ];
  122. $lists = Db::name('vote_player')->field('id,title,votes')
  123. ->where($map)->order('votes desc,id desc')->select();
  124. //
  125. $result = [
  126. 'info' => $info,
  127. 'jcps' => $jcps,
  128. 'dtps' => $dtps,
  129. 'lists' => $lists,
  130. ];
  131. $this->success(1,$result);
  132. }
  133. //选手票数排行榜,按票
  134. public function playerlist_votes(){
  135. $subject_id = 1;
  136. $map = [
  137. 'subject_id' => $subject_id,
  138. 'status' => 1,
  139. ];
  140. $lists = Db::name('vote_player')->field('id,title,votes')
  141. ->where($map)->order('votes desc,id desc')->autopage()->select();
  142. $lists = $this->mingci_vote($subject_id,$lists);
  143. $this->success(1,$lists);
  144. }
  145. //选手票数排行榜,按分
  146. public function playerlist_score(){
  147. $subject_id = 1;
  148. $map = [
  149. 'subject_id' => $subject_id,
  150. 'status' => 1,
  151. ];
  152. $lists = Db::name('vote_player')->field('id,title,score')
  153. ->where($map)->order('score desc,id desc')->autopage()->select();
  154. $lists = $this->mingci_score($subject_id,$lists);
  155. $this->success(1,$lists);
  156. }
  157. //oss视频截贞 https://www.alibabacloud.com/help/zh/oss/user-guide/video-snapshots?spm=a2c63.p38356.0.0.6aaa4b78aOSpJH
  158. //使用fast模式截取视频7s处的内容,输出为JPG格式的图片,宽度为800,高度为600。
  159. //处理后的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
  160. //获得我的名次,票数导向
  161. private function mingci_vote($subject_id,$list){
  162. if(empty($list)){
  163. return $list;
  164. }
  165. //准备数据
  166. $players = Db::name('vote_player')->where(['subject_id'=>$subject_id,'status'=>1])->order('votes desc,id desc')->column('id,votes');
  167. $id_arr = array_keys($players);
  168. foreach($list as $key => $val)
  169. {
  170. $mingci = 1; //名次
  171. $mingci = array_search($val['id'],$id_arr);
  172. $mingci++;
  173. //赋值名次
  174. $list[$key]['mingci'] = $mingci;
  175. }
  176. return $list;
  177. }
  178. //获得我的名次,分数导向
  179. private function mingci_score($subject_id,$list){
  180. if(empty($list)){
  181. return $list;
  182. }
  183. //准备数据
  184. $players = Db::name('vote_player')->where(['subject_id'=>$subject_id,'status'=>1])->order('score desc,id desc')->column('id,score');
  185. $id_arr = array_keys($players);
  186. foreach($list as $key => $val)
  187. {
  188. $mingci = 1; //名次
  189. $mingci = array_search($val['id'],$id_arr);
  190. $mingci++;
  191. //赋值名次
  192. $list[$key]['mingci'] = $mingci;
  193. }
  194. return $list;
  195. }
  196. }