Browse Source
feat(admin): 新增用户套餐管理功能
feat(admin): 新增用户套餐管理功能
- 添加用户套餐控制器及路由配置 - 实现用户当前套餐查询接口 - 实现管理员分配套餐接口 - 增加用户套餐模型时间格式化方法 - 创建套餐服务层处理业务逻辑 - 添加套餐参数验证规则master
5 changed files with 150 additions and 1 deletions
-
32app/controller/admin/UserPackageController.php
-
4app/model/user/UserPackage.php
-
13app/route/admin.php
-
75app/service/admin/UserPackageService.php
-
27app/validate/UserPackageValidate.php
@ -0,0 +1,32 @@ |
|||
<?php |
|||
namespace app\controller\admin; |
|||
|
|||
use app\service\admin\UserPackageService; |
|||
use app\validate\UserPackageValidate; |
|||
use plugin\piadmin\app\utils\ArrayUtils; |
|||
use support\Response; |
|||
|
|||
class UserPackageController |
|||
{ |
|||
|
|||
public function read(UserPackageService $service): Response |
|||
{ |
|||
$id = input('id'); |
|||
return success($service->readData($id)); |
|||
} |
|||
|
|||
public function give(UserPackageService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'uid' => '', |
|||
'package_id' => '', |
|||
'package_name' => '', |
|||
'package_level' => '', |
|||
'point' => '', |
|||
'end_time' => '', |
|||
]); |
|||
validate(UserPackageValidate::class)->check($params, 'update'); |
|||
return success($service->updateData(ArrayUtils::filterNotEmpty($params))); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,75 @@ |
|||
<?php |
|||
|
|||
namespace app\service\admin; |
|||
|
|||
use app\dao\user\UserPackageDao; |
|||
use app\dao\user\UserPointLogDao; |
|||
use plugin\piadmin\app\base\BaseService; |
|||
use plugin\piadmin\app\exception\ApiException; |
|||
use support\think\Db; |
|||
|
|||
class UserPackageService extends BaseService |
|||
{ |
|||
|
|||
protected $dao; |
|||
protected $userPointLogDao; |
|||
|
|||
public function __construct(UserPackageDao $dao) |
|||
{ |
|||
$this->dao = $dao; |
|||
$this->userPointLogDao = app()->make(UserPointLogDao::class); |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 修改信息 |
|||
* @param array $params |
|||
* @return array |
|||
*/ |
|||
public function updateData(array $params): array |
|||
{ |
|||
// 落库
|
|||
Db::startTrans(); |
|||
try { |
|||
$params['end_time'] = strtotime($params['end_time']); |
|||
$this->dao->update(['uid' => $params['uid']], $params); |
|||
//记录日志
|
|||
$this->userPointLogDao->save([ |
|||
'uid' => $params['uid'], |
|||
'type' => 3, |
|||
'point' => $params['point'], |
|||
'desc' => '管理员调整权益点数', |
|||
'code' => 'userPackageGive', |
|||
'balance' => $params['point'] |
|||
]); |
|||
Db::commit(); |
|||
} catch (\Exception $exception) { |
|||
Db::rollback(); |
|||
throw new ApiException($exception->getMessage()); |
|||
} |
|||
return $params; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 获取信息 |
|||
* @param mixed $id |
|||
* @return array |
|||
*/ |
|||
public function readData(mixed $id): array |
|||
{ |
|||
$package = $this->dao->get(['uid' => $id]); |
|||
if (empty($package)) { |
|||
$package = $this->dao->save([ |
|||
'uid' => $id, |
|||
'package_id' => 0, |
|||
'package_name' => '', |
|||
'package_level' => '', |
|||
'point' => 0, |
|||
'end_time' => 0, |
|||
]); |
|||
} |
|||
return $package->toArray(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
<?php |
|||
|
|||
namespace app\validate; |
|||
|
|||
use plugin\piadmin\app\base\BaseValidate; |
|||
|
|||
class UserPackageValidate extends BaseValidate |
|||
{ |
|||
protected $group = [ |
|||
'update' => [ |
|||
'uid' => 'require', |
|||
'package_id' => 'require', |
|||
'package_name' => 'require', |
|||
'package_level' => 'require', |
|||
'point' => 'require', |
|||
'end_time' => 'require', |
|||
], |
|||
]; |
|||
protected $message = [ |
|||
'uid.require' => '用户ID不能为空', |
|||
'package_id.require' => '套餐ID不能为空', |
|||
'package_name.require' => '套餐名称不能为空', |
|||
'package_level.require' => '套餐等级不能为空', |
|||
'point.require' => '可得积分不能为空', |
|||
'end_time.require' => '权益到期时间不能为空', |
|||
]; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue