Merge remote-tracking branch 'origin/master'

This commit is contained in:
knowpia
2022-09-09 13:43:38 +08:00
4 changed files with 135 additions and 20 deletions

View File

@@ -4,6 +4,9 @@ namespace app\controller;
use EasyWeChat\Factory;
use EasyWeChat\OfficialAccount\Application;
use think\facade\Config;
use think\facade\Route;
use think\facade\View;
class Wechat
{
@@ -17,7 +20,7 @@ class Wechat
*/
private function initWechat()
{
$this->app = Factory::officialAccount(config('wechat'));
$this->app = Factory::officialAccount(Config::get('wechat'));
}
/**
@@ -26,10 +29,14 @@ class Wechat
* @Date : 2022/9/9 11:29
* @Author : <Jason.C>
*/
public function url()
public function url(): string
{
$url = $GLOBALS['data']['data']['url'];
$redirect = Route::buildUrl('wechat/code', ['callback' => $url])
->suffix(false)
->domain(true);
$this->initWechat();
redirect($this->app->oauth->scopes(['snsapi_userinfo'])->redirect());
return $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($redirect);
}
/**
@@ -38,10 +45,12 @@ class Wechat
* @Date : 2022/9/9 11:29
* @Author : <Jason.C>
*/
public function callback()
public function code()
{
$this->initWechat();
$user = $this->app->oauth->user();
$code = $GLOBALS['data']['data']['code'];
$user = $this->app->oauth->userFromCode($code);
// $user 可以用的方法:
// $user->getId(); // 对应微信的 OPENID
// $user->getNickname(); // 对应微信的 nickname
@@ -57,9 +66,21 @@ class Wechat
* @Date : 2022/9/9 11:32
* @Author : <Jason.C>
*/
public function payment()
public function payment(): string
{
$config = Config::get('wechat.payment');
$payment = Factory::payment($config);
$unify = $payment->order->unify([
'body' => '商品订单',
'out_trade_no' => time(),
'total_fee' => 100,
'notify_url' => '',
'trade_type' => 'JSAPI',
'openid' => '$openid',
]);
$prepayId = $unify->prepay_id;
$jssdk = $payment->jssdk->bridgeConfig($prepayId);
return View::fetch('', ['jssdk' => $jssdk]);
}
/**

1
app/view/README.md Normal file
View File

@@ -0,0 +1 @@
如果不使用模板,可以删除该目录

View File

@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>微信支付</title>
<style>
* {
padding: 0;
margin: 0;
}
.background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
padding: 0;
margin: 0;
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
filter: blur(1px);
z-index: -1;
}
</style>
</head>
<body>
<div class="background" style="background-image: url('/static/field-gf7775feec_640.jpg')">
</div>
<script type="text/javascript" src="/layer/layer.js"></script>
<script>
function onBridgeReady() {
if (typeof WeixinJSBridge === 'undefined') {
alert('请在微信在打开页面!');
return false;
}
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{$jssdk},
function (res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
setTimeout(function () {
layer.open({
content: '支付成功'
, shadeClose: false
, btn: ['3秒后自动返回']
, time: 3
, end: function () {
window.history.back()
}
});
}, 1000);
} else {
layer.open({
content: '支付过程出现问题'
, shadeClose: false
, btn: ['3秒后自动返回']
, time: 3
, end: function () {
window.history.back()
}
});
}
})
}
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
} else {
onBridgeReady();
}
</script>
</body>
</html>