Wechat.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace app\common\library;
  3. use fast\Http;
  4. use think\Session;
  5. /**
  6. * 微信授权
  7. *
  8. */
  9. class Wechat{
  10. private $app_id = '';
  11. private $app_secret = '';
  12. private $scope = 'snsapi_userinfo';
  13. public function __construct($app_id, $app_secret)
  14. {
  15. $this->app_id = $app_id;
  16. $this->app_secret = $app_secret;
  17. }
  18. /**
  19. * 获取微信授权链接
  20. *
  21. * @return string
  22. */
  23. /*public function getAuthorizeUrl()
  24. {
  25. $redirect_uri = addon_url('epay/api/wechat', [], true, true);
  26. $redirect_uri = urlencode($redirect_uri);
  27. $state = \fast\Random::alnum();
  28. Session::set('state', $state);
  29. 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";
  30. }*/
  31. /**
  32. * 获取微信openid
  33. *
  34. * @return mixed|string
  35. */
  36. /*
  37. array(5) {
  38. ["access_token"] => string(89) "49_r4I-StVANS8uYWTUHG86GJ-g1iH6mMFWy-9LeOta--2l6Bzg8LVDQFu8VSARu87atETzVJFZ-fndy-aQqEb8wQ"
  39. ["expires_in"] => int(7200)
  40. ["refresh_token"] => string(89) "49_8Vfa-imGcRmUwcESvpvMcEkiuh8kmD_movl9bIz9DV5GonZrqfIya5NgT7G-NMeJ7KTuCjMrSJi4BtojjDxnCQ"
  41. ["openid"] => string(28) "o8lxjwRjlDortQKhTk1dpHjQxcBU"
  42. ["scope"] => string(15) "snsapi_userinfo"
  43. }
  44. * */
  45. public function getOpenid($code = '')
  46. {
  47. $openid = Session::get('openid');
  48. if (!$openid) {
  49. $token = $this->getAccessToken($code);
  50. $openid = isset($token['openid']) ? $token['openid'] : '';
  51. if ($openid) {
  52. Session::set("openid", $openid);
  53. }
  54. }
  55. return $openid;
  56. }
  57. /*
  58. array(2) {
  59. ["ret"] => bool(true)
  60. ["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":[]}"
  61. }
  62. */
  63. public function getwxuserinfo($code = '')
  64. {
  65. $wxuserinfo = Session::get('wxuserinfo');
  66. if (!$wxuserinfo) {
  67. $token = $this->getAccessToken($code);
  68. $openid = isset($token['openid']) ? $token['openid'] : '';
  69. $access_token = isset($token['access_token']) ? $token['access_token'] : '';
  70. $ret = Http::sendRequest('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
  71. if ($ret['ret']) {
  72. $wxuserinfo = json_decode($ret['msg'], true);
  73. Session::set('wxuserinfo', $wxuserinfo);
  74. }else{
  75. $wxuserinfo = [];
  76. }
  77. }
  78. return $wxuserinfo;
  79. }
  80. /**
  81. * 获取授权token网页授权
  82. *
  83. * @param string $code
  84. * @return mixed|string
  85. */
  86. public function getAccessToken($code = '')
  87. {
  88. $params = [
  89. 'appid' => $this->app_id,
  90. 'secret' => $this->app_secret,
  91. 'code' => $code,
  92. 'grant_type' => 'authorization_code'
  93. ];
  94. $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
  95. if ($ret['ret']) {
  96. $ar = json_decode($ret['msg'], true);
  97. return $ar;
  98. }
  99. return [];
  100. }
  101. //获取微信注册手机号
  102. public function getuserphonenumber($code = ''){
  103. $access_token = $this->getPublicAccessToken();
  104. $params = [
  105. 'code' => $code,
  106. ];
  107. $ret = Http::sendRequest('https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token, json_encode($params), 'POST');
  108. if ($ret['ret']) {
  109. $ar = json_decode($ret['msg'], true);
  110. return $ar;
  111. }
  112. return [];
  113. }
  114. public function getPublicAccessToken(){
  115. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->app_id.'&secret='.$this->app_secret);
  116. if ($ret['ret']) {
  117. $ar = json_decode($ret['msg'], true);
  118. return $ar['access_token'];
  119. }
  120. }
  121. //{"errcode":0,"errmsg":"ok","msgid":2054095443608862720}
  122. /*public function send($ac,$openid,$first,$keyword1,$keyword2,$keyword3,$remark,$color){
  123. $params = [
  124. "touser" => $openid,
  125. "template_id" => "lEUyDmLgwIaFDi9SNlIosXe-4fD43SiqSOZigIPOfJ8",
  126. 'url' => 'https://yanglaoweb.lanmaonet.com',
  127. "data" => [
  128. "first" => ["value"=>$first,"color"=>$color],
  129. "keyword1" => ["value"=>$keyword1,"color"=>"#173177"],
  130. "keyword2" => ["value"=>$keyword2,"color"=>"#173177"],
  131. "keyword3" => ["value"=>$keyword3,"color"=>"#173177"],
  132. "remark" => ["value"=>$remark,"color"=>$color],
  133. ],
  134. 'miniprogram' => [],
  135. ];
  136. $rest = curl_post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$ac,json_encode($params));
  137. }*/
  138. /*public function getJsticket($code = '')
  139. {
  140. $jsticket = Session::get('jsticket');
  141. if (!$jsticket) {
  142. $token = $this->getAccessToken($code);
  143. $params = [
  144. 'access_token' => 'token',
  145. 'type' => 'jsapi',
  146. ];
  147. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/ticket/getticket', $params, 'GET');
  148. if ($ret['ret']) {
  149. $ar = json_decode($ret['msg'], true);
  150. return $ar;
  151. }
  152. }
  153. return $jsticket;
  154. }*/
  155. //获取类目
  156. public function getcategory(){
  157. $access_token = $this->getPublicAccessToken();
  158. $params = [];
  159. $ret = Http::sendRequest('https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token='.$access_token, $params, 'GET');
  160. if ($ret['ret']) {
  161. $ar = json_decode($ret['msg'], true);
  162. return $ar;
  163. }
  164. return [];
  165. }
  166. //获取关键词列表
  167. public function getpubtemplatekeywords($tid){
  168. $access_token = $this->getPublicAccessToken();
  169. $params = [
  170. 'tid' => $tid,
  171. ];
  172. $ret = Http::sendRequest('https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token='.$access_token, $params, 'GET');
  173. if ($ret['ret']) {
  174. $ar = json_decode($ret['msg'], true);
  175. return $ar;
  176. }
  177. return [];
  178. }
  179. //获取所属类目下的公共模板
  180. public function getpubtemplatetitles(){
  181. $access_token = $this->getPublicAccessToken();
  182. $params = [
  183. 'ids' => 698, //类目 id
  184. 'start' => '' . 0,
  185. 'limit' => 30,
  186. ];
  187. $ret = Http::sendRequest('https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token='.$access_token, $params, 'GET');
  188. if ($ret['ret']) {
  189. $ar = json_decode($ret['msg'], true);
  190. return $ar;
  191. }
  192. return [];
  193. }
  194. //添加模板
  195. public function addtemplate(){
  196. $access_token = $this->getPublicAccessToken();
  197. $params = [];
  198. $params = [
  199. 'tid' => 515, //类目 id
  200. 'kidList' => '' . 0,
  201. 'sceneDesc' => 30,
  202. ];
  203. $ret = Http::sendRequest('https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token='.$access_token, $params, 'GET');
  204. if ($ret['ret']) {
  205. $ar = json_decode($ret['msg'], true);
  206. return $ar;
  207. }
  208. return [];
  209. }
  210. //发送订阅消息
  211. //https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html
  212. public function send($template_id,$openid,$data){
  213. $access_token = $this->getPublicAccessToken();
  214. $params = [
  215. 'template_id' => $template_id,
  216. 'page' => 'pages/index/index',
  217. 'touser' => $openid,
  218. 'data' => $data, //模板内容,格式形如{ "phrase3": { "value": "审核通过" }, "name1": { "value": "订阅" }, "date2": { "value": "2019-12-25 09:42" } }
  219. 'miniprogram_state' => 'trial', //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  220. 'lang' => 'zh_CN',
  221. ];
  222. $ret = Http::sendRequest('https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token, json_encode($params), 'POST');
  223. if ($ret['ret']) {
  224. $ar = json_decode($ret['msg'], true);
  225. return $ar;
  226. }
  227. return [];
  228. }
  229. }