第十三课 插件开发–增加文章评论

最后更新于:2022-04-01 20:42:23

>[danger]上节课我们讲了如何开发插件,这一节课我们用一个实例来讲解如何开发一个评论插件 有些做过其他系统的插件开发的同学,再来学iWebShop的插件开发,很容易就上手,有些也可能发现iWebShop插件开发时,对前端页面的控制目前支持得还不是很灵活。因此,我做这个评论插件,我就采用以下方法开发: 后端维护的功能,用插件实现,前端评论,使用我们之前学的模板开发的方式来做 ## 一、建立插件 在插件目录下增加“comment”目录,创建如下三个文件: ![](http://it.sunzoon.com/wp-content/uploads/2016/08/20160819193459.png) comment.php中加入以下代码: ~~~ class comment extends pluginBase{ //【必填】插件中文名称 public static function name() { return "三众科技评论插件"; } //【必填】插件的功能简介 public static function description() { return "文章评论插件"; } //安装插件代码 public static function install() { $commentDB = new IModel('article_comment');//表名article_comment //判断表是否存在 if($commentDB->exists()) { return true; } $data = array( "comment" => self::name(), "column" => array( "id" => array("type" => "int(11) unsigned",'auto_increment' => 1), "content" => array("type" => "text","comment" => "评论内容"), "create_time" => array("type" => "int(11) unsigned","default" => "0","comment" => "评论时间"), "recontents" => array("type" => "text","comment" => "回复内容"), "recomment_time" => array("type" => "int(11) unsigned","default" => "0","comment" => "回复时间"), "aid" => array("type" => "int(11) unsigned","default" => "0","comment" => "文章id"), "uid" => array("type" => "int(11) unsigned","default" => "0","comment" => "用户id") ), "index" => array("primary" => "id","key" => "aid,uid"), ); $commentDB->setData($data); //开始创建表 return $commentDB->createTable(); } //卸载插件代码 public static function uninstall() { $commentDB = new IModel('article_comment'); //删除表 return $commentDB->dropTable(); } //插件参数配置 public static function configName() { //在本拿了中无参数配置 } } ~~~ 之后在后台中就可以安装插件了 ![](http://it.sunzoon.com/wp-content/uploads/2016/08/20160819114735-1024x325.png)  以上为每个插件必须的,如不需要操作数据库的,可以不用写数据库代码。 ## 二、管理后台功能维护 功能注册,comment.php中加入以下代码: ~~~ public function reg() { //后台插件管理中增加该插件链接 plugin::reg("onSystemMenuCreate",function(){ $link = "/plugins/system_comment_list"; Menu::$menu["插件"]["插件管理"][$link] = $this->name(); }); //后台评论列表的链接 plugin::reg("onBeforeCreateAction@plugins@system_comment_list",function(){ self::controller()->system_comment_list = function(){$this->comment_list();}; }); //后台评论详情的链接 plugin::reg("onBeforeCreateAction@plugins@system_comment_edit",function(){ self::controller()->system_comment_edit = function(){$this->comment_edit();}; }); //后台评论回复的链接 plugin::reg("onBeforeCreateAction@plugins@system_comment_update",function(){ self::controller()->system_comment_update = function(){$this->comment_update();}; }); //后台评论删除的链接 plugin::reg("onBeforeCreateAction@plugins@system_comment_del",function(){ self::controller()->system_comment_del = function(){$this->comment_del();}; }); } ~~~ 功能实现,comment.php中加入以下代码: ~~~ public function comment_list() { $this->redirect('system_comment_list'); } public function comment_edit() { $id = IFilter::act(IReq::get('id')); $commentDB = new IQuery('article_comment as c'); $commentDB->join=" left join article as b on c.aid = b.id left join user as a on c.uid = a.id "; $commentDB->where=" c.id=$id "; $commentDB->fields ="c.id,FROM_UNIXTIME(c.create_time) as create_time,a.username,b.title,c.recomment_time,c.recontents,c.content"; $items = $commentDB->find(); $this->redirect('system_comment_edit',array('comment' => current($items))); } //回复评论 public function comment_update() { $id = IFilter::act(IReq::get('id')); $recontents = IFilter::act(IReq::get('recontents')); $data = array(); $data['recontents']=$recontents; $data['recomment_time']=time(); //保存数据库 $commentDB = new IModel('article_comment'); $commentDB->setData($data); $commentDB->update('id = '.$id); $this->redirect('system_comment_list'); } //回复删除 public function comment_del() { $commentDB = new IModel('article_comment'); $id = IFilter::act(IReq::get('id')); $commentDB->del('id = '.$id); $this->redirect('system_comment_list'); } ~~~ 后台管理页面,评论列表:system_comment_list.html ~~~
文章>评论管理
{set:$page= (isset($_GET['page'])&&(intval($_GET['page'])>0))?intval($_GET['page']):1;} {query: name=article_comment as c join=left join article as b on c.aid eq b.id left join user as a on c.uid eq a.id fields=c.id,FROM_UNIXTIME(c.create_time) as create_time,a.username,b.title,c.recomment_time page=$page order = c.id desc} {/query}
评论人 评论文章 评论时间 操作
{$item['username']} {$item['title']} {$item['create_time']} 查看 删除
{$query->getPageBar()}
~~~ 后台管理页面,评论详情:system_comment_edit.html ~~~
文章>查看评论
评论人: {$comment['username']}
评论时间:{$comment['create_time']}
评论文章:{$comment['title']}
评论内容:{$comment['content']}
回复评论:
~~~ 评论列表页: ![](http://it.sunzoon.com/wp-content/uploads/2016/08/20160819201409.png) 评论详情页面: ![](http://it.sunzoon.com/wp-content/uploads/2016/08/20160819201620.png) ## 三、前台功能 找到site\article,加入以下代码,用于显示评论 ~~~
{set:$where="c.aid=".$this->articleRow['id'];} {set:$page= (isset($_GET['page'])&&(intval($_GET['page'])>0))?intval($_GET['page']):1;} {query: name=article_comment as c join=left join user as a on c.uid eq a.id fields=c.id,c.content,FROM_UNIXTIME(c.create_time) as create_time,a.username,c.recomment_time,FROM_UNIXTIME(c.recomment_time) as reply_time,c.recontents page=$page pagesize=3 where=$where order = c.id desc}
{$item['username']}

评论内容:{$item['create_time']}

{$item['content']}

{if:$item['recomment_time']>0}

回复: {$item['reply_time']}

{$item['recontents']}

{/if}
{/query} {$query->getPageBar()}
评论内容:
~~~ ![](http://it.sunzoon.com/wp-content/uploads/2016/08/20160819202116.png) 打开site.php,加入以下代码 ~~~ public function article_comment_add() { if(!isset($this->user['user_id']) || !$this->user['user_id']) { IError::show(403,"未登录用户不能评论"); } if(!IReq::get('aid')) { IError::show(403,"传递的参数不完整"); } if(trim(IReq::get('content'))=="") { IError::show(403,"评论必须输入"); } $aid = IFilter::act(IReq::get('aid'),'int'); $data = array( 'content' => IFilter::act(IReq::get("content")), 'create_time' => time(), 'aid'=>$aid, 'uid'=>$this->user['user_id'], ); $tb_comment = new IModel("article_comment"); $tb_comment->setData($data); $re = $tb_comment->add(); if($re) { $this->redirect("/site/article_detail/id/".$aid); } else { IError::show(403,"评论失败"); } } ~~~ 以下代码用于实现评论提交功能。 ### 后记 以上只是举了一个例子说明如何做一个插件,还有前台的视图和控制器的代码实现,但就目前而言,iwebShop的插件还是比较适合做那些没界面的功能,或是在线留言等这些js控件的前端功能,一旦前台界面有交互,还是用非插件的模式实现比较方便点,每个工具都有优缺点,能实现功能的就是好工具 >[warning]如有不明白的地方,留言或是加入我们 “三众技术QQ交流群”一起讨论 ## 关于我们 >[danger][三众科技](http://www.sunzoon.com)资讯平台——大道至简,悦你所阅! >本教程由[三众简悦](http://it.sunzoon.com)原创,转载请注明出处,作者:bobball,由bobo整理成看云书籍 三众技术交流群:**543102562** 欢迎大家加入我们,共同讨论IT,互联网技术。同时可以扫描下面的二维码关注我们,谢谢! ![三众科技服务号](http://it.sunzoon.com/wp-content/uploads/2016/06/qrcode_for_gh_401d25b05314_344.jpg)
';