You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
386 lines
13 KiB
386 lines
13 KiB
<?php
|
|
|
|
|
|
namespace app\api\logic;
|
|
|
|
|
|
use app\common\basics\Logic;
|
|
use app\common\model\content\Article;
|
|
use app\common\model\content\ArticleCategory;
|
|
use app\common\model\content\Resource;
|
|
use app\common\model\content\ResourceCategory;
|
|
use app\common\model\user\User;
|
|
use app\common\model\user\UserResource;
|
|
use app\common\model\vote\Vote;
|
|
use app\common\model\vote\VoteCategory;
|
|
use app\common\model\vote\VoteLog;
|
|
use app\common\model\vote\VotePlayer;
|
|
use app\common\server\AreaServer;
|
|
use app\common\server\UrlServer;
|
|
use think\Db;
|
|
|
|
class VoteLogic extends Logic
|
|
{
|
|
|
|
/**
|
|
* @Notes: 资料分类
|
|
* @Author: 张无忌
|
|
* @param $get
|
|
* @return array
|
|
*/
|
|
public static function category($get)
|
|
{
|
|
try {
|
|
$model = new VoteCategory();
|
|
$where = [];
|
|
|
|
if(isset($get['pid']) ){
|
|
$where[] = ['pid','=',$get['pid']];
|
|
}
|
|
|
|
return $model->field(['id', 'name'])
|
|
->where([
|
|
['del', '=', 0],
|
|
['is_show', '=', 1]
|
|
])->where($where)->select()->toArray();
|
|
|
|
} catch (\Exception $e) {
|
|
return ['error'=>$e->getMessage()];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @Notes: 文章列表
|
|
* @Author: 张无忌
|
|
* @param $get
|
|
* @return array
|
|
*/
|
|
public static function lists($get)
|
|
{
|
|
try {
|
|
|
|
$where = [
|
|
['status', '=', 1],
|
|
['del', '=', 0],
|
|
];
|
|
if(isset($get['cid']) && $get['cid']){
|
|
$where[] = ['cid', '=', $get['cid']];
|
|
}
|
|
$order = [
|
|
'add_time' => 'asc'
|
|
];
|
|
|
|
$model = new Vote();
|
|
|
|
$count = $model->alias('a')->where($where)->count();
|
|
|
|
$list = $model->alias('b')
|
|
->where($where)
|
|
->order($order)
|
|
->page($get['page_no'], $get['page_size'])
|
|
->select()
|
|
->toArray();
|
|
|
|
foreach ($list as &$item) {
|
|
$item['status_show'] = $item['start_time']>time()?"未开始":($item['end_time']<time() ? "已结束":"进行中" );
|
|
$item['start_time_str'] = $item['start_time']? date("Y-m-d",$item['start_time']):"";
|
|
$item['end_time_str'] = $item['end_time']? date("Y-m-d",$item['end_time']):"";
|
|
$item['add_time'] = $item['add_time']? date("Y-m-d H:i:s",$item['add_time']):"";
|
|
$item['is_rank'] = $item['is_rank'] ? '显示' : '隐藏';
|
|
$item['is_redo'] = $item['is_redo']==0 ? '当天不可重复' : ($item['is_redo']==1?"全部不重复":"可重复");
|
|
$item['vote_str'] = ($item['vote_type']==0 ? '每天' : '总共').$item['vote_num']."票";
|
|
|
|
}
|
|
|
|
$more = is_more($count, $get['page_no'], $get['page_size']);
|
|
$data = [
|
|
'list' => $list,
|
|
'page_no' => $get['page_no'],
|
|
'page_size' => $get['page_size'],
|
|
'count' => $count,
|
|
'more' => $more
|
|
];
|
|
return $data;
|
|
|
|
} catch (\Exception $e) {
|
|
return ['error'=>$e->getMessage()];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @Notes: 文章列表
|
|
* @Author: 张无忌
|
|
* @param $get
|
|
* @return array
|
|
*/
|
|
public static function rank($get)
|
|
{
|
|
|
|
$where = [
|
|
['status', '=', 1],
|
|
['vote_id',"=",$get['id']]
|
|
];
|
|
$order = [
|
|
// 'vote_num' => 'desc',
|
|
'views' => 'desc'
|
|
];
|
|
|
|
$model = new VotePlayer();
|
|
|
|
|
|
$list = $model->alias('b')
|
|
->where($where)
|
|
->orderRaw("vote_num+base_vote desc")
|
|
->order($order)
|
|
->limit(50)
|
|
->select()
|
|
->toArray();
|
|
|
|
foreach ($list as &$item ){
|
|
$item['vote_num'] = $item['vote_num'] + $item['base_vote'];
|
|
}
|
|
|
|
$data = [
|
|
'list' => $list
|
|
];
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @Notes: 文章详细
|
|
* @Author: 张无忌
|
|
* @param $id
|
|
* @return array
|
|
*/
|
|
public static function detail($id,$uid)
|
|
{
|
|
$article = Vote::field('*')->where('id', $id)->findOrEmpty();
|
|
$palers = new VotePlayer();
|
|
if($article->isEmpty()) {
|
|
$article = [];
|
|
}else{
|
|
$article->views = $article->views + 1;
|
|
$article->save();
|
|
$article = $article->toArray();
|
|
$article['status_show'] = $article['start_time']<time()?"未开始":($article['end_time']<time() ? "已结束":"进行中" );
|
|
$article['start_time_str'] = $article['start_time']? date("Y-m-d H:i",$article['start_time']):"";
|
|
$article['end_time_str'] = $article['end_time']? date("Y-m-d H:i",$article['end_time']):"";
|
|
$article['add_time'] = $article['add_time']? date("Y-m-d H:i",$article['add_time']):"";
|
|
$article['is_rank'] = $article['is_rank'] ? '显示' : '隐藏';
|
|
$article['is_redo'] = $article['is_redo']==0 ? '当天不可重复' : ($article['is_redo']==1?"全部不重复":"可重复");
|
|
$article['vote_str'] = ($article['vote_type']==0 ? '每天' : '总共').$article['vote_num']."票";
|
|
$article['user_vote_count'] = VoteLogic::getUserPlayerVoteNums($article['id'],$uid,$article['vote_type']);
|
|
// 使用正则表达式进行替换
|
|
$pattern = '/<a[^>]+href=["\'](.*?\.(?:mp4|wav))["\'][^>]*>(.*?)<\/a>/i';
|
|
$replacement = '<video src="$1"></video>';
|
|
$article['content'] = preg_replace($pattern, $replacement, $article['content']);
|
|
$article['views'] = $article['views'] +$article['base_visit'] ;
|
|
// $article['images'] = explode(",",$article['images']);
|
|
if($article['images'] ){
|
|
$article['images'] = explode(",",$article['images']);
|
|
}else{
|
|
$article['images'] = [];
|
|
}
|
|
|
|
$article['vote_total'] = $article['vote_total'] + $article['base_vote'];
|
|
$article['player_count'] = $palers->where("vote_id","=",$id)->where("status",'=',1)->where("del","=",0)->count();
|
|
}
|
|
|
|
return $article;
|
|
}
|
|
|
|
public static function players($get,$uid)
|
|
{
|
|
$article = Vote::field('*')->where('id', $get['id'])->findOrEmpty();
|
|
|
|
$where = [
|
|
['status', '=', 1],
|
|
['vote_id',"=",$get['id']]
|
|
];
|
|
if(!empty($get['keyword']) && trim($get['keyword'])){
|
|
$where[] = ['name',"like","%{$get['keyword']}%"];
|
|
}
|
|
$order = [
|
|
'add_time' => 'asc'
|
|
];
|
|
|
|
$model = new VotePlayer();
|
|
|
|
$count = $model->alias('a')->where($where)->count();
|
|
|
|
$list = $model->alias('b')
|
|
->where($where)
|
|
->order($order)
|
|
->page($get['page_no'], $get['page_size'])
|
|
->select()
|
|
->toArray();
|
|
foreach ($list as &$item) {
|
|
$item['can_vote'] = VoteLogic::getPlayerVoteStatus($article['id'],$item['id'],$uid,$article['is_redo']);
|
|
$item['vote_num'] = $item['vote_num'] + $item['base_vote'];
|
|
}
|
|
|
|
$more = is_more($count, $get['page_no'], $get['page_size']);
|
|
$data = [
|
|
'list' => $list,
|
|
'page_no' => $get['page_no'],
|
|
'page_size' => $get['page_size'],
|
|
'count' => $count,
|
|
'more' => $more
|
|
];
|
|
return $data;
|
|
}
|
|
|
|
public static function playersDetail($id,$uid)
|
|
{
|
|
$article = VotePlayer::field('*')->where('id', $id)->findOrEmpty();
|
|
$vote = Vote::field('*')->where('id', $article['vote_id'])->findOrEmpty();
|
|
$mode = new VotePlayer();
|
|
if($article->isEmpty()) {
|
|
$article = [];
|
|
}else{
|
|
$article->views = $article->views + 1;
|
|
$article->save();
|
|
$article = $article->toArray();
|
|
$article['can_vote'] = VoteLogic::getPlayerVoteStatus($vote['id'],$article['id'],$uid,$vote['is_redo']);
|
|
$article['user_vote_count'] = VoteLogic::getUserPlayerVoteNums($vote['id'],$uid,$vote['vote_type']);
|
|
$article['title'] = $vote['title'];
|
|
$article['start_time'] = $vote['start_time'];
|
|
$article['end_time'] = $vote['end_time'];
|
|
if($article['images'] ){
|
|
$article['images'] = explode(",",$article['images']);
|
|
}else{
|
|
$article['images'] = [];
|
|
}
|
|
|
|
//->where("vote_num",">",$article['vote_num'])
|
|
$article['rank'] = $mode->where('vote_id', $article['vote_id'])->where('(vote_num+base_vote)>'.($article['vote_num']+$article['base_vote']))->count() + 1;
|
|
if($article['rank']>1){
|
|
$last = $mode->field("*")->where('vote_id', $article['vote_id'])
|
|
->where('(vote_num+base_vote)>'.($article['vote_num']+$article['base_vote']))
|
|
// ->where("vote_num",">",$article['vote_num'])
|
|
->orderRaw("vote_num+base_vote asc")
|
|
->limit(1)->select();
|
|
$article['rank_last'] = ($last[0]['vote_num'] +$last[0]['base_vote'] ) - ($article['vote_num']+$article['base_vote']);
|
|
}else{
|
|
$article['rank_last'] = 0;
|
|
}
|
|
|
|
$article['vote_num'] = $article['vote_num'] + $article['base_vote'];
|
|
|
|
}
|
|
|
|
return $article;
|
|
}
|
|
|
|
|
|
public static function vote($id,$uid)
|
|
{
|
|
$article = VotePlayer::field('*')->where('id', $id)->findOrEmpty();
|
|
$vote = Vote::field('*')->where('id', $article['vote_id'])->findOrEmpty();
|
|
if($article->isEmpty() || $vote->isEmpty()) {
|
|
return [
|
|
'msg' => '投票成功',
|
|
'data' => 1
|
|
];
|
|
}
|
|
$can_vote = VoteLogic::getPlayerVoteStatus($vote['id'],$article['id'],$uid,$vote['is_redo']);
|
|
$user_count = VoteLogic::getUserPlayerVoteNums($vote['id'],$uid,$vote['vote_type']);
|
|
if( !$can_vote ) {
|
|
return [
|
|
'msg' => '您已经给TA投过票啦!!',
|
|
'data' => 0
|
|
];
|
|
}
|
|
|
|
if($vote['vote_num']<=$user_count){
|
|
return [
|
|
'msg' => '您'.($vote['vote_type']==0?'今日':'所有').'的投票数量已达上限!!',
|
|
'data' => 0
|
|
];
|
|
}
|
|
|
|
$data = [
|
|
"user_id" => $uid,
|
|
"vid" => $vote['id'],
|
|
"pid" => $article['id'],
|
|
"create_time" => time(),
|
|
"update_time" => time(),
|
|
];
|
|
$voteLog = new VoteLog();
|
|
$res = $voteLog->insert($data);
|
|
if($res){
|
|
|
|
//总投票数和选手投票数+1
|
|
$article->vote_num = $article->vote_num + 1;
|
|
$article->save();
|
|
|
|
$vote->vote_total = $vote->vote_total + 1;
|
|
$vote->save();
|
|
return [
|
|
'msg' => '投票成功',
|
|
'data' => 1
|
|
];
|
|
|
|
}else{
|
|
return [
|
|
'msg' => '投票失败',
|
|
'data' => 0
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
public static function getPlayerVoteStatus($id=0,$player_id=0,$uid=0,$is_redo=0){
|
|
$voteLog = new VoteLog();
|
|
$can_vote = 1;
|
|
//当天不重复 统计当天是否给该选手投过票即可
|
|
if($is_redo == 0 ){
|
|
$where = [
|
|
["vid","=",$id],
|
|
["pid","=",$player_id],
|
|
["user_id","=",$uid],
|
|
["create_time",">",strtotime(date("Y-m-d"))]
|
|
];
|
|
|
|
$count = $voteLog->where($where)->count();
|
|
if($count){
|
|
$can_vote = 0;
|
|
}
|
|
}else if($is_redo = 1){ // 全部不重复 则统计活动该用户是否给过选手投票
|
|
$where = [
|
|
["vid","=",$id],
|
|
["pid","=",$player_id],
|
|
["user_id","=",$uid],
|
|
];
|
|
|
|
$count = $voteLog->where($where)->count();
|
|
if($count){
|
|
$can_vote = 0;
|
|
}
|
|
}
|
|
return $can_vote;
|
|
}
|
|
|
|
public static function getUserPlayerVoteNums($id=0,$uid=0,$vote_type=0){
|
|
$voteLog = new VoteLog();
|
|
$count = 0;
|
|
//当天不重复 统计当天是否给该选手投过票即可
|
|
if($vote_type == 0 ){
|
|
$where = [
|
|
["vid","=",$id],
|
|
["user_id","=",$uid],
|
|
["create_time",">",strtotime(date("Y-m-d"))]
|
|
];
|
|
$count = $voteLog->where($where)->count();
|
|
}else if($vote_type = 1){ // 全部不重复 则统计活动该用户是否给过选手投票
|
|
$where = [
|
|
["vid","=",$id],
|
|
["user_id","=",$uid],
|
|
];
|
|
$count = $voteLog->where($where)->count();
|
|
}
|
|
return $count;
|
|
}
|
|
|
|
|
|
}
|
|
|