WxUtils.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace addons\csmadmin\library;
  3. use think\Cache;
  4. use fast\Http;
  5. class WxUtils
  6. {
  7. public static function getAccessToken()
  8. {
  9. $config = get_addon_config(CsmContants::$ADDONS);
  10. if($config['wxappid']==null||$config['wxappid']==''){
  11. CsmUtils::error("微信配置没有维护!");
  12. }
  13. $key = 'csmwx_wx_accesstoken_'.$config['wxappid'].$config['wxappsecret'];
  14. $token = Cache::get($key);
  15. // if (!$token) {
  16. if ($token==null || $token=='') {
  17. $params = [
  18. 'grant_type' => 'client_credential',
  19. 'appid' => $config['wxappid'],
  20. 'secret' => $config['wxappsecret']
  21. ];
  22. $url = "https://api.weixin.qq.com/cgi-bin/token";
  23. $result = Http::sendRequest($url, $params, 'GET');
  24. trace($result);
  25. if ($result['ret']) {
  26. $msg = (array) json_decode($result['msg'], true);
  27. if (isset($msg['access_token'])) {
  28. $token = $msg['access_token'];
  29. Cache::set($key, $token, $msg['expires_in'] - 1);
  30. }
  31. }
  32. }
  33. return $token;
  34. }
  35. public static function qrcodeCreate($sceneId)
  36. {
  37. $token = static::getAccessToken();
  38. $params = [
  39. 'expire_seconds' => 86400, // 3600*24
  40. 'action_name' => 'QR_SCENE',
  41. 'action_info' => [
  42. 'scene' => [
  43. 'scene_id' => $sceneId
  44. ]
  45. ]
  46. ];
  47. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $token;
  48. $result = Http::sendRequest($url, json_encode($params));
  49. if ($result['ret']) {
  50. $msg = (array) json_decode($result['msg'], true);
  51. if (isset($msg['ticket'])) {
  52. $ticket = $msg['ticket'];
  53. return 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($ticket);
  54. }
  55. } else {
  56. return null;
  57. }
  58. }
  59. /**
  60. * 微信回调
  61. */
  62. public static function wxcallback($request)
  63. {
  64. trace("==sendWxCodeAjax==");
  65. $signature = $request->request('signature');
  66. $timestamp = $request->request('timestamp');
  67. $nonce = $request->request('nonce');
  68. $echostr = $request->request('echostr');
  69. echo $echostr; // 微信了微信地址认证用
  70. $config = get_addon_config(CsmContants::$ADDONS);
  71. $token = $config['wxtoken'];
  72. $tmpArr = array(
  73. $token,
  74. $timestamp,
  75. $nonce
  76. );
  77. sort($tmpArr, SORT_STRING);
  78. $tmpStr = implode($tmpArr);
  79. $tmpStr = sha1($tmpStr);
  80. if ($tmpStr == $signature) {
  81. trace("==sendWxCodeAjax.ok==");
  82. $content = file_get_contents('php://input');
  83. trace("sendWxCodeAjax.content=" . $content);
  84. if($content==null||$content==''){
  85. //如果未空,则是认为校验接口
  86. //trace("echostr=".$echostr);
  87. //echo $echostr;
  88. return;
  89. }
  90. $postObj = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
  91. $openid = $postObj->FromUserName; // openid
  92. $event = strtolower($postObj->Event);
  93. $eventKey = $postObj->EventKey;
  94. $wxuser = WxUtils::getInfoByOpenID($openid);
  95. trace('befor:'.$eventKey);
  96. $eventKey = (int)str_replace("qrscene_", "", $eventKey);
  97. trace('after:'.$eventKey);
  98. return [
  99. "eventKey" => $eventKey,
  100. "openid" => $openid,
  101. "event" => $event,
  102. 'wxuser' => $wxuser
  103. ];
  104. } else {
  105. return null;
  106. }
  107. }
  108. /**
  109. * 根据openid获取用户在微信的详细信息
  110. * 参考文档:https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId
  111. * 返回
  112. * 'ret' => boolean true
  113. * 'msg' => string '{"subscribe":1,"openid":"oB_aRt-Jp4QAPHzin_34LBOMhybE","nickname":"陈士明","sex":1,"language":"zh_CN","city":"浦东新区","province":"上海","country":"中国","headimgurl":"http:\/\/thirdwx.qlogo.cn\/mmopen\/wVRnW5uIXI1ibqkp0xtaicZaY25Po07JDHbTZPUickxTCX4HibvvQ6picEPSVPNKIz6gxA80MOsrM5UxQdtX0YMicHTdQ0ibULGX2Tl\/132","subscribe_time":1586088907,"remark":"","groupid":0,"tagid_list":[],"subscribe_scene":"ADD_SCENE_QR_CODE","qr_scene":1586088886,"qr_scene_str":""}' (length=471)
  114. */
  115. public static function getInfoByOpenID($userid)
  116. {
  117. $token = static::getAccessToken();
  118. $params = [];
  119. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $token . '&openid=' . $userid;
  120. $result = Http::sendRequest($url, json_encode($params));
  121. if ($result['msg'] != null) {
  122. return json_decode($result['msg'], true);
  123. } else {
  124. return null;
  125. }
  126. }
  127. }