Files
orico-official-website-old/app/index/controller/Pinglun.php
2024-10-29 14:04:59 +08:00

97 lines
4.0 KiB
PHP
Executable File

<?php
namespace app\index\controller;
use think\Loader;
use think\Cookie;
use think\Config;
class Pinglun extends BaseController {
public function lists($type = '', $cid = 0) {
$arg_where = ['stat' => 0, 'display' => 1];
if ($type) {
$arg_where['typeid'] = $type;
}
if ($cid) {
$arg_where['content_id'] = $cid;
}
$arg_order = ['id' => 'desc'];
$arg_field = ['*'];
$dataObject = Loader::model('Pinglun')->getPageList($arg_where, $arg_order, $arg_field, 12);
//header('content-type:text/html;charset=utf-8;');
$value['list'] = $dataObject->isEmpty() ? null : $dataObject->items();
$value['page'] = $dataObject->render();
$value['total'] = $dataObject->total();
if ($this->request->isAjax()) {
return $this->result($value, 'pinglun', true);
}
$value['cid'] = $cid;
$value['type'] = ucfirst($type);
$value['seo_title'] = config('article_seo_title')? : config('website_seo_title');
$value['seo_keyword'] = config('article_seo_keyword')? : config('website_seo_keyword');
$value['seo_description'] = config('article_seo_description')? : config('website_seo_description');
$this->assign($value);
return $this->fetch('pinglun');
}
public function add() {
if (!$this->customer_id) {
return $this->result(['code' => false, 'msg' => '请登录后再进行评论'], false, '请登录后再进行评论');
}
if ($this->request->isPost()) {
$row = Loader::model('Pinglun')->getRow(['customer_id' => $this->customer_id, 'stat' => 0], null, ['createtime' => 'desc']);
if ($row && $row['id']) {
$createtime = strtotime($row['createtime']);
if (time() - $createtime < 60) {
return $this->result(['code' => false, 'msg' => '您已评论过了请不要重复评论'], false, '您已评论过了请不要重复评论');
}
}
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->error('未知错误');
}
$validaterule = [
'pid' => 'require',
'cid' => 'require',
'typeid' => 'require',
'content' => 'require',
];
$validatemsg = [
'pid.require' => 'p不能为空',
'cid.require' => 'c内容为空',
'typeid.require' => 'type不能为空',
'content.require' => '内容不能为空',
];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
// 验证失败 输出错误信息
return $this->error($valid_result);
}
$addtime = date('Y-m-d H:i:s');
$txarr = [1, 2, 3, 4, 5];
shuffle($txarr);
$set = [
'customer_id' => $this->customer_id,
'cname' => session('customer_auth.firstname'),
'typeid' => isset($data['typeid']) ? $data['typeid'] : 'Article',
'pid' => isset($data['pid']) ? $data['pid'] : 0,
'content_id' => isset($data['cid']) ? $data['cid'] : 0,
'content' => $data['content'],
'tx' => $txarr[0],
'createtime' => $addtime,
'lasttime' => $addtime,
];
$model = Loader::model('Pinglun')->insertRow($set);
if ($model && $model->getData('id')) {
isset($data['cid']) ? Loader::model('Article')->where(['id' => $data['cid']])->setInc('commentcount') : '';
return $this->success('成功!成功!等待管理员的审核后,方可显示', url('/'));
} else {
return $this->error('操作失败');
}
}
return $this->result(['code' => false, 'msg' => '未知错误'], false, '未知错误');
}
}