<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $this->success();
    }

    public function mini_code()
    {
        $scene = 'kongjianyuyue';
        $page_url = '/pages/active/reserve';

        $value = config('wxMiniProgram');
        $appid = $value['appid'];
        $secret = $value['secret'];
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
        $res = file_get_contents($url);

        $token = json_decode($res, true)['access_token'];
        $URL = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $token;
        $data = [
//            'scene' => '', //二维码传入参数
            'page' => $page_url, //扫码后进入页面
            'env_version' => 'trial', //要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
            'width' => 280,  //二维码的宽度,单位 px,最小 280px,最大 1280px
            'auto_color' => false, //自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
            'is_hyaline' => false, //是否需要透明底色,为 true 时,生成透明底色的小程序
            'check_path' => false,
        ];
        $json = json_encode($data); //数组加密
        //$result = $this->api_notice_increment($URL, $json); //用CURL 进行POST请求
        $result = curl_post($URL,$json);

        $path  = ROOT_PATH . 'public/uploads/minicode'; //ROOT_PATH 我使用的是TP5框架
        $path2 =                   '/uploads/minicode';

        if (!file_exists($path)) { //判断目录是否存在
            mkdir($path, 0777, true);
        }
        $path  =  $path . '/' . $scene . '.png'; //最后要写入的目录及文件名
        $path2 = $path2 . '/' . $scene . '.png'; //最后要写入的目录及文件名

        file_put_contents($path, $result);
        dump($path2);
        dump(httpurllocal($path2));

        echo '<html><body><img src="'.httpurllocal($path2).'"></body></html>';
    }

    /**
     * 首页
     */
    public function home(){

        //
        $home = Db::name('home')->order('id asc')->select();
        $home = list_domain_image($home,['image','remark_image']);

        $home_top = [];
        $home_button = [];
        foreach ($home as $k => $v){
            if($v['category'] == 1){
                $home_top[] = $v;
            }else{
                $home_button[] = $v;
            }
        }

        $zhengwu = Db::name('zhengwufuwu')->where('status',1)->order('weigh','desc')->select();

        $gonggao = Db::name('message_sys')->field('id,title,info,updatetime')->order('weigh','desc')->find();

        $zhengce = Db::name('zhengcejiedu')->field('id,title,image,audio_file,video_file,updatetime')->order('weigh','desc')->find();
        $zhengce = info_domain_image($zhengce,['image','audio_file','video_file']);
        //
        $result = [
            'index_logo' => localpath_to_netpath(config('site.index_logo')),
            'index_title' => config('site.index_title'),
            'index_title_info' => config('site.index_title_info'),
            'ysszd_logo' => localpath_to_netpath(config('site.ysszd_logo')),
            'ysszd_title' => config('site.ysszd_title'),
            'ysszd_title_info' => config('site.ysszd_title_info'),
            'ysszd_icon' => localpath_to_netpath(config('site.ysszd_icon')),
            'ysszd_url' => config('site.ysszd_url'),

            'home_top' => $home_top,
            'home_button' => $home_button,
            'zhengwu' => $zhengwu,
            'gonggao' => $gonggao,
            'zhengce' => $zhengce,
        ];
        $this->success(1,$result);
    }

    //便民页
    public function bianmin(){

        $cate = Db::name('bianmin_category')->order('id', 'asc')->select();
        $bianmin = Db::name('bianmin')->order('id', 'asc')->select();
        $bianmin = list_domain_image($bianmin, ['image','remark_image']);

        foreach ($cate as $key => $item) {
            foreach ($bianmin as $value) {
                if($item['id'] == $value['category_id']){
                    $cate[$key]['child'][] = $value;
                }
            }
        }

        $rs = [
            'index_logo' => localpath_to_netpath(config('site.index_logo')),
            'index_title' => config('site.index_title'),
            'index_title_info' => config('site.index_title_info'),
            'bianmin' => $cate,
        ];
        $this->success(1,$rs);
    }


}