model = new AgentModel(); } /** * 获取代理列表 */ public function getDataList($params, $pageSize = 15) { $query = $this->model->where('mark', 1); // 审核状态筛选:1-已审核,2-待审核,3-驳回,4-冻结 if (isset($params['status']) && $params['status'] > 0) { $query->where('status', $params['status']); } // 关键词搜索(姓名、电话) if (isset($params['keyword']) && $params['keyword']) { $keyword = $params['keyword']; $query->where(function ($q) use ($keyword) { $q->where('real_name', 'like', '%' . $keyword . '%') ->orWhere('phone', 'like', '%' . $keyword . '%'); }); } $list = $query->with(['user']) ->withCount(['invites']) ->orderBy('create_time', 'desc') ->orderBy('id', 'desc') ->paginate($pageSize); $list = $list ? $list->toArray() : []; if ($list && isset($list['data'])) { foreach ($list['data'] as &$item) { // 确保 status 是整数类型 $item['status'] = (int)$item['status']; // 时间格式化:如果是时间戳则直接格式化,否则先转时间戳 $item['create_time'] = $item['create_time'] ? (is_numeric($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : date('Y-m-d H:i:s', strtotime($item['create_time']))) : ''; $item['update_time'] = $item['update_time'] ? (is_numeric($item['update_time']) ? date('Y-m-d H:i:s', $item['update_time']) : date('Y-m-d H:i:s', strtotime($item['update_time']))) : ''; $item['user'] = $item['user'] ?? []; // 计算团队人数(这里简化处理,实际应该查询下级用户) $item['team_count'] = 0; if (isset($item['user_id']) && $item['user_id']) { $item['team_count'] = \App\Models\MemberModel::where('parent_id', $item['user_id']) ->where('mark', 1) ->count(); } } } return [ 'msg' => '操作成功', 'code' => 0, 'data' => $list['data'] ?? [], 'count' => $list['total'] ?? 0, ]; } /** * 获取代理详情 */ public function getInfo($id) { $info = $this->model->where('id', $id)->where('mark', 1) ->with(['user']) ->first(); if (!$info) { return ['code' => 1, 'msg' => '代理不存在']; } $info = $info->toArray(); // 确保 status 是整数类型 $info['status'] = (int)$info['status']; // 时间格式化:如果是时间戳则直接格式化,否则先转时间戳 $info['create_time'] = $info['create_time'] ? (is_numeric($info['create_time']) ? date('Y-m-d H:i:s', $info['create_time']) : date('Y-m-d H:i:s', strtotime($info['create_time']))) : ''; $info['update_time'] = $info['update_time'] ? (is_numeric($info['update_time']) ? date('Y-m-d H:i:s', $info['update_time']) : date('Y-m-d H:i:s', strtotime($info['update_time']))) : ''; // 计算团队人数 $info['team_count'] = 0; if (isset($info['user_id']) && $info['user_id']) { $info['team_count'] = \App\Models\MemberModel::where('parent_id', $info['user_id']) ->where('mark', 1) ->count(); } return ['code' => 0, 'msg' => '操作成功', 'data' => $info]; } /** * 删除代理 */ public function delete() { $id = request()->post('id'); if (!$id) { return ['code' => 1, 'msg' => '参数错误']; } if (is_array($id)) { $result = $this->model->whereIn('id', $id)->update([ 'mark' => 0, 'update_time' => time() ]); } else { $result = $this->model->where('id', $id)->update([ 'mark' => 0, 'update_time' => time() ]); } if ($result) { ActionLogModel::setTitle("删除代理"); ActionLogModel::record(); RedisService::keyDel("caches:agents:*"); return ['code' => 0, 'msg' => '删除成功']; } return ['code' => 1, 'msg' => '删除失败']; } /** * 更新状态 */ public function status() { $id = request()->post('id'); $status = request()->post('status'); if (!$id || !isset($status)) { return ['code' => 1, 'msg' => '参数错误']; } $result = $this->model->where('id', $id)->update([ 'status' => $status, 'update_time' => time() ]); if ($result !== false) { ActionLogModel::setTitle("更新代理状态"); ActionLogModel::record(); RedisService::keyDel("caches:agents:*"); return ['code' => 0, 'msg' => '操作成功']; } return ['code' => 1, 'msg' => '操作失败']; } /** * 审核代理 */ public function audit() { $id = request()->post('id'); $status = request()->post('status'); // 1-已审核(通过),3-驳回 $confirmRemark = request()->post('confirm_remark', ''); if (!$id || !$status) { return ['code' => 1, 'msg' => '参数错误']; } $agent = $this->model->find($id); if (!$agent) { return ['code' => 1, 'msg' => '代理不存在']; } if ($agent->status != 2) { return ['code' => 1, 'msg' => '该代理不是待审核状态']; } if ($status == 3 && !$confirmRemark) { return ['code' => 1, 'msg' => '请填写驳回原因']; } $updateData = [ 'status' => $status, 'confirm_remark' => $confirmRemark, 'update_time' => time() ]; $result = $this->model->where('id', $id)->update($updateData); if ($result) { $statusText = $status == 1 ? '通过' : '驳回'; ActionLogModel::setTitle("审核代理-{$statusText}"); ActionLogModel::record(); RedisService::keyDel("caches:agents:*"); return ['code' => 0, 'msg' => "审核{$statusText}"]; } return ['code' => 1, 'msg' => '操作失败']; } /** * 冻结/解冻代理 */ public function freeze() { $id = request()->post('id'); $status = request()->post('status'); // 4-冻结,1-解冻 if (!$id || !$status) { return ['code' => 1, 'msg' => '参数错误']; } $agent = $this->model->find($id); if (!$agent) { return ['code' => 1, 'msg' => '代理不存在']; } $updateData = [ 'status' => $status, 'update_time' => time() ]; $result = $this->model->where('id', $id)->update($updateData); if ($result) { $statusText = $status == 4 ? '冻结' : '解冻'; ActionLogModel::setTitle("{$statusText}代理"); ActionLogModel::record(); RedisService::keyDel("caches:agents:*"); return ['code' => 0, 'msg' => "{$statusText}成功"]; } return ['code' => 1, 'msg' => '操作失败']; } }