<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/4/25
 * Time: 下午10:50
 */
namespace thrid_author;

require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
require_once(BASE_ROOT_PATH . '/core/framework/function/http.php');

use util;
use Log;

class wxauthor
{
    const appid = 'wx6b42e00ecaade538';
    const appsecret ='ee64233b3144d76217161666f8cb4c86';

    const authorize_url = 'https://open.weixin.qq.com/connect/oauth2/authorize';
    const access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
    const userinfo_url = 'https://api.weixin.qq.com/sns/userinfo';

    private $mAccessToken;
    private $mOpenID;
    private $mErrcode;
    private $mErrmsg;

    public function __construct()
    {
        $this->mErrcode = 0;
    }

    public function enter($origin_url)
    {
        $back_url = BASE_SITE_URL . '/mobile/wxauthor.php';
        $params = [ 'appid' => self::appid,
                    'redirect_uri' => $back_url,
                    'response_type' => 'code',
                    'scope' => 'snsapi_userinfo',
                    'state' => $_SESSION['MPHPSESSID']];
        $ref_url = util::http_add_params(self::authorize_url,$params);
        $ref_url .= '#wechat_redirect';
        $_SESSION['author_orgin_url'] = $origin_url;
        return $ref_url;
    }

    public function callback($code)
    {
        if($this->access_token($code) == false) {
            Log::record("wxauthor access token error code:{$this->mErrcode} msg:{$this->mErrmsg}");
            return false;
        }

        $user_info = $this->user_info();
        if($user_info == false) {
            Log::record("wxauthor user_info error code:{$this->mErrcode} msg:{$this->mErrmsg}");
            return false;
        }
        else
        {
            $_SESSION['wxauthor_time'] = time();
            return $user_info;
        }
    }

    private function access_token($code)
    {
        $params = ['appid' => self::appid,'secret' => self::appsecret,'code' => $code,'grant_type' => 'authorization_code'];
        $res = http_request(self::access_token_url,$params);
        if($res == false) return false;
        /*
         * 正确返回
         * { "access_token":"ACCESS_TOKEN","expires_in":7200, "refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE" }
         * 错误返回
         * {"errcode":40029,"errmsg":"invalid code"}
         */
        $info = json_decode($res, true);
        $this->mErrcode = intval($info['errcode']);
        if($this->mErrcode > 0) {
            $this->mErrmsg = $info['errmsg'];
            return false;
        }
        else {
            $this->mAccessToken = $info['access_token'];
            $this->mOpenID = $info['openid'];
            return true;
        }
    }

    private function user_info()
    {
        $params = ['access_token' => $this->mAccessToken,'openid' => $this->mOpenID,'lang' => 'zh_CN'];
        $res = http_request(self::userinfo_url,$params);
        if($res == false) return false;

        $info = json_decode($res, true);
        $this->mErrcode = intval($info['errcode']);
        if($this->mErrcode > 0) {
            $this->mErrmsg = $info['errmsg'];
            return false;
        }
        else {
            Log::record("user_info={$res}",Log::DEBUG);
            return $info;
        }
    }
}