IndexController.php 2.1 KB
<?php
// +----------------------------------------------------------------------
// | TcComment [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 Tangchao All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tangchao <79300975@qq.com>
// +----------------------------------------------------------------------
namespace plugins\tc_comment\controller;
use cmf\controller\PluginBaseController;
use think\Db;
use app\user\model\UserModel;

class IndexController extends PluginBaseController
{
    function index()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $post   = $data['post'];
            if(empty($post['content'])) $this->error('评论不能为空!');
            $user_id = cmf_get_current_user_id();
            $User=new UserModel();
            $user=$User->getUserInfo();
            if(empty($user)) $this->error('未登录!');
            $admin = Db::name("comment")->where('user_id', $user['id'])->field("create_time")->order(["create_time" => "DESC"])->find();
            if(time()-$admin['create_time']<60) $this->error('1分钟内只能留言一次!');
            $post ['user_id']=$user['id'];
            $post ['full_name']= $user['user_nickname']?$user['user_nickname']:($user['user_login']?$user['user_login']:$user['mobile']);
            if($post ['full_name'] == $user['mobile']) $post ['full_name']=substr_replace($post ['full_name'], '****', 3, 4);
            $post ['email']=$user['user_email'];
            $post ['create_time']=time();
            $post ['url']=$_SERVER['HTTP_REFERER'];
            $result = Db::name('comment')->insertGetId($post);
            if ($result) {
                Db::name('portal_post')->where('id', $post['object_id'])->setInc('comment_count');
                addScore($user_id,20,'评论文章');
                $this->success('留言成功! 获得20积分!', url('portal/Article/index', ['id' => $post['object_id']]));
            }else{
                $this->error('数据传入失败!');
            }
        }
    }
}