Membersnsfriend.php 16 KB

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