超管后台 - 编辑项目
This commit is contained in:
@@ -51,12 +51,12 @@ class UpdateAdministratorController extends BaseController
|
||||
'account' => 'require|/\S+/',
|
||||
'name' => 'require|/\S+/',
|
||||
'password' => '/\S+/',
|
||||
'permissionIds' => 'require|array',
|
||||
'permissionIds' => 'array',
|
||||
], [
|
||||
'id.require' => '缺少必要参数',
|
||||
'account.require' => '账号不能为空',
|
||||
'name.require' => '姓名不能为空',
|
||||
'permissionIds.require' => '请至少分配一种权限',
|
||||
'permissionIds.array' => '请至少分配一种权限',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
@@ -70,9 +70,10 @@ class UpdateAdministratorController extends BaseController
|
||||
* 判断是否有权限修改
|
||||
*
|
||||
* @param int $adminId
|
||||
* @param array $params
|
||||
* @return $this
|
||||
*/
|
||||
protected function checkPermission(int $adminId): self
|
||||
protected function checkPermission(int $adminId, array $params): self
|
||||
{
|
||||
$currentAdminId = $this->getAdminInfo('id');
|
||||
|
||||
@@ -80,6 +81,10 @@ class UpdateAdministratorController extends BaseController
|
||||
throw new \Exception('您没有权限修改其他管理员', 403);
|
||||
}
|
||||
|
||||
if ($params['id'] != 1 && empty($params['permissionIds'])) {
|
||||
throw new \Exception('请至少分配一种权限', 403);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -123,7 +128,7 @@ class UpdateAdministratorController extends BaseController
|
||||
// 被修改的管理员id
|
||||
$adminId = $params['id'] ?? 0;
|
||||
|
||||
$this->dataValidate($params)->checkPermission($adminId);
|
||||
$this->dataValidate($params)->checkPermission($adminId, $params);
|
||||
|
||||
Db::startTrans();
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ class CreateCompanyController extends BaseController
|
||||
*/
|
||||
protected function ckbCreateCompany(array $params): void
|
||||
{
|
||||
$params = ArrHelper::getValue('companyId,name,memo,status', $params);
|
||||
$params = ArrHelper::getValue('companyId=id,companyId,name,memo,status', $params);
|
||||
$result = CompanyModel::create($params);
|
||||
|
||||
if (!$result) {
|
||||
|
||||
@@ -29,7 +29,7 @@ class GetCompanyDetailForUpdateController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下古墓详情
|
||||
* 获取项目详情
|
||||
*
|
||||
* @param int $id
|
||||
* @return CompanyModel
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\company;
|
||||
|
||||
use app\common\model\Company as CompanyModel;
|
||||
use app\common\model\User as UsersModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use Eison\Utils\Helper\ArrHelper;
|
||||
use think\Db;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 公司控制器
|
||||
*/
|
||||
class UpdateCompanyController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 通过id获取项目详情
|
||||
*
|
||||
* @return CompanyModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getCompanyDetailById(): CompanyModel
|
||||
{
|
||||
$company = CompanyModel::find(
|
||||
$this->request->post('id/d', 0)
|
||||
);
|
||||
|
||||
if (!$company) {
|
||||
throw new \Exception('项目不存在', 404);
|
||||
}
|
||||
|
||||
// 外部使用
|
||||
$this->companyId = $company->id;
|
||||
|
||||
return $company;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过账号获取用户信息
|
||||
*
|
||||
* @return UsersModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getUserDetailByCompanyId(): ?UsersModel
|
||||
{
|
||||
$user = UsersModel::where(['companyId' => $this->companyId])->find();
|
||||
|
||||
if (!$user) {
|
||||
throw new \Exception('用户不存在', 404);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新项目信息
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function updateCompany(array $params): void
|
||||
{
|
||||
$params = ArrHelper::getValue('name,status,memo', $params);
|
||||
$params = ArrHelper::rmValue($params);
|
||||
|
||||
$company = $this->getCompanyDetailById();
|
||||
if (!$company->save($params)) {
|
||||
throw new \Exception('项目更新失败', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新账号信息
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
protected function updateUserAccount(array $params): void
|
||||
{
|
||||
$params = ArrHelper::getValue('username,account,password=passwordLocal,realName,status', $params);
|
||||
$params = ArrHelper::rmValue($params);
|
||||
|
||||
if (isset($params['passwordLocal'])) {
|
||||
$params['passwordMd5'] = md5($params['passwordLocal']);
|
||||
}
|
||||
|
||||
$user = $this->getUserDetailByCompanyId();
|
||||
if (!$user->save($params)) {
|
||||
throw new \Exception('用户账号更新失败', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return self
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function updateCkbAbout(array $params): self
|
||||
{
|
||||
// 1. 更新项目信息
|
||||
$this->updateCompany($params);
|
||||
|
||||
// 2. 更新账号信息
|
||||
$this->updateUserAccount($params);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return self
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function updateS2About(array $params): self
|
||||
{
|
||||
// 1. 更新项目信息
|
||||
$this->updateCompany($params);
|
||||
|
||||
// 2. 更新账号信息
|
||||
$this->updateUserAccount($params);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查项目名称是否已存在(排除自身)
|
||||
*
|
||||
* @param array $where
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function checkCompanyNameAndAccountExists(array $where): void
|
||||
{
|
||||
extract($where);
|
||||
|
||||
// 项目名称尽量不重名
|
||||
$exists = CompanyModel::where(compact('name'))->where('id', '<>', $id)->count() > 0;
|
||||
if ($exists) {
|
||||
throw new \Exception('项目名称已存在', 403);
|
||||
}
|
||||
|
||||
// 账号尽量不重名
|
||||
// TODO(数据迁移时,存客宝,主账号先查询出id,通过id查询出S2的最新信息,然后更新。)
|
||||
$exists = UsersModel::where(compact('account'))->where('companyId', '<>', $id)->count() > 0;
|
||||
if ($exists) {
|
||||
throw new \Exception('用户账号已存在', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据验证
|
||||
*
|
||||
* @param array $params
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function dataValidate(array $params): self
|
||||
{
|
||||
$validate = Validate::make([
|
||||
'id' => 'require',
|
||||
'name' => 'require|max:50|/\S+/',
|
||||
'username' => 'require|max:20|/\S+/',
|
||||
'account' => 'require|regex:/^1[3-9]\d{9}$/',
|
||||
'status' => 'require|in:0,1',
|
||||
'realName' => 'require|/\S+/',
|
||||
], [
|
||||
'id.require' => '非法请求',
|
||||
'name.require' => '请输入项目名称',
|
||||
'username.require' => '请输入用户昵称',
|
||||
'account.require' => '请输入账号',
|
||||
'account.regex' => '账号为手机号',
|
||||
'status.require' => '缺少重要参数',
|
||||
'status.in' => '非法参数',
|
||||
'realName.require' => '请输入真实姓名',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
throw new \Exception($validate->getError(), 400);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新项目信息
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$params = $this->request->only(['id', 'name', 'status', 'username', 'account', 'password', 'realName', 'memo']);
|
||||
|
||||
// 数据验证
|
||||
$this->dataValidate($params);
|
||||
$this->checkCompanyNameAndAccountExists(ArrHelper::getValue('id,name,account', $params));
|
||||
|
||||
Db::startTrans();
|
||||
$this->updateCkbAbout($params)->updateS2About($params);
|
||||
Db::commit();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '更新成功'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
|
||||
return json([
|
||||
'code' => $e->getCode(),
|
||||
'msg' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user