Membersnsfriend.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. * DSMall多用户商城
  9. * ============================================================================
  10. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  11. * 网站地址: http://www.csdeshang.com
  12. * ----------------------------------------------------------------------------
  13. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  14. * 不允许对程序代码以任何形式任何目的的再发布。
  15. * ============================================================================
  16. * 控制器
  17. */
  18. class Membersnsfriend extends BaseMember {
  19. public function initialize() {
  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. switch ($sextype) {
  25. case 1:
  26. return 'male';
  27. break;
  28. case 2:
  29. return 'female';
  30. break;
  31. default:
  32. return '';
  33. break;
  34. }
  35. }
  36. /**
  37. * 找人首页
  38. */
  39. public function index() {
  40. // 查看推荐
  41. $mtag_list = Db::name('snsmembertag')->where(array('mtag_recommend' => 1))->order('mtag_sort asc')->select()->toArray();
  42. if (!empty($mtag_list)) {
  43. // 查询已关注好友 ,不显示已关注好友
  44. $friend_array = Db::name('snsfriend')->field('friend_tomid')->where(array('friend_frommid' => session('member_id')))->select()->toArray();
  45. $friendid_array[] = session('member_id');
  46. if (!empty($friend_array)) {
  47. foreach ($friend_array as $val) {
  48. $friendid_array[] = $val['friend_tomid'];
  49. }
  50. }
  51. $mtagid_array = array();
  52. foreach ($mtag_list as $val) {
  53. $mtagid_array[] = $val['mtag_id'];
  54. }
  55. // 查询会员
  56. $where[]=array('sns_mtagmember.mtag_id','in', $mtagid_array); //查询条件
  57. $where[] = array('sns_mtagmember.member_id','not in', $friendid_array);
  58. $tagmember_list = Db::name('snsmtagmember')->alias('sns_mtagmember')
  59. ->field('sns_mtagmember.*,member.member_avatar,member.member_name')
  60. ->join('member member', 'sns_mtagmember.member_id=member.member_id')
  61. ->where($where)
  62. ->order('sns_mtagmember.recommend desc, sns_mtagmember.member_id asc')
  63. ->limit(count($mtagid_array) * 20)->select()->toArray();
  64. // 整理
  65. $tagmember_list = array_under_reset($tagmember_list, 'mtag_id', 2);
  66. View::assign('mtag_list', $mtag_list);
  67. View::assign('tagmember_list', $tagmember_list);
  68. }
  69. View::assign('type', 'index');
  70. $this->setMemberCurItem('find');
  71. $this->setMemberCurMenu('member_snsfriend');
  72. return View::fetch($this->template_dir . 'member_snsfriend_find');
  73. }
  74. /**
  75. * 找人搜索列表
  76. */
  77. public function findlist() {
  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. $mid = intval(input('param.mid'));
  171. if ($mid <= 0) {
  172. ds_json_encode(10001,lang('param_error'));
  173. }
  174. //验证会员信息
  175. $member_model = model('member');
  176. $condition_arr = array();
  177. $condition_arr[]=array('member_state','=',"1");
  178. $condition_arr[] = array('member_id','in', array($mid, session('member_id')));
  179. $member_list = $member_model->getMemberList($condition_arr);
  180. unset($condition_arr);
  181. if (empty($member_list)) {
  182. ds_json_encode(10001,lang('snsfriend_member_error'));
  183. }
  184. $self_info = array();
  185. $member_info = array();
  186. foreach ($member_list as $k => $v) {
  187. if ($v['member_id'] == session('member_id')) {
  188. $self_info = $v;
  189. } else {
  190. $member_info = $v;
  191. }
  192. }
  193. if (empty($self_info) || empty($member_info)) {
  194. ds_json_encode(10001,lang('snsfriend_member_error'));
  195. }
  196. //验证是否已经存在好友记录
  197. $snsfriend_model = model('snsfriend');
  198. $friend_count = $snsfriend_model->getSnsfriendCount(array('friend_frommid' => session('member_id'), 'friend_tomid' => "$mid"));
  199. if ($friend_count > 0) {
  200. ds_json_encode(10001,lang('snsfriend_havefollowed'));
  201. }
  202. //查询对方是否已经关注我,从而判断关注状态
  203. $friend_info = $snsfriend_model->getOneSnsfriend(array('friend_frommid' => "{$mid}", 'friend_tomid' => session('member_id')));
  204. $insert_arr = array();
  205. $insert_arr['friend_frommid'] = "{$self_info['member_id']}";
  206. $insert_arr['friend_frommname'] = "{$self_info['member_name']}";
  207. $insert_arr['friend_frommavatar'] = "{$self_info['member_avatar']}";
  208. $insert_arr['friend_tomid'] = "{$member_info['member_id']}";
  209. $insert_arr['friend_tomname'] = "{$member_info['member_name']}";
  210. $insert_arr['friend_tomavatar'] = "{$member_info['member_avatar']}";
  211. $insert_arr['friend_addtime'] = TIMESTAMP;
  212. if (empty($friend_info)) {
  213. $insert_arr['friend_followstate'] = '1'; //单方关注
  214. } else {
  215. $insert_arr['friend_followstate'] = '2'; //双方关注
  216. }
  217. $result = $snsfriend_model->addSnsfriend($insert_arr);
  218. if ($result) {
  219. //更新对方关注状态
  220. if (!empty($friend_info)) {
  221. $snsfriend_model->editSnsfriend(array('friend_followstate' => '2'), array('friend_id' => "{$friend_info['friend_id']}"));
  222. }
  223. ds_json_encode(10000,'',array('state' => $insert_arr['friend_followstate']));
  224. } else {
  225. ds_json_encode(10001,'');
  226. }
  227. }
  228. /**
  229. * 取消关注
  230. */
  231. public function delfollow() {
  232. $mid = intval(input('param.mid'));
  233. if ($mid <= 0) {
  234. ds_json_encode(10001,lang('param_error'));
  235. }
  236. //取消关注
  237. $snsfriend_model = model('snsfriend');
  238. $result = $snsfriend_model->delSnsfriend(array('friend_frommid' => session('member_id'), 'friend_tomid' => "$mid"));
  239. if ($result) {
  240. //更新对方的关注状态
  241. $snsfriend_model->editSnsfriend(array('friend_followstate' => '1'), array('friend_frommid' => "$mid", 'friend_tomid' => session('member_id')));
  242. ds_json_encode(10000,'');
  243. } else {
  244. ds_json_encode(10001,'');
  245. }
  246. }
  247. /**
  248. * 批量加关注
  249. */
  250. public function batch_addfollow() {
  251. // 验证参数
  252. if (trim(input('param.ids')) == '') {
  253. ds_json_encode(10001,lang('param_error'));
  254. }
  255. $ids = explode(',', trim(input('param.ids')));
  256. if (empty($ids)) {
  257. ds_json_encode(10001,lang('param_error'));
  258. }
  259. $member_info = Db::name('member')->where('member_id',session('member_id'))->find();
  260. if (empty($member_info)) {
  261. ds_json_encode(10001,lang('snsfriend_member_error'));
  262. }
  263. // 将被关注会员列表
  264. $pm_array = Db::name('member')->where(array(array('member_id','in', $ids)))->select()->toArray();
  265. // 查询是否已经关注
  266. $gz_array = Db::name('snsfriend')->where(array(array('friend_frommid' ,'=', session('member_id')), array('friend_tomid','in', $ids)))->select()->toArray();
  267. $gz_array = array_under_reset($gz_array, 'friend_tomid', 1);
  268. // 查询对方是否关注我
  269. $bgz_array = Db::name('snsfriend')->where(array(array('friend_frommid','in', $ids), array('friend_tomid' ,'=', session('member_id'))))->select()->toArray();
  270. $bgz_array = array_under_reset($bgz_array, 'friend_frommid', 1);
  271. if (!empty($pm_array)) {
  272. $insert_array = array();
  273. foreach ($pm_array as $val) {
  274. if (isset($gz_array[$val['member_id']])) // 已关注跳出循环
  275. continue;
  276. if ($val['member_id'] == session('member_id')) // 不关注自己
  277. continue;
  278. $insert = array();
  279. $insert['friend_frommid'] = $member_info['member_id'];
  280. $insert['friend_frommname'] = $member_info['member_name'];
  281. $insert['friend_frommavatar'] = $member_info['member_avatar'];
  282. $insert['friend_tomid'] = $val['member_id'];
  283. $insert['friend_tomname'] = $val['member_name'];
  284. $insert['friend_tomavatar'] = $val['member_avatar'];
  285. $insert['friend_addtime'] = TIMESTAMP;
  286. if (isset($bgz_array[$val['member_id']])) {
  287. $insert['friend_followstate'] = 2;
  288. Db::name('snsfriend')->update(array('friend_followstate' => 2, 'friend_id' => $bgz_array[$val['member_id']]['friend_id']));
  289. } else {
  290. $insert['friend_followstate'] = 1;
  291. }
  292. $insert_array[] = $insert;
  293. }
  294. // 插入
  295. Db::name('snsfriend')->insertAll($insert_array);
  296. }
  297. ds_json_encode(10000,lang('snsfriend_follow_succ'));
  298. }
  299. /**
  300. * 关注列表页面
  301. */
  302. public function follow() {
  303. $snsfriend_model = model('snsfriend');
  304. //关注列表
  305. $follow_list = $snsfriend_model->getSnsfriendList(array('friend_frommid' => session('member_id')), '*', 10, 'detail');
  306. if (!empty($follow_list)) {
  307. foreach ($follow_list as $k => $v) {
  308. $v['sex_class'] = $this->m_sex($v['member_sex']);
  309. $follow_list[$k] = $v;
  310. }
  311. }
  312. View::assign('follow_list', $follow_list);
  313. View::assign('show_page', $snsfriend_model->page_info->render());
  314. $this->setMemberCurItem('follow');
  315. $this->setMemberCurMenu('member_snsfriend');
  316. return View::fetch($this->template_dir . 'member_snsfriend_follow');
  317. }
  318. /**
  319. * 粉丝列表
  320. */
  321. public function fan() {
  322. $snsfriend_model = model('snsfriend');
  323. //关注列表
  324. $fan_list = $snsfriend_model->getSnsfriendList(array('friend_tomid' => session('member_id')), '*', 10, 'fromdetail');
  325. if (!empty($fan_list)) {
  326. foreach ($fan_list as $k => $v) {
  327. $v['sex_class'] = $this->m_sex($v['member_sex']);
  328. $fan_list[$k] = $v;
  329. }
  330. }
  331. View::assign('fan_list', $fan_list);
  332. View::assign('show_page', $snsfriend_model->page_info->render());
  333. $this->setMemberCurItem('fan');
  334. $this->setMemberCurMenu('member_snsfriend');
  335. return View::fetch($this->template_dir . 'member_snsfriend_fan');
  336. }
  337. /**
  338. * 用户中心右边,小导航
  339. *
  340. * @param string $menu_type 导航类型
  341. * @param string $menu_key 当前导航的menu_key
  342. * @param array $array 附加菜单
  343. * @return
  344. */
  345. protected function getMemberItemList() {
  346. $menu_array = array(
  347. array('name' => 'find', 'text' => lang('snsfriend_findmember'), 'url' => (string)url('Membersnsfriend/index')),
  348. array('name' => 'follow', 'text' => lang('snsfriend_follow'), 'url' => (string)url('Membersnsfriend/follow')),
  349. array('name' => 'fan', 'text' => lang('snsfriend_fans'), 'url' => (string)url('Membersnsfriend/fan'))
  350. );
  351. return $menu_array;
  352. }
  353. }