thinkphp5.1控制器自定义参数获取及场景验证,通过post方法获取参数,循环赋值,把需要验证的参数放到数组中进行字段校验
thinkphp5.1控制器自定义参数获取及场景验证,通过post方法获取参数,循环赋值,把需要验证的参数放到数组中进行字段校验
<?php
namespace app\api\controller;
use think\Controller;
use app\common\model\Coupon as Coupons;
class Coupon extends Controller
{
/*
* 新增优惠券
*/
public function create()
{
$optionParams = ['name', 'price','sub_name','order_limit_price','s_time','e_time','num','num_limit','coupon_price','type','mark'];
$params = [];
if (!empty($this->request->post())) {
foreach ($this->request->post() as $key => $value) {
if (in_array($key, $optionParams) && !empty($value)) {
$params[$key] = $value;
} else {
$params[$key] = '';
}
}
} else {
$this->result([], 0, '参数不能为空', 'json');
}
$resValidate = $this->validate($params, 'app\api\validate\Coupon.add');
if ($resValidate !== true){
$this->result([], 1, $resValidate, 'json');
}
$result = (new Coupons())->insert($this->request->post());
if ($result) {
$this->result([], 0, '新增成功', 'json');
} else {
$this->result([], 1, '新增失败', 'json');
}
}
/*
* 更新优惠券
*/
public function update()
{
$optionParams = ['id','name', 'price','sub_name','order_limit_price','s_time','e_time','num','num_limit','coupon_price','type','mark'];
$params = [];
if (!empty($this->request->post())) {
foreach ($this->request->post() as $key => $value) {
if (in_array($key, $optionParams) && !empty($value)) {
$params[$key] = $value;
} else {
$params[$key] = '';
}
}
} else {
$this->result([], 0, '参数不能为空', 'json');
}
$resValidate = $this->validate($params, 'app\api\validate\Coupon.edit');
if ($resValidate !== true){
$this->result([], 1, $resValidate, 'json');
}
$result = (new Coupons())->updateCoupon($this->request->post());
if ($result) {
$this->result([], 0, '更新成功', 'json');
} else {
$this->result([], 1, '更新失败', 'json');
}
}
}
CREATE TABLE `tp_coupon` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增id',
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '优惠券名称',
`sub_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '副标题',
`order_limit_price` bigint(20) UNSIGNED NOT NULL DEFAULT 0 COMMENT '优惠券使用门槛金额,订单金额大于此值是才能使用',
`num` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '优惠券总量',
`num_limit` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '限制领取量(每个会员限制领取数量)',
`coupon_price` bigint(20) UNSIGNED NOT NULL DEFAULT 0 COMMENT '优惠券金额(单位分)',
`s_time` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '开始时间',
`e_time` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '结束时间',
`type` tinyint(2) UNSIGNED NOT NULL DEFAULT 1 COMMENT '促销类型 1 优惠券',
`mark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '描述',
`create_time` int(11) UNSIGNED NULL DEFAULT 0,
`update_time` int(11) UNSIGNED NULL DEFAULT 0,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '优惠券' ROW_FORMAT = Dynamic;
转载注明:
感谢博主,喝杯咖啡~
感谢博主,喝杯咖啡~
还没有人发表评论