<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 小程序公共API服务
 */
namespace app\citys\controller\api\v1;
use app\citys\controller\api\Base;
use app\citys\model\CitysPage;
use app\citys\model\Citys;
use app\citys\model\CitysFollow;
use app\citys\model\CitysCate;
use app\citys\model\CitysType;  
use app\citys\model\CitysAdwords;
use app\citys\model\CitysConfig;

class Index extends Base{

    /**
     * 获取配置参数
     * @return void
     */
    public function getconfig(){
        $this->apiSign();
        $result = CitysConfig::config($this->member_miniapp_id);
        if(empty($result)){
            return enjson(204);
        }else{
            return enjson(200,$result->top);
        }
    }

    /**
     * 读取微信API
     * @param integer 读取ID
     * @return json
     */
    public function getTpl(){
        $param['id'] = $this->request->param('id/d',0);
        $this->apiSign($param);
        $rel = CitysCate::cateTpl($param['id']);
        return enjson(200,$rel);
    } 

    /**
     * @return \think\response\Json
     * 站点统计
     */
    public function sitedata(){
        $this->apiSign();
        $data['all']    = rand(4000,9999);
        $data['online'] = rand(1000,3000);
        return enjson(200,$data);
    }
    
    /**
     * @return \think\response\Json
     * 文本内容
     */
    public function pages(){
        $this->apiSign();
        $rel = CitysPage::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
        if($rel){
            $rel->disclaimer = strip_tags(str_replace('<img', '<img class="img"',dehtml($rel->disclaimer)),"<b><strong><img><br><p><u><em><span>");
            $rel->agreement  = strip_tags(str_replace('<img', '<img class="img"',dehtml($rel->agreement)),"<b><strong><img><br><p><u><em><span>");
            $rel->fraud      = strip_tags(str_replace('<img', '<img class="img"',dehtml($rel->fraud)),"<b><strong><img><br><p><u><em><span>");
            $rel->about      = strip_tags(str_replace('<img', '<img class="img"',dehtml($rel->about)),"<b><strong><img><br><p><u><em><span>");
            return enjson(200,$rel);
        }
        return enjson(204);
    }

    /**
     * 广告图片
     * @return void
     */
    public function adwords(){
        $param['ids'] = $this->request->param('ids');
        $this->apiSign($param);
        $group = explode('/',$param['ids']);
        $adword = [];
        foreach ($group as $value) {
            $adword[$value] = [];
        }
        $rel = CitysAdwords::where(['member_miniapp_id' => $this->miniapp_id,'group' => $group])->field('title,picture,link,open_type,group')->order('sort desc,id desc')->select();
        foreach ($rel as $rs) {
            $adword[$rs['group']][] = $rs;
        }
        return enjson(200,$adword);
    }

    /**
     * 读取我关注的信息列表
     * @param integer 读取ID
     * @return json
     */
    public function index(){
        $param['page']  = $this->request->param('page/d',1);
        $this->apiSign($param);
        $where = [];
        $where['member_miniapp_id'] = $this->member_miniapp_id;
        $where['is_lock']           = 0;
        $order = 'is_top desc,sort desc,id desc';
        if($this->user){
            $order = 'sort desc,id desc';
            $uid = CitysFollow::where(['uid' => $this->user->id])->column('like_uid');
            if(!empty($uid)){
                $where['uid'] = $uid;
            }
        }
        $data = Citys::apiLists($where,$order,$this->user);
        if(empty($data) && $this->user){
            $data = Citys::apiLists(['member_miniapp_id' => $this->member_miniapp_id,'is_lock' => 1],$order,$this->user);
        }
        return enjson(empty($data)?204:200,$data);
    }
    
    /**
     * 读取微信API
     * @param integer 读取ID
     * @return json
     */
    public function cate(){
        $this->apiSign();
        $data = CitysCate::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->select()->toArray();
        return enjson(200,$data);
    }

    /**
     * 主题信息
     * @param integer 读取ID
     * @return json
     */
    public function cateInfo(){
        $param['cate_id'] = $this->request->param('cate_id/d');
        $this->apiSign($param);
        $data = CitysCate::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $param['cate_id']])->field('id,name')->find();
        return enjson(200,$data);
    }

    /**
     * 类型筛选
     */
    public function types(){
        $this->apiSign();
        $rel = CitysType::where(['member_miniapp_id' => $this->member_miniapp_id])->append(['name'])->order('sort desc,id desc')->select()->toArray();
        return enjson(200,$rel);
    }


    /**
     * 城市号
     * @param integer 读取ID
     * @return json
     */
    public function info(){
        $param['page']    = $this->request->param('page/d',1);
        $param['cate_id'] = $this->request->param('cate_id/d');
        $param['type_id'] = $this->request->param('type_id/d');
        $param['keyword'] = $this->request->param('keyword/s');
        $this->apiSign($param);
        $where = [];
        $where['member_miniapp_id'] = $this->member_miniapp_id;
        $where['is_lock']           = 0;
        if(!empty($param['cate_id'])){
            $where['cate_id'] = $param['cate_id'];
        }
        if(!empty($param['type_id'])){
            $where['type_id'] = $param['type_id'];
        }
        $data = Citys::apiLists($where,'is_top desc,sort desc,id desc',$this->user,$param['keyword']);
        return enjson(empty($data)?204:200,$data);
    }
}
