wxauthor.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = AUTHOR_APPID;
  17. const appsecret = AUTHOR_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. if(defined('SERVER_TYPE') && SERVER_TYPE == 'panda') {
  32. $back_url = 'https://passport.lrlz.com/mobile/wxauthor.php';
  33. }
  34. elseif(defined('SERVER_TYPE') && SERVER_TYPE == 'car') {
  35. $back_url = 'http://cardev.lrlz.com/mobile/wxauthor.php';
  36. }
  37. else {
  38. Log::record("找不到微信OpenWxPayConfig密钥",Log::ERR);
  39. }
  40. $params = [ 'appid' => self::appid,
  41. 'redirect_uri' => $back_url,
  42. 'response_type' => 'code',
  43. 'scope' => 'snsapi_userinfo',
  44. 'state' => BASE_SITE_URL];
  45. $ref_url = util::http_add_params(self::authorize_url,$params);
  46. $ref_url .= '#wechat_redirect';
  47. wechat_helper::set_origin_url($origin_url);
  48. Log::record("originurl=" . wechat_helper::get_origin_url(),Log::DEBUG);
  49. return $ref_url;
  50. }
  51. public function callback($code)
  52. {
  53. if($this->access_token($code) == false) {
  54. Log::record("wxauthor access token error code:{$this->mErrcode} msg:{$this->mErrmsg}");
  55. return false;
  56. }
  57. $user_info = $this->user_info();
  58. if($user_info == false) {
  59. Log::record("wxauthor user_info error code:{$this->mErrcode} msg:{$this->mErrmsg}");
  60. return false;
  61. }
  62. else {
  63. $_SESSION['wxauthor_time'] = time();
  64. return $user_info;
  65. }
  66. }
  67. private function access_token($code)
  68. {
  69. $params = ['appid' => self::appid,'secret' => self::appsecret,'code' => $code,'grant_type' => 'authorization_code'];
  70. $res = http_request(self::access_token_url,$params);
  71. if($res == false) return false;
  72. /*
  73. * 正确返回
  74. * { "access_token":"ACCESS_TOKEN","expires_in":7200, "refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE" }
  75. * 错误返回
  76. * {"errcode":40029,"errmsg":"invalid code"}
  77. */
  78. $info = json_decode($res, true);
  79. $this->mErrcode = intval($info['errcode']);
  80. if($this->mErrcode > 0) {
  81. $this->mErrmsg = $info['errmsg'];
  82. return false;
  83. }
  84. else {
  85. $this->mAccessToken = $info['access_token'];
  86. $this->mOpenID = $info['openid'];
  87. return true;
  88. }
  89. }
  90. private function user_info()
  91. {
  92. $params = ['access_token' => $this->mAccessToken,'openid' => $this->mOpenID,'lang' => 'zh_CN'];
  93. $res = http_request(self::userinfo_url,$params);
  94. if($res == false) return false;
  95. $info = json_decode($res, true);
  96. $this->mErrcode = intval($info['errcode']);
  97. if($this->mErrcode > 0) {
  98. $this->mErrmsg = $info['errmsg'];
  99. return false;
  100. }
  101. else {
  102. Log::record("user_info={$res}",Log::DEBUG);
  103. return $info;
  104. }
  105. }
  106. }