Wechat.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\common\library;
  3. use fast\Http;
  4. use think\Cache;
  5. use think\Session;
  6. /**
  7. * 微信授权
  8. *
  9. */
  10. class Wechat
  11. {
  12. private $app_id = '';
  13. private $app_secret = '';
  14. private $scope = 'snsapi_userinfo';
  15. public function __construct()
  16. {
  17. $wxConfig = config('wechat_app_login');
  18. $this->app_id = $wxConfig['appid'];
  19. $this->app_secret = $wxConfig['secret'];
  20. }
  21. /**
  22. * 获取微信授权链接
  23. *
  24. * @return string
  25. */
  26. /*public function getAuthorizeUrl()
  27. {
  28. $redirect_uri = addon_url('epay/api/wechat', [], true, true);
  29. $redirect_uri = urlencode($redirect_uri);
  30. $state = \fast\Random::alnum();
  31. Session::set('state', $state);
  32. return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope={$this->scope}&state={$state}#wechat_redirect";
  33. }*/
  34. /**
  35. * 获取微信openid
  36. *
  37. * @return mixed|string
  38. */
  39. /*
  40. array(5) {
  41. ["access_token"] => string(89) "49_r4I-StVANS8uYWTUHG86GJ-g1iH6mMFWy-9LeOta--2l6Bzg8LVDQFu8VSARu87atETzVJFZ-fndy-aQqEb8wQ"
  42. ["expires_in"] => int(7200)
  43. ["refresh_token"] => string(89) "49_8Vfa-imGcRmUwcESvpvMcEkiuh8kmD_movl9bIz9DV5GonZrqfIya5NgT7G-NMeJ7KTuCjMrSJi4BtojjDxnCQ"
  44. ["openid"] => string(28) "o8lxjwRjlDortQKhTk1dpHjQxcBU"
  45. ["scope"] => string(15) "snsapi_userinfo"
  46. }
  47. * */
  48. public function getOpenid($code = '')
  49. {
  50. $openid = Session::get('openid');
  51. if (!$openid) {
  52. $token = $this->getAccessToken($code);
  53. $openid = isset($token['openid']) ? $token['openid'] : '';
  54. if ($openid) {
  55. Session::set("openid", $openid);
  56. }
  57. }
  58. return $openid;
  59. }
  60. /*
  61. array(2) {
  62. ["ret"] => bool(true)
  63. ["msg"] => string(307) "{"openid":"o8lxjwRjlDortQKhTk1dpHjQxcBU","nickname":"科","sex":1,"language":"zh_CN","city":"临沂","province":"山东","country":"中国","headimgurl":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/LGYWIv4F5vxZ2zCM9GEUynoQeJ6ibX9IfoKPAWLlGIugn1mgaAMPuqxzPBDQ3ktLEv2ia7HmOeJYTg5LofG8YlwQ\/132","privilege":[]}"
  64. }
  65. */
  66. public function getwxuserinfo($code = '')
  67. {
  68. $wxuserinfo = Session::get('wxuserinfo');
  69. if (!$wxuserinfo) {
  70. $token = $this->getAccessToken($code);
  71. $openid = isset($token['openid']) ? $token['openid'] : '';
  72. $access_token = isset($token['access_token']) ? $token['access_token'] : '';
  73. $ret = Http::sendRequest('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
  74. if ($ret['ret']) {
  75. $wxuserinfo = json_decode($ret['msg'], true);
  76. Session::set('wxuserinfo', $wxuserinfo);
  77. }else{
  78. $wxuserinfo = [];
  79. }
  80. }
  81. return $wxuserinfo;
  82. }
  83. /**
  84. * 获取授权token网页授权
  85. *
  86. * @param string $code
  87. * @return mixed|string
  88. */
  89. public function getAccessToken($code = '')
  90. {
  91. $params = [
  92. 'appid' => $this->app_id,
  93. 'secret' => $this->app_secret,
  94. 'code' => $code,
  95. 'grant_type' => 'authorization_code'
  96. ];
  97. $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
  98. if ($ret['ret']) {
  99. $ar = json_decode($ret['msg'], true);
  100. return $ar;
  101. }
  102. return [];
  103. }
  104. /* public function getPublicAccessToken(){
  105. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->app_id.'&secret='.$this->app_secret);
  106. if ($ret['ret']) {
  107. $ar = json_decode($ret['msg'], true);
  108. return $ar['access_token'];
  109. }
  110. }*/
  111. //{"errcode":0,"errmsg":"ok","msgid":2054095443608862720}
  112. /*public function send($ac,$openid,$first,$keyword1,$keyword2,$keyword3,$remark,$color){
  113. $params = [
  114. "touser" => $openid,
  115. "template_id" => "lEUyDmLgwIaFDi9SNlIosXe-4fD43SiqSOZigIPOfJ8",
  116. 'url' => 'https://yanglaoweb.lanmaonet.com',
  117. "data" => [
  118. "first" => ["value"=>$first,"color"=>$color],
  119. "keyword1" => ["value"=>$keyword1,"color"=>"#173177"],
  120. "keyword2" => ["value"=>$keyword2,"color"=>"#173177"],
  121. "keyword3" => ["value"=>$keyword3,"color"=>"#173177"],
  122. "remark" => ["value"=>$remark,"color"=>$color],
  123. ],
  124. 'miniprogram' => [],
  125. ];
  126. $rest = curl_post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ac,json_encode($params));
  127. }*/
  128. /*public function getJsticket($code = '')
  129. {
  130. $jsticket = Session::get('jsticket');
  131. if (!$jsticket) {
  132. $token = $this->getAccessToken($code);
  133. $params = [
  134. 'access_token' => 'token',
  135. 'type' => 'jsapi',
  136. ];
  137. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/ticket/getticket', $params, 'GET');
  138. if ($ret['ret']) {
  139. $ar = json_decode($ret['msg'], true);
  140. return $ar;
  141. }
  142. }
  143. return $jsticket;
  144. }*/
  145. }