198 lines
5.8 KiB
PHP
198 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace app\controller\manager;
|
|
|
|
use app\model\Activity as ActivityModel;
|
|
use app\model\ActivityTemplate as ActivityTemplateModel;
|
|
use app\model\ActivityTemplateType as ActivityTemplateTypeModel;
|
|
use app\validate\Activity as VActivity;
|
|
use app\model\System;
|
|
use Exception;
|
|
use think\db\exception\DataNotFoundException;
|
|
use think\db\exception\DbException;
|
|
use think\db\exception\ModelNotFoundException;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Config as CConfig;
|
|
use think\response\Json;
|
|
use think\response\View;
|
|
|
|
/**
|
|
* 抽奖模板分类
|
|
*
|
|
* Class ActivityTemplateType
|
|
* @package app\controller\manager
|
|
*/
|
|
class ActivityTemplate extends Base
|
|
{
|
|
|
|
protected function initialize()
|
|
{
|
|
parent::initialize();
|
|
CConfig::load('extra/upload', 'system_upload');
|
|
if(empty(config('system_upload'))){
|
|
$this->data['uploadConfig'] = System::findById(1);
|
|
}else{
|
|
$this->data['uploadConfig'] = config('system_upload');
|
|
}
|
|
}
|
|
/**
|
|
* 删除
|
|
*
|
|
* @return Json
|
|
*/
|
|
public function del(): Json
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$ids = input('post.ids/a', []);
|
|
if (empty($ids)) {
|
|
$ids[] = input('post.id/d');
|
|
}
|
|
ActivityTemplateModel::deleteByIds($ids);
|
|
return $this->json();
|
|
}
|
|
return $this->json(4001, '非法请求!');
|
|
}
|
|
|
|
/**
|
|
* 编辑
|
|
*
|
|
* @return Json|View
|
|
* @throws DataNotFoundException
|
|
* @throws DbException
|
|
* @throws ModelNotFoundException
|
|
* @throws Exception
|
|
*/
|
|
public function edit()
|
|
{
|
|
$id = input('id/d', 0);
|
|
|
|
if (!$info = ActivityTemplateModel::findById($id)) {
|
|
return $this->json(4001, '记录不存在');
|
|
}
|
|
|
|
if ($this->request->isPost()) {
|
|
$item = input('post.');
|
|
try {
|
|
validate(VActivity::class)->check($item);
|
|
} catch (ValidateException $e) {
|
|
// 验证失败 输出错误信息
|
|
return $this->json(4001,$e->getError());
|
|
}
|
|
|
|
try {
|
|
$info->save($item);
|
|
return $this->json();
|
|
} catch (ValidateException $e) {
|
|
return $this->json(4001, $e->getError());
|
|
}
|
|
}
|
|
|
|
$this->data['item'] = $info;
|
|
$this->data['class'] = ActivityTemplateTypeModel::findList([],[],1,0,null,["sort"=>"desc","id"=>"asc"])['list'];
|
|
$this->data['lotteryMethod'] = ActivityModel::AllLotteryMethod();
|
|
$this->data['type'] = ActivityModel::AllType();
|
|
$this->data['redackType'] = ActivityModel::AllRedackType();
|
|
$winnerKey = array_keys($info['winner']);
|
|
$this->data['maxKey'] = max($winnerKey)+1;
|
|
return $this->view();
|
|
}
|
|
|
|
/**
|
|
* 单个字段编辑
|
|
*
|
|
* @return Json
|
|
* @throws DataNotFoundException
|
|
* @throws DbException
|
|
* @throws ModelNotFoundException
|
|
* @throws Exception
|
|
*/
|
|
public function modify(): Json
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$item = input('post.');
|
|
$validate = $this->validateByApi($item, [
|
|
'field' => 'require',
|
|
'value' => 'require',
|
|
]);
|
|
|
|
if ($validate !== true) {
|
|
return $validate;
|
|
}
|
|
|
|
if (!$info = ActivityTemplateModel::findById($item['id'])) {
|
|
return $this->json(4001, '记录不存在');
|
|
}
|
|
|
|
$update = [$item['field'] => $item['value']];
|
|
|
|
try {
|
|
$info->save($update);
|
|
return $this->json();
|
|
} catch (ValidateException $e) {
|
|
return $this->json(4001, $e->getError());
|
|
}
|
|
}
|
|
return $this->json(4000, '非法请求');
|
|
}
|
|
|
|
/**
|
|
* 添加
|
|
*
|
|
* @return Json|View
|
|
* @throws Exception
|
|
*/
|
|
public function add()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$item = input('post.');
|
|
try {
|
|
validate(VActivity::class)->check($item);
|
|
} catch (ValidateException $e) {
|
|
// 验证失败 输出错误信息
|
|
return $this->json(4001,$e->getError());
|
|
}
|
|
try {
|
|
$item['create_time'] = date('Y-m-d H:i:s');
|
|
ActivityTemplateModel::create($item);
|
|
return $this->json(0, "success");
|
|
} catch (ValidateException $e) {
|
|
return $this->json(4001, $e->getError());
|
|
}
|
|
}
|
|
|
|
$this->data['class'] = ActivityTemplateTypeModel::findList([],[],1,0,null,["sort"=>"desc","id"=>"asc"])['list'];
|
|
$this->data['lotteryMethod'] = ActivityModel::AllLotteryMethod();
|
|
$this->data['type'] = ActivityModel::AllType();
|
|
$this->data['redackType'] = ActivityModel::AllRedackType();
|
|
|
|
return $this->view();
|
|
}
|
|
|
|
/**
|
|
* 列表
|
|
*
|
|
* @return View|Json
|
|
* @throws Exception
|
|
*/
|
|
public function index()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$page = input('page/d', 1);
|
|
$limit = input('size/d', 20);
|
|
$items = ActivityTemplateModel::findList([], [], $page, $limit, function ($q) {
|
|
|
|
return $q->with(["adminType"])
|
|
->withAttr("lottery_method", function ($value) {
|
|
return ActivityModel::AllLotteryMethod()[$value] ?? "";
|
|
})
|
|
->withAttr("lottery_method", function ($value) {
|
|
return ActivityModel::AllLotteryMethod()[$value] ?? "";
|
|
})
|
|
->order('id', 'desc');
|
|
});
|
|
|
|
return $this->json(0, '操作成功', $items);
|
|
}
|
|
return $this->view();
|
|
}
|
|
} |