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 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');//更新訂單 超時取消訂單  放在需要執行的地方,

放在需要執行的地方,如果代碼沒執行過就不會調用者方法!(感覺和類的調用方法沒什么兩樣!因為經過測試監聽方法里面耗時,會影響到此次運行的時間)





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM