1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
<?php

namespace app\api\controller;

use addons\cms\model\Archives;
use addons\cms\model\Block;
use app\admin\model\CmsLikeLog;
use app\admin\model\PriceStatus;
use app\admin\model\ReadLog;
use app\admin\model\SearchLog;
use app\common\controller\Api;
use addons\cms\model\Diydata;
use addons\cms\model\Modelx;
use function PHPSTORM_META\type;
use think\Config;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use addons\cms\library\OrderException;
use addons\cms\model\Archives as ArchivesModel;
use addons\cms\model\Channel;
use addons\cms\model\Comment;
use addons\third\model\Third;
use think\exception\HttpResponseException;
use Yansongda\Pay\Exceptions\GatewayException;


/**
 * 荐读好看接口
 */
class Cms extends Api
{
    protected $noNeedLogin = ['getAllArticle','getbanner','index','getChanel','getArticleByChanel','detail','getAdvertising','getADInfo','search','getADInfo'];
    protected $noNeedRight = ['*'];

    /**
     * 推荐信息接口
     * @param string $model_id 1:文章4:视频
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "资讯列表",
    "data": {
    "total": "总数量",
    "per_page": "每页数量",
    "current_page": "当前页码",
    "last_page": "总页码",
    "data": [
    {
    "id": "文章ID",
    "title": "文章标题",
    "image": "缩略图",
    "view": "浏览数",
    "comments": "评论数",
    "likes": "点赞量",
    "update_date": "更新时间"
    }
    ]
    }
    })
     */
    public function index()
    {
        $Archives=new Archives();
        $model_id=input('model_id');
        $tag['model_id']=$model_id;
        $tag['deletetime']=null;
        if (empty($tag['model_id'])){
            $this->error('必要参数不能为空!');
        }
        $tag['flag']='recommend';
        $orgin_list= $Archives
            ->where($tag)
            ->order('updatetime','desc')
            ->paginate(10)
            ->toArray();
        $this->success($orgin_list);
    }

    private function index1($model_id)
    {
        $Archives=new Archives();
        $tag['model_id']=$model_id;
        $tag['deletetime']=null;
        $tag['flag']='recommend';
        $orgin_list= $Archives
            ->where($tag)
            ->order('updatetime','desc')
            ->paginate(10)
            ->toArray();
        $this->success('',$orgin_list);
    }

    private function index1bak($model_id){
        $Archives=new Archives();
        $tag['model_id']=$model_id;
        $tag['deletetime']=null;
        $tag['flag']='recommend';
        $orgin_list= $Archives
            ->where($tag)
            ->order('updatetime','desc')
            ->select()
            ->toArray();
        return $orgin_list;
    }

    /**
     * 获取不同分类下的栏目
     * @param string $channel_id 8:荐读9:好看
     */
    public function getChanel(){
        $channel_id=input('channel_id/d');
        $where['parent_id']=$channel_id;
        $where['status']=['<>','hidden'];
        $channelList = \addons\cms\model\Channel::where($where)
            ->where('type', 'list')
            ->order('weigh DESC,id DESC')
            ->column('id,name');
        $return=[];
        $i=0;
        if (!empty($channelList)){
            foreach ($channelList as $k=>$v){
                $return[$i]['name']=$v;
                $return[$i]['list_id']=$k;
            $i++;
            }
        }
        $this->success(__('读取成功'),$return);
    }
    /**
     * 获取所有文章列表
     *
     */
    public function getAllArticle(){
        $channel_id=8;
        $where['parent_id']=$channel_id;
        $where['status']=['<>','hidden'];
        $channelList = \addons\cms\model\Channel::where($where)
            ->where('type', 'list')
            ->order('weigh DESC,id DESC')
            ->column('id,name');
        $return=[];
        $i=0;
        if (!empty($channelList)){
            $return[$i]['name']='推荐';
            $return[$i]['article_list']= $this->index1bak(1);
            $i++;
            foreach ($channelList as $k=>$v){
                $return[$i]['name']=$v;
                $return[$i]['list_id']=$k;
               $return[$i]['article_list']=$this->getArticleList($k);
                $i++;
            }
        }
        $this->success('',$return);
    }





    private function getArticleList($list_id){
        $Archives=new Archives();
        $tag['channel_id']=$list_id;
        $tag['deletetime']=null;
        $orgin_list= $Archives
            ->where($tag)
            ->order('updatetime','desc')
            ->select()
            ->toArray();
        return $orgin_list;
    }

    /**
     * 获取不同栏目下的文章
     * @param string $list_id 17:中医
     * @param string $model_id 1:文章4:视频(推荐信息)
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "资讯列表",
    "data": {
    "total": "总数量",
    "per_page": "每页数量",
    "current_page": "当前页码",
    "last_page": "总页码",
    "data": [
    {
    "id": "文章ID",
    "title": "文章标题",
    "image": "缩略图",
    "view": "浏览数",
    "comments": "评论数",
    "likes": "点赞量",
    "update_date": "更新时间"
    }
    ]
    }
    })
     */
    public function getArticleByChanel(){
        $Archives=new Archives();
        $list_id=input('list_id/d');
        $model_id=input('model_id/d');
        if (empty($list_id) && empty($model_id)){
            $this->error('缺少必要参数');
        }
        if(!empty($model_id)){
            $this->index1($model_id);
        }
        $tag['channel_id']=$list_id;
        $tag['deletetime']=null;
        $orgin_list= $Archives
            ->where($tag)
            ->order('updatetime','desc')
            ->paginate(10)
            ->toArray();
        if (empty($orgin_list)){
            $this->error('数据为空');
        }
        $this->success('',$orgin_list);
    }

    /**
     * @ApiTitle    (文章详情)
     * @ApiSummary  (文章详情)
     * @ApiMethod (POST)
     * @ApiSector   (三开相关接口)
     * @ApiParams   (name="id", type="integer", required=true, description="文章id 1")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturnParams   (name="read_score", type="object", description="获得积分数(read_score 为空 null时不弹框)")
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "资讯列表",
    "data": [
    {
    "title": "文章标题",
    "is_like": "是否点赞了",
    "description": "简介",
    "content": "图文详情",
    "views": "点击量",
    "comments": "评论数",
    "likes": "点赞量",
    "updatetime_text": "2019-09-23",
    "commentList": [
     "id":评论id
     "user_id":"用户id",
     "content":"回复内容",
     "user":[
        "id":id,
        "nickname":"用户昵称",
        "avatar":"头像"
     ],
     "createtime_text":"评论时间",
     ]
    }
    ]
    }
    })
     */
    public function detail()
    {
        $user_id=$this->auth->id;
        $article_id=input('id');
        if (!empty($user_id)){
           $Log=new ReadLog();
           $get_score=$Log->checkDailyRead($user_id,$article_id);
           $data['user_id']=$user_id;
           $data['article_id']=$article_id;
           $data['date']=date('Y-m-d');
           ReadLog::create($data);
        }


        $action = $this->request->post("action");
        if ($action && $this->request->isPost()) {
            return $this->$action();
        }
        $diyname = $this->request->param('diyname');
        if ($diyname && !is_numeric($diyname)) {
            $archives = ArchivesModel::getByDiyname($diyname);
        } else {
            $id = $diyname ? $diyname : $this->request->request('id', '');
            $archives = ArchivesModel::get($id);
        }
        if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
            $this->error(__('No specified article found'));
        }
        $channel = Channel::get($archives['channel_id']);
        if (!$channel) {
            $this->error(__('No specified channel found'));
        }
        $model = Modelx::get($channel['model_id']);
        if (!$model) {
            $this->error(__('No specified model found'));
        }
        $archives->setInc("views", 1);
        $addon = db($model['table'])->where('id', $archives['id'])->find();
        if ($addon) {
            if ($model->fields) {
                $fieldsContentList = $model->getFieldsContentList($model->id);
                //附加列表字段
                array_walk($fieldsContentList, function ($content, $field) use (&$addon) {
                    $addon[$field . '_text'] = isset($content[$addon[$field]]) ? $content[$addon[$field]] : $addon[$field];
                });
            }
            $archives->setData($addon);
        } else {
            $this->error(__('No specified article addon found'));
        }
        //小程序付费阅读将不可见
        $content = $archives->content;
        if ((isset($archives->price) && $archives->price <= 0) || $archives->is_paid_part_of_content || $archives->ispaid) {
            $value = $archives->getData('content');
            $value = str_replace(['##paidbegin##', '##paidend##'], ['<paid>', '</paid>'], $value);
            $value = str_replace(['$$paidbegin$$', '$$paidend$$'], ['<paid>', '</paid>'], $value);
            $pattern = '/<paid>(.*?)<\/paid>/is';
            if (preg_match($pattern, $value) && !$archives->ispaid) {
                $value = preg_replace($pattern, "<div class='alert alert-warning' style='background:#fcf8e3;border:1px solid #faf3cd;color:#c09853;padding:8px;'>付费内容已经隐藏,请付费后查看</div>", $value);
            }
            $content = $value;
        } else {
            if (isset($archives->price) && $archives->price > 0 && !$archives->ispaid) {
                $content = "<div class='alert alert-warning alert-paid' style='background:#fcf8e3;border:1px solid #faf3cd;color:#c09853;padding:8px;'>此文章为付费文章,需要支付¥{$archives->price}元才能查看</div>";
            }
        }
        

        $archives = array_merge($archives->toArray(), $addon);
        $archives['content'] = $content;
        $Price=new PriceStatus();

        if (!empty($archives['video'])){
            $archives['video'] = cdnurl($archives['video']);
            $Price->addStatus($user_id,'two');
        }else{
            $Price->addStatus($user_id,'one');
        }
        //$archives['updatetime_text']=date('Y-m-d',$archives['updatetime']);

        $commentList = Comment::getCommentList(['aid' => $archives['id']]);
        $commentList = $commentList->getCollection();
        foreach ($commentList as $index => &$item) {
            if ($item['user']) {
                $item['user']['avatar'] = cdnurl($item['user']['avatar'], true);
                $item['createtime_text'] =date('Y-m-d',$item['createtime']);
            }
        }
        $this->request->token();
        $channel = $channel->toArray();
        $channel['url'] = $channel['fullurl'];

        if (!empty($user_id)){
            $map_log['user_id']=$user_id;
            $map_log['article_id']=$article_id;
            $log=CmsLikeLog::get($map_log);
            if (empty($log)){
                $archives['is_like']=0;
            }else{
                $archives['is_like']=1;
            }
            $archives['read_score']=$get_score;
            unset($channel['channeltpl'], $channel['listtpl'], $channel['showtpl'], $channel['status'], $channel['weigh'], $channel['parent_id']);
        }

        $this->success('', ['archivesInfo' => $archives,'commentList' => $commentList]);
    }

    /**
     * @ApiTitle    (评论文章)
     * @ApiSummary  (评论文章)
     * @ApiMethod (POST)
     * @ApiParams   (name="aid", type="integer", required=true, description="文章id")
     * @ApiParams   (name="content", type="integer", required=true, description="回复内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     */
    public function post()
    {
        $params['aid']=input('aid/d');
        $params['content']=input('content');
        $params['type']='archives';
        $check_result=checkText($params['content']);
        if ($check_result!='ok'){
            $this->error('发送内容涉及敏感词汇,请重新填写');
        }
        try {
            $params = $this->request->post();
            \addons\cms\model\Comment::postComment($params);
        } catch (CommentException $e) {
            $this->success($e->getMessage());
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success(__('评论成功'));
    }

    public function zan(){
        $params['aid']=input('aid/d');
    }


    /**
     * @ApiTitle    (获取广告)
     * @ApiSummary  (获取广告)
     * @ApiMethod (POST)
     * @ApiParams   (name="type", type="integer", required=true, description="1:边栏广告2:区块广告")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "资讯列表",
    "data": {
    "id": "广告id",
    "title": "标题",
    "image": "广告图片",
    }
    })
     */
    public function getAdvertising(){
        $Block=new Block();
        $where['']='sidebarad';
        $type=input('type');
        $where['limit']='1';
        switch ($type){
            case 1:
                $where['condition']['type']='边栏';
                $where['limit']='5';
                break;
            case 2:
                $where['condition']['type']='区块';
                break;
            default:
                $where['condition']['type']='边栏';
                $where['limit']='5';
        }
        $where['cache']=true;
        $where['orderby']='rand';
        $block=$Block->getBlockList($where);
        $table_bar[0]='/pages/index/index';
        $table_bar[1]='/pages/clock/clock';
        $table_bar[2]='/pages/square/square';
        $table_bar[3]='/pages/center/center';

        if (!empty($block)){
            foreach ($block as $k=>$v){
                $block[$k]['url_type']=0;
                foreach ($table_bar as $k1=>$v1){
                    if ($v1==$v['url']){
                        $block[$k]['url_type']=1;
                    }
                }
            }
        }
        $this->success('',$block);
    }

    /**
     * @ApiTitle    (获取轮播图)
     * @ApiSummary  (获取轮播图)
     * @ApiMethod (POST)
     * @ApiParams   (name="type", type="integer", required=true, description="1:荐读列表轮播图2:荐读详情轮播图3:广场轮播图4:日记轮播图5:养生群轮播图")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "资讯列表",
    "data": {
    "id": "广告id",
    "title": "标题",
    "image": "图片",
    "url_type": "1:菜单栏链接2:其他连接",
    }
    })
     */
    public function getbanner(){
        $Block=new Block();
        $where['']='sidebarad';
        $type=input('type');
        $where['limit']='1';
        switch ($type){
            case 1:
                $where['condition']['type']='荐读列表轮播图';
                $where['limit']='5';
                break;
            case 2:
                $where['condition']['type']='荐读详情轮播图';
                $where['limit']='5';
                break;
            case 3:
                $where['condition']['type']='广场轮播图';
                $where['limit']='5';
                break;
            case 4:
                $where['condition']['type']='日记轮播图';
                $where['limit']='5';
                break;
            case 5:
                $where['condition']['type']='养生群轮播图';
                $where['limit']='5';
                break;
            default:
              $this->error('类型值错误!');
        }
        $where['cache']=true;
        $where['orderby']='rand';
        $block=$Block->getBlockList($where);
        $table_bar[0]='/pages/index/index';
        $table_bar[1]='/pages/clock/clock';
        $table_bar[2]='/pages/square/square';
        $table_bar[3]='/pages/center/center';

        if (!empty($block)){
             foreach ($block as $k=>$v){
                 $block[$k]['url_type']=0;
                 foreach ($table_bar as $k1=>$v1){
                     if ($v1==$v['url']){
                         $block[$k]['url_type']=1;
                     }
                 }
             }
        }
        $this->success('',$block);
    }




    /**
     * @ApiTitle    (获取广告详情)
     * @ApiSummary  (获取广告详情)
     * @ApiMethod (POST)
     * @ApiParams   (name="id", type="integer", required=true, description="广告id")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "资讯列表",
    "data": {
    "id": "广告id",
    "title": "标题",
    "image": "广告图片",
     "content":"广告内容"
    }
    })
     */
    public function getADInfo(){
        $Block=new Block();
        $id=input('id/d');
        if (empty($id)){
            $this->error('缺少必要参数!');
        }
        $where['id']=$id;
        $info=$Block->getBlockContent($where);
        $this->success('',$info);
    }

    /**
     * @ApiTitle     (提交搜索信息)
     * @ApiSummary   (提交搜索信息)
     * @ApiMethod (POST)
     * @ApiParams   (name="search", type="string", required=true, description="搜索内容")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "资讯列表",
    "data": [
    {
    "id": "文章ID",
    "title": "文章标题",
    "image": "缩略图",
    "view": "浏览数",
    "comments": "评论数",
    "likes": "点赞量",
    "update_date": "更新时间"
    }
    ]
     */
    public function searchPost(){
        $userId=$this->auth->id;
        $search=input('search');
        if (empty($search)){
            $this->error('搜索内容不能为空!');
        }
        $SearchLog=new SearchLog();
        $SearchLog->addOne($userId,$search);
        $map['condition']['title|keywords']=['like',"%$search%"];
        $Archives=new Archives();
        $list=$Archives->getArchivesList($map);
        $return=[];
        if (!empty($list)){
           $return=$list->toArray();
           foreach ($return as $k=>$v){
               if ($v['model_id']==4){
                   $return[$k]['video']=1;
               }else{
                   $return[$k]['video']=0;
               }
           }
        }
        $this->success('',$return);
    }

    /**
     * @ApiTitle     (搜索页面显示信息)
     * @ApiSummary   (搜索页面显示信息)
     * @ApiMethod (POST)
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "资讯列表",
    "data": [
    {
    "self": "我的历史搜索记录",
    "hot": "热搜记录"
    }
    ]
     */
    public function search(){
        $userId=$this->auth->id;
        $SearchLog=new SearchLog();
        $return=$SearchLog->getSearchLog($userId);
        $this->success('',$return);
    }

    /**
     * @ApiTitle     (文章点赞)
     * @ApiSummary   (文章点赞)
     * @ApiMethod (POST)
     * @ApiParams   (name="id", type="integer", required=true, description="文章id")
     * @ApiReturn   ({
    "code":"状态码",
    "msg": "资讯列表",
    "data": [
    {
    "self": "我的历史搜索记录",
    "hot": "热搜记录"
    }
    ]
     */
    public function vote()
    {
        $id = (int)$this->request->post("id");
        $type ='like';
        if (!$id || !$type) {
            $this->error(__('Operation failed'));
        }
        $map['user_id']=$this->auth->id;
        $map['article_id']=$id;
        $isin=CmsLikeLog::get($map);
        if (empty($isin)){
            CmsLikeLog::create($map);
        }else{
            CmsLikeLog::get($map)->delete();
            $type='dislike';
        }
        $archives = ArchivesModel::get($id);
        if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
            $this->error(__('No specified article found'));
        }
        if ($type=='like'){
            $archives->where('id', $id)->setInc('likes');
        }else{
            $archives->where('id', $id)->setDec('likes');
        }
        $archives = ArchivesModel::get($id);
        $this->success(__('Operation completed'), ['likes' => $archives->likes, 'dislikes' => $archives->dislikes, 'likeratio' => $archives->likeratio]);
    }
}