Membersnsfriend.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 好友控制器
  15. */
  16. class Membersnsfriend extends MobileMember {
  17. public function initialize() {
  18. parent::initialize(); // TODO: Change the autogenerated stub
  19. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/member_snsfriend.lang.php');
  20. }
  21. /**
  22. * @api {POST} api/Membersnsfriend/member_list 查询会员
  23. * @apiVersion 1.0.0
  24. * @apiGroup Membersnsfriend
  25. *
  26. * @apiHeader {String} X-DS-KEY 用户授权token
  27. *
  28. * @apiParam {String} m_name 用户名
  29. * @apiParam {Int} page 页码
  30. * @apiParam {Int} per_page 每页数量
  31. *
  32. * @apiSuccess {String} code 返回码,10000为成功
  33. * @apiSuccess {String} message 返回消息
  34. * @apiSuccess {Object} result 返回数据
  35. * @apiSuccess {Object[]} result.member_list 用户列表
  36. * @apiSuccess {Int} result.member_list.u_id 用户ID
  37. * @apiSuccess {String} result.member_list.u_name 用户名称
  38. * @apiSuccess {String} result.member_list.truename 真实姓名
  39. * @apiSuccess {String} result.member_list.avatar 用户头像
  40. * @apiSuccess {Int} result.page_total 总页数
  41. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  42. */
  43. public function member_list() {
  44. $member_list = array();
  45. $member_model = model('member');
  46. $condition = array();
  47. $condition[] = array('member_state', '=', '1');
  48. $condition[] = array('member_id', '<>', $this->member_info['member_id']);
  49. $condition[] = array('member_name', 'like', '%' . trim(input('post.m_name')) . '%'); //会员名称
  50. $list = $member_model->getMemberList($condition, 'member_id,member_name,member_truename,member_avatar', $this->pagesize);
  51. if (!empty($list) && is_array($list)) {
  52. foreach ($list as $k => $v) {
  53. $member = array();
  54. $member['u_id'] = $v['member_id'];
  55. $member['u_name'] = $v['member_name'];
  56. $member['truename'] = $v['member_truename'];
  57. $member['avatar'] = get_member_avatar($v['member_avatar']);
  58. $member_list[] = $member;
  59. }
  60. }
  61. $result = array_merge(array('member_list' => $member_list), mobile_page($member_model->page_info));
  62. ds_json_encode(10000, '', $result);
  63. }
  64. /**
  65. * @api {POST} api/Membersnsfriend/friend_list 好友列表
  66. * @apiVersion 1.0.0
  67. * @apiGroup Membersnsfriend
  68. *
  69. * @apiHeader {String} X-DS-KEY 用户授权token
  70. *
  71. * @apiParam {Int} page 页码
  72. * @apiParam {Int} per_page 每页数量
  73. *
  74. * @apiSuccess {String} code 返回码,10000为成功
  75. * @apiSuccess {String} message 返回消息
  76. * @apiSuccess {Object} result 返回数据
  77. * @apiSuccess {Object[]} result.friend_list 好友列表
  78. * @apiSuccess {Int} result.friend_list.u_id 用户ID
  79. * @apiSuccess {String} result.friend_list.u_name 用户名称
  80. * @apiSuccess {Int} result.friend_list.friend 是否是好友 0否1是
  81. * @apiSuccess {String} result.friend_list.avatar 用户头像
  82. * @apiSuccess {Int} result.page_total 总页数
  83. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  84. */
  85. public function friend_list() {
  86. $snsfriend_model = model('snsfriend');
  87. $member_id = $this->member_info['member_id'];
  88. $friend_list = $snsfriend_model->getFriendList(array('friend_frommid' => $member_id), $this->pagesize);
  89. $result = array_merge(array('friend_list' => array_values($friend_list)), mobile_page($snsfriend_model->page_info));
  90. ds_json_encode(10000, '', $result);
  91. }
  92. /**
  93. * @api {POST} api/Membersnsfriend/friend_add 添加好友
  94. * @apiVersion 1.0.0
  95. * @apiGroup Membersnsfriend
  96. *
  97. * @apiHeader {String} X-DS-KEY 用户授权token
  98. *
  99. * @apiParam {Int} m_id 用户ID
  100. *
  101. * @apiSuccess {String} code 返回码,10000为成功
  102. * @apiSuccess {String} message 返回消息
  103. */
  104. public function friend_add() {
  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. $m_id = intval(input('post.m_id'));
  168. if ($m_id <= 0) {
  169. ds_json_encode(10001, lang('param_error'));
  170. }
  171. $snsfriend_model = model('snsfriend');
  172. $condition = array();
  173. $condition[] = array('friend_tomid','=',$m_id);
  174. $condition[] = array('friend_frommid','=',$this->member_info['member_id']);
  175. $result = $snsfriend_model->delSnsfriend($condition);
  176. if ($result) {
  177. //更新对方的关注状态
  178. $snsfriend_model->editSnsfriend(array('friend_followstate' => '1'), array('friend_frommid' => $m_id, 'friend_tomid' => $this->member_info['member_id']));
  179. ds_json_encode(10000, '', 1);
  180. } else {
  181. ds_json_encode(10001, lang('ds_common_op_fail'));
  182. }
  183. }
  184. }