| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace App\Services\Common;
- use App\Models\AgentModel;
- use App\Models\ActionLogModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 代理服务
- */
- class AgentService extends BaseService
- {
- public function __construct()
- {
- $this->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'])
- ->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' => '操作失败'];
- }
- }
|