Events.php
7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* 用于检测业务代码死循环或者长时间阻塞等问题
* 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
* 然后观察一段时间workerman.log看是否有process_timeout异常
*/
//declare(ticks=1);
/**
* wale
* 主要是处理 onMessage onClose
*/
use think\Config;
use WeMini\Newtmpl;
use Workerman\Lib\Timer;
class Events
{
/**
* 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次。
* 可以在这里为每一个businessWorker进程做一些全局初始化工作,例如设置定时器,初始化redis等连接等。
*
* @param $businessWorker
*
* @throws Exception
*/
public static function onWorkerStart($businessWorker)
{
echo 'WorkerStart id: ' . $businessWorker->id;
$config = [
'appid' => 'wx670b02d7406045d1',
'appsecret' => '2c2aa784ee902248d7360dde74c8e299',
'mch_id' => '1577676721',
'mch_key' => 'MmXiXHO16b89tNx75t3T8rHTYMQBbn19',
//'notify_url' => '127.0.0.1/wake/public/api/order/notify',
'notify_url' => 'https://wake.w.brotop.cn/api/order/notify',
//'course_sign_tmp' => '0A7sPV2VrDo7_3Fo5q9z-Ddh-m85_GvF15ANspbo70Ey',
'course_sign_tmp' => 'Y24N0zGcKj2tEFOh1elMoy5j5ppRK8MSGGINyaX5gYc',
//'course_start_tmp' => '0A7sPV2VrDo7_3Fo5q9z-Ddh-m85_GvF15ANspbo70Ey',
'course_start_tmp' => 'Y24N0zGcKj2tEFOh1elMoy5j5ppRK8MSGGINyaX5gYc',
'order_over_tmp' => '586FN0OLJ1DPZgwPwh7Ke2P4wewhm7l67awB77qrOi8',
];
//订单到期
$timer = 60*1;
Timer::add($timer, function ($config){
error_log('order_over');
$template = new Newtmpl($config);
$order = new \app\admin\model\Order();
$order = $order->where(['status'=>'success','end'=>['<',date('Y-m-d H:i:s')]])->with('user')->select();
$notice = new \app\admin\model\UserNotice();
foreach ($order as $k){
$order = new \app\admin\model\Order();
$order->save(['status'=>'over'],['id'=>$k['id']]);
$insert = [
'user_id' => $k['id'],
'type' => 'order_over',
'content_id' => $k['id'],
'time' => date('Y-m-d H:i:s'),
];
$notice->insert($insert);
//小程序通知
$data = [
'touser' => $k['user']['openid'],
'template_id' => $config['order_over_tmp'],
'form_id'=> $k['id'],
'page'=>'pages/classReserve/classReserve',
'data'=>[
"thing1" => ["value" => "维克猩球"],
"number2" => ["value" => "0"],
"name3" => ["value" => $k['user']['nickname']],
],
];
$template->send($data);
}
});
//开课提醒
$timer = 60*1;
Timer::add($timer, function ($config){
error_log('course_start');
$template = new Newtmpl($config);
$setting = new \app\admin\model\CourseSetting();
$setting = $setting->where(['setting_key'=>'course_start'])->find();
if(empty($setting)){
$time = 3600;
}else{
$time = $setting['setting_value']*3600;
}
$courseStore = new \app\admin\model\CourseStore();
$courseStore = $courseStore->with(['store','course'])->where(['notify'=>false,'status'=>'confirmed','start' => ['<',date('Y-m-d H:i:s',time()+$time)] ])->select();
$notice = new \app\admin\model\UserNotice();
foreach ($courseStore as $k){
$courseStore = new \app\admin\model\CourseStore();
$courseStore->save(['notify'=>true],['id'=>$k['id']]);
$sign = new \app\admin\model\CourseSign();
$sign = $sign->with(['user','CourseStore'])->where(['course_store_id'=>$k['id']])->select();
foreach ($sign as $v){
$insert = [
'user_id' => $v['user_id'],
'type' => 'course_start',
'content_id' => $v['id'],
'time' => date('Y-m-d H:i:s'),
];
$notice->insert($insert);
//小程序通知
$data = [
'touser' => $v['user']['openid'],
'template_id' => $config['course_start_tmp'],
'form_id'=> $v['id'],
'page'=>'pages/classReserve/classReserve',
'data'=>[
"thing2" => ["value" => $k['course']['name']],
"thing4" => ["value" => $k['store']['address']],
"date1" => ["value" => $v['CourseStore']['start']],
"name3" => ["value" => $v['user']['nickname']],
"thing5" => ["value" => "课程即将开始"],
],
];
$template->send($data);
}
}
});
//完成课程
$timer = 60*1;
Timer::add($timer, function (){
error_log('course_over');
$courseStore = new \app\admin\model\CourseStore();
$courseStoreData = $courseStore->where(['status'=>'confirmed','end' => ['<',date('Y-m-d H:i:s',time())], 'is_end'=>false])->select();
foreach ($courseStoreData as $k){
$sign = new \app\admin\model\CourseSign();
$sign_data = $sign->with('user')->where(['status'=>'publish','course_store_id'=>$k['id']])->select();
foreach ($sign_data as $v){
$sign->save(['status'=>'finish','finish_time'=>date('Y-m-d H:i:s')],['id'=>$v['id']]);
}
$courseStore = new \app\admin\model\CourseStore();
$courseStore->save(['is_end'=>true],['id'=>$k['id']]);
}
});
}
/**
* 有消息时
*
* @param int $client_id
* @param mixed $message
*
* @return void
*/
public static function onMessage($client_id, $message)
{
// debug
echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id session:" . json_encode($_SESSION) . " onMessage:" . $message . "\n";
// 客户端传递的是json数据
$message_data = json_decode($message, true);
if ($message_data) {
//是否回传给自己
$returnMe = empty($message_data['returnMe']) ? false : true;
// 根据类型执行不同的业务
switch ($message_data['type']) {
// 客户端回应服务端的心跳
case 'pong':
break;
default:
break;
}
}
}
/**
* 当客户端断开连接时
*
* @param integer $client_id 客户端id
*/
public static function onClose($client_id)
{
// debug
echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
}
}