where(array('user_id'=>$this->userId))->order('group_id desc')->select(); //加入的群组 $join = 'gu LEFT JOIN t_group g ON gu.group_id=g.group_id'; $res2 = M('GroupUser')->join($join)->where(array('gu.user_id'=>$this->userId,'g.user_id'=>array('neq',$this->userId)))->order('gu_id desc')->field('g.*')->select(); if ($res1==NULL && $res2==NULL){ $res = array(); }else{ $res = array_merge($res1,$res2); } $data = array('code'=>0,'msg'=>'','data'=>$res); $this->returnData($data);exit; } //编辑群组 public function edit() { $userIds = I('uid'); $groupId = I('group_id'); $groupName = I('group_name'); if(empty($userIds)){ $data = array('code'=>1,'msg'=>'成员不能为空'); $this->returnData($data);exit; } $userInfo = M('User')->where(array('user_id'=>$this->userId))->field('user_id,user_name')->find(); if (empty($groupId)){ //新增 if (mb_strlen($groupName,'UTF8')<2 || mb_strlen($groupName,'UTF8')>12){ $data = array('code'=>1,'msg'=>'群组名称2到12个汉字'); $this->returnData($data);exit; } $groupId = M('Group')->add(array('group_name'=>$groupName,'user_id'=>$this->userId,'operate_dt'=>time(),)); $userIdArr = explode(',', trim($userIds,',')); foreach ($userIdArr as $uid){ $dataList[] = array('user_id'=>$uid, 'group_id'=>$groupId, 'operate_dt'=>time(),); $noticeContent = $userInfo['user_name'].'已成功邀您加入“'.$groupName.'”群组,详情可进入到我的好友-群组中查看'; $noticeList[] = array('user_id'=>$this->userId,'ref_user_id'=>$uid,'notice_content'=>$noticeContent,'operate_dt'=>time(),'notice_type'=>1,'notice_title'=>'群消息',); M('User')->where(array('user_id'=>$uid))->setInc('friend_notice'); } $dataList[] = array('user_id'=>$this->userId, 'group_id'=>$groupId, 'operate_dt'=>time(),); M('Notice')->addAll($noticeList); M('GroupUser')->addAll($dataList); M('Group')->where(array('group_id'=>$groupId))->setInc('group_num',count($dataList)); }else{ //修改 if (!empty($groupName)){ if (mb_strlen($groupName,'UTF8')<2 || mb_strlen($groupName,'UTF8')>12){ $data = array('code'=>1,'msg'=>'群组名称2到12个汉字'); $this->returnData($data);exit; } } $info = M('Group')->where(array('group_id'=>$groupId))->find(); if ($info['user_id']!=$this->userId){ $data = array('code'=>1,'msg'=>'无权限编辑该群组'); $this->returnData($data);exit; } if ($info['group_name'] != $groupName && !empty($groupName)){ M('Group')->where(array('group_id'=>$info['group_id']))->save(array('group_name'=>$groupName)); } $userIdArr = explode(',', $userIds); foreach ($userIdArr as $uid){ $groupUserInfo = M('GroupUser')->where(array('group_id'=>$info['group_id'],'user_id'=>$uid))->find(); if ($groupUserInfo){ continue; } $noticeContent = $userInfo['user_name'].'已成功邀您加入“'.$groupName.'”群组,详情可进入到我的好友-群组中查看'; $noticeList[] = array('user_id'=>$this->userId,'ref_user_id'=>$uid,'notice_content'=>$noticeContent,'operate_dt'=>time(),'notice_type'=>1,'notice_title'=>'群消息',); M('User')->where(array('user_id'=>$uid))->setInc('friend_notice'); $dataList[] = array('user_id'=>$uid, 'group_id'=>$groupId, 'operate_dt'=>time(),); } //$dataList[] = array('user_id'=>$this->userId, 'group_id'=>$groupId, 'operate_dt'=>time(),); if (count($dataList)){ M('Notice')->addAll($noticeList); M('GroupUser')->addAll($dataList); M('Group')->where(array('group_id'=>$groupId))->setInc('group_num',count($dataList)); } } $data = array('code'=>0,'msg'=>'群组编辑成功'); $this->returnData($data);exit; } //删除群组 public function del(){ $groupId = I('group_id'); $info = M('Group')->where(array('group_id'=>$groupId))->find(); if ($info==NULL){ $data = array('code'=>1,'msg'=>'群组id错误'); $this->returnData($data);exit; } if ($info['user_id']==$this->userId){ M('GroupUser')->where(array('group_id'=>$info['group_id'],'user_id'=>$this->userId,))->delete(); //自己创建的群组,删除后,最早的成员,设置成群管理员 $groupUserInfo = M('GroupUser')->where(array('group_id'=>$info['group_id'],))->order('gu_id asc')->limit(1)->find(); if ($groupUserInfo){ M('Group')->where(array('group_id'=>$groupId))->save(array('user_id'=>$groupUserInfo['user_id'])); }else{ M('Group')->where(array('group_id'=>$groupId))->delete(); } }else{ M('GroupUser')->where(array('group_id'=>$info['group_id'],'user_id'=>$this->userId,))->delete(); } $data = array('code'=>0,'msg'=>'退出群组成功'); $this->returnData($data);exit; } //获取已经添加指定群的用户 public function getUserListByGroupId(){ $groupId = I('group_id'); $join = 'gu LEFT JOIN t_user u ON gu.user_id=u.user_id'; $res = M('GroupUser')->join($join)->where(array('group_id'=>$groupId,'gu.user_id'=>array('neq','u.user_id'),))->field('u.user_id,user_phone,user_name,user_level,user_sex,user_icon')->select(); //echo M('GroupUser')->getLastSql();exit; $userNameArr = $this->getUserNickname(); if ($res==NULL){ $res = array(); } foreach ($res as $key=>$val){ if (isset($userNameArr[$val['user_id']])){ $res[$key]['user_nickname'] = $userNameArr[$val['user_id']]['user_nickname']; } } $data = array('code'=>0,'msg'=>'','data'=>$res); $this->returnData($data);exit; } }