| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use think\facade\Db;
- //好友控制器
- class Friend extends BaseController
- {
-
- //获取我下面的好友列表
- public function getMyFriends()
- {
- $userId = $this->getUser()->user_id; //用户ID
- $map1 = ['user_id', '=', $userId];
- $map2 = ['friend_id', '=', $userId];
- $friends = Db::table('tb_friend')
- ->where(['state' => 1]) //状态必须是已通过
- ->where(function($query) use($map1, $map2) {
- $query->whereOr([$map1, $map2]);
- })
- ->select();
- $friendsData = [];
- foreach ($friends as $friend) {
- $friendId = 0;
- //好友是双向的,先排除自己
- if ($friend['user_id'] == $userId) {
- $firendId = $friend['friend_id'];
- } else {
- $friendId = $friend['user_id'];
- }
- $user = Db::table('tb_user')
- ->where(['id' => $friendId])
- ->find();
- unset($user['password']);
- unset($user['balance']);
- $friendsData[] = $user;
- }
- $this->success('success', [
- 'friendCount' => count($friendsData), //好友列表数量
- 'data' => $friendsData, //返回所有好友的user数据
- ]);
- }
-
- //添加好友
- public function addFriend()
- {
- $friendId = $this->request->post('friendId'); //需要加好友的用户ID
- $msg = $this->request->post('msg'); //加好友验证消息
- if (empty($msg) || !is_numeric($friendId)) {
- $this->fail(500, '参数校验错误');
- }
- $user = $this->getUser();
- //先判断申请有没有到期,否则不能重复申请加好友
- $friend = Db::table('tb_friend')
- ->where(['user_id' => $user->user_id])
- ->where(['friend_id' => $friendId])
- ->find();
- if ($friend && $friend['state'] == 1) {
- $this->fail(501, '对方已经是您的好友。');
- }
- if ($friend && $friend['state'] == 2) {
- $this->fail(503, '对方已拒绝,暂时还无法加好友。');
- }
- if ($friend && $friend['expired_at'] > time()) {
- $this->fail(502, '对方还未同意,请耐心等待');
- }
- $requestId = 0;
- if (!$friend) {
- $requestId = Db::table('tb_friend')
- ->insertGetId([
- 'user_id' => $user->user_id,
- 'friend_id' => $friendId,
- 'state' => 0,
- 'msg' => $msg,
- 'created_at' => time(),
- 'updated_at' => time(),
- 'expired_at' => time() + 60 * 60 * 48, //48小时后到期,期间内不可以重复添加
- ]);
- } else {
- $requestId = $friend['id'];
- Db::table('tb_friend')
- ->where(['user_id' => $user->user_id])
- ->where(['friend_id' => $friendId])
- ->update([
- 'updated_at' => time(),
- 'expired_at' => time() + 60 * 60 * 48,
- ]);
- }
- $this->success('success', [
- 'requestId' => $requestId,
- ]);
- }
-
- //接受添加好友
- public function acceptFriend()
- {
- $requestId = $this->request->post('requestId'); //请求ID
- $isAccept = $this->request->post('isAccept'); //是否接受添加好友 1:接受 0:拒绝
- if (!is_numeric($requestId) || !is_numeric($isAccept)) {
- $this->fail(500, '参数校验错误');
- }
- $isAccept = intval($isAccept);
- if (!in_array($isAccept, [0, 1])) {
- $this->fail(500, '参数校验错误');
- }
- //requestId是否存在
- $friend = Db::table('tb_friend')
- ->where(['friend_id' => $this->getUser()->user_id])
- ->where(['id' => $requestId])
- ->find();
- if (!$friend) {
- $this->fail(501, 'requestId不存在');
- }
- //判断是否到期
- if ($friend['expired_at'] < time()) {
- $this->fail(502, '好友验证已经过期,无法通过');
- }
- Db::table('tb_friend')
- ->where(['id' => $requestId])
- ->where(['friend_id' => $this->getUser()->user_id])
- ->update([
- 'updated_at' => time(),
- 'friend_id' => $this->getUser()->user_id,
- 'state' => $isAccept == 1 ? 1 : 2,
- ]);
- $this->success('success', null);
- }
-
-
- //获取好友验证消息,过期的不再获取
- public function getAcceptFriendMessage()
- {
- $data = Db::table('tb_friend')
- ->where(['friend_id' => $this->getUser()->user_id])
- ->where('expired_at', '<', time())
- ->order('created_at DESC')
- ->select();
- $this->success('success', $data);
- }
- }
|