Membersnsfriend.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 好友控制器
  10. */
  11. class Membersnsfriend extends MobileMember
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize(); // TODO: Change the autogenerated stub
  16. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/member_snsfriend.lang.php');
  17. }
  18. /**
  19. * @api {POST} api/Membersnsfriend/member_list 查询会员
  20. * @apiVersion 1.0.0
  21. * @apiGroup Membersnsfriend
  22. *
  23. * @apiHeader {String} X-DS-KEY 用户授权token
  24. *
  25. * @apiParam {String} m_name 用户名
  26. * @apiParam {Int} page 页码
  27. * @apiParam {Int} per_page 每页数量
  28. *
  29. * @apiSuccess {String} code 返回码,10000为成功
  30. * @apiSuccess {String} message 返回消息
  31. * @apiSuccess {Object} result 返回数据
  32. * @apiSuccess {Object[]} result.member_list 用户列表
  33. * @apiSuccess {Int} result.member_list.u_id 用户ID
  34. * @apiSuccess {String} result.member_list.u_name 用户名称
  35. * @apiSuccess {String} result.member_list.truename 真实姓名
  36. * @apiSuccess {String} result.member_list.avatar 用户头像
  37. * @apiSuccess {Int} result.page_total 总页数
  38. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  39. */
  40. public function member_list()
  41. {
  42. $member_list = array();
  43. $member_model = model('member');
  44. $condition = array();
  45. $condition[] = array('member_state', '=', '1');
  46. $condition[] = array('member_id', '<>', $this->member_info['member_id']);
  47. $condition[] = array('member_name', 'like', '%' . trim(input('post.m_name')) . '%'); //会员名称
  48. $list = $member_model->getMemberList($condition, 'member_id,member_name,member_truename,member_avatar', $this->pagesize);
  49. if (!empty($list) && is_array($list)) {
  50. foreach ($list as $k => $v) {
  51. $member = array();
  52. $member['u_id'] = $v['member_id'];
  53. $member['u_name'] = $v['member_name'];
  54. $member['truename'] = $v['member_truename'];
  55. $member['avatar'] = get_member_avatar($v['member_avatar']);
  56. $member_list[] = $member;
  57. }
  58. }
  59. $result = array_merge(array('member_list' => $member_list), mobile_page($member_model->page_info));
  60. ds_json_encode(10000, '', $result);
  61. }
  62. /**
  63. * @api {POST} api/Membersnsfriend/friend_list 好友列表
  64. * @apiVersion 1.0.0
  65. * @apiGroup Membersnsfriend
  66. *
  67. * @apiHeader {String} X-DS-KEY 用户授权token
  68. *
  69. * @apiParam {Int} page 页码
  70. * @apiParam {Int} per_page 每页数量
  71. *
  72. * @apiSuccess {String} code 返回码,10000为成功
  73. * @apiSuccess {String} message 返回消息
  74. * @apiSuccess {Object} result 返回数据
  75. * @apiSuccess {Object[]} result.friend_list 好友列表
  76. * @apiSuccess {Int} result.friend_list.u_id 用户ID
  77. * @apiSuccess {String} result.friend_list.u_name 用户名称
  78. * @apiSuccess {Int} result.friend_list.friend 是否是好友 0否1是
  79. * @apiSuccess {String} result.friend_list.avatar 用户头像
  80. * @apiSuccess {Int} result.page_total 总页数
  81. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  82. */
  83. public function friend_list()
  84. {
  85. $snsfriend_model = model('snsfriend');
  86. $member_id = $this->member_info['member_id'];
  87. $friend_list = $snsfriend_model->getFriendList(array('friend_frommid' => $member_id), $this->pagesize);
  88. $result = array_merge(array('friend_list' => array_values($friend_list)), mobile_page($snsfriend_model->page_info));
  89. ds_json_encode(10000, '', $result);
  90. }
  91. /**
  92. * @api {POST} api/Membersnsfriend/friend_add 添加好友
  93. * @apiVersion 1.0.0
  94. * @apiGroup Membersnsfriend
  95. *
  96. * @apiHeader {String} X-DS-KEY 用户授权token
  97. *
  98. * @apiParam {Int} m_id 用户ID
  99. *
  100. * @apiSuccess {String} code 返回码,10000为成功
  101. * @apiSuccess {String} message 返回消息
  102. */
  103. public function friend_add()
  104. {
  105. $member_info = array();
  106. $self_info = $this->member_info;
  107. $m_id = intval(input('post.m_id'));
  108. if ($m_id < 1 || $m_id == $self_info['member_id']) {
  109. ds_json_encode(10001, lang('param_error'));
  110. }
  111. //验证会员信息
  112. $member_model = model('member');
  113. $condition = array();
  114. $condition[] = array('member_state', '=', '1');
  115. $condition[] = array('member_id', '=', $m_id);
  116. $member_info = $member_model->getMemberInfo($condition);
  117. if (empty($member_info)) { //验证会员信息
  118. ds_json_encode(10001, lang('snsfriend_member_error'));
  119. }
  120. $snsfriend_model = model('snsfriend');
  121. $count = $snsfriend_model->getSnsfriendCount(array(
  122. 'friend_tomid' => $m_id, 'friend_frommid' => $self_info['member_id']
  123. ));
  124. if ($count > 0) { //判断是否已经存在好友记录
  125. ds_json_encode(10001, lang('snsfriend_havefollowed'));
  126. }
  127. $insert_arr = array();
  128. $insert_arr['friend_frommid'] = $self_info['member_id'];
  129. $insert_arr['friend_frommname'] = $self_info['member_name'];
  130. $insert_arr['friend_frommavatar'] = $self_info['member_avatar'];
  131. $insert_arr['friend_tomid'] = $member_info['member_id'];
  132. $insert_arr['friend_tomname'] = $member_info['member_name'];
  133. $insert_arr['friend_tomavatar'] = $member_info['member_avatar'];
  134. $insert_arr['friend_addtime'] = TIMESTAMP;
  135. $friend_info = $snsfriend_model->getOneSnsfriend(array(
  136. 'friend_frommid' => $m_id,
  137. 'friend_tomid' => $self_info['member_id']
  138. ));
  139. if (empty($friend_info)) {
  140. $insert_arr['friend_followstate'] = '1'; //单方关注
  141. } else {
  142. $insert_arr['friend_followstate'] = '2'; //双方关注
  143. }
  144. $result = $snsfriend_model->addSnsfriend($insert_arr);
  145. if ($result) {
  146. if (!empty($friend_info)) { //更新对方关注状态
  147. $snsfriend_model->editSnsfriend(array('friend_followstate' => '2'), array('friend_id' => $friend_info['friend_id']));
  148. }
  149. ds_json_encode(10000, lang('snsfriend_follow_succ'), 1);
  150. } else {
  151. ds_json_encode(10001, lang('snsfriend_follow_fail'));
  152. }
  153. }
  154. /**
  155. * @api {POST} api/Membersnsfriend/friend_del 删除好友
  156. * @apiVersion 1.0.0
  157. * @apiGroup Membersnsfriend
  158. *
  159. * @apiHeader {String} X-DS-KEY 用户授权token
  160. *
  161. * @apiParam {Int} m_id 用户ID
  162. *
  163. * @apiSuccess {String} code 返回码,10000为成功
  164. * @apiSuccess {String} message 返回消息
  165. */
  166. public function friend_del()
  167. {
  168. $m_id = intval(input('post.m_id'));
  169. if ($m_id <= 0) {
  170. ds_json_encode(10001, lang('param_error'));
  171. }
  172. $snsfriend_model = model('snsfriend');
  173. $condition = array();
  174. $condition[] = array('friend_tomid', '=', $m_id);
  175. $condition[] = array('friend_frommid', '=', $this->member_info['member_id']);
  176. $result = $snsfriend_model->delSnsfriend($condition);
  177. if ($result) {
  178. //更新对方的关注状态
  179. $snsfriend_model->editSnsfriend(array('friend_followstate' => '1'), array('friend_frommid' => $m_id, 'friend_tomid' => $this->member_info['member_id']));
  180. ds_json_encode(10000, '', 1);
  181. } else {
  182. ds_json_encode(10001, lang('ds_common_op_fail'));
  183. }
  184. }
  185. }