Last active
November 26, 2020 15:10
-
-
Save benzBrake/2ff9ff79100ff2ee0e01cf9a71ee77fb to your computer and use it in GitHub Desktop.
like single for typecho | Typecho 免插件文章点赞
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function likeSingle($cid) { | |
$db = likeSingleDb(); | |
if ($db == null) Typecho_Response::throwJson(array('status'=>-1,'msg'=>'数据库有误!')); | |
$prefix = $db->getPrefix(); | |
if(!cid) Typecho_Response::throwJson(array('status'=>0,'msg'=>'请选择喜欢的文章!')); | |
$likes = Typecho_Cookie::get('likes'); | |
if(empty($likes)){ | |
$likes = array(); | |
} else { | |
$likes = explode(',', $likes); | |
} | |
if(!in_array($cid,$likes)){ | |
$row = $this->db->fetchRow($this->db->select('table.contents.likes')->from('table.contents')->where('cid = ?', $cid)->limit(1)); | |
$this->db->query($this->db->update('table.contents')->rows(array('table.contents.likes' => (int)$row['likes']+1))->where('cid = ?', $cid)); | |
array_push($likes,$cid); | |
$likes = implode(',', $likes); | |
Typecho_Cookie::set('likes',$likes); | |
Typecho_Response::throwJson(array('status'=>1,'msg'=>'点赞成功')); | |
} | |
} | |
function likeSingleDb() { | |
$db = Typecho_Db::get(); | |
$type = explode('_', $db->getAdapterName()); | |
$type = array_pop($type); | |
$prefix = $db->getPrefix(); | |
try { | |
$select = $db->select('table.contents.likes')->from('table.contents'); | |
$db->query($select, Typecho_Db::READ); | |
return $db; | |
} catch (Typecho_Db_Exception $e) { | |
if(('Mysql' == $type && (1054 == $code || '42S22' == $code)) || ('SQLite' == $type && ('HY000' == $code || 1 == $code))) { | |
try { | |
if ('Mysql' == $type) { | |
$db->query("ALTER TABLE `".$prefix."contents` ADD `likes` INT(8) NOT NULL DEFAULT 0;"); | |
} else if ('SQLite' == $type) { | |
$db->query("ALTER TABLE `".$prefix."contents` ADD `likes` Integer NOT NULL DEFAULT 0"); | |
} else { | |
return null; | |
} | |
return $db; | |
} catch (Typecho_Db_Exception $e) { | |
return null; | |
} | |
} | |
} | |
return null; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
如何调用呢