-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAction.php
99 lines (89 loc) · 2.43 KB
/
Action.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
<?php
/**
* 同步语雀文档操作类
*
* @package YuqueSync
* @author Juexe
* @version 1.0.0
*/
class YuqueSync_Action extends Typecho_Widget implements Widget_Interface_Do
{
/**
* 插件配置
*
* @access private
* @var Typecho_Config
*/
private $_config;
/**
* 构造方法
*/
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
/* 获取插件配置 */
$this->_config = Typecho_Widget::widget('Widget_Options')->plugin('YuqueSync');
}
/**
* 获取 key 用户
*/
public function get_user()
{
return $this->yuque_get("https://www.yuque.com/api/v2/user");
}
/**
* 获取文档列表
*/
public function get_repo_docs($repo = null)
{
if ($repo===null) {
$repo = $this->request->filter('strip_tags', 'trim', 'xss')->repo;
}
$username = $this->_config->username;
return $this->yuque_get("https://www.yuque.com/api/v2/repos/$username/$repo/docs");
}
/**
* 获取仓库列表
*/
public function get_repos()
{
$username = $this->_config->username;
return $this->yuque_get("https://www.yuque.com/api/v2/users/$username/repos");
}
/**
* 获取文章详情
*/
public function get_doc_details()
{
$username = $this->_config->username;
$repo = $this->request->filter('strip_tags', 'trim', 'xss')->repo;
$slug = $this->request->filter('strip_tags', 'trim', 'xss')->slug;
return $this->yuque_get("https://www.yuque.com/api/v2/repos/{$username}/{$repo}/docs/{$slug}?raw=1");
}
/**
* 请求语雀接口
*/
public function yuque_get($url)
{
$token = $this->_config->token;
$client = Typecho_Http_Client::get();
$client->setMethod('GET');
$client->setHeader('User-Agent', 'Typecho-Yuque-Sync');
$client->setHeader('X-Auth-Token', $token);
$client->send($url);
return json_decode($client->getResponseBody(), true);
}
/**
* 接口需要实现的入口函数
*
* @access public
* @return void
*/
public function action()
{
$do = $this->request->filter('strip_tags', 'trim', 'xss')->do;
if (method_exists($this, $do)) {
return $this->response->throwJson($this->$do());
}
}
}