| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\MemberCollectModel;
- use App\Models\VideoCollectModel;
- use App\Models\VideoCommentModel;
- use App\Models\VideoModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- use BN\Red;
- use Illuminate\Support\Facades\DB;
- /**
- * 短视频评论管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class VideoCommentService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * MemberCollectService constructor.
- */
- public function __construct()
- {
- $this->model = new VideoCommentModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 列表数据
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 12, $field='', $userId=0)
- {
- $where = ['a.mark' => 1,'a.status'=>1];
- $field = $field? $field : 'lev_a.*';
- $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
- $order = 'a.id desc';
- if($sortType == 1){
- $order = 'lev_a.like_num desc, lev_a.create_time desc, lev_a.id desc';
- }
- $model = $this->model->with(['member'])
- ->from('video_comments as a')
- ->where($where)
- ->where(function ($query) use ($params) {
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId > 0) {
- $query->where('a.user_id', $userId);
- }
- $videoId = isset($params['video_id']) ? $params['video_id'] : 0;
- if ($videoId > 0) {
- $query->where('a.video_id', $videoId);
- }
- $isRead = isset($params['is_read'])? $params['is_read'] : 0;
- $isRead = $isRead>0? $isRead : 0;
- if ($isRead > 0) {
- $query->where('a.is_read', $isRead);
- }
- })
- ->where(function ($query) use ($params) {
- $keyword = isset($params['kw']) ? $params['kw'] : '';
- if ($keyword) {
- $query->where('a.content','like',"%{$keyword}%");
- }
- });
- $countModel = clone $model;
- $total = $countModel->count('a.id');
- $list = $model->where(function ($query) use ($params) {
- $replyId = isset($params['reply_id']) ? $params['reply_id'] : -1;
- if ($replyId >= 0) {
- $query->where('a.reply_id', $replyId);
- }
- })->selectRaw($field)
- ->orderByRaw($order)
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as $k=> &$item) {
- $item['time_text'] = isset($item['create_time']) && $item['create_time']? dateFormat($item['create_time']) : '';
- $member = isset($item['member'])? $item['member'] : [];
- if($member){
- $member['avatar'] = isset($member['avatar'])? get_image_url($member['avatar']) : '';
- }
- $item['member'] = $member;
- $isLike = RedisService::get("caches:videos:comment_{$userId}_{$item['id']}");
- $item['is_like'] = $isLike? 1 : 0;
- $replyData = $this->getReplyList($item['id'], $userId, $pageSize);
- $item['reply_count'] = isset($replyData['total'])? $replyData['total'] : 0;
- $item['reply_list'] = isset($replyData['list'])? $replyData['list'] : [];
- $item['albums'] = $item['albums']? get_images_preview($item['albums']) : [];
- $item['reply_show'] = (rand(100,500)>200 || $k==0) && $item['reply_count']>0? 1 : 0;
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'all_total' => $total,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 回复列表
- * @param $replyId
- * @param $userId
- * @param int $pageSize
- * @return array|mixed
- */
- public function getReplyList($replyId, $userId, $pageSize=12)
- {
- $cacheKey = "caches:videos:comment_reply_list_{$replyId}_{$userId}_{$pageSize}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $model = $this->model->with(['member'])
- ->from('video_comments as a')
- ->where(['a.reply_id'=> $replyId,'a.status'=>1,'a.mark'=>1])
- ->select(['a.*'])
- ->orderBy('a.create_time','desc')
- ->orderBy('a.id','desc');
- $countModel = clone $model;
- $total = $countModel->count('a.id');
- $datas = $model->limit($pageSize)->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- foreach ($datas as &$item){
- $item['time_text'] = isset($item['create_time']) && $item['create_time']? dateFormat($item['create_time']) : '';
- $member = isset($item['member'])? $item['member'] : [];
- if($member){
- $member['avatar'] = isset($member['avatar'])? get_image_url($member['avatar']) : '';
- }
- $item['member'] = $member;
- $isLike = RedisService::get("caches:videos:comment_{$userId}_{$item['id']}");
- $item['is_like'] = $isLike? 1 : 0;
- $item['albums'] = $item['albums']? get_images_preview($item['albums']) : [];
- }
- RedisService::set($cacheKey, ['total'=> $total,'list'=> $datas], rand(30,60));
- }
- return ['total'=> $total,'list'=> $datas];
- }
- /**
- * 发布评论
- * @param $userId
- * @param $params
- * @return bool
- */
- public function publish($userId, $params)
- {
- $albums = isset($params['albums'])? get_format_images($params['albums']) : '';
- $videoId = isset($params['video_id'])? intval($params['video_id']) : 0;
- $data = [
- 'user_id'=> $userId,
- 'reply_id'=> isset($params['reply_id'])? intval($params['reply_id']) : 0,
- 'video_id'=> $videoId,
- 'reply_to_uid'=> isset($params['reply_to_uid'])? intval($params['reply_to_uid']) : 0,
- 'albums'=> $albums,
- 'content'=> isset($params['content'])? trim($params['content']) : '',
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'status'=> 1,
- 'mark'=> 1,
- ];
- if($this->model->insertGetId($data)){
- VideoModel::where(['id'=> $videoId])->update(['comment_num'=>DB::raw("comment_num + 1"),'update_time'=>time()]);
- $this->error = 2942;
- return true;
- }
- $this->error = 2943;
- return true;
- }
- /**
- * 点赞
- * @param $userId
- * @param $params
- * @return bool
- */
- public function like($userId, $params)
- {
- $id = isset($params['id'])? $params['id'] : 0;
- $status = isset($params['status'])? $params['status'] : 1;
- $cacheKey = "caches:videos:comment_{$userId}_{$id}";
- if(RedisService::get($cacheKey)){
- return false;
- }
- if(!$info = $this->model->where(['id'=> $id,'mark'=>1])->select(['id','like_num'])->first()){
- $this->error = 2941;
- return false;
- }
- $updateData = ['update_time'=>time()];
- $updateData['like_num'] = ($status == 1 ? $info['like_num']+1 : max(0, $info['like_num']-1));
- if ($this->model->where(['id' => $id, 'mark' => 1])->update($updateData)) {
- if($status == 1){
- RedisService::set($cacheKey, $info, 7 * 86400);
- }else{
- RedisService::clear($cacheKey);
- }
- $this->error = 1002;
- return true;
- }
- $this->error = 1003;
- return false;
- }
- /**
- * 删除
- * @param $id
- * @return bool
- */
- public function deleteRow($id)
- {
- $videoId = $this->model->where(['id'=> $id])->value('video_id');
- $this->model->where(['id'=> $id])->delete();
- try {
- if($videoId>0){
- VideoModel::where(['id'=> $videoId])->update(['comment_num'=>DB::raw("comment_num - 1"),'update_time'=>time()]);
- }
- } catch (\Exception $exception){
- }
- return true;
- }
- }
|