1、安装依赖包
我们这里用的是 EasyWeChat
EasyWeCha官网 https://www.easywechat.com/
安装地址 https://github.com/easywechat/docs
相关文档 https://www.easywechat.com/docs/4.1/payment/index
- composer安装
$ composer require overtrue/wechat:~4.0 -vvv
1、封装服务类
<?php
namespace app\common\service;
use EasyWeChat\Factory;
use EasyWeChat\MiniProgram\Application;
use think\Hook;
/**
* 微信服务
* @package app\common\service
*/
class WeChatService
{
//微信公众号配置
private $officeConfig = [
'app_id' => 'wx727ac3b3f4439a25',
'secret' => '23471aaeb7d0ab3679da9f9a7d58bb25',
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
//...
];
//微信支付
private $payConfig = [
// 必要配置
'app_id' => 'xxxx',
'mch_id' => 'your-mch-id',
'key' => 'key-for-signature', // API 密钥
// 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!!
'key_path' => 'path/to/your/key', // XXX: 绝对路径!!!!
'notify_url' => '默认的订单回调地址', // 你也可以在下单时单独设置来想覆盖它
];
//微信小程序配置
private $miniConfig = [
'app_id' => 'wx3cf0f39249eb0exx',
'secret' => 'f1c242f4f28f735d4687abb469072axx',
// 下面为可选项
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => PUBLIC_PATH . '/logs/wechat.log',
],
];
//开放平台配置
private $openConfig = [
'app_id' => '开放平台第三方平台 APPID',
'secret' => '开放平台第三方平台 Secret',
'token' => '开放平台第三方平台 Token',
'aes_key' => '开放平台第三方平台 AES Key'
];
private $token = ''; //获取小程序的ACCESS_TOKEN
private $isContract = false; //是否开启支付中签约
/**
* @ApiTitle (实例化)
* @param int $type
* @return bool|Application|\EasyWeChat\OfficialAccount\Application|\EasyWeChat\OpenPlatform\Application|\EasyWeChat\Payment\Application
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function connect($type = 0)
{
//实例化对象
if ($type == 0) $app = Factory::miniProgram($this->miniConfig); //微信小程序
if ($type == 1) $app = Factory::officialAccount($this->officeConfig); //微信公众号
if ($type == 2) $app = Factory::payment($this->payConfig); //微信支付
if ($type == 3) $app = Factory::openPlatform($this->payConfig); //微信开放平台
//获取token
$accessToken = $app->access_token;
$this->token = $accessToken->getToken()['access_token']; // token 数组 token['access_token'] 字符串
return $app ?? false;
}
/**
* @ApiTitle (生成小程序二维码)
* @return bool|int
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function createCode()
{
$app = $this->connect(0);
$response = $app->app_code->getUnlimit('scene-value', [
'page' => 'path/to/page',
'width' => 600,
]);
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
$dir = 'qrCode/' . date('Ymd', time()) . '/';
if (!file_exists($dir)) mkdir($dir, 0777, true);
$url = PUBLIC_PATH . $dir . 'app_mini_code.png';
$response->save($url);
//保存到OSS
$urlOss = Hook::listen("upload_oss", $url, null, true);
//删除本地图片和文件夹
if (file_exists($url)) {
unlink($url);
rmdir($dir);
}
}
return $urlOss ?? false;
}
}
其中微信小程序、公众号、微信支付和开放平台等配置都在这里面写,如果需要企业微信等配置自行根据自己的业务修改里面的代码即可。
3、调用服务类
- 我们只要 new 一个服务类就可以使用里面的方法了
public function createWeChatCode()
{
$code = (new WeChatService)->createCode();
$this->success('生成小程序码成功', $code);
}
- 调用 EasyWeChat 包的方法,只需要 connect 一下就可以了,其中需要传参
$app = (new WeChatService())->connect(1);
感谢你的观看,如果有什么问题欢迎在留言区留言。