二十三*二十四:订单表,基本验证

一:订单表对应模型实体

php bin/swoft entity:create --table=orders_main --pool=db.pool --path=@app/Models

 

 

 二:自定义验证器

 

<?php


namespace App\Http\MyValidator;

use Swoft\Validator\Annotation\Mapping\IsFloat;
use Swoft\Validator\Annotation\Mapping\IsInt;
use Swoft\Validator\Annotation\Mapping\IsString;
use Swoft\Validator\Annotation\Mapping\Max;
use Swoft\Validator\Annotation\Mapping\Min;
use Swoft\Validator\Annotation\Mapping\Validator;

/**
* 主订单验证
* @Validator(name="orders")
*/
class OrderValidator
{
/**
* @IsString(name="order_no",message="订单号不能为空")
* @var string
*/
protected $order_no;

/**
* @IsInt(name="user_id",message="用户ID不能为空")
* @Min(value=1,message="用户ID不正确")
* @var int
*/
protected $user_id;

/**
* @IsInt(name="order_status",message="订单状态不能空")
* @Min(value=0,message="状态不正确min")
* @Max(value=5,message="状态不正确max")
* @var int
*/
protected $order_status;

/**
* @IsFloat(name="order_money",message="订单金额不能为空")
* @Min(value=1,message="订单金额不正确")
* @var int
*/
protected $order_money;
}


 三:协程方法的链式调用写法

 

 

 

 

//为了获取请求参数和响应数据的方便,我们做了RequestResponse公共函数的封装;
//$p=NewProduct($id,"测试商品-链式调用");
//为了代码的简洁,封装了isGetisPost全局函数;
$myRequest=new MyRequest();
$resultInfo=$myRequest->if(isGet())->then(function () use ($id){
$product=Products::find($id);
if($product)
{
sgo(function ()use($product){
\Swoole\Coroutine::sleep(5);
$product->increment('prod_click');
echo "click_down".PHP_EOL;//这个在控制台显示看效果;
});

sgo(function ()use($id)
{
\Swoole\Coroutine::sleep(7);
ProductsView::updateOrInsert(//里面有两个数组
["prod_id"=>$id,"view_ip"=>ip(),"view_date"=>date("Y-m-d")],//第一个数组判断哪些是重复的
//["view_num"=>"view_num+1"]// 这种累加方式是不对的;要使用Db::raw()的方式;
["view_num"=>Db::raw("view_num+1")]
);
echo "viewlog_down".PHP_EOL;//这个在控制台显示看效果;
});

return $product;
}
})->if(isPost())->then(function () use($id)
{
//\validate(request()->post(),"ProductValidator");
//return $product;
})->getResult();
return $resultInfo;

/**
协程的链式写法 上面方法的改造写法
$myRequest=new MyRequest();
$resultInfo=$myRequest->if(isGet())->then(function () use ($id) {
$product = Products::find($id);
return $product;
})

->go(function (MyRequest $self){
\Swoole\Coroutine::sleep(5);
$product=$self->getResult();获取上面的查询结果
if($product)
{
$product->increment('prod_click');
echo "click_down".PHP_EOL;//这个在控制台显示看效果;
}
})
->go(function ()use($id)
{
\Swoole\Coroutine::sleep(7);
ProductsView::updateOrInsert(//里面有两个数组
["prod_id"=>$id,"view_ip"=>ip(),"view_date"=>date("Y-m-d")],//第一个数组判断哪些是重复的
//["view_num"=>"view_num+1"]// 这种累加方式是不对的;要使用Db::raw()的方式;
["view_num"=>Db::raw("view_num+1")]
);
echo "viewlog_down".PHP_EOL;//这个在控制台显示看效果;
})
->if(isPost())->then(function () use($id)
{
//\validate(request()->post(),"ProductValidator");
//return $product;
})->getResult();
return $resultInfo;
*/
posted @ 2019-11-20 13:38  痞子胥  阅读(99)  评论(0)    收藏  举报