Wechat.php 5.6 KB

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