Wechat.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 getPublicAccessToken(){
  109. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->app_id.'&secret='.$this->app_secret);
  110. if ($ret['ret']) {
  111. $ar = json_decode($ret['msg'], true);
  112. return $ar['access_token'];
  113. }
  114. }
  115. //{"errcode":0,"errmsg":"ok","msgid":2054095443608862720}
  116. public function send($ac,$openid,$first,$keyword1,$keyword2,$keyword3,$remark,$color){
  117. $params = [
  118. "touser" => $openid,
  119. "template_id" => "lEUyDmLgwIaFDi9SNlIosXe-4fD43SiqSOZigIPOfJ8",
  120. 'url' => 'https://yanglaoweb.lanmaonet.com',
  121. "data" => [
  122. "first" => ["value"=>$first,"color"=>$color],
  123. "keyword1" => ["value"=>$keyword1,"color"=>"#173177"],
  124. "keyword2" => ["value"=>$keyword2,"color"=>"#173177"],
  125. "keyword3" => ["value"=>$keyword3,"color"=>"#173177"],
  126. "remark" => ["value"=>$remark,"color"=>$color],
  127. ],
  128. 'miniprogram' => [],
  129. ];
  130. $rest = curl_post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ac,json_encode($params));
  131. }
  132. public function getJsticket($code = '')
  133. {
  134. $jsticket = Session::get('jsticket');
  135. if (!$jsticket) {
  136. $token = $this->getAccessToken($code);
  137. $params = [
  138. 'access_token' => 'token',
  139. 'type' => 'jsapi',
  140. ];
  141. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/ticket/getticket', $params, 'GET');
  142. if ($ret['ret']) {
  143. $ar = json_decode($ret['msg'], true);
  144. return $ar;
  145. }
  146. }
  147. return $jsticket;
  148. }
  149. }