<?php
namespace Base\Domain\lib\WeChatOpen\WeChatLogin;

use Base\Domain\lib\WeChatOpen\WeChatLogin\Api\AccessToken;
use Base\Domain\lib\WeChatOpen\WeChatLogin\Api\Code;
use Base\Domain\lib\WeChatOpen\WeChatLogin\Api\Userinfo;

class WeChatLogin {

    //微信公众号基础配置初始化
    public $weChatConfig = [];

    /**
     * 配置初始化
     */
    public function init($appid, $secret, $login_redirect_uri){
        $this->weChatConfig = [
            'appid'=>$appid,
            'secret'=>$secret,
            'login_redirect_uri'=>urlencode($login_redirect_uri)
        ];
    }

    /**
     * 获取微信code二维码
     *
     * @param $is_base64 boolean 是否返回base64还是链接模式
     *
     * @return string
     */
    public function getWeChatCode($state, $is_base64 = false, $scope = 'snsapi_login'){
       $result = (new Code())->requestApi($this->weChatConfig['appid'], $this->weChatConfig['login_redirect_uri'], $state, $scope);
        return $result;
    }

    /**
     * 获取AccessToken
     */
    public function getAccessToken($code){
      $result =  (new AccessToken())->requestApi($this->weChatConfig['appid'], $this->weChatConfig['secret'], $code);
      return $result;
    }

    /**
     * 获取微信个人信息
     */
    public function getUserinfo($access_token, $openid){
      $result =  (new Userinfo())->requestApi($access_token, $openid);
      return $result;
    }


}
