ApiAdmin/app/controller/admin/AppGroup.php
2020-10-12 23:25:27 +08:00

141 lines
3.9 KiB
PHP

<?php
declare (strict_types=1);
/**
*
* @since 2018-02-11
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
namespace app\controller\admin;
use app\model\AdminApp;
use app\model\AdminAppGroup;
use app\util\ReturnCode;
use think\Response;
class AppGroup extends Base {
/**
* 获取应用组列表
* @return \think\Response
* @throws \think\db\exception\DbException
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function index(): Response {
$limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
$start = $this->request->get('page', 1);
$keywords = $this->request->get('keywords', '');
$type = $this->request->get('type', '');
$status = $this->request->get('status', '');
$obj = new AdminAppGroup();
if (strlen($status)) {
$obj = $obj->where('status', $status);
}
if ($type) {
switch ($type) {
case 1:
if (strlen($keywords)) {
$obj = $obj->where('hash', $keywords);
}
break;
case 2:
$obj = $obj->whereLike('name', "%{$keywords}%");
break;
}
}
$listObj = $obj->paginate(['page' => $start, 'list_rows' => $limit])->toArray();
return $this->buildSuccess([
'list' => $listObj['data'],
'count' => $listObj['total']
]);
}
/**
* 获取全部有效的应用组
* @return Response
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function getAll(): Response {
$listInfo = (new AdminAppGroup())->where(['status' => 1])->select();
return $this->buildSuccess([
'list' => $listInfo
]);
}
/**
* 应用组状态编辑
* @return Response
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function changeStatus(): Response {
$id = $this->request->get('id');
$status = $this->request->get('status');
$res = AdminAppGroup::update([
'id' => $id,
'status' => $status
]);
if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
}
return $this->buildSuccess();
}
/**
* 添加应用组
* @return Response
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function add(): Response {
$postData = $this->request->post();
$res = AdminAppGroup::create($postData);
if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
}
return $this->buildSuccess();
}
/**
* 应用组编辑
* @return Response
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function edit(): Response {
$postData = $this->request->post();
$res = AdminAppGroup::update($postData);
if ($res === false) {
return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
}
return $this->buildSuccess();
}
/**
* 应用组删除
* @return Response
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
public function del(): Response {
$hash = $this->request->get('hash');
if (!$hash) {
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
}
$has = (new AdminApp())->where(['app_group' => $hash])->count();
if ($has) {
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '当前分组存在' . $has . '个应用,禁止删除');
}
AdminAppGroup::destroy(['hash' => $hash]);
return $this->buildSuccess();
}
}