wxauthor.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 = 'wxaf698e1cc5f2073b';
  17. const appsecret ='57029331b7f7dbb5913c9d7e14e91278';
  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 = 'https://cardev.lrlz.com/mobile/wxauthor.php';
  32. $params = [ 'appid' => self::appid,
  33. 'redirect_uri' => $back_url,
  34. 'response_type' => 'code',
  35. 'scope' => 'snsapi_userinfo',
  36. 'state' => urlencode(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. return $ref_url;
  41. }
  42. public function callback($code)
  43. {
  44. if($this->access_token($code) == false) {
  45. Log::record("wxauthor access token error code:{$this->mErrcode} msg:{$this->mErrmsg}");
  46. return false;
  47. }
  48. $user_info = $this->user_info();
  49. if($user_info == false) {
  50. Log::record("wxauthor user_info error code:{$this->mErrcode} msg:{$this->mErrmsg}");
  51. return false;
  52. }
  53. else {
  54. $_SESSION['wxauthor_time'] = time();
  55. return $user_info;
  56. }
  57. }
  58. private function access_token($code)
  59. {
  60. $params = ['appid' => self::appid,'secret' => self::appsecret,'code' => $code,'grant_type' => 'authorization_code'];
  61. $res = http_request(self::access_token_url,$params);
  62. if($res == false) return false;
  63. /*
  64. * 正确返回
  65. * { "access_token":"ACCESS_TOKEN","expires_in":7200, "refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE" }
  66. * 错误返回
  67. * {"errcode":40029,"errmsg":"invalid code"}
  68. */
  69. $info = json_decode($res, true);
  70. $this->mErrcode = intval($info['errcode']);
  71. if($this->mErrcode > 0) {
  72. $this->mErrmsg = $info['errmsg'];
  73. return false;
  74. }
  75. else {
  76. $this->mAccessToken = $info['access_token'];
  77. $this->mOpenID = $info['openid'];
  78. return true;
  79. }
  80. }
  81. private function user_info()
  82. {
  83. $params = ['access_token' => $this->mAccessToken,'openid' => $this->mOpenID,'lang' => 'zh_CN'];
  84. $res = http_request(self::userinfo_url,$params);
  85. if($res == false) return false;
  86. $info = json_decode($res, true);
  87. $this->mErrcode = intval($info['errcode']);
  88. if($this->mErrcode > 0) {
  89. $this->mErrmsg = $info['errmsg'];
  90. return false;
  91. }
  92. else {
  93. Log::record("user_info={$res}",Log::DEBUG);
  94. return $info;
  95. }
  96. }
  97. }