Friend.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. //好友控制器
  6. class Friend extends BaseController
  7. {
  8. //获取我下面的好友列表
  9. public function getMyFriends()
  10. {
  11. $userId = $this->getUser()->user_id; //用户ID
  12. $map1 = ['user_id', '=', $userId];
  13. $map2 = ['friend_id', '=', $userId];
  14. $friends = Db::table('tb_friend')
  15. ->where(['state' => 1]) //状态必须是已通过
  16. ->where(function($query) use($map1, $map2) {
  17. $query->whereOr([$map1, $map2]);
  18. })
  19. ->select();
  20. $friendsData = [];
  21. foreach ($friends as $friend) {
  22. $friendId = 0;
  23. //好友是双向的,先排除自己
  24. if ($friend['user_id'] == $userId) {
  25. $firendId = $friend['friend_id'];
  26. } else {
  27. $friendId = $friend['user_id'];
  28. }
  29. $user = Db::table('tb_user')
  30. ->where(['id' => $friendId])
  31. ->find();
  32. unset($user['password']);
  33. unset($user['balance']);
  34. $friendsData[] = $user;
  35. }
  36. $this->success('success', [
  37. 'friendCount' => count($friendsData), //好友列表数量
  38. 'data' => $friendsData, //返回所有好友的user数据
  39. ]);
  40. }
  41. //添加好友
  42. public function addFriend()
  43. {
  44. $friendId = $this->request->post('friendId'); //需要加好友的用户ID
  45. $msg = $this->request->post('msg'); //加好友验证消息
  46. if (empty($msg) || !is_numeric($friendId)) {
  47. $this->fail(500, '参数校验错误');
  48. }
  49. $user = $this->getUser();
  50. //先判断申请有没有到期,否则不能重复申请加好友
  51. $friend = Db::table('tb_friend')
  52. ->where(['user_id' => $user->user_id])
  53. ->where(['friend_id' => $friendId])
  54. ->find();
  55. if ($friend && $friend['state'] == 1) {
  56. $this->fail(501, '对方已经是您的好友。');
  57. }
  58. if ($friend && $friend['state'] == 2) {
  59. $this->fail(503, '对方已拒绝,暂时还无法加好友。');
  60. }
  61. if ($friend && $friend['expired_at'] > time()) {
  62. $this->fail(502, '对方还未同意,请耐心等待');
  63. }
  64. $requestId = 0;
  65. if (!$friend) {
  66. $requestId = Db::table('tb_friend')
  67. ->insertGetId([
  68. 'user_id' => $user->user_id,
  69. 'friend_id' => $friendId,
  70. 'state' => 0,
  71. 'msg' => $msg,
  72. 'created_at' => time(),
  73. 'updated_at' => time(),
  74. 'expired_at' => time() + 60 * 60 * 48, //48小时后到期,期间内不可以重复添加
  75. ]);
  76. } else {
  77. $requestId = $friend['id'];
  78. Db::table('tb_friend')
  79. ->where(['user_id' => $user->user_id])
  80. ->where(['friend_id' => $friendId])
  81. ->update([
  82. 'updated_at' => time(),
  83. 'expired_at' => time() + 60 * 60 * 48,
  84. ]);
  85. }
  86. $this->success('success', [
  87. 'requestId' => $requestId,
  88. ]);
  89. }
  90. //接受添加好友
  91. public function acceptFriend()
  92. {
  93. $requestId = $this->request->post('requestId'); //请求ID
  94. $isAccept = $this->request->post('isAccept'); //是否接受添加好友 1:接受 0:拒绝
  95. if (!is_numeric($requestId) || !is_numeric($isAccept)) {
  96. $this->fail(500, '参数校验错误');
  97. }
  98. $isAccept = intval($isAccept);
  99. if (!in_array($isAccept, [0, 1])) {
  100. $this->fail(500, '参数校验错误');
  101. }
  102. //requestId是否存在
  103. $friend = Db::table('tb_friend')
  104. ->where(['friend_id' => $this->getUser()->user_id])
  105. ->where(['id' => $requestId])
  106. ->find();
  107. if (!$friend) {
  108. $this->fail(501, 'requestId不存在');
  109. }
  110. //判断是否到期
  111. if ($friend['expired_at'] < time()) {
  112. $this->fail(502, '好友验证已经过期,无法通过');
  113. }
  114. Db::table('tb_friend')
  115. ->where(['id' => $requestId])
  116. ->where(['friend_id' => $this->getUser()->user_id])
  117. ->update([
  118. 'updated_at' => time(),
  119. 'friend_id' => $this->getUser()->user_id,
  120. 'state' => $isAccept == 1 ? 1 : 2,
  121. ]);
  122. $this->success('success', null);
  123. }
  124. //获取好友验证消息,过期的不再获取
  125. public function getAcceptFriendMessage()
  126. {
  127. $data = Db::table('tb_friend')
  128. ->where(['friend_id' => $this->getUser()->user_id])
  129. ->where('expired_at', '<', time())
  130. ->order('created_at DESC')
  131. ->select();
  132. $this->success('success', $data);
  133. }
  134. }