// +---------------------------------------------------------------------- declare (strict_types = 1); namespace addon\wxchat\controller\member; use addon\wxchat\model\WxchatFamily; use addon\wxchat\model\WxchatFamilyMember; use think\facade\Db; use think\facade\Request; use ywxapp\controller\FrontendBase; use ywxapp\model\MemberProfile as ProfileModel; /** * 家族:创建 / 加入 / 退出 / 成员 / 家族群聊(复用 group 消息通道) */ class Family extends FrontendBase { /** * 创建家族 */ public function create() { $uid = $this->auth->uid; $name = trim((string) Request::post('name', '')); $avatar = trim((string) Request::post('avatar', '')); $notice = trim((string) Request::post('notice', '')); if (! $name) { $this->error('请填写家族名称'); } $family = new WxchatFamily(); $family->name = $name; $family->avatar = $avatar; $family->owner_uid = $uid; $family->notice = $notice; $family->member_count = 1; $family->status = 1; $family->create_at = time(); $family->save(); $member = new WxchatFamilyMember(); $member->family_id = $family->id; $member->uid = $uid; $member->role = 2; // 族长 $member->join_at = time(); $member->save(); $this->success(['family_id' => $family->id], '创建成功'); } /** * 加入家族 */ public function join() { $uid = $this->auth->uid; $familyId = (int) Request::post('family_id', 0); $family = WxchatFamily::where('id', $familyId)->where('status', 1)->find(); if (! $family) { $this->error('家族不存在或已解散'); } if (WxchatFamilyMember::where('family_id', $familyId)->where('uid', $uid)->find()) { $this->error('你已在家族中'); } $member = new WxchatFamilyMember(); $member->family_id = $familyId; $member->uid = $uid; $member->role = 0; $member->join_at = time(); $member->save(); $family->where('id', $familyId)->inc('member_count')->update(); $this->success([], '已加入'); } /** * 退出家族(族长退出则解散家族) */ public function quit() { $uid = $this->auth->uid; $familyId = (int) Request::post('family_id', 0); $member = WxchatFamilyMember::where('family_id', $familyId)->where('uid', $uid)->find(); if (! $member) { $this->error('你不在该家族'); } if ($member->role == 2) { // 族长退出 -> 解散 WxchatFamily::where('id', $familyId)->update(['status' => 0]); WxchatFamilyMember::where('family_id', $familyId)->delete(); } else { $member->delete(); WxchatFamily::where('id', $familyId)->where('member_count', '>', 0)->dec('member_count')->update(); } $this->success([], '已退出'); } /** * 家族信息 */ public function info() { $familyId = (int) Request::get('family_id', 0); $family = WxchatFamily::where('id', $familyId)->where('status', 1)->find(); if (! $family) { $this->error('家族不存在'); } $this->success($family); } /** * 成员列表(含昵称/头像) */ public function members() { $familyId = (int) Request::get('family_id', 0); $list = Db::name('wxchat_family_members')->alias('m') ->join('user u', 'u.uid = m.uid') ->join('user_profile p', 'p.uid = m.uid', 'left') ->where('m.family_id', $familyId) ->field('m.uid,m.role,m.join_at,u.nickname,u.avatar,p.gender,p.residecity') ->order('m.role', 'desc') ->select(); $this->success($list); } }