Skip to content

Commit

Permalink
add: 文章点赞
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanDecode committed Apr 27, 2019
1 parent 2b5cef8 commit 527e0a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
37 changes: 37 additions & 0 deletions Action.php
@@ -0,0 +1,37 @@
<?php
/**
* Action for VOID Plugin
*
* @author AlanDecode | 熊猫小A
*/

class VOID_Action extends Typecho_Widget implements Widget_Interface_Do
{
/**
* 点赞自增
*/
public function up(){
$db = Typecho_Db::get();
$cid=$this->request->filter('int')->cid;
if($cid){
try {
$row = $db->fetchRow($db->select('likes')
->from('table.contents')
->where('cid = ?', $cid));
$db->query($db->update('table.contents')
->rows(array('likes' => (int)$row['likes']+1))
->where('cid = ?', $cid));
$this->response->throwJson("success");
} catch (Exception $ex) {
echo $ex->getCode();
}
} else {
echo "error";
}
}

public function action(){
$this->on($this->request->is('up'))->up();
$this->response->goBack();
}
}
7 changes: 6 additions & 1 deletion Plugin.php
Expand Up @@ -39,6 +39,11 @@ public static function activate()
// 注册 hook
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->finishPublish = array('VOID_Plugin', 'updateWordCount');

/** 点赞相关 */
// 创建字段
if (!array_key_exists('likes', $db->fetchRow($db->select()->from('table.contents'))))
$db->query('ALTER TABLE `'. $prefix .'contents` ADD `likes` INT(10) DEFAULT 0;');
Helper::addAction('void_like', 'VOID_Action');
}

/**
Expand All @@ -51,7 +56,7 @@ public static function activate()
*/
public static function deactivate()
{

Helper::removeAction("void_like");
}

/**
Expand Down
2 changes: 2 additions & 0 deletions libs/WordCount.php
Expand Up @@ -2,6 +2,8 @@
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 字数统计相关
*
* @author AlanDecode | 熊猫小A
*/

class VOID_WordCount
Expand Down

0 comments on commit 527e0a3

Please sign in to comment.