Skip to content

Commit

Permalink
更新社区维护版JSON,修正相关文档
Browse files Browse the repository at this point in the history
  • Loading branch information
jzwalk committed Jun 28, 2020
1 parent 4bf2060 commit 8920dd3
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 39 deletions.
53 changes: 29 additions & 24 deletions JSON/Action.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

// header('Access-Control-Allow-Origin: *');

class JSON_Action extends Typecho_Widget implements Widget_Interface_Do {

private $db;
private $res;

const LACK_PARAMETER = "缺少参数";
const LACK_PARAMETER = "Not found";

public function __construct($request, $response, $params = NULL) {
parent::__construct($request, $response, $params);
Expand All @@ -19,7 +23,7 @@ public function __construct($request, $response, $params = NULL) {
}

private function defaults() {
$this->export("Hello World!");
$this->export(null);
}
private function count() {
$select = $this->db->select("COUNT(*) AS counts")
Expand All @@ -31,21 +35,20 @@ private function count() {
return $this->export($res);

}
//参数 pageSize, page, authorId, created, cid, category, commentsNumMax, commentsNumMin, allowComment
//文章参数 pageSize, page, authorId, created, cid, category, commentsNumMax, commentsNumMin, allowComment
private function posts() {
$pageSize = (int) self::GET('pageSize', 5);
$pageSize = (int) self::GET('pageSize', 1000);
$page = (int) self::GET('page', 1);
$authorId = self::GET('authorId', 0);
$offset = $pageSize * ( $page - 1 );

$select = $this->db->select('cid', 'title', 'created', 'type', 'slug', 'text')->from('table.contents')
$select = $this->db->select('cid', 'title', 'created', 'type', 'slug', 'text', 'authorId')->from('table.contents')
->where('type = ?', 'post')
->where('status = ?', 'publish')
->where('created < ?', time())
->order('table.contents.created', Typecho_Db::SORT_DESC)
->offset($offset)
->limit($pageSize);
$test = $this->db->fetchAll($select);
// 根据cid偏移获取文章
if(isset($_GET['cid'])) {
$cid = self::GET('cid');
Expand Down Expand Up @@ -88,15 +91,17 @@ private function posts() {
$result = array();
foreach($posts as $post) {
$post = $this->widget("Widget_Abstract_Contents")->push($post);
$post['tag'] = $this->db->fetchAll($this->db->select('name')->from('table.metas')
$post['author'] = $this->db->fetchAll($this->db->select('uid', 'name', 'mail', 'url', 'screenName')->from('table.users')->where('uid = ?', $post['authorId']));
$post['tag'] = $this->db->fetchAll($this->db->select('name')->from('table.metas')
->join('table.relationships', 'table.metas.mid = table.relationships.mid', Typecho_DB::LEFT_JOIN)
->where('table.relationships.cid = ?', $post['cid'])
->where('table.metas.type = ?', 'tag'));
$post['thumb'] = $this->db->fetchAll($this->db->select('str_value')->from('table.fields')->where('cid = ?', $post['cid']))?$this->db->fetchAll($this->db->select('str_value')->from('table.fields')->where('cid = ?', $post['cid'])):array(array("str_value"=>"https://ww4.sinaimg.cn/large/a15b4afegw1f8sqaz6y6bj20go06j0u2"));
$result[] = $post;
}
$this->export($result);
}
//参数 content
//页面参数 content
private function pageList() {
$content = self::GET('content', false);

Expand All @@ -116,12 +121,12 @@ private function pageList() {
}
$this->export($pageList);
}
//参数 cid, slug
//单篇参数 cid, slug
private function single() {
if(!isset($_GET['cid']) && !isset($_GET['slug']))
$this->export(self::LACK_PARAMETER, 404);

$select = $this->db->select('cid', 'created', 'type', 'slug', 'text')->from('table.contents');
$select = $this->db->select('cid', 'title', 'created', 'type', 'slug', 'text')->from('table.contents');
if(isset($_GET['cid'])) $select->where('cid = ?', $_GET['cid']);
if(isset($_GET['slug'])) $select->where('slug = ?', $_GET['slug']);
$post = $this->db->fetchRow($select);
Expand All @@ -135,7 +140,7 @@ private function post() {
private function page() {
$this->single();
}
//参数 authorId, cid
//相关文章参数 authorId, cid
private function relatedPosts() {
if(!isset($_GET['authorId']) && !isset($_GET['cid'])) {
$this->export(self::LACK_PARAMETER, 404);
Expand All @@ -160,21 +165,22 @@ private function relatedPosts() {
}
$this->export($relatedPosts);
}
//参数 pageSize
//最近文章参数 pageSize
private function recentPost() {
$pageSize = self::GET('pageSize', 10);

$this->widget("Widget_Contents_Post_Recent", "pageSize=$pageSize")->to($post);
$this->widget("Widget_Contents_Post_Recent", "pageSize={$pageSize}")->to($post);
$recentPost = array();
while($post->next()) {
$recentPost[] = array(
"cid" => $post->cid,
"title" => $post->title,
"permalink" => $post->permalink
);
}
$this->export($recentPost);
}
//参数 pageSize, parentId, ignoreAuthor, showCommentOnly
//评论参数 pageSize, parentId, ignoreAuthor, showCommentOnly
private function recentComments() {
$pageSize = self::GET('pageSize', 10);
$parentId = self::GET('parentId', 0);
Expand Down Expand Up @@ -204,7 +210,7 @@ private function recentComments() {
}
$this->export($recentComments);
}
//参数 ignore, childMode
//分类参数 ignore, childMode
private function categoryList() {
$ignores = explode(',', self::GET('ignore'));
$childMode = self::GET('childMode', false);
Expand Down Expand Up @@ -234,15 +240,14 @@ private function categoryList() {
if($childMode) $categoryList = array_values($parent);
$this->export($categoryList);
}
//参数 sort, count, ignoreZeroCount, desc, limit
//标签参数 sort, ignoreZeroCount, desc, limit
private function tagCloud() {
$sort = self::GET('sort', 'count');
$ignoreZeroCount = self::GET('ignoreZeroCount', false);
$desc = self::GET('desc', true);
$limit = self::GET('limit', 0);

$tagCloud = array();
$this->widget("Widget_Metas_Tag_Cloud", "sort=$sort&ignoreZeroCount=$ignoreZeroCount&desc=$desc&limit=$limit")->to($tags);
$this->widget("Widget_Metas_Tag_Cloud", "sort={$sort}&ignoreZeroCount={$ignoreZeroCount}&desc={$desc}&limit={$limit}")->to($tags);
while($tags->next()) {
$tagCloud[] = array(
"name" => $tags->name,
Expand All @@ -252,7 +257,7 @@ private function tagCloud() {
}
$this->export($tagCloud);
}
//参数 format, type, limit
//归档参数 format, type, limit
private function archive() {
$format = self::GET('format', 'Y-m');
$type = self::GET('type', 'month');
Expand All @@ -273,7 +278,6 @@ private function archive() {
if(isset($result[$date])) {
$result[$date]['count'] ++;
} else {

$result[$date]['year'] = date('Y', $timeStamp);
$result[$date]['month'] = date('m', $timeStamp);
$result[$date]['day'] = date('d', $timeStamp);
Expand All @@ -289,7 +293,8 @@ private function archive() {

$this->export($result);
}
//参数 user
/**
//用户参数 user
private function info() {
$user = self::GET('user', 0);
Expand All @@ -304,7 +309,7 @@ private function info() {
$this->export($result);
}
/** 实验型升级接口
//系统升级接口(未测试)
private function upgrade() {
$generator = $this->widget("Widget_Options")->generator;
$generator = explode(' ', $generator);
Expand Down Expand Up @@ -351,7 +356,7 @@ private function update($url) {
curl_exec($curl);
curl_close($curl);
}
*/
*/
public function export($data = array(), $status = 200) {
$this->res->throwJson(array('status'=>$status, 'data'=>$data));
exit();
Expand Down
8 changes: 4 additions & 4 deletions JSON/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 支持使用“api/接口名”路径按参数获取json格式的博客数据
* 使用api接口输出json博客数据 【<a href="https://github.com/typecho-fans/plugins" target="_blank">TF</a>社区维护版】
*
* @package JSON
* @author 公子
* @version 0.1
* @link https://imnerd.org
* @author 姬长信,SangSir,公子
* @version 1.1
* @link https://github.com/typecho-fans/plugins/tree/master/JSON
*/
class JSON_Plugin implements Typecho_Plugin_Interface
{
Expand Down
88 changes: 81 additions & 7 deletions JSON/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
### Json格式数据输出插件JSON v0.1
<a href="https://typecho-fans.github.io">
<img src="https://typecho-fans.github.io/text-logo.svg" alt="TF Logo" title="Typecho Fans开源作品社区" align="right" height="100" />
</a>

支持使用“api/接口名”路径按参数获取json格式的博客数据。
JSON v1.1 - 社区维护版
======================
<h4 align="center">—— 以“api/接口名”路径按参数输出json格式博客数据插件,可用于各类映射扩展。</h4>

> upgrade接口改注释。
<p align="center">
<a href="#使用说明">使用说明</a> •
<a href="#版本历史">版本历史</a> •
<a href="#贡献作者">贡献作者</a> •
<a href="#附注链接">附注/链接</a> •
<a href="#授权协议">授权协议</a>
</p>

---

## 使用说明

<table>
<tr>
<td>

###### 一款基于Typecho的开放式API插件,支持按接口参数输出文章,首页,评论,分类等数据用于生成微信小程序。

**使用方法**
##### 1. 下载本插件,放在 `usr/plugins/` 目录中,确保文件夹名为 JSON;
##### 2. 激活插件,试试访问 http://[example].com/api/[action] 吧!

**注意**
* 以上地址中的[action]请替换下表列出的接口名,其后可再接?[参数名]=[]
* 未开启地址重写功能的博客需要在域名后接/index.php再/api/[action]同上。

|接口名|可用参数|默认值|说明|
|---|:---:|:---:|---|
|posts|pageSize<br/>page<br/>authorId<br/>created<br/>cid<br/>category<br/>commentsNumMax<br/>commentsNumMin<br/>allowComment|5<br/>1<br/>0<br/>-<br/>-<br/>-<br/>-<br/>-<br/>-|文章综合数据|
|posts|pageSize<br/>page<br/>authorId<br/>created<br/>cid<br/>category<br/>commentsNumMax<br/>commentsNumMin<br/>allowComment|1000<br/>1<br/>0<br/>-<br/>-<br/>-<br/>-<br/>-<br/>-|文章综合数据|
|pageList|content|false|页面综合数据|
|single|cid<br/>slug|-<br/>-|指定单页数据|
|post|同上|同上|指定文章数据|
Expand All @@ -17,7 +45,53 @@
|categoryList|ignore<br/>childMode|-<br/>false|分类列表数据|
|tagCloud|sort<br/>ignoreZeroCount<br/>desc<br/>limit|count<br/>false<br/>true<br/>0|标签列表数据|
|archive|format<br/>type<br/>limit|Y-m<br/>month<br/>0|归档列表数据|
|info|user|0|配置信息数据|
|count|-|-|发表文章总数|
|info|user|0|用户配置数据<br/>(安全考虑已注释)|
|count|-|-|博客文章总数|
|upgrade|-|-|系统检测升级<br/>(安全考虑已注释)|

演示地址:[https://sangsir.com/api/posts](https://sangsir.com/api/posts)

</td>
</tr>
</table>

## 版本历史

* v1.1 (20-6-27 [@jzwalk](https://github.com/jzwalk))
* 合并2个衍生版本改动:
* 文章接口增加作者数据输出[@hkq15](https://gitee.com/hkq15)
* 文章默认输出页数改为1000[@insoxin](https://github.com/insoxin)
* v1.0 (17-11-06 [@SangSir](https://github.com/szj1006))
* 简化部分接口,调整代码格式,文章数据增加thumb字段。
* v0.1 (17-01-08 [@jzwalk](https://github.com/jzwalk))
* 随公子GitHub仓库作品引入Typecho-Fans目录,产生社区维护版;
* upgrade接口改注释提高安全性,未改动版本号。
* v0.1 (14-10-12 [@lizheming](https://github.com/lizheming))
* 原作在GitHub发布。

## 贡献作者

[![jzwalk](https://avatars1.githubusercontent.com/u/252331?v=3&s=100)](https://github.com/jzwalk) | [![hkq15](https://portrait.gitee.com/uploads/avatars/user/526/1579006_hkq15_1578955047.png!avatar100)](https://github.com/hkq15) | [![insoxin](https://avatars1.githubusercontent.com/u/19371836?v=3&s=100)](https://github.com/insoxin) | [![szj1006](https://avatars1.githubusercontent.com/u/9147062?v=3&s=100)](https://github.com/szj1006) | [![lizheming](https://avatars1.githubusercontent.com/u/424491?v=3&s=100)](https://github.com/lizheming)
:---:|:---:|:---:|:---:|:---:
[jzwalk](https://github.com/jzwalk) (2020) | [hkq15](https://gitee.com/hkq15) (2019) | [insoxin](https://github.com/insoxin) (2018) | [szj1006](https://github.com/szj1006) (2017) | [lizheming](https://github.com/lizheming) (2014)

*为避免作者栏显示过长,插件信息仅选取登记3个署名,如有异议可协商修改。

## 附注/链接

本社区维护版已包含以下各版本的可用增量功能:

* [精简版(hkq15)](https://gitee.com/hkq15/Typecho-api/tree/master/JSON) - 文章接口增加作者数据输出。
* [小程序版(insoxin)](https://github.com/insoxin/typecho-json-miniprogram) - 进一步简化接口字段,增加面板设置(效用不明)。
* [精简版(szj1006)](https://github.com/szj1006/typecho-api) - 去除部分接口,增加缩略图字段。
* [原版](https://github.com/lizheming/JSON) - 实现各接口数据输出功能。

欢迎社区成员继续贡献代码参与更新。

本插件最初仅为测试用功能实现较简易,安全性等考虑更加周密的同类插件推荐[Restful](https://github.com/moefront/typecho-plugin-Restful)(支持写入接口)。

## 授权协议

TF目录下的社区维护版作品如果没有明确声明,默认采用与Typecho相同的[GPLv2](https://github.com/typecho/typecho/blob/master/LICENSE.txt)开源协议。(要求提及出处,保持开源并注明修改。)

###### 示例可参考作者博客:https://imnerd.org/api/posts
> JSON原作未附协议声明,原作者保留所有权利。 © [公子](https://github.com/lizheming)
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@
[GoodLuck](GoodLuck) | “手气不错”链接显示随机文章插件 | 1.0.2 | [Ryan](https://github.com/benzBrake) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-D_to_G/GoodLuck.zip)
[GoogleAnalytics](GoogleAnalytics) | 异步加载Google Analytics统计插件 | 1.0.0 | [WeiCN](https://github.com/naicfeng) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-D_to_G/GoogleAnalytics_WeiCN.zip)
[GoogleCodePrettify](GoogleCodePrettify) | 谷歌Prettify代码高亮效果渲染插件 | 2.0.0 | [公子](https://github.com/lizheming) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-D_to_G/GoogleCodePrettify.zip)
[GravatarCache](GravatarCache) | Gravatar头像本地自动缓存插件 | 2.0.2 | [Byends](https://github.com/visamz) | [Special](https://github.com/typecho-fans/plugins/releases/download/plugins-D_to_G/GravatarCache.zip)
[GravatarCache](GravatarCache) | Gravatar头像自动在本地缓存插件 | 2.0.2 | [Byends](https://github.com/visamz) | [Special](https://github.com/typecho-fans/plugins/releases/download/plugins-D_to_G/GravatarCache.zip)
[Hermit](Hermit) | 虾米云音乐SM2列表型播放器插件 | 1.3.1 | [mufeng](https://github.com/iMuFeng) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/Hermit.zip)
[HighSlide](HighSlide) | 多功能灯箱弹窗效果+页面相册插件 | 1.4.7 | [羽中](https://github.com/jzwalk) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/HighSlide.zip)
[HighSlide](HighSlide) | 图片灯箱弹窗效果+页面相册插件 | 1.4.7 | [羽中](https://github.com/jzwalk) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/HighSlide.zip)
[Html2Text](Html2Text) | Html代码转Markdown格式插件 | 0.1.0 | [冰剑](https://github.com/binjoo) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/Html2Text.zip)
[IQapTcha](IQapTcha) | 评论滑动解锁验证+规则过滤插件 | 1.1.2 | [Byends](https://github.com/visamz) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/IQapTcha.zip)
[JSON](JSON) | Json格式输出博客数据API插件 | 0.1 | [公子](https://github.com/lizheming) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/JSON.zip)
[JSON](JSON) | 以Json格式输出博客数据API插件 | 1.1 | [姬长信](https://github.com/insoxin),[SangSir](https://gitee.com/hkq15),[公子](https://github.com/lizheming) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/JSON.zip)
[JWPlayer](JWPlayer) | Html5流媒体播放器Jwplayer插件 | 1.0.9 | [羽中](https://github.com/jzwalk) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/JWPlayer.zip)
[JustFeed](JustFeed) | 文章Feed尾部添加定制信息插件 | 0.1.2 | [eallion](https://github.com/eallion), jKey | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/JustFeed.zip)
[JustFeed](JustFeed) | 在文章Feed尾部添加定制信息插件 | 0.1.2 | [eallion](https://github.com/eallion), jKey | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/JustFeed.zip)
[Keywords](Keywords) | 文章指定关键词自动添加链接插件 | 1.0.8 | [羽中](https://github.com/jzwalk) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/Keywords.zip)
[LREditor](LREditor) | Markdown编辑器左右预览插件 | 0.0.4 | [公子](http://github.com/lizheming) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/LREditor.zip)
[Like](Like) | Typecho文章点赞与排行统计插件 | 1.0.1 | [skylzl](https://github.com/xiaogouxo) | [Download](https://github.com/typecho-fans/plugins/releases/download/plugins-H_to_L/Like.zip)
Expand Down
Binary file modified ZIP_CDN/JSON.zip
Binary file not shown.

0 comments on commit 8920dd3

Please sign in to comment.