Agent.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace app\admin\controller\commission;
  3. use app\common\controller\Backend;
  4. use app\common\model\commission\Agent as AgentModel;
  5. use app\common\model\User as UserModel;
  6. use app\common\model\commission\Log as LogModel;
  7. use app\common\model\commission\Level as LevelModel;
  8. use app\common\Service\Commission\Agent as AgentService;
  9. use EasyWeChat\OfficialAccount\Server\Handlers\EchoStrHandler;
  10. use think\Db;
  11. class Agent extends Backend
  12. {
  13. protected $noNeedRight = ['select'];
  14. protected $model = null;
  15. /**
  16. * 快速搜索时执行查找的字段
  17. */
  18. protected $searchFields = 'user_id,user.nickname,user.mobile';
  19. /**
  20. * 是否是关联查询
  21. */
  22. protected $relationSearch = true;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new AgentModel();
  27. }
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. if (!$this->request->isAjax()) {
  34. return $this->view->fetch();
  35. }
  36. //如果发送的来源是 Selectpage,则转发到 Selectpage
  37. if ($this->request->request('keyField')) {
  38. return $this->selectpage();
  39. }
  40. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  41. // 处理特殊筛选条件
  42. $filter = $this->request->get("filter", '');
  43. if ($filter) {
  44. $filter = json_decode($filter, true);
  45. if (isset($filter['tabActive'])) {
  46. if ($filter['tabActive'] === 'pending') {
  47. $where['status'] = 'pending';
  48. } elseif ($filter['tabActive'] === '0') {
  49. $where['level_status'] = ['>', 0];
  50. }
  51. }
  52. }
  53. $list = $this->model
  54. ->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->paginate($limit);
  58. $result = ['total' => $list->total(), 'rows' => $list->items()];
  59. return json($result);
  60. }
  61. /**
  62. * 详情
  63. *
  64. * @param $id
  65. */
  66. public function detail($ids = null)
  67. {
  68. $row = $this->model->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])
  69. ->where('user_id', $ids)
  70. ->find();
  71. if (!$row) {
  72. $this->error(__('No Results were found'));
  73. }
  74. $this->view->assign('row', $row);
  75. return $this->view->fetch();
  76. }
  77. /**
  78. * 团队
  79. *
  80. * @param $id
  81. */
  82. public function team($ids = null)
  83. {
  84. // 获取ID参数,优先使用URL中的id参数
  85. $ids = $ids ?: $this->request->param('id');
  86. // 将当前分销商ID传递给前端
  87. $this->assignconfig('current_agent_id', $ids);
  88. if (!$this->request->isAjax()) {
  89. // 获取当前分销商详细信息用于模板渲染
  90. $currentAgent = $this->model->with(['user.parent_user', 'level_info'])
  91. ->where('user_id', $ids)
  92. ->find();
  93. if (!$currentAgent) {
  94. $this->error(__('No Results were found'));
  95. }
  96. $this->view->assign('currentAgent', $currentAgent);
  97. return $this->view->fetch();
  98. }
  99. //如果发送的来源是 Selectpage,则转发到 Selectpage
  100. if ($this->request->request('keyField')) {
  101. return $this->selectpage();
  102. }
  103. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  104. // 以分销商表为主表查询团队成员
  105. // 安全的排序字段处理
  106. // $allowedSorts = ['user_id', 'status', 'level', 'total_income', 'pending_reward', 'become_time'];
  107. // $safeSort = in_array($sort, $allowedSorts) ? $sort : 'user_id';
  108. $list = $this->model
  109. ->hasWhere('user', function ($query) use ($ids) {
  110. return $query->where('parent_user_id', $ids);
  111. })
  112. ->with(['user', 'level_info'])
  113. // ->order($safeSort, $order ?: 'desc')
  114. ->paginate($limit);
  115. $result = array("total" => $list->total(), "rows" => $list->items());
  116. return json($result);
  117. }
  118. // 选择分销商
  119. public function select()
  120. {
  121. if (!$this->request->isAjax()) {
  122. return $this->view->fetch();
  123. }
  124. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  125. $data = $this->model
  126. ->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])
  127. ->where($where)
  128. ->order($sort, $order)
  129. ->paginate($limit);
  130. $result = ['total' => $data->total(), 'rows' => $data->items()];
  131. return json($result);
  132. }
  133. /**
  134. * 编辑
  135. *
  136. * @param $id
  137. */
  138. public function edit($id = null)
  139. {
  140. $params = $this->request->only(['status', 'upgrade_lock', 'level_status', 'level']);
  141. $result = Db::transaction(function () use ($id, $params) {
  142. $row = $this->model->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])->where('user_id', $id)->find();
  143. if (!$row) {
  144. $this->error('未找到该分销商');
  145. }
  146. foreach ($params as $field => $value) {
  147. switch ($field) {
  148. case 'status': // 修改状态
  149. return $this->changeStatus($row, $value);
  150. break;
  151. case 'level_status': // 审核等级
  152. return $this->changeLevelStatus($row, $value);
  153. break;
  154. case 'level': // 修改等级
  155. return $this->changeLevel($row, $value);
  156. break;
  157. default:
  158. return $row->save([$field => $value]);
  159. }
  160. }
  161. });
  162. if ($result) {
  163. $this->success('更新成功', null, $result);
  164. } else {
  165. $this->error('更新失败');
  166. }
  167. }
  168. // 修改状态
  169. private function changeStatus($row, $value)
  170. {
  171. $result = $row->save(['status' => $value]);
  172. if ($result) {
  173. LogModel::add($row->user_id, 'agent', ['type' => 'status', 'value' => $value]);
  174. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  175. }
  176. return $result;
  177. }
  178. // 审核等级
  179. private function changeLevelStatus($row, $value)
  180. {
  181. if ($row->level_status == 0 && $value > 0) {
  182. $this->error('非法操作');
  183. }
  184. if ($value == 0) { // 拒绝操作
  185. return $row->save(['level_status' => 0]);
  186. } else { // 同意操作
  187. if ($row->upgrade_level) {
  188. $result = $row->save(['level_status' => 0, 'level' => $row->upgrade_level->level]);
  189. if ($result) {
  190. LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $row->upgrade_level]);
  191. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  192. }
  193. return $result;
  194. }
  195. }
  196. return false;
  197. }
  198. // 修改等级
  199. private function changeLevel($row, $value)
  200. {
  201. $level = LevelModel::find($value);
  202. if ($level) {
  203. $result = $row->save(['level' => $level->level]);
  204. if ($result) {
  205. LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $level]);
  206. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  207. }
  208. return $result;
  209. } else {
  210. $this->error('未找到该等级');
  211. }
  212. }
  213. // 更换推荐人
  214. public function changeParentUser($id)
  215. {
  216. $userAgent = new AgentService($id);
  217. if (!$userAgent->user) {
  218. $this->error('未找到该用户');
  219. }
  220. $parentUserId = $this->request->param('parent_user_id', 0);
  221. // 更换推荐人检查
  222. if ($parentUserId != 0) {
  223. $parentAgent = new AgentService($parentUserId);
  224. if (!$parentAgent->isAgentAvaliable()) {
  225. $this->error('选中用户暂未成为分销商,不能成为推荐人');
  226. }
  227. if (!$this->checkChangeParentAgent($id, $parentUserId)) {
  228. $this->error('不能绑定该上级');
  229. }
  230. LogModel::add($parentUserId, 'share', ['user' => $userAgent->user]);
  231. if ($userAgent->isAgentAvaliable()) {
  232. LogModel::add($id, 'bind', ['user' => $parentAgent->user ?? NULL]);
  233. }
  234. }
  235. $lastParentUserId = $userAgent->user->parent_user_id;
  236. $userAgent->user->parent_user_id = $parentUserId;
  237. $userAgent->user->save();
  238. if ($lastParentUserId > 0) {
  239. $userAgent->createAsyncAgentUpgrade($lastParentUserId);
  240. }
  241. if ($parentUserId > 0) {
  242. $userAgent->createAsyncAgentUpgrade($parentUserId);
  243. }
  244. $this->success('绑定成功');
  245. }
  246. // 递归往上找推荐人,防止出现推荐循环
  247. private function checkChangeParentAgent($userId, $parentUserId)
  248. {
  249. if ($userId == $parentUserId) {
  250. $this->error('推荐人不能是本人');
  251. }
  252. if ($parentUserId == 0) {
  253. return true;
  254. }
  255. $parentAgent = UserModel::find($parentUserId);
  256. if ($parentAgent) {
  257. if ($parentAgent->parent_user_id == $userId) {
  258. $this->error("已选中分销商的上级团队中已存在该用户");
  259. }
  260. if ($parentAgent->parent_user_id == 0) {
  261. return true;
  262. } else {
  263. return $this->checkChangeParentAgent($userId, $parentAgent->parent_user_id);
  264. }
  265. }
  266. return false;
  267. }
  268. }