123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\api\controller;
- use think\Db;
- /**
- * 喜欢接口
- */
- class Like extends Common
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 添加喜欢列表
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function addLikeList() {
- $user_id = $this->request->request('user_id',0,"intval"); // 喜欢的用户ID
- $fans_id = $this->auth->id; // 添加到我喜欢,我就是粉丝
- if (!$user_id || !$fans_id) {
- $this->error(__('Invalid parameters'));
- }
- if($user_id == $fans_id) {
- $this->error(__('自己对自己 不能心动哦?'));
- }
- $userLikeModel = new \app\common\model\UserLike();
- $where = [];
- $where["user_id"] = $user_id;
- $where["fans_id"] = $fans_id;
- $userlikeInfo = $userLikeModel->where($where)->find();
- $data = [];
- if($userlikeInfo) {
- $data = $userlikeInfo;
- $res = true;
- } else {
- // 获取是否是有眼缘
- // $fate = \app\common\model\UserFate::where(['user_id'=>$fans_id,'fate_user_id'=>$user_id])->find();
- $data["user_id"] = $user_id;
- $data["fans_id"] = $fans_id;
- // $data["is_fate"] = $fate?1:0;
- $data["createtime"] = time();
- $res = $userLikeModel->insert($data);
- }
- if ($res) {
- // +message
- $user_info = \app\common\model\User::where(['id'=>$user_id])->find();
- $title = '喜欢通知!';
- $content = $user_info->nickname.': 我关注了你哦,找我聊聊吧~';
- \app\common\model\SysMsg::sendSysMsg($user_id,6,$title,$content);
- $this->success(__('添加成功!'), $data);
- } else {
- $this->success(__('添加失败!'));
- }
- }
- /**
- * 获取喜欢我的
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function getFansList() {
- $page = $this->request->request('page',1); // 分页
- $pageNum = $this->request->request('pageNum',10); // 分页
- // 分页搜索构建
- $pageStart = ($page-1)*$pageNum;
- $userLikeModel = new \app\common\model\UserLike();
- $fansList = $userLikeModel->getFansList($this->auth->id,$pageStart,$pageNum);
- if ($fansList) {
- $fate = \app\common\model\UserFate::where(['user_id'=>$this->auth->id])->select();
- $fate_user_id = array_column($fate,'fate_user_id');
- foreach($fansList as $k => $v) {
- if(in_array($v['user_id'],$fate_user_id)) {
- $fansList[$k]['is_fate'] = 1;
- } else {
- $fansList[$k]['is_fate'] = 0;
- }
- }
- $this->success(__('获取成功!'), $fansList);
- } else {
- $this->success(__('数据为空!'),[]);
- }
- }
- /**
- * 获取我喜欢的
- * @date 2020-10-27
- * @createby 虎嗅网络科技
- */
- public function getLikeList() {
- $page = $this->request->request('page',1); // 分页
- $pageNum = $this->request->request('pageNum',10); // 分页
- // 分页搜索构建
- $pageStart = ($page-1)*$pageNum;
- $userLikeModel = new \app\common\model\UserLike();
- $idolList = $userLikeModel->getUserList($this->auth->id,$pageStart,$pageNum);
- if ($idolList) {
- $fate = \app\common\model\UserFate::where(['user_id'=>$this->auth->id])->select();
- $fate_user_id = array_column($fate,'fate_user_id');
- foreach($idolList as $k => $v) {
- if(in_array($v['user_id'],$fate_user_id)) {
- $idolList[$k]['is_fate'] = 1;
- } else {
- $idolList[$k]['is_fate'] = 0;
- }
- }
- $this->success(__('获取成功!'), $idolList);
- } else {
- $this->success(__('数据为空!'),[]);
- }
- }
- /**
- * 删除我喜欢
- */
- public function delLike() {
- $id = $this->request->request('id',1); // 喜欢ID
- if($id <= 0) $this->error('参数错误!');
- $userLikeModel = new \app\common\model\UserLike();
- $where = [];
- $where['id'] = $id;
- $where['fans_id'] = $this->auth->id;
- $userLikeInfo = $userLikeModel->where($where)->find();
- if(!$userLikeInfo) $this->error('数据未找到,或无权限!');
- $res = $userLikeInfo->delete();
- if ($res) {
- $this->success(__('删除成功!'));
- } else {
- $this->success(__('删除失败!'));
- }
- }
- /**
- * 删除我喜欢
- */
- public function delLikeByUserId() {
- $user_id = $this->request->request('user_id',1); // 被喜欢用户ID
- if($user_id <= 0) $this->error('参数错误!');
- $userLikeModel = new \app\common\model\UserLike();
- $where = [];
- $where['user_id'] = $user_id;
- $where['fans_id'] = $this->auth->id;
- $userLikeInfo = $userLikeModel->where($where)->find();
- if(!$userLikeInfo) $this->error('数据未找到,或无权限!');
- $res = $userLikeInfo->delete();
- if ($res) {
- $this->success(__('删除成功!'));
- } else {
- $this->success(__('删除失败!'));
- }
- }
- }
|