<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 话题中心
 */
class Topichub extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function __construct(){
        exit;//简讯用不到这个
    }

    //话题列表
    public function lists(){
        $list = Db::name('topic_hub')->where('status',1)->order('weight desc,id desc')->autopage()->select();
        $list = list_domain_image($list,['image']);
        $this->success('success',$list);
    }

    //某个话题详情
    public function info(){
        $id   = input('id',0);
        $info = Db::name('topic_hub')->where(['status'=>1,'id'=>$id])->find();
        $info = info_domain_image($info,['image','bg_image']);

        //是否关注
        $is_follow = Db::name('user_follow_topic')->where(['uid'=>$this->auth->id,'topic_id'=>$id])->find();
        $info['is_follow'] = !empty($is_follow) ? 1 : 0;

        //增加话题浏览次数
        Db::name('topic_hub')->where(['status'=>1,'id'=>$id])->setInc('look_number');

        $this->success('success',$info);
    }

    //关注话题
    public function follow_one(){
        $topic_id = input('topic_id',0);
        if(!$topic_id){
            $this->error(__('Invalid parameters'));
        }

        $topic_hub = Db::name('topic_hub')->find($topic_id);
        if(empty($topic_hub)){
            $this->error('此话题不存在');
        }

        $map = [
            'uid' => $this->auth->id,
            'topic_id' => $topic_id,
        ];

        $check = Db::name('user_follow_topic')->where($map)->find();
        if($check){
            //取关
            $rs = Db::name('user_follow_topic')->where($map)->delete();
            $this->success('操作成功');
        }

        $id = Db::name('user_follow_topic')->insertGetId($map);

        $this->success('操作成功',$id);
    }




}