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