Files
heping-api/app/controller/Wechat.php
2022-09-09 13:40:45 +08:00

97 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\controller;
use EasyWeChat\Factory;
use EasyWeChat\OfficialAccount\Application;
use think\facade\Config;
use think\facade\Route;
use think\facade\View;
class Wechat
{
private Application $app;
/**
* Notes : 初始化微信
*
* @Date : 2022/9/9 11:30
* @Author : <Jason.C>
*/
private function initWechat()
{
$this->app = Factory::officialAccount(Config::get('wechat'));
}
/**
* Notes : 获取微信授权的地址并跳转
*
* @Date : 2022/9/9 11:29
* @Author : <Jason.C>
*/
public function url(): string
{
$url = $GLOBALS['data']['data']['url'];
$redirect = Route::buildUrl('wechat/code', ['callback' => $url])
->suffix(false)
->domain(true);
$this->initWechat();
return $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($redirect);
}
/**
* Notes : 微信授权回调地址,获取到用户信息后,自行保存
*
* @Date : 2022/9/9 11:29
* @Author : <Jason.C>
*/
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
// $user->getName(); // 对应微信的 nickname
// $user->getAvatar(); // 头像网址
// $user->getOriginal(); // 原始API返回的结果
// $user->getToken(); // access_token 比如用于地址共享时使用
}
/**
* Notes : 显示支付的页面,支付逻辑
*
* @Date : 2022/9/9 11:32
* @Author : <Jason.C>
*/
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]);
}
/**
* Notes : 支付完成回调
*
* @Date : 2022/9/9 11:33
* @Author : <Jason.C>
*/
public function paid()
{
}
}