一、
创建一个任务类
- php artisan make:job QueuedTest //执行之后app/Jobs目录下生成一个QueuedTest.php
二、创建控制器
- php artisan make:controller QueuedController
- 执行之后生成app/Http/Controllers/QueuedController.php文件
修改内容1 <?php 2 namespace AppHttpControllers; 3 use AppHttpControllersController; 4 use AppUsersModel; 5 use AppCustemorModel; 6 use AppProductModel; 7 use IlluminateHttpRequest; 8 use AppHttpRequests; 9 use AppJobsQueue; 10 use IlluminateSupportFacadesDB; 11 use IlluminateSupportFacadesRedis; 12 use IxudraCurlFacadesCurl; 13 class QueuedController extends Controller 14 { 15 16 //点击商品订购跳转 17 public function goods(Request $request){ 18 $id=$request['id']; 19 $goods=DB::table('product')->where('id',$id)->get(); 20 $good=json_decode($goods,true); 21 $data['name']=$good[0]['name']; 22 $data['price']=$good[0]['price']; 23 $data['created_at']=time(); 24 $data['updated_at']=time(); 25 DB::table('custemor')->insert($data); 26 $arr=DB::table('product')->where('id',$id)->get(); 27 $arr1=json_decode($arr,true); 28 $this->dispatch(new Queue($arr1));//进入队列 29 } 30 31 32 33 }
-
//进入jop/queue.php
<?php namespace AppJobs; use AppUsersModel; use AppCustemorModel; use AppProductModel; use IlluminateSupportFacadesDB; use IlluminateBusQueueable; use IlluminateFoundationBusDispatchable; use IlluminateQueueSerializesModels; use IlluminateQueueInteractsWithQueue; use IlluminateContractsQueueShouldQueue; use IlluminateSupportFacadesRedis; class Queue implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; private $data; /** * Queue constructor. * @param $data */ public function __construct($data) { $this->data = $data; } /** * Execute the job. * * @return void */ public function handle() { $id=$this->data[0]['id']; // var_dump($id);die; $query=new ProductModel(); $model=$query->info($id); //减去库存 $ids= Redis::get('id');//获取用户id if($model){ $users = DB::table('users') ->select('balance') ->where('id',$ids) ->get(); $user=json_decode($users,true); $pro = DB::table('product') ->select('price') ->where('id',$id) ->get(); $pros=json_decode($pro,true); //var_dump($pros);die; $edit=DB::table('users')->where('id', $ids)->update(['balance' =>(int)$user[0]['balance']-(int)$pros[0]['price']]);//下单进行扣费操作 $cus=new CustemorModel(); $status=$cus->status($id);//更改状态 if($edit && $status){ echo "下单成功"; }else{ echo "余额不足"; } } } }//更改订单状态
-
<?php namespace App; use IlluminateSupportFacadesDB; use IlluminateDatabaseEloquentModel; class CustemorModel extends Model { protected $table="product"; public function status($id){ $query=DB::table('custemor')->where('pid',$id)->update(['status'=>1]); return $query; } }
//减去商品库存
-
1 <?php 2 namespace App; 3 use IlluminateSupportFacadesDB; 4 use IlluminateDatabaseEloquentModel; 5 class ProductModel extends Model 6 { 7 protected $table="product"; 8 public function show(){ 9 $data= DB::table('product')->get(); 10 return $data; 11 } 12 //查询商品是否有库存 13 public function info($id){ 14 $users = DB::table('product') 15 ->select('stock_num','price') 16 ->where('id',$id) 17 ->get(); 18 $user=json_decode($users,true); 19 if($users <= '0'){ 20 return "库存不足"; 21 }else{ 22 $edit=DB::table('product')->where('id', $id)->update(['stock_num'=>(int)$user[0]['stock_num']-1]); 23 return $edit; 24 } 25 return $user; 26 } 27 //商品添加 28 public function add($data){ 29 $query=DB::table('product')->insert($data); 30 return $query; 31 } 32 33 }