This commit is contained in:
60
Zan/Action.php
Normal file
60
Zan/Action.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
class Zan_Action extends Typecho_Widget implements Widget_Interface_Do {
|
||||
protected $db;
|
||||
|
||||
public function __construct($request, $response, $params = NULL) {
|
||||
parent::__construct($request, $response, $params);
|
||||
$this->db = Typecho_Db::get();
|
||||
}
|
||||
|
||||
public function showZan($cid){
|
||||
$html = '<a href="javascript:;" class="post-zan" data-cid="';
|
||||
$html .= $cid;
|
||||
$html .= '">赞 (<span>';
|
||||
$html .= self::getZan($cid);
|
||||
$html .= '</span>)</a>';
|
||||
echo $html;
|
||||
}
|
||||
|
||||
private function getZan($cid){
|
||||
$exist = $this->db->fetchRow($this->db->select('int_value')->from('table.fields')->where('cid = ? AND name = ?', $cid, 'zan'));
|
||||
if (empty($exist)) {
|
||||
return 0;
|
||||
}else{
|
||||
return $exist['int_value'];
|
||||
}
|
||||
}
|
||||
|
||||
private function addZan($cid){
|
||||
$exist = $this->db->fetchRow($this->db->select('int_value')->from('table.fields')->where('cid = ? AND name = ?', $cid, 'zan'));
|
||||
$zan = array('cid'=>$cid, 'name'=>'zan', 'type'=>'int', 'str_value'=>NULL, 'int_value'=>1, 'float_value'=>0);
|
||||
$result = null;
|
||||
if (empty($exist)) {
|
||||
$result = $this->db->query($this->db->insert('table.fields')->rows($zan));
|
||||
} else {
|
||||
$zan['int_value'] = $exist['int_value'] + 1;
|
||||
$result = $this->db->query($this->db->update('table.fields')->rows($zan)->where('cid = ? AND name = ?', $cid, 'zan'));
|
||||
}
|
||||
|
||||
if($result){
|
||||
$cookie = Typecho_Cookie::get("__zan_cids");
|
||||
$cids = null;
|
||||
if($cookie){
|
||||
$cids = Json::decode($cookie, true);
|
||||
$cids[$cid] = isset($cids[$cid]) ? $cids[$cid] + 1 : 0;
|
||||
}else{
|
||||
$cids = array($cid=>0);
|
||||
}
|
||||
Typecho_Cookie::set("__zan_cids", Json::encode($cids));
|
||||
}
|
||||
}
|
||||
|
||||
public function action() {
|
||||
//if($this->request->isGet() && $this->request->is('cid')){
|
||||
if($this->request->isPost() && $this->request->is('cid')){
|
||||
$this->addZan($this->request->get('cid'));
|
||||
$this->response->throwJson(array('result' => 1, 'message' => _t('感谢您的赞!')));
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
46
Zan/Plugin.php
Normal file
46
Zan/Plugin.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* 赞一个
|
||||
*
|
||||
* @package Zan
|
||||
* @author 冰剑
|
||||
* @version 1.0.0
|
||||
* @link http://www.binjoo.net
|
||||
*/
|
||||
class Zan_Plugin implements Typecho_Plugin_Interface {
|
||||
public static function activate() {
|
||||
//Typecho_Plugin::factory('Widget_Archive')->header = array('Zan_Plugin', 'headlink');
|
||||
Typecho_Plugin::factory('Widget_Archive')->footer = array('Zan_Plugin', 'footlink');
|
||||
Helper::addAction('Zan', 'Zan_Action');
|
||||
}
|
||||
public static function deactivate(){
|
||||
Helper::removeAction('Zan');
|
||||
}
|
||||
public static function config(Typecho_Widget_Helper_Form $form){}
|
||||
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
|
||||
public static function headlink() {
|
||||
$css_url = Typecho_Common::url('Zan/css/Zan.min.css', Helper::options()->pluginUrl);
|
||||
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$css_url}\" media=\"all\" />\n";
|
||||
}
|
||||
public static function footlink() {
|
||||
//echo Typecho_Common::url('action/WeChat?customreply', '');
|
||||
$script = '<script type="text/javascript">//<![CDATA[
|
||||
window.jQuery || document.write("<script type=\"text/javascript\" src=\"http://cdn.staticfile.org/jquery/1.8.3/jquery.min.js\"><\/script>")//]]></script>
|
||||
';
|
||||
$script .= "<script type=\"text/javascript\">";
|
||||
$script .= '$(document).ready(function(){
|
||||
$(".post-zan").on("click", function(){
|
||||
var zan = $(this);
|
||||
$.post("' . Typecho_Widget::widget('Widget_Security')->getIndex('action/Zan') . '", {cid: zan.attr("data-cid")},function(data){
|
||||
if(data.result == 1){
|
||||
var val = zan.find("span").text();
|
||||
zan.find("span").text(parseInt(val) + 1);
|
||||
}
|
||||
//alert(data.message);
|
||||
}, "json");
|
||||
})
|
||||
})';
|
||||
$script .= "</script>\n";
|
||||
echo $script;
|
||||
}
|
||||
}
|
||||
18
Zan/README.md
Normal file
18
Zan/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
## 插件说明 ##
|
||||
版本: v1.0.0
|
||||
作者: [冰剑](https://github.com/binjoo)
|
||||
|
||||
使用自定义字段和jquery实现带动态计数的赞字链接。
|
||||
|
||||
> 添加jquery库CDN引用。
|
||||
|
||||
## 使用方法 ##
|
||||
|
||||
1. 下载插件
|
||||
2. 将插件上传到 /usr/plugins/ 这个目录下
|
||||
3. 启用当前插件
|
||||
4. 在模版post.php文件中你要插入`赞`的地方加入代码`<?php Typecho_Widget::widget('Zan_Action')->showZan($this->cid); ?>`
|
||||
|
||||
## 更新记录 ##
|
||||
####v1.0.0
|
||||
- 基本的赞功能
|
||||
Reference in New Issue
Block a user