Wechat.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. /*if (!isset($_REQUEST['code'])) {
  70. return '';
  71. } else {
  72. $code = $_REQUEST['code'];*/
  73. $token = $this->getAccessToken($code);
  74. $openid = isset($token['openid']) ? $token['openid'] : '';
  75. $access_token = isset($token['access_token']) ? $token['access_token'] : '';
  76. $ret = Http::sendRequest('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
  77. if ($ret['ret']) {
  78. $wxuserinfo = json_decode($ret['msg'], true);
  79. Session::set('wxuserinfo', $wxuserinfo);
  80. }else{
  81. $wxuserinfo = [];
  82. }
  83. /*}*/
  84. }
  85. return $wxuserinfo;
  86. }
  87. /**
  88. * 获取授权token网页授权
  89. *
  90. * @param string $code
  91. * @return mixed|string
  92. */
  93. public function getAccessToken($code = '')
  94. {
  95. $params = [
  96. 'appid' => $this->app_id,
  97. 'secret' => $this->app_secret,
  98. 'code' => $code,
  99. 'grant_type' => 'authorization_code'
  100. ];
  101. $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
  102. if ($ret['ret']) {
  103. $ar = json_decode($ret['msg'], true);
  104. return $ar;
  105. }
  106. return [];
  107. }
  108. public function getuserphonenumber($code = ''){
  109. $access_token = $this->getPublicAccessToken();
  110. $params = [
  111. 'code' => $code,
  112. ];
  113. $ret = Http::sendRequest('https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token, json_encode($params), 'POST');
  114. if ($ret['ret']) {
  115. $ar = json_decode($ret['msg'], true);
  116. return $ar;
  117. }
  118. return [];
  119. }
  120. public function getPublicAccessToken(){
  121. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->app_id.'&secret='.$this->app_secret);
  122. if ($ret['ret']) {
  123. $ar = json_decode($ret['msg'], true);
  124. return $ar['access_token'];
  125. }
  126. }
  127. //{"errcode":0,"errmsg":"ok","msgid":2054095443608862720}
  128. public function send($ac,$openid,$first,$keyword1,$keyword2,$keyword3,$remark,$color){
  129. $params = [
  130. "touser" => $openid,
  131. "template_id" => "lEUyDmLgwIaFDi9SNlIosXe-4fD43SiqSOZigIPOfJ8",
  132. 'url' => 'https://***.lanmaonet.com',
  133. "data" => [
  134. "first" => ["value"=>$first,"color"=>$color],
  135. "keyword1" => ["value"=>$keyword1,"color"=>"#173177"],
  136. "keyword2" => ["value"=>$keyword2,"color"=>"#173177"],
  137. "keyword3" => ["value"=>$keyword3,"color"=>"#173177"],
  138. "remark" => ["value"=>$remark,"color"=>$color],
  139. ],
  140. 'miniprogram' => [],
  141. ];
  142. $rest = curl_post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ac,json_encode($params));
  143. }
  144. public function getJsticket($code = '')
  145. {
  146. $jsticket = Session::get('jsticket');
  147. if (!$jsticket) {
  148. $token = $this->getAccessToken($code);
  149. $params = [
  150. 'access_token' => 'token',
  151. 'type' => 'jsapi',
  152. ];
  153. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/ticket/getticket', $params, 'GET');
  154. if ($ret['ret']) {
  155. $ar = json_decode($ret['msg'], true);
  156. return $ar;
  157. }
  158. }
  159. return $jsticket;
  160. }
  161. }