123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?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;
- use wechat_helper;
- class wxauthor
- {
- const appid = PUB_APPID;
- const appsecret = PUB_APPSECRET;
- 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' => BASE_SITE_URL];
- $ref_url = util::http_add_params(self::authorize_url,$params);
- $ref_url .= '#wechat_redirect';
- wechat_helper::set_origin_url($origin_url);
- Log::record("originurl=" . wechat_helper::get_origin_url(),Log::DEBUG);
- 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;
- }
- }
- }
|