diff --git a/mobile/plugins/payment/wxpay.php b/mobile/plugins/payment/wxpay.php index 15bf300..9e8d9ec 100755 --- a/mobile/plugins/payment/wxpay.php +++ b/mobile/plugins/payment/wxpay.php @@ -124,7 +124,73 @@ class wxpay // require_once("../connect/wechat.php"); // return wxchat::redirect("shop/mobile"); // $_SESSION['openid'] = 'sdf4asgdf2ag1'; - return false; + // return false; + + // 设置应用信息 + $appid = 'wx79343915f99167e6'; + $appsecret = 'f2f72c5e0ac29c2373bfaf22cf059c02'; + $redirect_uri = 'https://shop.heavenk.com/mobile/plugins/payment/wxpay.php'; // 回调URL + + // 构造授权链接 + $auth_url = 'https://open.weixin.qq.com/connect/oauth2/authorize' + . '?appid=' . urlencode($appid) + . '&redirect_uri=' . urlencode($redirect_uri) + . '&response_type=code' + . '&scope=snsapi_userinfo' // snsapi_base 只获取openid,snsapi_userinfo 获取用户信息 + . '&state=STATE'; // wechat_redirect + + // 如果用户已经授权,则重定向到回调URL,并获取用户的openid + if (isset($_GET['code'])) { + $code = $_GET['code']; + + // $code = '100010'; + + // 构造获取access_token的URL + $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token' + . '?appid=' . urlencode($appid) + . '&secret=' . urlencode($appsecret) + . '&code=' . urlencode($code) + . '&grant_type=authorization_code'; + + // 发送HTTP请求获取access_token + + // 创建一个cURL句柄 + $ch = curl_init(); + + // 设置请求的URL和其他选项 + curl_setopt($ch, CURLOPT_URL, $token_url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + // 发送HTTP GET请求并获取响应 + $response = curl_exec($ch); + + // 检查是否有错误发生 + if(curl_errno($ch)) { + echo 'cURL Error: ' . curl_error($ch); + } + + // 关闭cURL句柄 + curl_close($ch); + + // 输出响应 + $response = json_decode($response); + // var_dump($response->openid); + $_SESSION['openid'] = $response->openid; + + + // $client = new \GuzzleHttp\Client(); + // $response = $client->request('GET', $token_url); + // $result = json_decode($response->getBody(), true); + // $openid = $result['openid']; + + // // 在这里处理获取到的openid + // echo $openid; + } else { + // 如果没有授权,则重定向到授权页面 + header('Location: ' . $auth_url); + var_dump($_GET['code']); + exit(); + } } // 设置必填参数 diff --git a/mobile/source/index.html b/mobile/source/index.html index b48b490..5ee0c0f 100755 --- a/mobile/source/index.html +++ b/mobile/source/index.html @@ -1,7 +1,7 @@
-