* @Date: 2026-04-29 02:32:33 * @LastEditors: YwxApp * @LastEditTime: 2026-07-21 23:24:13 * @Description: * @FilePath: \ywxapp_dev\app\backend\controller\User.php * @CustomString: Copyright (c) 2026 YwxApp */ declare(strict_types=1); namespace app\backend\controller; use think\facade\Db; use think\facade\View; use think\Request; use ywxapp\controller\BackendBase; use ywxapp\model\MemberUser as UserModel; use ywxapp\model\MemberGroup as GroupModel; use app\backend\validate\User as UserValidate; use think\exception\ValidateException; use think\db\exception\DbException; /** * Member 类 * * @author ywxapp */ class User extends BackendBase { /** * Summary of needLogin * @var array */ protected $noNeedLogin = []; /** * Summary of needRight * @var array */ protected $noNeedVerify = []; /** * 控制器初始化 initialize * @return void */ protected function initialize() {} /** * 获取用户列表 */ public function index() { $page = $this->request->param('page/d', 1); $limit = $this->request->param('limit/d', 15); if ($this->request->isAjax()) { $admins = UserModel::with(['groups'])->page($page, $limit)->select(); $this->result->success($admins); } View::assign('title', '登录'); return View::fetch(); } /** * 数据创建 * * @param Request $request * @return \think\Response */ public function create() { if ($this->request->isAjax()) { $groups = GroupModel::where('status', 1)->select(); $this->result->success(['groups' => $groups]); } View::assign('title', '登录'); return View::fetch('user/index'); } /** * 数据保存 * * @param Request $request * @return \think\Response */ public function save() { $params = $this->request->only(['account', 'nickname', 'password', 'confirmpass', 'mobile', 'email', 'role_ids', 'status'], 'post'); $groupIds = $this->request->param('group_ids/a', []); try { validate(UserValidate::class)->check($params); Db::transaction(function () use ($params, $groupIds) { $data = UserModel::create($params); $data->groups()->saveAll($groupIds); }); $this->result->success(); } catch (ValidateException $e) { $this->result->error($e->getMessage()); } catch (DbException $e) { $this->result->error($e->getMessage(), 2); } } /** * 数据编辑 * * @param Request $request * @param int|null $ids * @return \think\Response */ public function edit( $id = null) { if ($this->request->isAjax()) { $data = UserModel::with(['groups']) ->where('id', $id) ->find(); $groups = GroupModel::where('status', 1)->select(); $this->result->success(['info' => $data, 'groups' => $groups]); } View::assign('title', '登录'); return View::fetch('user/index'); } /** * 数据更新 * * @param Request $request * @param int|null $ids * @return \think\Response */ public function update() { $data = $this->request->only(['id', 'account', 'nickname', 'password', 'confirmpass', 'mobile', 'email', 'status','group_ids'], 'put'); $groupIds = $this->request->put('group_ids/a', []); $info = UserModel::find($data['id']); if (! $info) { $this->result->error('用户不存在',404); } $info->save($data); $info->groups()->sync($groupIds); $this->result->success($info,"用户更新成功"); } /** * 获取详情 */ public function read() { $id = $this->request->param('id/d', 0); $info = UserModel::with(['groups', 'rules']) ->find($id); if (! $info) { $this->result->error('用户不存在',404); } $this->result->success($info,"用户更新成功"); } /** * 数据回收站 * * @param Request $request * @return \think\Response */ public function recyclebin() { $page = $this->request->param('page', 1); $size = $this->request->param('size', 15); if ($this->request->isAjax()) { $admins = UserModel::onlyTrashed()->with(['groups']) ->paginate([ 'page' => $page, 'list_rows' => $size, ]); $this->result->success($admins->items()); } View::assign('title', '回收站'); return View::fetch('user/index'); } /** * 数据删除 */ public function delete() { if ($this->request->isAjax() && $this->request->isDelete()) { $ids = $this->request->param('ids', ''); $force = $this->request->param('force', false); if (empty($ids)) { $this->result->error('请选择要删除的数据'); } $idsArray = explode(',', $ids); try { Db::transaction(function () use ($idsArray, $force) { if ($force) { UserModel::onlyTrashed()->whereIn('id', $idsArray)->select()->each(function ($item) { $item->roles()->detach(); $item->force()->delete(); }); } else { UserModel::destroy($idsArray); } }); $this->result->success(); } catch (\think\exception\HttpResponseException $e) { throw $e; // 不要 return,不要吞掉! } catch (\Exception $e) { \think\facade\Log::error('批量删除管理员失败', [ 'exception' => $e->__toString(), 'admin_ids' => $idsArray, ]); $this->result->error('删除失败: ' . ($e->getMessage() ?: $e->__toString()), 500); } } } public function restore( $ids = '') { if ($this->request->isAjax() && $this->request->isPost()) { $ids = $this->request->param('ids', ''); if (empty($ids)) { $this->result->error('请选择要恢复的数据'); } $idsArray = explode(',', $ids); Db::startTrans(); try { UserModel::withTrashed() ->where('id', 'in', $idsArray) ->select() ->each(function ($item) { $item->restore(); }); Db::commit(); $this->result->success('恢复成功'); } catch (DbException $e) { Db::rollback(); $this->result->error('恢复失败: ' . $e->getMessage()); } } } }