Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
295 lines (266 loc) · 8.92 KB
/
Copy pathPlugin.php
File metadata and controls
295 lines (266 loc) · 8.92 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 微信运动步数展示与同步插件,使博客可以显示微信运动步数并每天自动同步更新
*
* @package WeFootStep
* @author 璇
* @version 1.2.0
* @link https://blog.ybyq.wang/
* @Github https://github.com/BXCQ/WeFootStep
*/
class WeFootStep_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return string
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
// 创建数据表
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
// 创建步数表
$adapter = $db->getAdapterName();
if (strpos($adapter, 'Mysql') !== false) {
$db->query("CREATE TABLE IF NOT EXISTS `{$prefix}we_foot_step` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL,
`step_count` INT(11) NOT NULL DEFAULT 0,
`created` INT(10) UNSIGNED NOT NULL,
`modified` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;");
} else {
$db->query("CREATE TABLE IF NOT EXISTS `{$prefix}we_foot_step` (
`id` INTEGER PRIMARY KEY,
`date` DATE NOT NULL,
`step_count` INT NOT NULL DEFAULT 0,
`created` INT NOT NULL,
`modified` INT NOT NULL,
UNIQUE(`date`)
)");
}
// 注册Action
Helper::addAction('WeFootStep', 'WeFootStep_Action');
// 注册路由
Helper::addRoute('wefootstep_sync_route', '/wefootstep/sync', 'WeFootStep_Action', 'sync');
Helper::addRoute('wefootstep_data_route', '/wefootstep/data', 'WeFootStep_Action', 'getStepData');
// 添加后台管理菜单 (已移除)
// Helper::addPanel(3, 'panel.php', '微信步数', '管理微信运动步数', 'administrator');
return _t('插件已启用,请进行相关设置');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
*/
public static function deactivate()
{
// 删除路由
Helper::removeRoute('wefootstep_sync_route');
Helper::removeRoute('wefootstep_data_route');
// 删除后台管理菜单和Action (已移除)
// Helper::removePanel(3, 'panel.php');
Helper::removeAction('WeFootStep');
return _t('插件已禁用');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
// 显示样式
$displayStyle = new Typecho_Widget_Helper_Form_Element_Radio(
'displayStyle',
array(
'bar' => _t('进度条'),
'text' => _t('文本'),
'card' => _t('卡片'),
),
'bar',
_t('显示样式'),
_t('选择微信步数的显示样式')
);
$form->addInput($displayStyle);
// 显示位置
$displayPosition = new Typecho_Widget_Helper_Form_Element_Radio(
'displayPosition',
array(
'sidebar' => _t('侧边栏'),
'footer' => _t('页脚'),
'custom' => _t('自定义位置(使用短代码)'),
),
'sidebar',
_t('显示位置'),
_t('选择微信步数的显示位置,选择自定义位置需要在模板中添加短代码 <!-- WeFootStep -->')
);
$form->addInput($displayPosition);
// 小程序AppID
$appid = new Typecho_Widget_Helper_Form_Element_Text(
'appid',
null,
null,
_t('微信小程序AppID'),
_t('用于获取微信运动数据的小程序AppID')
);
$form->addInput($appid);
// 小程序AppSecret
$secret = new Typecho_Widget_Helper_Form_Element_Text(
'secret',
null,
null,
_t('微信小程序AppSecret'),
_t('用于获取微信运动数据的小程序AppSecret')
);
$form->addInput($secret);
// 卸载是否删除数据
$dropTable = new Typecho_Widget_Helper_Form_Element_Radio(
'dropTable',
array(
'0' => _t('否'),
'1' => _t('是'),
),
'0',
_t('卸载时是否删除数据'),
_t('选择是否在卸载插件时删除数据表')
);
$form->addInput($dropTable);
// 添加测试区域,显示最新接收到的数据
echo '<div class="typecho-option" id="wefootstep-debug">';
echo '<label class="typecho-label">调试信息</label>';
echo '<div class="typecho-option-content">';
// 显示最新步数数据
$db = Typecho_Db::get();
$latestStep = $db->fetchRow(
$db->select()
->from('table.we_foot_step')
->order('date', Typecho_Db::SORT_DESC)
->limit(1)
);
if ($latestStep) {
echo '<p><strong>最新步数记录:</strong> ' . $latestStep['step_count'] . ' 步 (' . $latestStep['date'] . ')</p>';
} else {
echo '<p><strong>最新步数记录:</strong> 暂无数据</p>';
}
// 显示请求日志
$logFile = dirname(__FILE__) . '/request_log.txt';
if (file_exists($logFile)) {
echo '<p><strong>请求日志:</strong></p>';
echo '<pre style="max-height:300px;overflow:auto;background:#f6f6f6;padding:10px;border:1px solid #ddd;font-size:12px;">';
echo htmlspecialchars(file_get_contents($logFile));
echo '</pre>';
} else {
echo '<p><strong>请求日志:</strong> 暂无日志</p>';
}
echo '</div>';
echo '</div>';
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
// 个人用户不需要配置
}
/**
* 插件实现方法
*
* @access public
* @return string
*/
public static function render()
{
// 直接使用Widget类进行渲染
$widget = Typecho_Widget::widget('WeFootStep_Widget');
ob_start();
$widget->render();
$html = ob_get_clean();
return $html;
}
/**
* 临时导航栏包装方法,用于安全地过渡
*
* @access public
* @param string $navBar
* @return string
*/
public static function navBarWrapper($navBar)
{
// 此方法为空,以防止因旧钩子注册而导致的致命错误。
// 在停用并重新激活插件后,此方法可以被安全地移除。
return $navBar;
}
/**
* 在页面底部添加内容
*
* @access public
* @param string $footer
* @return string
*/
public static function footer($footer = '')
{
// All JavaScript logic is now being moved back into headnav.php
// to ensure execution context and timing are correct.
// Therefore, we no longer need to load any scripts here.
// This part handles the original plugin functionality of rendering a widget
// in the footer based on plugin settings.
$plugin_options = Helper::options()->plugin('WeFootStep');
if (isset($plugin_options->displayPosition) && $plugin_options->displayPosition == 'footer') {
$html = self::render();
return $footer . $html;
}
return $footer;
}
/**
* 在页面头部添加内容
*
* @access public
* @param string $header
* @return string
*/
public static function header($header = '')
{
// 不再需要在 header 中加载 echarts,统一移动到 footer
return $header;
}
/**
* 删除目录及文件
*
* @access private
* @param string $dir
* @return void
*/
private static function deleteDir($dir)
{
if (!is_dir($dir)) {
return;
}
$files = scandir($dir);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
$path = $dir . $file;
if (is_dir($path)) {
self::deleteDir($path . '/');
} else {
@unlink($path);
}
}
}
@rmdir($dir);
}
}