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

223 lines
10 KiB
PHP
Executable File

<?php
namespace app\index\controller;
use think\Loader;
use think\Cookie;
use think\Config;
class Article extends BaseController {
public function lists() {
$arg_where = ['a.siteid' => $this->siteid, 'country_code' => $this->country_code];
$arg_order = ['a.id' => 'desc'];
$search = array();
$skeyword = $this->request->get('skeyword', '', 'urldecode,strval');
if ($skeyword != '') {
$search['skeyword'] = $skeyword;
$arg_where['a.name'] =['like', '%' . $search['skeyword'] . '%'];
$search['name'] = $search['skeyword'];
}
$arg_field = ['a.id', 'a.cid', 'a.name', 'a.jump_link', 'a.sort', 'a.headline', 'a.ishot', 'a.recommend', 'a.writer', 'a.source', 'a.viewcount', 'a.zancount', 'a.commentcount', 'a.description', 'a.picture', 'a.tags', 'a.createtime', 'c.id' => 'categoryid', 'c.name' => 'categoryname'];
$dataObject = Loader::model('Article')->getCateArticleLists($arg_where, $arg_order, $arg_field, 12);
$value = [
'list' => $dataObject->isEmpty() ? null : $dataObject->items(), //$dataObject->getCollection()->toArray()
'page' => $dataObject->render(),
'category' => ['id' => 0, 'name' => '新闻资讯'],
];
$cate_list = model('article_category')->where(['country_code' => $this->country_code, 'pid' => 0, 'isshow' => 1, 'stat' => 0])->order(['sort' => 'asc'])->select();
$value['cate_list'] = $cate_list;
$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);
$this->assign('search',$search);
return $this->fetch();
}
public function catelists($id = 0) {
if ($id > 0) {
$category = Loader::model('ArticleCategory')->getRow($id);
}
if (empty($category)) {
return exception('数据有误,请检查后再操作');
}
$arg_where = ['cid' => $id, 'a.country_code' => $this->country_code];
$skeyword = $this->request->get('skeyword', '', 'urldecode,strval');
if ($skeyword != '') {
$skeyword = trim($skeyword);
$search['skeyword'] = $skeyword;
$arg_where['a.name'] = ['like', '%' . $search['skeyword'] . '%'];
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
$value['search'] = $search;
}
switch ($category['classtype']) {
case 2:
$template = $category['templist'];
$arg_order = ['a.createtime' => 'desc'];
$arg_field = ['a.id', 'a.cid', 'a.name', 'a.jump_link', 'a.sort', 'a.headline', 'a.ishot', 'a.recommend', 'a.writer', 'a.source', 'a.viewcount', 'a.zancount', 'a.commentcount', 'a.description', 'a.picture', 'a.tags', 'a.createtime', 'c.id' => 'categoryid', 'c.name' => 'categoryname'];
$dataObject = Loader::model('Article')->getCateArticleLists($arg_where, $arg_order, $arg_field, 12);
$value = [
'list' => $dataObject->isEmpty() ? null : $dataObject->items(), //$dataObject->getCollection()->toArray()
'page' => $dataObject->render(),
];
break;
case 3:
header('location:' . $category['url']);
exit;
break;
default:
$template = $category['tempindex'];
break;
}
$cate_list = model('article_category')->where(['country_code' => $this->country_code, 'pid' => 0, 'isshow' => 1, 'stat' => 0])->order(['sort' => 'asc'])->select();
$value['cate_list'] = $cate_list;
$value['category'] = $category;
$value['seo_title'] = $category['seo_title']? : config('website_seo_title');
$value['seo_keyword'] = $category['seo_keyword']? : config('website_seo_keyword');
$value['seo_description'] = $category['seo_description']? : config('website_seo_description');
$this->assign($value);
return $this->fetch($template);
}
public function detail($id = 0) {
if ($id > 0) {
//$jssdk=new \JsSdk\JsSdk('wx0d4ac6d59abca442','8103022bb6ed902eb5c685800a78fd51');
//$signPackage = $jssdk->getSignPackage();
//$this->assign('singpackage',$signPackage);
$article = Loader::model('Article')->getRow($id);
if (empty($article)) {
return exception('数据有误,请检查后再操作');
}
//$addarticle = Loader::model('ArticleAddition')->getRow(['aid' => $article['id']]);
$category = Loader::model('ArticleCategory')->getRow($article['cid']);
if (empty($category)) {
return exception('数据有误,请检查后再操作');
}
$template = $category['tempdetail'];
//$prev_detail = Loader::model('Article')->getRow(['id' => ['gt', $id], 'cid' => $category['id'], 'stat' => 0], ['id', 'name'], ['id' => 'asc']);
//$next_detail = Loader::model('Article')->getRow(['id' => ['lt', $id], 'cid' => $category['id'], 'stat' => 0], ['id', 'name'], ['id' => 'desc']);
$value = [
'detail' => $article,
//'addarticle' => $addarticle,
'category' => $category,
//'prev_detail' => $prev_detail,
//'next_detail' => $next_detail,
];
//$topid = Loader::model('ArticleCategory')->getTopParentID($article['cid']);
//$value['topid'] = $topid;
$arg_where = ['content_id' => $id, 'stat' => 0, 'display' => 1];
$arg_order = ['createtime' => 'desc'];
$arg_field = ['*'];
$dataObject = Loader::model('Pinglun')->getPageList($arg_where, $arg_order, $arg_field, 5);
//header('content-type:text/html;charset=utf-8;');
$value['list'] = $dataObject->isEmpty() ? null : $dataObject->items();
$value['total'] = $dataObject->total();
$value['seo_title'] = $article['seo_title']? : $article['name'] . '-' . config('website_seo_title');
$value['seo_keyword'] = $article['seo_keyword']? : config('website_seo_keyword');
$value['seo_description'] = $article['seo_description']? : config('website_seo_description');
$this->assign($value);
$this->viewcount($id);
return $this->fetch($template);
} else {
return exception('数据有误,请检查后再操作');
}
}
public function zan($id = 0) {
$id = intval($id);
if ($id > 0) {
$article = Loader::model('Article')->getRow(['id' => $id], ['id', 'zancount']);
if (empty($article)) {
return $this->error('Error');
}
$article['zancount'] = $article['zancount'] + 1;
$result = $article->save();
if ($result) {
return $this->success('Zan', null, $article['zancount']);
} else {
return $this->error('Zan Error');
}
}
return $this->error('Error');
}
protected function viewcount($id) {
$view = Cookie::get('articleview', 'history'); //print_r($history);exit;
if (empty($view) || $view != $id) {
Loader::model('Article')->where(['id' => $id])->setInc('viewcount');
Cookie::set('articleview', $id, ['prefix' => 'history', 'expire' => 3600]);
}
}
protected function historyarticle($id) {
$article = Cookie::get('article', 'history'); //print_r($history);exit;
if (isset($article) && !empty($article)) {
$article_ids = explode(',', $article);
if ($article_ids[0] != $id) {
array_unshift($article_ids, $id);
$article_ids = array_unique($article_ids);
$num = Config::get('history_number') > 0 ? Config::get('history_number') : 10;
//$article_ids = array_slice($article_ids, 0, $num);
while (count($article_ids) > $num) {
array_pop($article_ids);
}
Cookie::set('article', implode(',', $article_ids), ['prefix' => 'history', 'setcookie' => true, 'expire' => 3600 * 24 * 30]);
}
} else {
Cookie::set('article', $id, ['prefix' => 'history', 'setcookie' => true, 'expire' => 3600 * 24 * 30]);
}
}
//添加留言 2024-03-01 Martin
public function addcomment()
{
$data = $this->request->post();
if (empty($data) || !is_array($data))
return $this->json(403, 'Data error ');
if (trim($data['name']) == '')
return $this->json(403-2, 'Name cannot be empty');
if (trim($data['email']) == '')
return $this->json(403-4, 'Email cannot be empty');
if (trim($data['comment']) == '')
return $this->json(403-5, 'Comment cannot be empty');
// if ($data['buy_source'] == '')
// return $this->json(-5, '购买渠道不能为空');
/*if ($data['customer_telephone'] != '' && !preg_match("/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/", $data['customer_telephone']))
return $this->json(-6, 'Phone format error ');*/
if ($data['email'] != '' && !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $data['email']))
return $this->json(403-6, 'Email Address format error');
$insert_data = [
'article_id' => trim($data['article_id']),
'name' => trim($data['name']),
'email' => trim($data['email']),
'content' => trim($data['comment']),
'add_time' => date("Y-m-d H:i:s",time()),
'siteid' => $this->siteid,
'country_code' => $this->country_code,
];
$result = model('article_comment')->insert($insert_data);
if (!$result)
return $this->json(201, 'Failure to submit');
return $this->json(200, 'ok');
}
}