<?php

namespace app\api\controller;

use app\admin\model\Company;
use app\common\model\Ads;
use app\common\model\Config;
use app\common\model\Menu;
use app\common\controller\Api;
use app\common\model\User;
use app\common\model\UserRelation;
use liuniu\UtilService;
use liuniu\WechatService;
use think\Hook;
use think\Request;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $banner = Ads::getbanner($this->cid);
        $bottom_menu = Menu::lst($this->cid, 29);
        $menu = Menu::lst($this->cid, 30);
        $all_ash = Config::where('name', 'all_ash')->value('value');
        $this->success('获取成功', compact('banner', 'bottom_menu', 'menu', 'all_ash'));
    }

    /**
     * 获取单位信息
     */
    public function company()
    {

        $rs = Company::where('id', $this->cid)->field('company_name,company_tel,sys_appid,wechat_appid,wechat_share_image,wechat_qrcode_image,routine_appid,share_title,share_info,wx_open_appid')->find();

        if ($rs) {
            $rs = $rs->toArray();
            $rs['all_ash'] = Config::where('name', 'all_ash')->value('value');
            $this->success('获取成功', $rs);
        }
    }

    public function wxconfig()
    {
        $this->success('获取成功', json_decode(WechatService::jsSdk(input('url'), $this->cid), true));
    }

    public function geocoder(Request $request)
    {
        list($latitude, $longitude) = UtilService::getMore(
            [
                ['latitude', ''],
                ['longitude', ''],
            ], $request, true
        );
        if ($latitude == '' || $longitude == '') $this->error('请先获取定位');
        $url = sprintf("https://apis.map.qq.com/ws/geocoder/v1/?location=%s,%s&key=VYZBZ-P2TRG-RMIQ3-ITAIN-2DKBK-CKFQQ", $latitude, $longitude);
        $rs = file_get_contents($url);
        $this->success('获取成功', json_decode($rs, true));
    }

    public function MiniScheme(Request $request)
    {
        $param = UtilService::postMore(
            [
                ['path', ''],
                ['query', ''],
            ], $request
        );
        $this->success(UtilService::getMiniScheme($this->cid, $param));
    }

    public function Minilink(Request $request)
    {
        $param = UtilService::postMore(
            [
                ['path', ''],
                ['query', ''],
            ], $request
        );
        $this->success(UtilService::getMiniLink($this->cid, $param));
    }

    public function test()
    {
        $params = json_decode('["oH3Q06tuUH-yzGX0r-FM2V6ucNYA",{"openid":"oH3Q06tuUH-yzGX0r-FM2V6ucNYA","nickname":"\u4e0d\u6253\u81ea\u62db","sex":0,"language":"","city":"","province":"","country":"","headimgurl":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/AoXC8Dwxrsic913iaUibZgQcRMLCiaDg6T95xqe7KBXl0frpvV9z3UpKrhPxylxytVZyaCBFaiaJV8qR00BuIZG8X9w\/132","subscribe":0,"cid":2},0,1]', true);

        Hook::exec("\\app\admin\\behavior\\User", "WechatOauth", $params);
        $user = User::where('id', UserRelation::openidToUid("oH3Q06tuUH-yzGX0r-FM2V6ucNYA", 'openid'))->find();
        dump($user);
    }
}
