123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- namespace app\admin\controller\commission;
- use app\common\controller\Backend;
- use app\common\model\commission\Agent as AgentModel;
- use app\common\model\User as UserModel;
- use app\common\model\commission\Log as LogModel;
- use app\common\model\commission\Level as LevelModel;
- use app\common\Service\Commission\Agent as AgentService;
- use EasyWeChat\OfficialAccount\Server\Handlers\EchoStrHandler;
- use think\Db;
- class Agent extends Backend
- {
- protected $noNeedRight = ['select'];
- protected $model = null;
-
- /**
- * 快速搜索时执行查找的字段
- */
- protected $searchFields = 'user_id,user.nickname,user.mobile';
-
- /**
- * 是否是关联查询
- */
- protected $relationSearch = true;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new AgentModel();
- }
- /**
- * 查看
- */
- public function index()
- {
- if (!$this->request->isAjax()) {
- return $this->view->fetch();
- }
- //如果发送的来源是 Selectpage,则转发到 Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
-
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
-
- // 处理特殊筛选条件
- $filter = $this->request->get("filter", '');
- if ($filter) {
- $filter = json_decode($filter, true);
- if (isset($filter['tabActive'])) {
- if ($filter['tabActive'] === 'pending') {
- $where['status'] = 'pending';
- } elseif ($filter['tabActive'] === '0') {
- $where['level_status'] = ['>', 0];
- }
- }
- }
-
- $list = $this->model
- ->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
-
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- /**
- * 详情
- *
- * @param $id
- */
- public function detail($ids = null)
- {
-
- $row = $this->model->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])
- ->where('user_id', $ids)
- ->find();
- if (!$row) {
- $this->error(__('No Results were found'));
- }
-
- // 如果expire_time为空,设置默认值为become_time + 1年
- // if (!$row->expire_time && $row->become_time) {
- // $row->expire_time = strtotime('+1 year', $row->become_time);
- // }
-
- $this->view->assign('row', $row);
- return $this->view->fetch();
- }
- /**
- * 团队
- *
- * @param $id
- */
- public function team($ids = null)
- {
- // 获取ID参数,优先使用URL中的id参数
- $ids = $ids ?: $this->request->param('id');
-
- // 将当前分销商ID传递给前端
- $this->assignconfig('current_agent_id', $ids);
-
- if (!$this->request->isAjax()) {
- // 获取当前分销商详细信息用于模板渲染
- $currentAgent = $this->model->with(['user.parent_user', 'level_info'])
- ->where('user_id', $ids)
- ->find();
-
- if (!$currentAgent) {
- $this->error(__('No Results were found'));
- }
-
- $this->view->assign('currentAgent', $currentAgent);
- return $this->view->fetch();
- }
- //如果发送的来源是 Selectpage,则转发到 Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
- // 以分销商表为主表查询团队成员
- // 安全的排序字段处理
- // $allowedSorts = ['user_id', 'status', 'level', 'total_income', 'pending_reward', 'become_time'];
- // $safeSort = in_array($sort, $allowedSorts) ? $sort : 'user_id';
-
- $list = $this->model
- ->hasWhere('user', function ($query) use ($ids) {
- return $query->where('parent_user_id', $ids);
- })
- ->with(['user', 'level_info'])
- // ->order($safeSort, $order ?: 'desc')
- ->paginate($limit);
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- // 选择分销商
- public function select()
- {
- if (!$this->request->isAjax()) {
- return $this->view->fetch();
- }
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
-
- $data = $this->model
- ->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
-
- $result = ['total' => $data->total(), 'rows' => $data->items()];
- return json($result);
- }
- /**
- * 编辑
- *
- * @param $id
- */
- public function edit($ids = null)
- {
- $params = $this->request->only(['status', 'upgrade_lock', 'level_status', 'level']);
- $row = $this->model->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])->where('user_id', $ids)->find();
- if (!$row) {
- $this->error('未找到该分销商');
- }
-
- $result = false;
- foreach ($params as $field => $value) {
- switch ($field) {
- case 'status': // 修改状态
- $result = $this->changeStatus($row, $value);
- break;
- case 'level_status': // 审核等级
- $result = $this->changeLevelStatus($row, $value);
- break;
- case 'level': // 修改等级
- $result = $this->changeLevel($row, $value);
- break;
- default:
- $result = $row->save([$field => $value]);
- }
- }
-
- if ($result) {
- $this->success('更新成功');
- } else {
- $this->error('更新失败');
- }
- }
- // 修改状态
- private function changeStatus($row, $value)
- {
- $result = $row->save(['status' => $value]);
-
- if ($result) {
- LogModel::add($row->user_id, 'agent', ['type' => 'status', 'value' => $value]);
- (new AgentService($row->user_id))->createAsyncAgentUpgrade();
- }
- return $result;
- }
- // 审核等级
- private function changeLevelStatus($row, $value)
- {
- if ($row->level_status == 0 && $value > 0) {
- $this->error('非法操作');
- }
- if ($value == 0) { // 拒绝操作
- return $row->save(['level_status' => 0]);
- } else { // 同意操作
- if ($row->upgrade_level) {
- $result = $row->save(['level_status' => 0, 'level' => $row->upgrade_level->level]);
- if ($result) {
- LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $row->upgrade_level]);
- (new AgentService($row->user_id))->createAsyncAgentUpgrade();
- }
- return $result;
- }
- }
- return false;
- }
- // 修改等级
- private function changeLevel($row, $value)
- {
- $level = LevelModel::find($value);
- if ($level) {
- $result = $row->save(['level' => $level->level]);
- if ($result) {
- LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $level]);
- (new AgentService($row->user_id))->createAsyncAgentUpgrade();
- }
- return $result;
- } else {
- $this->error('未找到该等级');
- }
- }
- // 更换推荐人
- public function changeParentUser($id)
- {
- $userAgent = new AgentService($id);
- if (!$userAgent->user) {
- $this->error('未找到该用户');
- }
- $parentUserId = $this->request->param('parent_user_id', 0);
- // 更换推荐人检查
- if ($parentUserId != 0) {
- $parentAgent = new AgentService($parentUserId);
- if (!$parentAgent->isAgentAvaliable()) {
- $this->error('选中用户暂未成为分销商,不能成为推荐人');
- }
- if (!$this->checkChangeParentAgent($id, $parentUserId)) {
- $this->error('不能绑定该上级');
- }
- LogModel::add($parentUserId, 'share', ['user' => $userAgent->user]);
- if ($userAgent->isAgentAvaliable()) {
- LogModel::add($id, 'bind', ['user' => $parentAgent->user ?? NULL]);
- }
- }
- $lastParentUserId = $userAgent->user->parent_user_id;
- $userAgent->user->parent_user_id = $parentUserId;
- $userAgent->user->save();
- if ($lastParentUserId > 0) {
- $userAgent->createAsyncAgentUpgrade($lastParentUserId);
- }
- if ($parentUserId > 0) {
- $userAgent->createAsyncAgentUpgrade($parentUserId);
- }
- $this->success('绑定成功');
- }
- // 递归往上找推荐人,防止出现推荐循环
- private function checkChangeParentAgent($userId, $parentUserId)
- {
- if ($userId == $parentUserId) {
- $this->error('推荐人不能是本人');
- }
- if ($parentUserId == 0) {
- return true;
- }
- $parentAgent = UserModel::find($parentUserId);
- if ($parentAgent) {
- if ($parentAgent->parent_user_id == $userId) {
- $this->error("已选中分销商的上级团队中已存在该用户");
- }
- if ($parentAgent->parent_user_id == 0) {
- return true;
- } else {
- return $this->checkChangeParentAgent($userId, $parentAgent->parent_user_id);
- }
- }
- return false;
- }
- /**
- * 更新分销商到期时间
- */
- public function updateExpireTime()
- {
- $userId = $this->request->param('user_id');
- $expireTime = $this->request->param('expire_time');
-
- if (!$userId) {
- $this->error(__('参数错误'));
- }
- // 验证分销商是否存在
- $agent = AgentModel::get($userId);
- if (!$agent) {
- $this->error(__('分销商不存在'));
- }
- // 验证时间格式
- if ($expireTime && !strtotime($expireTime)) {
- $this->error(__('时间格式不正确'));
- }
- // try {
- // 更新到期时间
- $updateData = [];
- if ($expireTime) {
- $updateData['expire_time'] = strtotime($expireTime);
- } else {
- $updateData['expire_time'] = null; // 设置为null表示永久有效
- }
- $result = AgentModel::where('user_id', $userId)->update($updateData);
-
- if ($result !== false) {
- $this->success(__('修改成功'));
- } else {
- $this->error(__('修改失败'));
- }
- // } catch (\Exception $e) {
- // $this->error(__('修改失败: %s', $e->getMessage()));
- // }
- }
- /**
- * 设置默认到期时间
- * 在用户成为分销商时调用,设置默认到期时间为成为分销商时间+1年
- */
- public function setDefaultExpireTime($userId, $becomeTime = null)
- {
- if (!$becomeTime) {
- $becomeTime = time(); // 如果没有传入成为分销商时间,使用当前时间
- }
- // 计算默认到期时间(1年后)
- $expireTime = strtotime('+1 year', $becomeTime);
- // 更新到期时间
- AgentModel::where('user_id', $userId)->update([
- 'expire_time' => $expireTime
- ]);
- return $expireTime;
- }
- }
|