request->input('limit', 10); $list = $this->adminUserModel->where('is_del',UserCode::ENABLE)->paginate($limit,$this->filed)->toArray(); $roleIds = array_column($list['data'], 'role_id'); $roleList = $this->adminRoleModel->getDataByIds($roleIds); foreach ($list['data'] as &$item) { $item['role_name'] = $roleList[$item['role_id']]['name'] ?? '普通管理员'; } return $this->return->success('success', ['list' => $list]); } /** * 添加 * @return array * @throws Exception */ public function add(): array { $name = $this->request->input('chinese_name'); $account = $this->request->input('account'); $oldAccount = $this->adminUserModel->getAdminInfoByAccount($account); $oldName = $this->adminUserModel->getAdminInfoByName($name); if (!empty($oldName) && !empty($oldAccount)) throw new ErrException('账号或者员工已存在'); $salt = StringUtil::randStr(6); $defaultPassword = config('system.admin_default_password'); $model = new AdminUser(); $model->username = $account; $model->mobile = $account; $model->chinese_name = $name; $model->salt = $salt; $model->password = $this->cryptoFactory->cryptoClass('admin-password',$defaultPassword,$salt)->encrypt(); $model->status = $this->request->input('status', 1); $model->avatar = $this->request->input('avatar',0); $model->role_id = $this->request->input('role_id', 0); $model->city_id = $this->request->input('city_id', 0); // $model->section_id = $this->request->input('section_id', 0); if (!$model->save()) throw new ErrException('账号添加失败'); //写入关联表 $res = match ($model->role_id) { RoleCode::DRIVER => $this->addDriver($model->id,$model->chinese_name), RoleCode::CHEF => $this->addChef($model->id), RoleCode::WAREHOUSE => $this->addWarehouseKeeper($model->id), default => true, }; if (!$res) throw new ErrException('关联表添加失败'); return $this->return->success(); } /** * 修改 * @return array */ public function edit(): array { $id = (int)$this->request->input('id'); $name = $this->request->input('chinese_name'); $account = $this->request->input('account'); $roleId = (int)$this->request->input('role_id', 0); $info = $this->adminUserModel->getAdminInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $oldAccount = $this->adminUserModel->getAdminInfoByAccount($account); $oldName = $this->adminUserModel->getAdminInfoByName($name); if (!empty($oldName) && $oldName->id != $info->id) throw new ErrException('员工已存在'); if (!empty($oldAccount) && $oldAccount->id != $info->id) throw new ErrException('账号已存在'); if ($info->role_id != $roleId) { //写入关联表 $del = match ($info->role_id) { RoleCode::DRIVER => $this->delDriver($info->id), RoleCode::CHEF => $this->delChef($info->id), RoleCode::WAREHOUSE => $this->delWarehouseKeeper($info->id), default => true, }; $add = match ($roleId) { RoleCode::DRIVER => $this->addDriver($info->id,$info->chinese_name), RoleCode::CHEF => $this->addChef($info->id), RoleCode::WAREHOUSE => $this->addWarehouseKeeper($info->id), default => true, }; if (!$del || !$add) throw new ErrException('关联表修改失败'); } $info->username = $account; $info->mobile = $account; $info->chinese_name = $name; $info->status = $this->request->input('status', 1); $info->avatar = $this->request->input('avatar',0); $info->role_id = $roleId; $info->city_id = $this->request->input('city_id', 0); // $info->section_id = $this->request->input('section_id', 0); if (!$info->save()) throw new ErrException('账号修改失败'); return $this->return->success(); } /** * 删除 * @return array */ public function delete(): array { $id = (int)$this->request->input('id'); if ($this->adminId == $id) throw new ErrException('不可删除自己'); $info = $this->adminUserModel->getAdminInfoById($id); if (empty($info)) throw new ErrException('员工不存在'); $info->is_del = UserCode::IS_DEL; $del = match ($info->role_id) { RoleCode::DRIVER => $this->delDriver($info->id), RoleCode::CHEF => $this->delChef($info->id), RoleCode::WAREHOUSE => $this->delWarehouseKeeper($info->id), default => true, }; if (!$info->save() || !$del) throw new ErrException('账号删除失败'); return $this->return->success(); } /** * 详情 * @return array */ public function get(): array { $id = (int)$this->request->input('id'); $info = $this->adminUserModel->where('id', $id)->where('is_del',UserCode::ENABLE)->first($this->filed); if (empty($info)) throw new ErrException('员工不存在'); $roleName = $this->adminRoleModel->where('id', $info->role_id)->value('name'); $res = $info->toArray(); $res['role_name'] = $roleName ?? '普通管理员'; return $this->return->success('success', ['info' => $res]); } /** * 重置密码 * @return array * @throws Exception */ public function resetPassword(): array { $id = (int)$this->request->input('id'); $info = $this->adminUserModel->getAdminInfoById($id); if (empty($info)) throw new ErrException('员工不存在'); $salt = StringUtil::randStr(6); $defaultPassword = config('system.admin_default_password'); $info->salt = $salt; $info->password = $this->cryptoFactory->cryptoClass('admin-password',$defaultPassword,$salt)->encrypt(); if (!$info->save()) throw new ErrException('密码重置失败'); return $this->return->success(); } }