request->get('username'))) { $username = $this->request->get('username'); $where[] = ['bu.username', 'like', '%' . $username . '%']; } // 昵称搜索 if (!empty($this->request->get('nickname'))) { $nickname = $this->request->get('nickname'); $where[] = ['bu.nickname', 'like', '%' . $nickname . '%']; } // 邮箱搜索 if (!empty($this->request->get('email'))) { $email = $this->request->get('email'); $where[] = ['bu.email', 'like', '%' . $email . '%']; } // 手机号搜索 if (!empty($this->request->get('mobile'))) { $mobile = $this->request->get('mobile'); $where[] = ['bu.mobile', 'like', '%' . $mobile . '%']; } // 状态筛选 if ($this->request->has('status') && $this->request->get('status') !== '') { $status = $this->request->get('status'); $where[] = ['bu.status', '=', $status]; } // 企业搜索(通过企业成员表关联) if (!empty($this->request->get('company'))) { $company = $this->request->get('company'); $companyIds = Db::name('ba_company') ->where('name', 'like', '%' . $company . '%') ->column('id'); if (!empty($companyIds)) { $userIds = Db::name('ba_company_member') ->where('company_id', 'in', $companyIds) ->column('user_id'); if (!empty($userIds)) { $where[] = ['bu.id', 'in', $userIds]; } else { // 如果没有匹配的用户,返回空结果 $where[] = ['bu.id', '=', 0]; } } else { // 如果没有匹配的企业,返回空结果 $where[] = ['bu.id', '=', 0]; } } // 排序 $order = []; if (!empty($this->request->get('order'))) { $orderParam = $this->request->get('order'); $orderArray = explode(',', $orderParam); if (count($orderArray) === 2) { $order[$orderArray[0]] = $orderArray[1]; } } else { $order['bu.id'] = 'desc'; } // 分页 $page = $this->request->get('page', 1); $limit = $this->request->get('limit', 20); // 查询用户列表 $query = Db::table('ba_user') ->alias('bu') ->field([ 'bu.id', 'bu.username', 'bu.nickname', 'bu.email', 'bu.mobile', 'bu.avatar', 'bu.gender', 'bu.status', 'bu.create_time', 'bu.last_login_time', 'bu.last_login_ip', 'bu.join_ip', 'bu.motto', 'bu.invitation_code', 'GROUP_CONCAT(DISTINCT bc.name SEPARATOR ", ") as company_names', 'GROUP_CONCAT(DISTINCT inviter.username SEPARATOR ", ") as inviter_names' ]) ->leftJoin('ba_company_member bcm', 'bu.id = bcm.user_id') ->leftJoin('ba_company bc', 'bcm.company_id = bc.id AND bc.status = 1') ->leftJoin('ba_user_invitation bui', 'bu.id = bui.invitee_id') ->leftJoin('ba_user inviter', 'bui.inviter_id = inviter.id') ->where($where) ->group('bu.id') ->order($order); $userList = $query->paginate([ 'list_rows' => $limit, 'page' => $page ]); // 处理时间戳字段 $items = $userList->items(); foreach ($items as &$item) { // 转换时间戳为日期时间格式 if (!empty($item['create_time'])) { $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']); } if (!empty($item['last_login_time'])) { $item['last_login_time'] = date('Y-m-d H:i:s', $item['last_login_time']); } if (!empty($item['join_time'])) { $item['join_time'] = date('Y-m-d H:i:s', $item['join_time']); } // 处理性别显示 $genderMap = [ 0 => '未知', 1 => '男', 2 => '女' ]; $item['gender_text'] = $genderMap[$item['gender']] ?? '未知'; // 处理状态显示 if ($item['status'] === 'enable') { $item['status_text'] = '启用'; } elseif ($item['status'] === 'disable') { $item['status_text'] = '禁用'; } else { $item['status_text'] = $item['status']; } // 如果企业名为空,设置为空字符串 if (empty($item['company_names'])) { $item['company_names'] = ''; } // 如果邀请人为空,设置为空字符串 if (empty($item['inviter_names'])) { $item['inviter_names'] = ''; } } $this->success('查询成功', [ 'list' => $items, 'total' => $userList->total() ]); } /** * 新增/编辑用户 * @return void */ public function edit():void { $id = $this->request->post('id'); $username = $this->request->post('name'); $email = $this->request->post('email'); $password = $this->request->post('password'); $company = $this->request->post('company'); $company_id = $this->request->post('company_id'); $status = $this->request->post('status', 1); if (empty($username) || empty($email)) { $this->error(__('用户名和邮箱不能为空')); } // 验证邮箱格式 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $this->error(__('邮箱格式不正确')); } // 检查用户名是否已存在(编辑时排除自己) $checkUsername = Db::name('user') ->where('username', $username); if (!empty($id)) { $checkUsername->where('id', '<>', $id); } if ($checkUsername->find()) { $this->error(__('用户名已存在')); } // 检查邮箱是否已存在(编辑时排除自己) $checkEmail = Db::name('user') ->where('email', $email); if (!empty($id)) { $checkEmail->where('id', '<>', $id); } if ($checkEmail->find()) { $this->error(__('邮箱已存在')); } $data = [ 'username' => $username, 'nickname' => $username, 'email' => $email, 'status' => $status, 'update_time' => time() ]; // 如果有密码,进行加密 if (!empty($password)) { $data['password'] = password_hash($password, PASSWORD_DEFAULT); } Db::startTrans(); try { if (!empty($id)) { // 编辑 $result = Db::name('user') ->where('id', $id) ->update($data); $message = '编辑成功'; } else { // 新增 $data['join_time'] = time(); $data['create_time'] = time(); // 生成邀请码(可选) if (empty($data['invitation_code'])) { $data['invitation_code'] = $this->generateInvitationCode(); } $result = Db::name('user') ->insert($data); $id = Db::name('user')->getLastInsID(); $message = '新增成功'; } // 处理企业成员关系 if ($company_id > 0) { // 检查企业是否存在 $company = Db::name('company') ->where('id', $company_id) ->where('status', 1) ->find(); if ($company) { // 检查是否已经是该企业成员 $member = Db::name('company_member') ->where('user_id', $id) ->find(); if ($member) { // 更新企业成员关系 Db::name('company_member') ->where('user_id', $id) ->update([ 'company_id' => $company_id, 'email' => $email, 'name' =>$username, 'update_time' => date('Y-m-d H:i:s') ]); } else { // 新增企业成员关系 Db::name('company_member') ->insert([ 'company_id' => $company_id, 'user_id' => $id, 'email' => $email, 'name' =>$username, 'role' => 2, // 普通成员 'status' => 1, 'join_time' => date('Y-m-d H:i:s'), 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s') ]); } } } else { // 如果企业ID为空,删除企业成员关系 Db::name('company_member') ->where('user_id', $id) ->delete(); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error('操作失败: ' . $e->getMessage()); } if ($result) { $this->success($message, ['id' => $id]); } else { $this->error(__('操作失败')); } } /** * 生成邀请码 * @return string */ private function generateInvitationCode(): string { $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $code = ''; for ($i = 0; $i < 6; $i++) { $code .= $characters[rand(0, strlen($characters) - 1)]; } // 检查是否已存在 $exists = Db::name('user') ->where('invitation_code', $code) ->find(); if ($exists) { return $this->generateInvitationCode(); } return $code; } /** * 用户详情 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function detail(): void { $id = $this->request->get('id'); if (empty($id)) { $this->error(__('参数错误')); } // 获取用户基础信息 $userInfo = Db::name('user') ->where('id', $id) ->find(); if (!$userInfo) { $this->error(__('用户不存在')); } // 转换时间戳字段 if (!empty($userInfo['create_time'])) { $userInfo['create_time_str'] = date('Y-m-d H:i:s', $userInfo['create_time']); } if (!empty($userInfo['last_login_time'])) { $userInfo['last_login_time_str'] = date('Y-m-d H:i:s', $userInfo['last_login_time']); } if (!empty($userInfo['join_time'])) { $userInfo['join_time_str'] = date('Y-m-d H:i:s', $userInfo['join_time']); } if (!empty($userInfo['update_time'])) { $userInfo['update_time_str'] = date('Y-m-d H:i:s', $userInfo['update_time']); } // 获取用户所在企业信息 $companyInfo = Db::name('company_member') ->alias('bcm') ->leftJoin('ba_company bc', 'bcm.company_id = bc.id') ->where('bcm.user_id', $id) ->field('bc.id, bc.name') ->find(); $userInfo['company_id'] = $companyInfo['id'] ?? 0; $userInfo['company_name'] = $companyInfo['name'] ?? ''; // 获取用户佣金信息 $commissionInfo = Db::name('user_commission') ->where('user_id', $id) ->find(); if (!$commissionInfo) { // 如果没有佣金记录,创建默认记录 $commissionInfo = [ 'total_commission' => '0.00', 'current_commission' => '0.00', 'frozen_commission' => '0.00', 'withdrawn_commission' => '0.00', 'promotion_link' => $this->generatePromotionLink($id), 'invitation_count' => 0 ]; } // 获取用户购买的产品列表(包括套餐信息) $productList = Db::name('user_product_usage') ->alias('up') ->leftJoin('ba_product p', 'up.product_id = p.id') ->leftJoin('ba_package_price pp', 'up.package_price_id = pp.id') ->leftJoin('ba_package pk', 'pp.package_id = pk.id') ->where('up.user_id', $id) ->field('up.*, p.name as product_name, pp.spec_name as package_name, pp.card_type, pk.name as package_full_name') ->order('up.create_time', 'desc') ->select() ->toArray(); // 处理产品状态和显示信息 foreach ($productList as &$product) { // 确定产品名称 if (!empty($product['package_full_name'])) { $product['display_name'] = $product['package_full_name'] . ' (' . $product['package_name'] . ')'; } else { $product['display_name'] = $product['product_name'] ?? '未知产品'; } // 处理卡类型显示 $cardTypeMap = [ 1 => '次卡', 2 => '积分卡', 3 => '日卡', 4 => '周卡', 5 => '季卡', 6 => '月卡', 7 => '年卡' ]; $product['card_type_text'] = $cardTypeMap[$product['card_type']] ?? '未知'; // 处理购买时间 if (!empty($product['create_time'])) { $product['buy_time'] = date('Y-m-d H:i:s', strtotime($product['create_time'])); } else { $product['buy_time'] = ''; } // 处理到期时间 if (!empty($product['end_time'])) { $product['expire_time'] = date('Y-m-d', strtotime($product['end_time'])); } else { $product['expire_time'] = ''; } // 判断状态 $currentTime = time(); $endTime = !empty($product['end_time']) ? strtotime($product['end_time']) : 0; if ($product['status'] == 2) { $product['status_text'] = '已过期'; } elseif ($product['status'] == 3) { $product['status_text'] = '已用完'; } elseif ($endTime > 0 && $endTime < $currentTime) { $product['status_text'] = '已过期'; } elseif ($product['remaining_times'] == 0 && $product['card_type'] == 1) { $product['status_text'] = '已用完'; } elseif ($product['remaining_points'] == 0 && $product['card_type'] == 2) { $product['status_text'] = '已用完'; } else { $product['status_text'] = '正常'; } } // 获取下级人员(通过邀请关系) $subordinateList = Db::name('user_invitation') ->alias('bui') ->leftJoin('ba_user u', 'bui.invitee_id = u.id') ->leftJoin('ba_user_commission uc', 'u.id = uc.user_id') ->where('bui.inviter_id', $id) ->field('u.id, u.username, u.nickname, u.email, uc.current_commission, bui.create_time') ->order('bui.create_time', 'desc') ->select() ->toArray(); // 处理下级人员数据 foreach ($subordinateList as &$subordinate) { if (!empty($subordinate['create_time'])) { $subordinate['bind_time'] = date('Y-m-d H:i:s', strtotime($subordinate['create_time'])); } else { $subordinate['bind_time'] = ''; } $subordinate['name'] = $subordinate['nickname'] ?: $subordinate['username']; $subordinate['commission_amount'] = $subordinate['current_commission'] ?? '0.00'; $subordinate['status_text'] = '正常'; } $result = [ 'user_info' => $userInfo, 'commission_info' => $commissionInfo, 'product_list' => $productList, 'subordinate_list' => $subordinateList ]; $this->success('查询成功', $result); } /** * 删除用户 * @return void */ public function delete(): void { $id = $this->request->post('id'); if (empty($id)) { $this->error(__('参数错误')); } // 检查是否存在 $user = Db::name('user') ->where('id', $id) ->find(); if (!$user) { $this->error(__('用户不存在')); } // 检查是否有下级员工(通过邀请关系) $hasSubordinate = Db::name('user_invitation') ->where('inviter_id', $id) ->count(); if ($hasSubordinate > 0) { $this->error(__('该用户存在下级员工,请先处理下级关系')); } Db::startTrans(); try { // 删除用户 $result = Db::name('user') ->where('id', $id) ->delete(); // 删除用户佣金记录 Db::name('user_commission') ->where('user_id', $id) ->delete(); // 删除用户产品使用记录 Db::name('user_product_usage') ->where('user_id', $id) ->delete(); // 删除企业成员关系 Db::name('company_member') ->where('user_id', $id) ->delete(); // 删除邀请关系(如果该用户是别人的下级) Db::name('user_invitation') ->where('invitee_id', $id) ->delete(); // 删除邀请码 Db::name('invitation_code') ->where('user_id', $id) ->delete(); // 删除用户产品记录 Db::name('user_product') ->where('user_id', $id) ->delete(); // 删除用户关系表记录 Db::name('user_relation') ->where('user_id', $id) ->whereOr('subordinate_id', $id) ->delete(); // 删除佣金流水记录 Db::name('commission_flow') ->where('user_id', $id) ->whereOr('from_user_id', $id) ->delete(); // 删除分销记录 Db::name('distribution_record') ->where('buyer_id', $id) ->whereOr('distributor_id', $id) ->delete(); Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error('删除失败: ' . $e->getMessage()); } if ($result) { $this->success('删除成功'); } else { $this->error(__('删除失败')); } } /** * 解除下级关系 * @return void */ public function removeSubordinate(): void { $inviteeId = $this->request->post('invitee_id'); $inviterId = $this->request->post('inviter_id'); if (empty($inviteeId) || empty($inviterId)) { $this->error(__('参数错误')); } Db::startTrans(); try { $result = Db::name('ba_user_invitation') ->where('inviter_id', $inviterId) ->where('invitee_id', $inviteeId) ->delete(); Db::commit(); if ($result) { $this->success('解除成功'); } else { $this->error(__('解除失败')); } } catch (\Exception $e) { Db::rollback(); $this->error('解除失败: ' . $e->getMessage()); } } /** * 获取用户状态选项 * @return void */ public function statusOptions():void { $options = [ ['value' => 1, 'label' => '启用'], ['value' => 2, 'label' => '禁用'], ['value' => 3, 'label' => '待审核'], ['value' => 4, 'label' => '审核拒绝'] ]; $this->success('获取成功', $options); } /** * 生成推广链接 */ private function generatePromotionLink(int $userId): string { // 获取用户的邀请码 $user = Db::table('ba_user') ->where('id', $userId) ->field('invitation_code') ->find(); $invitationCode = $user['invitation_code'] ?? ''; if (empty($invitationCode)) { // 如果用户还没有邀请码,先生成一个 $invitationCode = $this->generateInvitationCode(); Db::table('ba_user') ->where('id', $userId) ->update(['invitation_code' => $invitationCode]); } $domain = request()->domain(); return $domain . '/#/user/register?invite=' . $invitationCode; } }