1. 程式人生 > 實用技巧 >ThinkPHP5的鉤子事件 新增 HOOK,fastadmin鉤子操作,自動執行

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 <[email protected]> 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 app\api\behavior;
 4 use think\Db;
 5 use think\Config;
 6 
 7 class Vip
 8 {
 9     public function run(&$params)
10     {
11         // if (request()->isPost()) {
12         //     \app\admin\model\AdminLog::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');//更新訂單 超時取消訂單  放在需要執行的地方,

放在需要執行的地方,如果程式碼沒執行過就不會呼叫者方法!(感覺和類的呼叫方法沒什麼兩樣!因為經過測試監聽方法裡面耗時,會影響到此次執行的時間)