<?php

namespace app\controller\api;

use app\extra\basic\Base;
use app\extra\tools\DouyinUserInfo;
use app\model\system\SystemData;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;


#[Controller(prefix: "/wx_api/page")]
class Page extends Base
{


    #[Route(path: "about",methods: "get")]
    public function getAbout(Request $request)
    {
        try {
            $data = sConf("page.about");
            return json_encode(['code' => 1,'data' => $data],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    #[Route(path: "rule",methods: "get")]
    public function getRule(Request $request)
    {
        try {
            $data = sConf("page.rule");
            return json_encode(['code' => 1,'data' => $data]);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    #[Route(path: "privacy",methods: "get")]
    public function getPrivacy(Request $request)
    {
        try {
            $data = sConf("page.privacy");
//            return json_encode(['code' => 1,'data' => $data]);
            return successTrans("success.data",compact("data"));
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    #[Route(path: "agreements",methods: "get")]
    public function getAgreements(Request $request)
    {
        try {
            $data = sConf("page.agreements");
            return json_encode(['code' => 1,'data' => $data]);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 三级城市信息
     * @param Request $request
     * @return Response
     */
    #[Route(path: "city",methods: "get")]
    public function getCityData(Request $request): Response
    {
        try {
            $data = json_decode(file_get_contents(base_path()."/city.json"),true);
            return successTrans("success.data",$data);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 数据字典
     * @param Request $request
     * @return Response
     */
    #[Route(path: "dic",methods: "get")]
    public function getDicData(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "type.require"  => trans("empty.require")
            ],$request->all());
            if (!is_array($param)) return error($param);
            $data = (new SystemData)->where("code",$param['type'])->value("content");
            $data = !empty($data) ? json_decode($data,true) : [];
            return successTrans("success.data",$data);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    /**
     * 数据字典
     * @param Request $request
     * @return Response
     */
    #[Route(path: "dy",methods: "get")]
    public function getDyUserData(Request $request): Response
    {
        try {
            $url = "https://v.douyin.com/pVGt_Mbl_0Y/";
            $data = (new DouyinUserInfo)->getInfo($url);
            print_r($data);
            return successTrans("success.data",$data);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

}