wxauthor.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/4/25
  6. * Time: 下午10:50
  7. */
  8. namespace thrid_author;
  9. require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
  10. require_once(BASE_ROOT_PATH . '/core/framework/function/http.php');
  11. use util;
  12. use Log;
  13. use wechat_helper;
  14. class wxauthor
  15. {
  16. const appid = PUB_APPID;
  17. const appsecret = PUB_APPSECRET;
  18. const authorize_url = 'https://open.weixin.qq.com/connect/oauth2/authorize';
  19. const access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
  20. const userinfo_url = 'https://api.weixin.qq.com/sns/userinfo';
  21. private $mAccessToken;
  22. private $mOpenID;
  23. private $mErrcode;
  24. private $mErrmsg;
  25. public function __construct()
  26. {
  27. $this->mErrcode = 0;
  28. }
  29. public function enter($origin_url)
  30. {
  31. $back_url = BASE_SITE_URL . "/mobile/wxauthor.php";
  32. $params = [ 'appid' => self::appid,
  33. 'redirect_uri' => $back_url,
  34. 'response_type' => 'code',
  35. 'scope' => 'snsapi_userinfo',
  36. 'state' => BASE_SITE_URL];
  37. $ref_url = util::http_add_params(self::authorize_url,$params);
  38. $ref_url .= '#wechat_redirect';
  39. wechat_helper::set_origin_url($origin_url);
  40. Log::record("originurl=" . wechat_helper::get_origin_url(),Log::DEBUG);
  41. return $ref_url;
  42. }
  43. public function callback($code)
  44. {
  45. if($this->access_token($code) == false) {
  46. Log::record("wxauthor access token error code:{$this->mErrcode} msg:{$this->mErrmsg}");
  47. return false;
  48. }
  49. $user_info = $this->user_info();
  50. if($user_info == false) {
  51. Log::record("wxauthor user_info error code:{$this->mErrcode} msg:{$this->mErrmsg}");
  52. return false;
  53. }
  54. else {
  55. $_SESSION['wxauthor_time'] = time();
  56. return $user_info;
  57. }
  58. }
  59. private function access_token($code)
  60. {
  61. $params = ['appid' => self::appid,'secret' => self::appsecret,'code' => $code,'grant_type' => 'authorization_code'];
  62. $res = http_request(self::access_token_url,$params);
  63. if($res == false) return false;
  64. /*
  65. * 正确返回
  66. * { "access_token":"ACCESS_TOKEN","expires_in":7200, "refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE" }
  67. * 错误返回
  68. * {"errcode":40029,"errmsg":"invalid code"}
  69. */
  70. $info = json_decode($res, true);
  71. $this->mErrcode = intval($info['errcode']);
  72. if($this->mErrcode > 0) {
  73. $this->mErrmsg = $info['errmsg'];
  74. return false;
  75. }
  76. else {
  77. $this->mAccessToken = $info['access_token'];
  78. $this->mOpenID = $info['openid'];
  79. return true;
  80. }
  81. }
  82. private function user_info()
  83. {
  84. $params = ['access_token' => $this->mAccessToken,'openid' => $this->mOpenID,'lang' => 'zh_CN'];
  85. $res = http_request(self::userinfo_url,$params);
  86. if($res == false) return false;
  87. $info = json_decode($res, true);
  88. $this->mErrcode = intval($info['errcode']);
  89. if($this->mErrcode > 0) {
  90. $this->mErrmsg = $info['errmsg'];
  91. return false;
  92. }
  93. else {
  94. Log::record("user_info={$res}",Log::DEBUG);
  95. return $info;
  96. }
  97. }
  98. }