<?php

namespace Base\Domain\lib\WeChatOpen\WeChatLogin\Api;
use PhalApi\Exception\BadRequestException;

/**
 * Class AccessToken 根据code获取tokenid
 * @package Base\Domain\Authorization
 */
class AccessToken extends ApiBase {

    //接口
   protected $api = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";

    /**
     * 请求api接口
     * @return string
     */
    public function requestApi($appid, $secret, $code){
        $resultApi = sprintf($this->api, $appid, $secret, $code);
        $result = $this->curl->get($resultApi);

        if (!empty($result)){
            $result = json_decode($result, true);
            //错误码不为空
            if (!empty($result['errcode'])){
                throw new BadRequestException($result['errmsg']);
            }
        }

        return $result;
    }


}
