Membersnsfriend.php 8.1 KB

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