-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
141 lines (110 loc) · 5.24 KB
/
index.php
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
<?php
namespace App\Http\Controllers\xxx;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cache;
class IndexController extends Controller
{
protected $wechat_robot_timeout = 60; // 微信机器人超时分钟数
public function wechatRobot(Request $request){
$wechat_robot_status_cache = 'xxx:Index:wechatrobot:wechatrobot_status';
$wechat_robot_cache = 'xxx:Index:wechatrobot:wechatrobot';
if(!Cache::has($wechat_robot_status_cache) || Cache::get($wechat_robot_status_cache) === 0){
$data['task_switch'] = false;
$data['task'] = [
'username' => '',
'words' => '',
'send_time' => time(),
];
$data['auto_reply_switch'] = false;
$data['auto_reply'] = '';
$data['is_logout'] = true;
$data['is_using'] = false;
}else{
$data = Cache::get($wechat_robot_cache);
$data['is_using'] = true; // 有人正在使用
}
$view_name = 'xxx.wechatrobot';
return view($view_name)->with($data);
}
public function ajax_postWechatRobot(Request $request){
$wechat_robot_cache = 'xxx:Index:wechatrobot:wechatrobot';
$wechat_robot_status_cache = 'xxx:Index:wechatrobot:wechatrobot_status';
if(!Cache::has($wechat_robot_status_cache) || Cache::get($wechat_robot_status_cache) === 0){
$request_data = $request->all();
if(isset($request_data['task_switch'])){
$request_data['task_switch'] = true;
}else{
$request_data['task_switch'] = false;
}
if(isset($request_data['auto_reply_switch'])){
$request_data['auto_reply_switch'] = true;
}else{
$request_data['auto_reply_switch'] = false;
}
if(empty($request_data['task']['username'])){
$request_data['task']['username'] = md5('HeadFile Studio');
}
$request_data['task']['send_time'] = strtotime($request_data['task']['send_time']);
$request_data['is_logout'] = false; //暂时都是false
$final_data['task_switch'] = $request_data['task_switch'];
$final_data['task']['username'] = $request_data['task']['username'];
$final_data['task']['words'] = $request_data['task']['words'];
$final_data['task']['send_time'] = $request_data['task']['send_time'];
$final_data['auto_reply_switch'] = $request_data['auto_reply_switch'];
$final_data['auto_reply'] = $request_data['auto_reply'];
$final_data['is_logout'] = $request_data['is_logout'];
Cache::put($wechat_robot_cache, $final_data, $this->wechat_robot_timeout); // 60分钟自动退出登录
Cache::put($wechat_robot_status_cache, 1, $this->wechat_robot_timeout); // 60分钟自动退出登录
return 'ok';
}else{
return 'in use'; // 有人正在使用
}
}
public function ajax_getWechatRobot(Request $request){
$wechat_robot_cache = 'xxx:Index:wechatrobot:wechatrobot';
$wechat_robot_last_cache = 'xxx:Index:wechatrobot:wechatrobot_last';
$wechat_robot_status_cache = 'xxx:Index:wechatrobot:wechatrobot_status';
if(!Cache::has($wechat_robot_status_cache) || Cache::get($wechat_robot_status_cache) === 0){
// 登录过期,退出登录
if(!Cache::has($wechat_robot_cache)){
return '';
}else {
$wechat_robot['task_switch'] = false;
$wechat_robot['task']['username'] = md5('HeadFile Studio');
$wechat_robot['task']['words'] = '';
$wechat_robot['task']['send_time'] = time() - 1000000;
$wechat_robot['auto_reply_switch'] = false;
$wechat_robot['auto_reply'] = '';
$wechat_robot['is_logout'] = true; // 登出
return json_encode($wechat_robot);
}
}else {
$wechat_robot = Cache::get($wechat_robot_cache);
$wechat_robot_last = Cache::get($wechat_robot_last_cache);
if (md5(serialize($wechat_robot)) != md5(serialize($wechat_robot_last))) {
// 不同,返回
Cache::put($wechat_robot_last_cache, $wechat_robot, $this->wechat_robot_timeout); // 存储上次的,用于对比
return json_encode($wechat_robot);
} else {
return '';
}
}
}
public function ajax_logoutWechatRobot(Request $request){
$wechat_robot_status_cache = 'xxx:Index:wechatrobot:wechatrobot_status';
Cache::put($wechat_robot_status_cache, 0, $this->wechat_robot_timeout);
return 'ok';
}
public function ajax_initWechatRobot(Request $request){
$wechat_robot_cache = 'xxx:Index:wechatrobot:wechatrobot';
$wechat_robot_last_cache = 'xxx:Index:wechatrobot:wechatrobot_last';
$wechat_robot_status_cache = 'xxx:Index:wechatrobot:wechatrobot_status';
Cache::forget($wechat_robot_cache);
Cache::forget($wechat_robot_last_cache);
Cache::forget($wechat_robot_status_cache);
return 'ok';
}
}
?>