Membersnsfriend.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. * ============================================================================
  8. *
  9. * ============================================================================
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 控制器
  15. */
  16. class Membersnsfriend extends BaseMember
  17. {
  18. public function initialize()
  19. {
  20. parent::initialize(); // TODO: Change the autogenerated stub
  21. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/member_snsfriend.lang.php');
  22. }
  23. private function m_sex($sextype)
  24. {
  25. switch ($sextype) {
  26. case 1:
  27. return 'male';
  28. break;
  29. case 2:
  30. return 'female';
  31. break;
  32. default:
  33. return '';
  34. break;
  35. }
  36. }
  37. /**
  38. * 找人首页
  39. */
  40. public function index()
  41. {
  42. // 查看推荐
  43. $mtag_list = Db::name('snsmembertag')->where(array('mtag_recommend' => 1))->order('mtag_sort asc')->select()->toArray();
  44. if (!empty($mtag_list)) {
  45. // 查询已关注好友 ,不显示已关注好友
  46. $friend_array = Db::name('snsfriend')->field('friend_tomid')->where(array('friend_frommid' => session('member_id')))->select()->toArray();
  47. $friendid_array[] = session('member_id');
  48. if (!empty($friend_array)) {
  49. foreach ($friend_array as $val) {
  50. $friendid_array[] = $val['friend_tomid'];
  51. }
  52. }
  53. $mtagid_array = array();
  54. foreach ($mtag_list as $val) {
  55. $mtagid_array[] = $val['mtag_id'];
  56. }
  57. // 查询会员
  58. $where[] = array('sns_mtagmember.mtag_id', 'in', $mtagid_array); //查询条件
  59. $where[] = array('sns_mtagmember.member_id', 'not in', $friendid_array);
  60. $tagmember_list = Db::name('snsmtagmember')->alias('sns_mtagmember')
  61. ->field('sns_mtagmember.*,member.member_avatar,member.member_name')
  62. ->join('member member', 'sns_mtagmember.member_id=member.member_id')
  63. ->where($where)
  64. ->order('sns_mtagmember.recommend desc, sns_mtagmember.member_id asc')
  65. ->limit(count($mtagid_array) * 20)->select()->toArray();
  66. // 整理
  67. $tagmember_list = array_under_reset($tagmember_list, 'mtag_id', 2);
  68. View::assign('mtag_list', $mtag_list);
  69. View::assign('tagmember_list', $tagmember_list);
  70. }
  71. View::assign('type', 'index');
  72. $this->setMemberCurItem('find');
  73. $this->setMemberCurMenu('member_snsfriend');
  74. return View::fetch($this->template_dir . 'member_snsfriend_find');
  75. }
  76. /**
  77. * 找人搜索列表
  78. */
  79. public function findlist()
  80. {
  81. $searchname = trim(input('post.searchname'));
  82. if (empty($searchname)) {
  83. $this->error(lang('searchname_not_empty'));
  84. }
  85. //查询关注会员id
  86. $followlist = Db::name('snsfriend')->field('friend_tomid, friend_followstate')->where(array('friend_frommid' => session('member_id')))->select()->toArray();
  87. unset($condition_arr);
  88. $followlist_new = array();
  89. if (!empty($followlist)) {
  90. foreach ($followlist as $k => $v) {
  91. $followlist_new[$v['friend_tomid']] = $v;
  92. }
  93. }
  94. //查询会员
  95. // 查询条件
  96. $where = array();
  97. $where[] = array('member_state', '=', 1);
  98. $where[] = array('member_id', '<>', session('member_id'));
  99. $where[] = array('member_name', 'like', '%' . $searchname . '%'); // 会员名称
  100. // 省份
  101. $member_provinceid = intval(input('post.provinceid'));
  102. if ($member_provinceid > 0) {
  103. $where[] = array('member_provinceid', '=', $member_provinceid);
  104. }
  105. // 城市
  106. $member_cityid = intval(input('post.cityid'));
  107. if ($member_cityid > 0) {
  108. $where[] = array('member_cityid', '=', $member_cityid);
  109. }
  110. // 地区
  111. $member_areaid = intval(input('post.areaid'));
  112. if ($member_areaid > 0) {
  113. $where[] = array('member_areaid', '=', $member_areaid);
  114. }
  115. // 性别
  116. $member_sex = intval(input('post.sex'));
  117. if ($member_sex > 0) {
  118. $where[] = array('member_sex', '=', $member_sex);
  119. }
  120. // 年龄
  121. $member_birthday = intval(input('post.age'));
  122. if ($member_birthday > 0) {
  123. switch ($member_birthday) {
  124. case 1:
  125. $s_time = strtotime((date('Y') - 18) . '-' . date('m') . '-' . date('d'));
  126. $e_time = strtotime(date('Y-m-d'));
  127. $where[] = array('member_birthday', 'BETWEEN', $s_time . ',' . $e_time);
  128. break;
  129. case 2:
  130. $s_time = strtotime((date('Y') - 24) . '-' . date('m-d'));
  131. $e_time = strtotime((date('Y') - 18) . '-' . date('m') . '-' . (date('d') - 1));
  132. $where[] = array('member_birthday', 'BETWEEN', $s_time . ',' . $e_time);
  133. break;
  134. case 3:
  135. $s_time = strtotime((date('Y') - 30) . '-' . date('m-d'));
  136. $e_time = strtotime((date('Y') - 24) . '-' . date('m') . '-' . (date('d') - 1));
  137. $where[] = array('member_birthday', 'BETWEEN', $s_time . ',' . $e_time);
  138. break;
  139. case 4:
  140. $s_time = strtotime((date('Y') - 35) . '-' . date('m-d'));
  141. $e_time = strtotime((date('Y') - 30) . '-' . date('m') . '-' . (date('d') - 1));
  142. $where[] = array('member_birthday', 'BETWEEN', $s_time . ',' . $e_time);
  143. break;
  144. case 5:
  145. $e_time = strtotime((date('Y') - 35) . '-' . date('m') . '-' . (date('d') - 1));
  146. $where[] = array('member_birthday', '<=', $e_time);
  147. break;
  148. }
  149. }
  150. $memberlist = Db::name('member')->where($where)->limit(50)->select()->toArray();
  151. if (!empty($memberlist)) {
  152. $followid_arr = array_keys($followlist_new);
  153. foreach ($memberlist as $k => $v) {
  154. if (in_array($v['member_id'], $followid_arr)) {
  155. $v['followstate'] = $followlist_new[$v['member_id']]['friend_followstate'];
  156. } else {
  157. $v['followstate'] = 0;
  158. }
  159. //性别
  160. $v['sex_class'] = $this->m_sex($v['member_sex']);
  161. $memberlist[$k] = $v;
  162. }
  163. }
  164. View::assign('memberlist', $memberlist);
  165. $this->setMemberCurItem('find');
  166. $this->setMemberCurMenu('member_snsfriend');
  167. return View::fetch($this->template_dir . 'member_snsfriend_findlist');
  168. }
  169. /**
  170. * 加关注
  171. */
  172. public function addfollow()
  173. {
  174. $mid = intval(input('param.mid'));
  175. if ($mid <= 0) {
  176. ds_json_encode(10001, lang('param_error'));
  177. }
  178. //验证会员信息
  179. $member_model = model('member');
  180. $condition_arr = array();
  181. $condition_arr[] = array('member_state', '=', "1");
  182. $condition_arr[] = array('member_id', 'in', array($mid, session('member_id')));
  183. $member_list = $member_model->getMemberList($condition_arr);
  184. unset($condition_arr);
  185. if (empty($member_list)) {
  186. ds_json_encode(10001, lang('snsfriend_member_error'));
  187. }
  188. $self_info = array();
  189. $member_info = array();
  190. foreach ($member_list as $k => $v) {
  191. if ($v['member_id'] == session('member_id')) {
  192. $self_info = $v;
  193. } else {
  194. $member_info = $v;
  195. }
  196. }
  197. if (empty($self_info) || empty($member_info)) {
  198. ds_json_encode(10001, lang('snsfriend_member_error'));
  199. }
  200. //验证是否已经存在好友记录
  201. $snsfriend_model = model('snsfriend');
  202. $friend_count = $snsfriend_model->getSnsfriendCount(array('friend_frommid' => session('member_id'), 'friend_tomid' => "$mid"));
  203. if ($friend_count > 0) {
  204. ds_json_encode(10001, lang('snsfriend_havefollowed'));
  205. }
  206. //查询对方是否已经关注我,从而判断关注状态
  207. $friend_info = $snsfriend_model->getOneSnsfriend(array('friend_frommid' => "{$mid}", 'friend_tomid' => session('member_id')));
  208. $insert_arr = array();
  209. $insert_arr['friend_frommid'] = "{$self_info['member_id']}";
  210. $insert_arr['friend_frommname'] = "{$self_info['member_name']}";
  211. $insert_arr['friend_frommavatar'] = "{$self_info['member_avatar']}";
  212. $insert_arr['friend_tomid'] = "{$member_info['member_id']}";
  213. $insert_arr['friend_tomname'] = "{$member_info['member_name']}";
  214. $insert_arr['friend_tomavatar'] = "{$member_info['member_avatar']}";
  215. $insert_arr['friend_addtime'] = TIMESTAMP;
  216. if (empty($friend_info)) {
  217. $insert_arr['friend_followstate'] = '1'; //单方关注
  218. } else {
  219. $insert_arr['friend_followstate'] = '2'; //双方关注
  220. }
  221. $result = $snsfriend_model->addSnsfriend($insert_arr);
  222. if ($result) {
  223. //更新对方关注状态
  224. if (!empty($friend_info)) {
  225. $snsfriend_model->editSnsfriend(array('friend_followstate' => '2'), array('friend_id' => "{$friend_info['friend_id']}"));
  226. }
  227. ds_json_encode(10000, '', array('state' => $insert_arr['friend_followstate']));
  228. } else {
  229. ds_json_encode(10001, '');
  230. }
  231. }
  232. /**
  233. * 取消关注
  234. */
  235. public function delfollow()
  236. {
  237. $mid = intval(input('param.mid'));
  238. if ($mid <= 0) {
  239. ds_json_encode(10001, lang('param_error'));
  240. }
  241. //取消关注
  242. $snsfriend_model = model('snsfriend');
  243. $result = $snsfriend_model->delSnsfriend(array('friend_frommid' => session('member_id'), 'friend_tomid' => "$mid"));
  244. if ($result) {
  245. //更新对方的关注状态
  246. $snsfriend_model->editSnsfriend(array('friend_followstate' => '1'), array('friend_frommid' => "$mid", 'friend_tomid' => session('member_id')));
  247. ds_json_encode(10000, '');
  248. } else {
  249. ds_json_encode(10001, '');
  250. }
  251. }
  252. /**
  253. * 批量加关注
  254. */
  255. public function batch_addfollow()
  256. {
  257. // 验证参数
  258. if (trim(input('param.ids')) == '') {
  259. ds_json_encode(10001, lang('param_error'));
  260. }
  261. $ids = explode(',', trim(input('param.ids')));
  262. if (empty($ids)) {
  263. ds_json_encode(10001, lang('param_error'));
  264. }
  265. $member_info = Db::name('member')->where('member_id', session('member_id'))->find();
  266. if (empty($member_info)) {
  267. ds_json_encode(10001, lang('snsfriend_member_error'));
  268. }
  269. // 将被关注会员列表
  270. $pm_array = Db::name('member')->where(array(array('member_id', 'in', $ids)))->select()->toArray();
  271. // 查询是否已经关注
  272. $gz_array = Db::name('snsfriend')->where(array(array('friend_frommid', '=', session('member_id')), array('friend_tomid', 'in', $ids)))->select()->toArray();
  273. $gz_array = array_under_reset($gz_array, 'friend_tomid', 1);
  274. // 查询对方是否关注我
  275. $bgz_array = Db::name('snsfriend')->where(array(array('friend_frommid', 'in', $ids), array('friend_tomid', '=', session('member_id'))))->select()->toArray();
  276. $bgz_array = array_under_reset($bgz_array, 'friend_frommid', 1);
  277. if (!empty($pm_array)) {
  278. $insert_array = array();
  279. foreach ($pm_array as $val) {
  280. if (isset($gz_array[$val['member_id']])) // 已关注跳出循环
  281. continue;
  282. if ($val['member_id'] == session('member_id')) // 不关注自己
  283. continue;
  284. $insert = array();
  285. $insert['friend_frommid'] = $member_info['member_id'];
  286. $insert['friend_frommname'] = $member_info['member_name'];
  287. $insert['friend_frommavatar'] = $member_info['member_avatar'];
  288. $insert['friend_tomid'] = $val['member_id'];
  289. $insert['friend_tomname'] = $val['member_name'];
  290. $insert['friend_tomavatar'] = $val['member_avatar'];
  291. $insert['friend_addtime'] = TIMESTAMP;
  292. if (isset($bgz_array[$val['member_id']])) {
  293. $insert['friend_followstate'] = 2;
  294. Db::name('snsfriend')->update(array('friend_followstate' => 2, 'friend_id' => $bgz_array[$val['member_id']]['friend_id']));
  295. } else {
  296. $insert['friend_followstate'] = 1;
  297. }
  298. $insert_array[] = $insert;
  299. }
  300. // 插入
  301. Db::name('snsfriend')->insertAll($insert_array);
  302. }
  303. ds_json_encode(10000, lang('snsfriend_follow_succ'));
  304. }
  305. /**
  306. * 关注列表页面
  307. */
  308. public function follow()
  309. {
  310. $snsfriend_model = model('snsfriend');
  311. //关注列表
  312. $follow_list = $snsfriend_model->getSnsfriendList(array('friend_frommid' => session('member_id')), '*', 10, 'detail');
  313. if (!empty($follow_list)) {
  314. foreach ($follow_list as $k => $v) {
  315. $v['sex_class'] = $this->m_sex($v['member_sex']);
  316. $follow_list[$k] = $v;
  317. }
  318. }
  319. View::assign('follow_list', $follow_list);
  320. View::assign('show_page', $snsfriend_model->page_info->render());
  321. $this->setMemberCurItem('follow');
  322. $this->setMemberCurMenu('member_snsfriend');
  323. return View::fetch($this->template_dir . 'member_snsfriend_follow');
  324. }
  325. /**
  326. * 粉丝列表
  327. */
  328. public function fan()
  329. {
  330. $snsfriend_model = model('snsfriend');
  331. //关注列表
  332. $fan_list = $snsfriend_model->getSnsfriendList(array('friend_tomid' => session('member_id')), '*', 10, 'fromdetail');
  333. if (!empty($fan_list)) {
  334. foreach ($fan_list as $k => $v) {
  335. $v['sex_class'] = $this->m_sex($v['member_sex']);
  336. $fan_list[$k] = $v;
  337. }
  338. }
  339. View::assign('fan_list', $fan_list);
  340. View::assign('show_page', $snsfriend_model->page_info->render());
  341. $this->setMemberCurItem('fan');
  342. $this->setMemberCurMenu('member_snsfriend');
  343. return View::fetch($this->template_dir . 'member_snsfriend_fan');
  344. }
  345. /**
  346. * 用户中心右边,小导航
  347. *
  348. * @param string $menu_type 导航类型
  349. * @param string $menu_key 当前导航的menu_key
  350. * @param array $array 附加菜单
  351. * @return
  352. */
  353. protected function getMemberItemList()
  354. {
  355. $menu_array = array(
  356. array('name' => 'find', 'text' => lang('snsfriend_findmember'), 'url' => (string)url('Membersnsfriend/index')),
  357. array('name' => 'follow', 'text' => lang('snsfriend_follow'), 'url' => (string)url('Membersnsfriend/follow')),
  358. array('name' => 'fan', 'text' => lang('snsfriend_fans'), 'url' => (string)url('Membersnsfriend/fan'))
  359. );
  360. return $menu_array;
  361. }
  362. }