Browse Source
feat(admin): 新增套餐管理功能
feat(admin): 新增套餐管理功能
- 添加套餐模型、控制器、服务层和验证规则 - 实现套餐的增删改查及列表展示接口 - 配置权限控制和路由规则 - 提供下拉数据接口用于前端选择框 - 完善事务处理和异常捕获机制master
6 changed files with 282 additions and 1 deletions
-
67app/controller/admin/PackageController.php
-
15app/dao/admin/PackageDao.php
-
33app/model/admin/Package.php
-
13app/route/admin.php
-
119app/service/admin/PackageService.php
-
36app/validate/PackageValidate.php
@ -0,0 +1,67 @@ |
|||
<?php |
|||
namespace app\controller\admin; |
|||
|
|||
use app\service\admin\PackageService; |
|||
use app\validate\PackageValidate; |
|||
use plugin\piadmin\app\utils\ArrayUtils; |
|||
use support\Response; |
|||
|
|||
class PackageController |
|||
{ |
|||
public function save(PackageService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'type' => 1, |
|||
'name' => '', |
|||
'price' => '', |
|||
'level' => '', |
|||
'point' => 0, |
|||
'status' => 1 |
|||
]); |
|||
validate(PackageValidate::class)->check($params, 'save'); |
|||
return success($service->saveData($params)); |
|||
} |
|||
|
|||
public function update(PackageService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'id' => '', |
|||
'type' => 1, |
|||
'name' => '', |
|||
'price' => '', |
|||
'level' => '', |
|||
'point' => 0, |
|||
'status' => 1 |
|||
]); |
|||
validate(PackageValidate::class)->check($params, 'update'); |
|||
return success($service->updateData(ArrayUtils::filterNotEmpty($params))); |
|||
} |
|||
|
|||
public function index(PackageService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'name' => '', |
|||
'type' => '', |
|||
'begin_time' => '', |
|||
'end_time' => '' |
|||
]); |
|||
return success($service->listData($params)); |
|||
} |
|||
|
|||
public function read(PackageService $service): Response |
|||
{ |
|||
$id = input('id'); |
|||
return success($service->readData($id)); |
|||
} |
|||
|
|||
public function delete(PackageService $service): Response |
|||
{ |
|||
$ids = input('ids'); |
|||
return success($service->deleteData($ids)); |
|||
} |
|||
|
|||
public function pureIndex(PackageService $service) |
|||
{ |
|||
return success($service->selectData()); |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
<?php |
|||
|
|||
namespace app\dao\admin; |
|||
|
|||
use app\model\admin\Package; |
|||
use plugin\piadmin\app\base\BaseDao; |
|||
|
|||
class PackageDao extends BaseDao |
|||
{ |
|||
protected function setModel(): string |
|||
{ |
|||
return Package::class; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
<?php |
|||
|
|||
namespace app\model\admin; |
|||
|
|||
use plugin\piadmin\app\base\BaseModel; |
|||
|
|||
/** |
|||
* 套餐模型 |
|||
*/ |
|||
class Package extends BaseModel |
|||
{ |
|||
/** |
|||
* The table associated with the model. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $table = 'geo_package'; |
|||
|
|||
/** |
|||
* The primary key associated with the table. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $primaryKey = 'id'; |
|||
|
|||
/** |
|||
* Indicates if the model should be timestamped. |
|||
* |
|||
* @var bool |
|||
*/ |
|||
public $timestamps = true; |
|||
|
|||
} |
|||
@ -0,0 +1,119 @@ |
|||
<?php |
|||
|
|||
namespace app\service\admin; |
|||
|
|||
use app\dao\admin\PackageDao; |
|||
use plugin\piadmin\app\base\BaseService; |
|||
use plugin\piadmin\app\exception\ApiException; |
|||
use plugin\piadmin\app\utils\RequestUtils; |
|||
use support\think\Db; |
|||
|
|||
class PackageService extends BaseService |
|||
{ |
|||
|
|||
protected $dao; |
|||
|
|||
public function __construct(PackageDao $dao) |
|||
{ |
|||
$this->dao = $dao; |
|||
} |
|||
|
|||
/** |
|||
* 保存信息 |
|||
* @param array $params |
|||
* @return array |
|||
*/ |
|||
public function saveData(array $params): array |
|||
{ |
|||
Db::startTrans(); |
|||
try { |
|||
$data = $this->dao->save($params); |
|||
Db::commit(); |
|||
} catch (\Exception $exception) { |
|||
Db::rollback(); |
|||
throw new ApiException($exception->getMessage()); |
|||
} |
|||
return $data->toArray(); |
|||
} |
|||
|
|||
/** |
|||
* 修改信息 |
|||
* @param array $params |
|||
* @return array |
|||
*/ |
|||
public function updateData(array $params): array |
|||
{ |
|||
// 落库
|
|||
Db::startTrans(); |
|||
try { |
|||
$this->dao->update(['id' => $params['id']], $params); |
|||
Db::commit(); |
|||
} catch (\Exception $exception) { |
|||
Db::rollback(); |
|||
throw new ApiException($exception->getMessage()); |
|||
} |
|||
return $params; |
|||
} |
|||
|
|||
/** |
|||
* 获取列表 |
|||
* @param array $params |
|||
* @return array |
|||
*/ |
|||
public function listData(array $params): array |
|||
{ |
|||
[$page, $limit] = RequestUtils::getPageParameter(); |
|||
[$sortRule, $sortField] = RequestUtils::getSortParameter(); |
|||
$query = [ |
|||
'delete_time' => 0 |
|||
]; |
|||
if (isNotBlank($params['name'])) { |
|||
$query[] = ['name', 'like', '%' . $params['name'] . '%']; |
|||
} |
|||
$list = $this->dao->getList($query, '*', $page, $limit, "$sortField $sortRule"); |
|||
$count = $this->dao->getCount($query); |
|||
return compact('list', 'count'); |
|||
} |
|||
|
|||
/** |
|||
* 获取信息 |
|||
* @param mixed $id |
|||
* @return array |
|||
*/ |
|||
public function readData(mixed $id): array |
|||
{ |
|||
$package = $this->dao->get(['id' => $id]); |
|||
if (empty($package)) { |
|||
throw new ApiException('数据不存在'); |
|||
} |
|||
return $package->toArray(); |
|||
} |
|||
|
|||
/** |
|||
* 删除信息 |
|||
* @param mixed $id |
|||
* @return array |
|||
*/ |
|||
public function deleteData(array $ids): array |
|||
{ |
|||
// 落库
|
|||
Db::startTrans(); |
|||
try { |
|||
$this->dao->update([['id', 'in', $ids]], ['delete_time' => time()]); |
|||
Db::commit(); |
|||
} catch (\Exception $exception) { |
|||
Db::rollback(); |
|||
throw new ApiException($exception->getMessage()); |
|||
} |
|||
return ['id' => $ids]; |
|||
} |
|||
|
|||
public function selectData() |
|||
{ |
|||
$query = [ |
|||
'delete_time' => 0 |
|||
]; |
|||
$list = $this->dao->getList($query, '*', 0, 0, 'id DESC'); |
|||
return $list; |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
<?php |
|||
|
|||
namespace app\validate; |
|||
|
|||
use plugin\piadmin\app\base\BaseValidate; |
|||
|
|||
class PackageValidate extends BaseValidate |
|||
{ |
|||
protected $group = [ |
|||
'save' => [ |
|||
'name' => 'require', |
|||
'price' => 'require|number', |
|||
'level' => 'require|number', |
|||
'point' => 'require', |
|||
'status' => 'require' |
|||
], |
|||
'update' => [ |
|||
'id' => 'require', |
|||
'name' => 'require', |
|||
'price' => 'require|number', |
|||
'level' => 'require|number', |
|||
'point' => 'require', |
|||
'status' => 'require' |
|||
], |
|||
]; |
|||
protected $message = [ |
|||
'id.require' => 'ID不能为空', |
|||
'name.require' => '套餐名称不能为空', |
|||
'price.require' => '价格不能为空', |
|||
'price.number' => '价格只能为数字', |
|||
'level.require' => '等级不能为空', |
|||
'level.number' => '等级只能为数字', |
|||
'point' => '可得点数不能为空', |
|||
'status' => '状态不能为空' |
|||
]; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue