ThinkPHP5的钩子事件 添加 HOOK,fastadmin钩子操作,自动执行

在一些数据量不大的情况下,想用接口来判断某个数据是否超时,等延时执行啥的,

代码修改三个地方即可

第一个 /application/api/tags.php

 1 <?php
 2 
 3 // +----------------------------------------------------------------------
 4 // | ThinkPHP [ WE CAN DO IT JUST THINK ]
 5 // +----------------------------------------------------------------------
 6 // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
 7 // +----------------------------------------------------------------------
 8 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 9 // +----------------------------------------------------------------------
10 // | Author: liu21st <liu21st@gmail.com>
11 // +----------------------------------------------------------------------
12 // 应用行为扩展定义文件
13 return [
14     // 应用结束
15     'app_end'      => [
16         'app\api\behavior\Vip',
17     ],
18     // 自己写个订单更新  PHP技术QQ群153073132
19     'order_update'      => [
20         'app\api\behavior\Vip','OrderUpdate'
21     ],
22 ];
OrderUpdate是执行的方法,
app\api\behavior\Vip对应你的  (我这里是卸载api接口里面的)

第二个

就是刚刚写的路径里面那个,没有就自行添 、也可以修改成已经有的里面 run方法一进来也会执行所以需要就留着不需要就可以删除掉
 1 <?php
 2 
 3 namespace appapiehavior;
 4 use thinkDb;
 5 use thinkConfig;
 6 
 7 class Vip
 8 {
 9     public function run(&$params)
10     {
11         // if (request()->isPost()) {
12         //     appadminmodelAdminLog::record();
13         // }
14 
15         //先查询会员
16 
17         $Vip_list = Db::name('user')->where('vip_etime','>',time())->select();
18 
19         $stime = strtotime('first Day of this month 00:00:00');
20         $etime = strtotime('first Day of next month 00:00:00');
21 
22         // 再查询是否赠送
23         // vip_month
24         foreach ($Vip_list as $key => $us) {
25             if(empty($us['vip_month']) || $us['vip_month'] < 1){
26                 //赠送vip的优惠券
27 
28                 $res = Db::name('coupons_user')->where('uid',$us['id'])->where('createtime','>',$stime)->where('createtime','<',$etime)->select();
29                 if(!$res){
30                     $indata['uid'] = $us['id'];
31 
32                     $coupons = Db::name('coupons')->find();//优惠券的id
33 
34                     $indata['couponsid'] = $coupons['id'];
35                     $indata['createtime'] = time();
36                     $indata['end_time'] =  $indata['createtime'] + $coupons['timeu'] * 86400;//到期时间
37                     $indata['status'] = '0';//使用状态:0=待使用,1=已使用,2=已过期
38                     $indata['is_self'] = '0';//领取的方式:0=赠送的,1=自己领取的
39                     
40 
41                     $ci = 0;
42                     for ($i=0; $i < Config::get('site.give_num'); $i++) { 
43                        $res = Db::name('coupons_user')->insert($indata);
44                        if($res){
45                             $ci++;
46                        }
47                     }
48 
49                 }
50             }
51         }
52 
53 
54 
55 
56     }
57 
58     //这就是 自己写的   订单超时 就修改
59     public function OrderUpdate()
60     {
61         $M = Config::get('site.overtime_minute');//超时时间分钟
62 
63         //状态:0=待支付,1=已支付,2=超时,3=已退款,4=申请退款中
64         $list = Db::name('route_school')
65             ->where('pay_status','0')
66             ->where('createtime','<',(time()-$M*60))
67             ->update(['pay_status'=>'2']);
68     }
69 }

  第三就是直接放在需要监听的地方(类似于调用类)

 Hook::listen('order_update');//更新订单 超时取消订单  放在需要执行的地方,

放在需要执行的地方,如果代码没执行过就不会调用者方法!(感觉和类的调用方法没什么两样!因为经过测试监听方法里面耗时,会影响到此次运行的时间)




原文地址:https://www.cnblogs.com/xiaohe520/p/14042504.html