From 102aae8d08aee37a8eb707bed3862ced8b2fb783 Mon Sep 17 00:00:00 2001 From: wangxinglong <2371974647@qq.com> Date: Tue, 1 Mar 2022 16:19:41 +0800 Subject: [PATCH] setter --- app/controller/manager/ActivityTemplate.php | 58 ++-- app/model/Activity.php | 32 +++ app/model/ActivityTemplate.php | 35 +++ app/validate/Activity.php | 29 ++ public/static/manager/js/activity_template.js | 4 +- public/static/manager/js/add_activity.js | 53 +++- view/manager/activity_template/add.html | 101 ++++++- view/manager/activity_template/edit.html | 257 +++++++++++++++++- 8 files changed, 526 insertions(+), 43 deletions(-) create mode 100644 app/validate/Activity.php diff --git a/app/controller/manager/ActivityTemplate.php b/app/controller/manager/ActivityTemplate.php index 257b5c4..5e0f455 100644 --- a/app/controller/manager/ActivityTemplate.php +++ b/app/controller/manager/ActivityTemplate.php @@ -2,9 +2,10 @@ namespace app\controller\manager; +use app\model\Activity as ActivityModel; use app\model\ActivityTemplate as ActivityTemplateModel; - -use app\model\ArchivesModelField; +use app\model\ActivityTemplateType as ActivityTemplateTypeModel; +use app\validate\Activity as VActivity; use app\model\System; use Exception; use think\db\exception\DataNotFoundException; @@ -71,12 +72,11 @@ class ActivityTemplate extends Base if ($this->request->isPost()) { $item = input('post.'); - $validate = $this->validateByApi($item, [ - 'name' => 'require', - ]); - - if ($validate !== true) { - return $validate; + try { + validate(VActivity::class)->check($item); + } catch (ValidateException $e) { + // 验证失败 输出错误信息 + return $this->json(4001,$e->getError()); } try { @@ -88,7 +88,12 @@ class ActivityTemplate extends Base } $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(); } @@ -140,27 +145,26 @@ class ActivityTemplate extends Base { if ($this->request->isPost()) { $item = input('post.'); - - $validate = $this->validateByApi($item, [ - 'name' => 'require', - ]); - - if ($validate !== true) { - return $validate; - } - $tag = ActivityTemplateModel::findOne(["name"=>$item["name"]]); - if(!empty($tag)){ - return $this->json(0, "success", ["id" => $tag["id"]]); + 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'); - $id = ActivityTemplateModel::insertGetId($item); - return $this->json(0, "success", ["id" => $id]); + 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(); } @@ -176,7 +180,15 @@ class ActivityTemplate extends Base $page = input('page/d', 1); $limit = input('size/d', 20); $items = ActivityTemplateModel::findList([], [], $page, $limit, function ($q) { - return $q->order('id', 'desc'); + + 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); diff --git a/app/model/Activity.php b/app/model/Activity.php index 1b6f7e7..d551951 100644 --- a/app/model/Activity.php +++ b/app/model/Activity.php @@ -5,5 +5,37 @@ namespace app\model; //抽奖 class Activity extends Base { + const lottery_method_time = "time";//按时间开奖 + const lottery_method_people_number = "people_number";//按人数开奖 + const lottery_method_immediate = "immediate";//即抽即开 + const type_prize = "prize";//通用抽奖 + const type_redpack = "redpack";//红包抽奖 + + + const redpack_type_random = "random";//拼手气红包 + const redpack_type_uniformity = "uniformity";//均匀分配红包 + + public static function AllLotteryMethod() + { + return [ + self::lottery_method_time =>"按时间开奖", + self::lottery_method_people_number =>"按人数开奖", + self::lottery_method_immediate =>"即抽即开", + ]; + } + public static function AllType() + { + return [ + self::type_prize =>"通用抽奖", + self::type_redpack =>"红包抽奖", + ]; + } + public static function AllRedackType() + { + return [ + self::redpack_type_random =>"拼手气红包", + self::redpack_type_uniformity =>"均匀分配红包", + ]; + } } diff --git a/app/model/ActivityTemplate.php b/app/model/ActivityTemplate.php index 23417e3..3289450 100644 --- a/app/model/ActivityTemplate.php +++ b/app/model/ActivityTemplate.php @@ -3,7 +3,42 @@ namespace app\model; //抽奖模板 +use think\Model; + class ActivityTemplate extends Base { + public function adminType(){ + return $this->hasOne(ActivityTemplateType::class,"id","type_id")->bind(["type_text"=>"name"]); + } + /** + * 设置内容 + * */ + public function setWinnerAttr($value,$data) + { + $content = array_column($value,null,"sort"); + $contentKey = array_keys($content); + sort($contentKey); + $contentData =[]; + foreach ($contentKey as $ck){ + $contentData[]= $content[$ck]; + } + return json_encode($contentData,JSON_UNESCAPED_UNICODE); + } + + /** + * 设置内容 + * */ + public function getWinnerAttr($value,$data) + { + return json_decode($value,true); + } + + public static function onAfterInsert( $template) + { + $template->sort = $template->id; + $template->save(); + } + + } diff --git a/app/validate/Activity.php b/app/validate/Activity.php new file mode 100644 index 0000000..57a268a --- /dev/null +++ b/app/validate/Activity.php @@ -0,0 +1,29 @@ + 'require', + 'cover|封面' => 'require', + 'type_id|所属分类' => 'require', + 'type|抽奖类型' => 'require', + 'wechat_waiter_avatar|客服头像' => 'require', + 'wechat_waiter_number|客服微信号' => 'require', + 'lottery_method|开奖方式' => 'require', + 'lottery_time|开奖时间' => 'requireIf:lottery_method,time', + 'lottery_people_number|开奖人数' => 'requireIf:lottery_method,people_number', + 'redpack_type|红包类型' => 'requireIf:type,redpack', + 'redpack_money|红包总金额' => 'requireIf:type,redpack|>:0', + 'redpack_num|红包个数' => 'requireIf:type,redpack|>:0', + 'participants|参与者头像' => 'require', + 'winner|奖品和中奖人' => 'require', + ]; + + protected $scene = [ + 'edit' => ['old_password'], //修改密码 + ]; +} \ No newline at end of file diff --git a/public/static/manager/js/activity_template.js b/public/static/manager/js/activity_template.js index cdf2e66..aed2754 100644 --- a/public/static/manager/js/activity_template.js +++ b/public/static/manager/js/activity_template.js @@ -35,9 +35,9 @@ layui.use(['jquery', 'laytpl', 'table', 'form', 'miniTab'], function () { page: true, cols: [[ {type: 'checkbox'}, - {field: 'name', minWidth: 200, title: '名称'}, + {field: 'title', minWidth: 200, title: '名称'}, {field: 'type_text', title: '类型'}, - {field: 'lottery_method_text', title: '开奖方式'}, + {field: 'lottery_method', title: '开奖方式'}, {field: 'sort', align: 'center', title: '排序', edit: 'text'}, {templet: '#row-operate', align: 'center', title: '操作'} ]], diff --git a/public/static/manager/js/add_activity.js b/public/static/manager/js/add_activity.js index 63742b9..7651e51 100644 --- a/public/static/manager/js/add_activity.js +++ b/public/static/manager/js/add_activity.js @@ -1,8 +1,55 @@ -layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'tree', 'xmSelect', 'laydate','upload'], function () { +layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'tree', 'xmSelect', 'laydate','upload',"layer"], function () { let $ = layui.jquery, layer = layui.layer, - upload = layui.upload; - let image_exts = $('#image_exts').data('exts') ? $('#image_exts').data('exts') : "jpg|png|gif|bmp|jpeg";//图片上传后缀 + upload = layui.upload, + form = layui.form, + laydate = layui.laydate; + //日期时间选择器 + laydate.render({ + elem: '#lottery_time' + ,type: 'datetime' + }); + //监听开奖类型 + form.on('select(lottery_method)', function(data){ + switch (data.value) { + case "time": + $("#lottery_time_row").show(); + $("#lottery_people_number_row").hide(); + break; + case "people_number": + $("#lottery_time_row").hide(); + $("#lottery_people_number_row").show(); + break; + default: + $("#lottery_time_row").hide(); + $("#lottery_people_number_row").hide(); + ; + } + return false; + }); + //监听红包类型 + form.on('select(type)', function(data){ + + switch (data.value) { + case "prize": + $("#type_redpack_row").hide(); + $("#redpack_money_row").hide(); + $("#redpack_num_row").hide(); + break; + case "redpack": + $("#type_redpack_row").show(); + $("#redpack_money_row").show(); + $("#redpack_num_row").show(); + break; + default: + $("#type_redpack_row").hide(); + $("#redpack_money_row").hide(); + $("#redpack_num_row").hide(); + break; + } + return false; + }); + let image_exts = $('#image_exts').data('exts') ? $('#image_exts').data('exts') : "jpg|png|gif|bmp|jpeg";//图片上传后缀 // 删除时间 $('body').on('click', '.upload-file-div-close', function () { $(this).parent().remove() diff --git a/view/manager/activity_template/add.html b/view/manager/activity_template/add.html index dd7515d..2c5c7e8 100644 --- a/view/manager/activity_template/add.html +++ b/view/manager/activity_template/add.html @@ -11,7 +11,93 @@