初始化仓库
This commit is contained in:
165
app/common/service/wechat/WeChatConfigService.php
Normal file
165
app/common/service/wechat/WeChatConfigService.php
Normal file
@ -0,0 +1,165 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service\wechat;
|
||||
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\user\UserTerminalEnum;
|
||||
use app\common\model\pay\PayConfig;
|
||||
use app\common\service\ConfigService;
|
||||
|
||||
/**
|
||||
* 微信配置类
|
||||
* Class WeChatConfigService
|
||||
* @package app\common\service
|
||||
*/
|
||||
class WeChatConfigService
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 获取小程序配置
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/9/6 19:49
|
||||
*/
|
||||
public static function getMnpConfig()
|
||||
{
|
||||
return [
|
||||
'app_id' => ConfigService::get('mnp_setting', 'app_id'),
|
||||
'secret' => ConfigService::get('mnp_setting', 'app_secret'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取微信公众号配置
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/9/6 19:49
|
||||
*/
|
||||
public static function getOaConfig()
|
||||
{
|
||||
return [
|
||||
'app_id' => ConfigService::get('oa_setting', 'app_id'),
|
||||
'secret' => ConfigService::get('oa_setting', 'app_secret'),
|
||||
'token' => ConfigService::get('oa_setting', 'token'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取微信开放平台配置
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/10/20 15:51
|
||||
*/
|
||||
public static function getOpConfig()
|
||||
{
|
||||
return [
|
||||
'app_id' => ConfigService::get('open_platform', 'app_id'),
|
||||
'secret' => ConfigService::get('open_platform', 'app_secret'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 根据终端获取支付配置
|
||||
* @param $terminal
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 15:45
|
||||
*/
|
||||
public static function getPayConfigByTerminal($terminal)
|
||||
{
|
||||
switch ($terminal) {
|
||||
case UserTerminalEnum::WECHAT_MMP:
|
||||
$notifyUrl = (string)url('pay/notifyMnp', [], false, true);
|
||||
break;
|
||||
case UserTerminalEnum::WECHAT_OA:
|
||||
case UserTerminalEnum::PC:
|
||||
case UserTerminalEnum::H5:
|
||||
$notifyUrl = (string)url('pay/notifyOa', [], false, true);
|
||||
break;
|
||||
case UserTerminalEnum::ANDROID:
|
||||
case UserTerminalEnum::IOS:
|
||||
$notifyUrl = (string)url('pay/notifyApp', [], false, true);
|
||||
break;
|
||||
}
|
||||
|
||||
$pay = PayConfig::where(['pay_way' => PayEnum::WECHAT_PAY])->findOrEmpty()->toArray();
|
||||
//判断是否已经存在证书文件夹,不存在则新建
|
||||
if (!file_exists(app()->getRootPath() . 'runtime/cert')) {
|
||||
mkdir(app()->getRootPath() . 'runtime/cert', 0775, true);
|
||||
}
|
||||
//写入文件
|
||||
$apiclientCert = $pay['config']['apiclient_cert'] ?? '';
|
||||
$apiclientKey = $pay['config']['apiclient_key'] ?? '';
|
||||
|
||||
$certPath = app()->getRootPath() . 'runtime/cert/' . md5($apiclientCert) . '.pem';
|
||||
$keyPath = app()->getRootPath() . 'runtime/cert/' . md5($apiclientKey) . '.pem';
|
||||
|
||||
if (!empty($apiclientCert) && !file_exists($certPath)) {
|
||||
static::setCert($certPath, trim($apiclientCert));
|
||||
}
|
||||
if (!empty($apiclientKey) && !file_exists($keyPath)) {
|
||||
static::setCert($keyPath, trim($apiclientKey));
|
||||
}
|
||||
|
||||
return [
|
||||
// 商户号
|
||||
'mch_id' => $pay['config']['mch_id'] ?? '',
|
||||
// 商户证书
|
||||
'private_key' => $keyPath,
|
||||
'certificate' => $certPath,
|
||||
// v3 API 秘钥
|
||||
'secret_key' => $pay['config']['pay_sign_key'] ?? '',
|
||||
'notify_url' => $notifyUrl,
|
||||
'http' => [
|
||||
'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
|
||||
'timeout' => 5.0,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 临时写入证书
|
||||
* @param $path
|
||||
* @param $cert
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 15:48
|
||||
*/
|
||||
public static function setCert($path, $cert)
|
||||
{
|
||||
$fopenPath = fopen($path, 'w');
|
||||
fwrite($fopenPath, $cert);
|
||||
fclose($fopenPath);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
101
app/common/service/wechat/WeChatMnpService.php
Normal file
101
app/common/service/wechat/WeChatMnpService.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service\wechat;
|
||||
|
||||
|
||||
use EasyWeChat\Kernel\Exceptions\Exception;
|
||||
use EasyWeChat\MiniApp\Application;
|
||||
|
||||
|
||||
/**
|
||||
* 微信功能类
|
||||
* Class WeChatMnpService
|
||||
* @package app\common\service
|
||||
*/
|
||||
class WeChatMnpService
|
||||
{
|
||||
|
||||
protected $app;
|
||||
|
||||
protected $config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = $this->getConfig();
|
||||
$this->app = new Application($this->config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 配置
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 12:03
|
||||
*/
|
||||
protected function getConfig()
|
||||
{
|
||||
$config = WeChatConfigService::getMnpConfig();
|
||||
if (empty($config['app_id']) || empty($config['secret'])) {
|
||||
throw new \Exception('请先设置小程序配置');
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 小程序-根据code获取微信信息
|
||||
* @param string $code
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 11:03
|
||||
*/
|
||||
public function getMnpResByCode(string $code)
|
||||
{
|
||||
$utils = $this->app->getUtils();
|
||||
$response = $utils->codeToSession($code);
|
||||
|
||||
if (!isset($response['openid']) || empty($response['openid'])) {
|
||||
throw new Exception('获取openID失败');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取手机号
|
||||
* @param string $code
|
||||
* @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 11:46
|
||||
*/
|
||||
public function getUserPhoneNumber(string $code)
|
||||
{
|
||||
return $this->app->getClient()->postJson('wxa/business/getuserphonenumber', [
|
||||
'code' => $code,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
157
app/common/service/wechat/WeChatOaService.php
Normal file
157
app/common/service/wechat/WeChatOaService.php
Normal file
@ -0,0 +1,157 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service\wechat;
|
||||
|
||||
|
||||
use EasyWeChat\Kernel\Exceptions\Exception;
|
||||
use EasyWeChat\OfficialAccount\Application;
|
||||
|
||||
|
||||
/**
|
||||
* 公众号相关
|
||||
* Class WeChatOaService
|
||||
* @package app\common\service\wechat
|
||||
*/
|
||||
class WeChatOaService
|
||||
{
|
||||
|
||||
protected $app;
|
||||
|
||||
protected $config;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = $this->getConfig();
|
||||
$this->app = new Application($this->config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes easywechat服务端
|
||||
* @return \EasyWeChat\Kernel\Contracts\Server|\EasyWeChat\OfficialAccount\Server
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \ReflectionException
|
||||
* @throws \Throwable
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 14:22
|
||||
*/
|
||||
public function getServer()
|
||||
{
|
||||
return $this->app->getServer();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 配置
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 12:03
|
||||
*/
|
||||
protected function getConfig()
|
||||
{
|
||||
$config = WeChatConfigService::getOaConfig();
|
||||
if (empty($config['app_id']) || empty($config['secret'])) {
|
||||
throw new Exception('请先设置公众号配置');
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 公众号-根据code获取微信信息
|
||||
* @param string $code
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 11:04
|
||||
*/
|
||||
public function getOaResByCode(string $code)
|
||||
{
|
||||
$response = $this->app->getOAuth()
|
||||
->scopes(['snsapi_userinfo'])
|
||||
->userFromCode($code)
|
||||
->getRaw();
|
||||
|
||||
if (!isset($response['openid']) || empty($response['openid'])) {
|
||||
throw new Exception('获取openID失败');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 公众号跳转url
|
||||
* @param string $url
|
||||
* @return mixed
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 10:35
|
||||
*/
|
||||
public function getCodeUrl(string $url)
|
||||
{
|
||||
return $this->app->getOAuth()
|
||||
->scopes(['snsapi_userinfo'])
|
||||
->redirect($url);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 创建公众号菜单
|
||||
* @param array $buttons
|
||||
* @param array $matchRule
|
||||
* @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
||||
* @author 段誉
|
||||
* @date 2023/2/27 12:07
|
||||
*/
|
||||
public function createMenu(array $buttons, array $matchRule = [])
|
||||
{
|
||||
if (!empty($matchRule)) {
|
||||
return $this->app->getClient()->postJson('cgi-bin/menu/addconditional', [
|
||||
'button' => $buttons,
|
||||
'matchrule' => $matchRule,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->app->getClient()->postJson('cgi-bin/menu/create', ['button' => $buttons]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取jssdkConfig
|
||||
* @param $url
|
||||
* @param $jsApiList
|
||||
* @param array $openTagList
|
||||
* @param false $debug
|
||||
* @return mixed[]
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
||||
* @author 段誉
|
||||
* @date 2023/3/1 11:46
|
||||
*/
|
||||
public function getJsConfig($url, $jsApiList, $openTagList = [], $debug = false)
|
||||
{
|
||||
return $this->app->getUtils()->buildJsSdkConfig($url, $jsApiList, $openTagList, $debug);
|
||||
}
|
||||
|
||||
}
|
||||
82
app/common/service/wechat/WeChatRequestService.php
Normal file
82
app/common/service/wechat/WeChatRequestService.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service\wechat;
|
||||
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use WpOrg\Requests\Requests;
|
||||
|
||||
|
||||
/**
|
||||
* 自定义微信请求
|
||||
* Class WeChatRequestService
|
||||
* @package app\common\service\wechat
|
||||
*/
|
||||
class WeChatRequestService extends BaseLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 获取网站扫码登录地址
|
||||
* @param $appId
|
||||
* @param $redirectUri
|
||||
* @param $state
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/10/20 18:20
|
||||
*/
|
||||
public static function getScanCodeUrl($appId, $redirectUri, $state)
|
||||
{
|
||||
$url = 'https://open.weixin.qq.com/connect/qrconnect?';
|
||||
$url .= 'appid=' . $appId . '&redirect_uri=' . $redirectUri . '&response_type=code&scope=snsapi_login';
|
||||
$url .= '&state=' . $state . '#wechat_redirect';
|
||||
return $url;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 通过code获取用户信息(access_token,openid,unionid等)
|
||||
* @param $code
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/10/21 10:16
|
||||
*/
|
||||
public static function getUserAuthByCode($code)
|
||||
{
|
||||
$config = WeChatConfigService::getOpConfig();
|
||||
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
|
||||
$url .= '?appid=' . $config['app_id'] . '&secret=' . $config['secret'] . '&code=' . $code;
|
||||
$url .= '&grant_type=authorization_code';
|
||||
$requests = Requests::get($url);
|
||||
return json_decode($requests->body, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 通过授权信息获取用户信息
|
||||
* @param $accessToken
|
||||
* @param $openId
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/10/21 10:21
|
||||
*/
|
||||
public static function getUserInfoByAuth($accessToken, $openId)
|
||||
{
|
||||
$url = 'https://api.weixin.qq.com/sns/userinfo';
|
||||
$url .= '?access_token=' . $accessToken . '&openid=' . $openId;
|
||||
$response = Requests::get($url);
|
||||
return json_decode($response->body, true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user