HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/www.kvrdp.com/addons/favorite/controller/Index.php
<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 收藏管理
// +----------------------------------------------------------------------
namespace addons\favorite\Controller;

use app\cms\model\Cms as Cms_Model;
use app\member\controller\MemberBase;
use think\Db;

class Index extends MemberBase
{
    protected $noNeedLogin = ['post'];

    public function index()
    {
        if ($this->request->isAjax()) {
            $limit = $this->request->param('limit/d', 10);
            $page  = $this->request->param('page/d', 10);
            $where = array('uid' => $this->userid);
            $total = Db::name('favorite')->where($where)->count();
            $_list = Db::name('favorite')->where($where)->page($page, $limit)->order("id", "DESC")->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = time_format($v['create_time']);
                $tid                      = explode("-", $v['tid']);
                $_list[$k]['url']         = buildContentUrl($tid[0], $tid[1]);
                $_list[$k]['catid']       = $tid[0];
                $_list[$k]['cid']         = $tid[1];
            }
            $result = array("code" => 0, "count" => $total, "data" => $_list);
            return json($result);
        }
        return $this->fetch();
    }

    public function post()
    {
        if (!\app\member\service\User::instance()->id) {
            $this->error('请先登录!');
        }
        $catid    = $this->request->param('catid/d', 0);
        $id       = $this->request->param('id/d', 0);
        $category = getCategory($catid);
        if (empty($category)) {
            $this->error('栏目不存在!');
        }
        $modelid   = $category['modelid'];
        $Cms_Model = new Cms_Model;
        $info      = $Cms_Model->getContent($modelid, "id={$id}");
        if (!$info) {
            $this->error('内容不存在!');
        }

        $model_cache = cache("Model");
        $tablename   = ucwords($model_cache[$modelid]['tablename']);

        $res = Db::name('favorite')->where('tid', $catid . '-' . $id)->where('uid', $this->userid)->find();
        if ($res) {
            //取消收藏
            Db::name('favorite')->where('tid', $catid . '-' . $id)->where('uid', $this->userid)->delete();
            Db::name($tablename)->where('id', $id)->setDec('favorite');
            $this->success("取消收藏成功!", '', ['num' => (int) $info['favorite'] - 1]);
        } else {
            //收藏
            Db::name('favorite')->insert([
                'tid'         => $catid . '-' . $id,
                'uid'         => $this->userid,
                'title'       => $info['title'],
                'create_time' => time(),
            ]);
            Db::name($tablename)->where('id', $id)->setInc('favorite');
            $this->success("收藏成功!", '', ['num' => (int) $info['favorite'] + 1]);
        }
    }
}