Wechat.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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($app_id, $app_secret)
  16. {
  17. $this->app_id = $app_id;
  18. $this->app_secret = $app_secret;
  19. }
  20. /**
  21. * 获取微信授权链接
  22. *
  23. * @return string
  24. */
  25. /*public function getAuthorizeUrl()
  26. {
  27. $redirect_uri = addon_url('epay/api/wechat', [], true, true);
  28. $redirect_uri = urlencode($redirect_uri);
  29. $state = \fast\Random::alnum();
  30. Session::set('state', $state);
  31. 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";
  32. }*/
  33. /**
  34. * 获取微信openid
  35. *
  36. * @return mixed|string
  37. */
  38. /*
  39. array(5) {
  40. ["access_token"] => string(89) "49_r4I-StVANS8uYWTUHG86GJ-g1iH6mMFWy-9LeOta--2l6Bzg8LVDQFu8VSARu87atETzVJFZ-fndy-aQqEb8wQ"
  41. ["expires_in"] => int(7200)
  42. ["refresh_token"] => string(89) "49_8Vfa-imGcRmUwcESvpvMcEkiuh8kmD_movl9bIz9DV5GonZrqfIya5NgT7G-NMeJ7KTuCjMrSJi4BtojjDxnCQ"
  43. ["openid"] => string(28) "o8lxjwRjlDortQKhTk1dpHjQxcBU"
  44. ["scope"] => string(15) "snsapi_userinfo"
  45. }
  46. * */
  47. public function getOpenid($code = '')
  48. {
  49. $openid = Session::get('openid');
  50. if (!$openid) {
  51. $token = $this->getAccessToken($code);
  52. $openid = isset($token['openid']) ? $token['openid'] : '';
  53. if ($openid) {
  54. Session::set("openid", $openid);
  55. }
  56. }
  57. return $openid;
  58. }
  59. /*
  60. array(2) {
  61. ["ret"] => bool(true)
  62. ["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":[]}"
  63. }
  64. */
  65. public function getwxuserinfo($code = '')
  66. {
  67. $wxuserinfo = Session::get('wxuserinfo');
  68. if (!$wxuserinfo) {
  69. $token = $this->getAccessToken($code);
  70. $openid = isset($token['openid']) ? $token['openid'] : '';
  71. $access_token = isset($token['access_token']) ? $token['access_token'] : '';
  72. $ret = Http::sendRequest('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
  73. if ($ret['ret']) {
  74. $wxuserinfo = json_decode($ret['msg'], true);
  75. Session::set('wxuserinfo', $wxuserinfo);
  76. }else{
  77. $wxuserinfo = [];
  78. }
  79. }
  80. return $wxuserinfo;
  81. }
  82. /**
  83. * 获取授权token网页授权
  84. *
  85. * @param string $code
  86. * @return mixed|string
  87. */
  88. public function getAccessToken($code = '')
  89. {
  90. $params = [
  91. 'appid' => $this->app_id,
  92. 'secret' => $this->app_secret,
  93. 'code' => $code,
  94. 'grant_type' => 'authorization_code'
  95. ];
  96. $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
  97. if ($ret['ret']) {
  98. $ar = json_decode($ret['msg'], true);
  99. return $ar;
  100. }
  101. return [];
  102. }
  103. //获取微信注册手机号
  104. public function getuserphonenumber($code = ''){
  105. $access_token = $this->getPublicAccessToken();
  106. $params = [
  107. 'code' => $code,
  108. ];
  109. $ret = Http::sendRequest('https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token, json_encode($params), 'POST');
  110. if ($ret['ret']) {
  111. $ar = json_decode($ret['msg'], true);
  112. return $ar;
  113. }
  114. return [];
  115. }
  116. public function getPublicAccessToken(){
  117. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->app_id.'&secret='.$this->app_secret);
  118. if ($ret['ret']) {
  119. $ar = json_decode($ret['msg'], true);
  120. return $ar['access_token'];
  121. }
  122. }
  123. //{"errcode":0,"errmsg":"ok","msgid":2054095443608862720}
  124. /*public function send($ac,$openid,$first,$keyword1,$keyword2,$keyword3,$remark,$color){
  125. $params = [
  126. "touser" => $openid,
  127. "template_id" => "lEUyDmLgwIaFDi9SNlIosXe-4fD43SiqSOZigIPOfJ8",
  128. 'url' => 'https://yanglaoweb.lanmaonet.com',
  129. "data" => [
  130. "first" => ["value"=>$first,"color"=>$color],
  131. "keyword1" => ["value"=>$keyword1,"color"=>"#173177"],
  132. "keyword2" => ["value"=>$keyword2,"color"=>"#173177"],
  133. "keyword3" => ["value"=>$keyword3,"color"=>"#173177"],
  134. "remark" => ["value"=>$remark,"color"=>$color],
  135. ],
  136. 'miniprogram' => [],
  137. ];
  138. $rest = curl_post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ac,json_encode($params));
  139. }*/
  140. /*public function getJsticket($code = '')
  141. {
  142. $jsticket = Session::get('jsticket');
  143. if (!$jsticket) {
  144. $token = $this->getAccessToken($code);
  145. $params = [
  146. 'access_token' => 'token',
  147. 'type' => 'jsapi',
  148. ];
  149. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/ticket/getticket', $params, 'GET');
  150. if ($ret['ret']) {
  151. $ar = json_decode($ret['msg'], true);
  152. return $ar;
  153. }
  154. }
  155. return $jsticket;
  156. }*/
  157. }