| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?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\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 用户收藏点赞管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class MemberCollectService
- * @package App\Services\Api
- */
- class MemberCollectService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * MemberCollectService constructor.
- */
- public function __construct()
- {
- $this->model = new MemberCollectModel();
- }
- /**
- * 静态入口
- * @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($userId, $params, $pageSize = 15, $field='')
- {
- $where = ['a.mark' => 1,'a.status'=>1];
- $field = $field? $field : 'lev_a.id,lev_a.user_id,lev_a.type,lev_a.collect_uid,lev_b.nickname,lev_b.avatar,lev_c.nickname as c_nickname,lev_c.avatar as c_avatar';
- $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
- $order = 'id desc';
- if($sortType == 1){
- $order = 'create_time desc, lev_a.id desc';
- }
- $list = $this->model->from('member_collect as a')
- ->leftJoin('member as b', function($join){
- $join->on('b.id', '=', 'a.user_id')
- ->where('b.status', 1)
- ->where('b.mark', 1);
- })
- ->leftJoin('member as c', function($join){
- $join->on('c.id', '=', 'a.collect_uid')
- ->where('c.status', 1)
- ->where('c.mark', 1);
- })
- ->where($where)
- ->where(function ($query) use ($params, $userId) {
- $status = isset($params['status'])? $params['status'] : 0;
- $status = $status>0? $status : 1;
- if ($status > 0) {
- $query->where('a.status', $status);
- }
- $followType = isset($params['follow_type'])? $params['follow_type'] : 0;
- if (in_array($followType,[1,2])) {
- // 我关注的人
- if($followType == 1){
- $query->where('a.user_id', $userId);
- }
- // 关注我的人
- else if($followType == 2){
- $query->where('a.collect_uid', $userId);
- }
- }else{
- // 我关注的和关注我的人
- $query->where(function($query) use($userId){
- $query->where('a.user_id',$userId)
- ->orWhere('a.collect_uid', $userId);
- });
- }
- })
- ->where(function ($query) use ($params) {
- $keyword = isset($params['kw']) ? $params['kw'] : '';
- $followType = isset($params['follow_type'])? $params['follow_type'] : 0;
- if ($keyword) {
- if($followType == 1){
- $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.id','like',"%{$keyword}%");
- }else if($followType == 2){
- $query->where('c.nickname', 'like', "%{$keyword}%")->orWhere('c.id','like',"%{$keyword}%");
- }else{
- $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.id','like',"%{$keyword}%")
- ->where('c.nickname', 'like', "%{$keyword}%")->orWhere('c.id','like',"%{$keyword}%");
- }
- }
- })
- ->selectRaw($field)
- ->orderByRaw($order)
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as &$item) {
- $item['avatar'] = isset($item['avatar']) && $item['avatar'] ? get_image_url($item['avatar']) : '';
- $item['c_avatar'] = isset($item['c_avatar']) && $item['c_avatar'] ? get_image_url($item['c_avatar']) : '';
- // 我关注的用户
- if($userId == $item['user_id']){
- $item['nickname'] = $item['c_nickname'];
- $item['avatar'] = $item['c_avatar']? $item['c_avatar'] : get_image_url('/images/member/logo.png');
- }
- $item['checked'] = false;
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 是否已经收藏或点赞过
- * @param $userId
- * @param $collectUid
- * @param int $type 类型:
- * @return array|mixed
- */
- public function checkCollect($userId, $collectUid, $type=1)
- {
- $cacheKey = "caches:member:collect:u{$userId}_c{$collectUid}_{$type}";
- $data = RedisService::get($cacheKey);
- if($data){
- return $data? 1: 2;
- }
- $data = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type,'status'=>1,'mark'=>1])->value('id');
- if($data){
- RedisService::set($cacheKey, $data, rand(5, 10));
- }
- return $data? 1 : 2;
- }
- /**
- * 收藏点赞
- * @param $userId
- * @param $dynamicId
- * @param $type
- * @param int $status
- * @return mixed
- */
- public function follow($userId, $params)
- {
- $collectUid = isset($params['id'])? intval($params['id']) : 0;
- $type = isset($params['type'])? intval($params['type']) : 1;
- $status = isset($params['status'])? intval($params['status']) : 1;
- if($collectUid<=0 || !in_array($type, [1,2]) || !in_array($status, [1,2])){
- $this->error = 2501;
- return false;
- }
- $id = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type])->value('id');
- $data = [
- 'user_id'=> $userId,
- 'type'=> $type,
- 'collect_uid'=> $collectUid,
- 'update_time'=> time(),
- 'status'=> $status,
- 'mark'=> 1,
- ];
- if(!$id){
- $data['create_time'] = time();
- if(!$this->model->insertGetId($data)){
- return false;
- }
- }else{
- if(!$this->model->where('id', $id)->update($data)){
- return false;
- }
- }
- $this->error = 1002;
- RedisService::clear("caches:member:collect:u{$userId}_c{$collectUid}_{$type}");
- $isFans = $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId,'type'=> $type])->value('id');
- return ['is_fans'=>$isFans? 1:0];
- }
- /**
- * 获取(被)收藏/关注/点赞数量
- * @param $userId 用户ID
- * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢
- * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞
- * @return array|mixed
- */
- public function getCount($userId, $type=1, $ctype=1)
- {
- $cacheKey = "caches:member:collect:{$userId}_{$type}_{$ctype}";
- $data = RedisService::get($cacheKey);
- if($data){
- return $data;
- }
- $field = 'collect_uid';
- if($ctype == 2){
- $field = 'user_id';
- }
- $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id');
- if($data){
- RedisService::set($cacheKey, $data, 3600);
- }
- return $data;
- }
- /**
- * 取消
- * @param $userId
- * @return bool
- */
- public function cancel($userId)
- {
- // 参数
- $ids = request()->post('ids','');
- $ids = $ids? explode(',', $ids) : [];
- $type = request()->post('type',0);
- if (empty($ids) && $type == 0) {
- $this->error = 1033;
- return false;
- }
- if($type){
- $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
- }else {
- $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]);
- }
- $this->error = 1002;
- return true;
- }
- }
|