'client_credential', 'appid' => $config['wxappid'], 'secret' => $config['wxappsecret'] ]; $url = "https://api.weixin.qq.com/cgi-bin/token"; $result = Http::sendRequest($url, $params, 'GET'); trace($result); if ($result['ret']) { $msg = (array) json_decode($result['msg'], true); if (isset($msg['access_token'])) { $token = $msg['access_token']; Cache::set($key, $token, $msg['expires_in'] - 1); } } } return $token; } public static function qrcodeCreate($sceneId) { $token = static::getAccessToken(); $params = [ 'expire_seconds' => 86400, // 3600*24 'action_name' => 'QR_SCENE', 'action_info' => [ 'scene' => [ 'scene_id' => $sceneId ] ] ]; $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $token; $result = Http::sendRequest($url, json_encode($params)); if ($result['ret']) { $msg = (array) json_decode($result['msg'], true); if (isset($msg['ticket'])) { $ticket = $msg['ticket']; return 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($ticket); } } else { return null; } } /** * 微信回调 */ public static function wxcallback($request) { trace("==sendWxCodeAjax=="); $signature = $request->request('signature'); $timestamp = $request->request('timestamp'); $nonce = $request->request('nonce'); $echostr = $request->request('echostr'); echo $echostr; // 微信了微信地址认证用 $config = get_addon_config(CsmContants::$ADDONS); $token = $config['wxtoken']; $tmpArr = array( $token, $timestamp, $nonce ); sort($tmpArr, SORT_STRING); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if ($tmpStr == $signature) { trace("==sendWxCodeAjax.ok=="); $content = file_get_contents('php://input'); trace("sendWxCodeAjax.content=" . $content); if($content==null||$content==''){ //如果未空,则是认为校验接口 //trace("echostr=".$echostr); //echo $echostr; return; } $postObj = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA); $openid = $postObj->FromUserName; // openid $event = strtolower($postObj->Event); $eventKey = $postObj->EventKey; $wxuser = WxUtils::getInfoByOpenID($openid); trace('befor:'.$eventKey); $eventKey = (int)str_replace("qrscene_", "", $eventKey); trace('after:'.$eventKey); return [ "eventKey" => $eventKey, "openid" => $openid, "event" => $event, 'wxuser' => $wxuser ]; } else { return null; } } /** * 根据openid获取用户在微信的详细信息 * 参考文档:https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId * 返回 * 'ret' => boolean true * '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) */ public static function getInfoByOpenID($userid) { $token = static::getAccessToken(); $params = []; $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $token . '&openid=' . $userid; $result = Http::sendRequest($url, json_encode($params)); if ($result['msg'] != null) { return json_decode($result['msg'], true); } else { return null; } } }