Compare commits

...

5 Commits

  1. 119
      app/common/service/Isvapi.php
  2. 3
      app/common/service/Yinghuo.php
  3. 7
      app/common/service/wechat/MiniProgram.php
  4. 22
      app/common/service/wechat/OpenPlatform.php
  5. 6
      app/wechat/controller/IndexController.php
  6. 1
      config/route.php
  7. 1980
      front/yarn.lock

@ -0,0 +1,119 @@
<?php
namespace app\common\service;
class Isvapi {
private $componentAppid;
private $componentSecret;
private $domain;
public function __construct($componentAppid, $componentSecret, $domain = "https://partner.isvapi.com/openapi") {
$this->componentAppid = $componentAppid;
$this->componentSecret = $componentSecret;
$this->domain = $domain;
}
/**
* 查询授权应用authorizer_access_token示例
*/
public function getAuthorizerToken($authorizerAppid, $returnComponentToken = 1) {
$timestamp = time(); //发起请求时的时间戳(秒)
$body = [
'authorizer_appid' => $authorizerAppid,
'return_component_token' => $returnComponentToken
];
//获取签名
$sign = $this->genSign($timestamp, $body);
$url = "{$this->domain}/wechat/get_authorizer_token?component_appid={$this->componentAppid}&timestamp=$timestamp&sign=$sign";
//发起请求
return $this->sendPostJson($url, $body);
// var_dump($data);
}
/**
* 查询开放平台component_access_token示例
*/
public function getComponentToken() {
$timestamp = time(); //发起请求时的时间戳(秒)
//没有请求参数时
$body = [];
//获取签名
$sign = $this->genSign($timestamp, $body);
$url = "{$this->domain}/wechat/get_component_token?component_appid={$this->componentAppid}&timestamp=$timestamp&sign=$sign";
//发起请求
return $this->sendPostJson($url, $body);
}
/**
* 获取授权链接
*/
public function getAuthUrl() {
$timestamp = time(); //发起请求时的时间戳(秒)
//没有请求参数时
$body = [];
//获取签名
$sign = $this->genSign($timestamp, $body);
$url = "{$this->domain}/wechat/get_auth_url?component_appid={$this->componentAppid}&timestamp=$timestamp&sign=$sign";
//发起请求
return $this->sendPostJson($url, $body);
// var_dump($data);
}
/**
* 生成签名
* @param int $timestamp 时间戳
* @param array $body 请求参数
* @return string
*/
private function genSign(int $timestamp, array $body) {
$bodyMd5 = md5($this->genJsonBody($body));
return md5("{$this->componentAppid},$bodyMd5,$timestamp,{$this->componentSecret}");
}
/**
* 生成Json格式的body
* @param array $body
* @return string
*/
private function genJsonBody(array $body) {
return (count($body) > 0) ? json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : '{}';
}
/**
* 发送请求
* @param string $url 请求地址
* @param array $body 请求参数
* @return array
*/
private function sendPostJson(string $url, array $body) {
$body = $this->genJsonBody($body);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json; charset=utf-8',
]);
$resp = curl_exec($ch);
curl_close($ch);
/*echo PHP_EOL;
echo "请求地址:" . $url . PHP_EOL;
echo "请求参数:" . $body . PHP_EOL;
echo "请求结果:" . $resp . PHP_EOL;
echo PHP_EOL;*/
return json_decode($resp, true);
}
}

@ -140,7 +140,8 @@ class Yinghuo
"form" => [
"enabled" => true,
"app_id" => $appid,
"app_secret" => "fastRegisterApp"
"app_secret" => "fastRegisterApp",
"enableShipping" => false,
]
];
return $this->curl('/store/client.wxapp.setting/update', $data, 'store');

@ -4,6 +4,7 @@ namespace app\common\service\wechat;
use app\admin\model\Authorizers;
use app\common\service\Isvapi;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\Kernel\Http\Response;
use EasyWeChat\Kernel\Support\Collection;
@ -16,8 +17,10 @@ class MiniProgram extends OpenPlatform
{
public function getMiniProgram($appid): \EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Application
{
$platform = Authorizers::where('appid', $appid)->find();
return $this->app->miniProgram($platform['appid'], $platform['refreshtoken']);
// $platform = Authorizers::where('appid', $appid)->find();
$wechat = new Isvapi('wx3a67b967164b59d1', '604cf7409d1ace34a1fb8226cef89c30');
$result = $wechat->getAuthorizerToken($appid);
return $this->app->miniProgram($appid, $result['data']['authorizer_refresh_token']);
}
/**

@ -5,9 +5,11 @@ use app\admin\model\Authorizers;
use app\common\model\WxcallbackBiz;
use app\common\model\WxcallbackComponent;
use app\common\service\BaseServices;
use app\common\service\Isvapi;
use EasyWeChat\Factory;
use EasyWeChat\OpenPlatform\Application;
use support\Log;
use support\Cache;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use think\db\exception\DataNotFoundException;
@ -54,6 +56,11 @@ class OpenPlatform extends BaseServices {
],
],
]);
$wechat = new Isvapi($platform['app_id'], $platform['secret']);
$result = $wechat->getComponentToken();
$this->app['verify_ticket']->setTicket($result['data']['component_access_token']);
$this->app['access_token']->setToken($result['data']['component_access_token']);
}
/**
@ -76,8 +83,16 @@ class OpenPlatform extends BaseServices {
$this->addComponentCallBackRecord($message);
switch ($message['InfoType']) {
case 'notify_third_fastregisterbetaapp':
// 过滤掉重复请求
$key = 'wechat_mp_' . md5(json_encode($message));
if (Cache::has($key)) {
Log::info('收到重复注册试用小程序请求,已过滤');
return 'success';
} else {
Cache::set($key, '', 10);
}
FastRegisterApp::callback($message);
break;
return 'success';
case 'authorized':
case 'updateauthorized':
$this->addAuthorizerInfo($message);
@ -89,6 +104,7 @@ class OpenPlatform extends BaseServices {
break;
}
}
return 'success';
});
// 消息与事件通知 日志记录
$message = $this->app->server->getMessage();
@ -212,7 +228,7 @@ class OpenPlatform extends BaseServices {
'appid' => $data['AppId'],
'authorizer_appid' => $data['AuthorizerAppid'] ?? null,
'infotype' => $data['InfoType'],
'postbody' => json_encode($data),
'postbody' => json_encode($data, JSON_UNESCAPED_UNICODE),
'receivetime' => $data['CreateTime']
];
WxcallbackComponent::create($row);
@ -225,7 +241,7 @@ class OpenPlatform extends BaseServices {
'tousername' => $data['ToUserName'],
'msgtype' => $data['MsgType'],
'event' => $data['Event'],
'postbody' => json_encode($data),
'postbody' => json_encode($data, JSON_UNESCAPED_UNICODE),
'receivetime' => $data['CreateTime']
];
WxcallbackBiz::create($row);

@ -27,12 +27,12 @@ class IndexController extends BaseController
$key = 'wechat_mp_' . md5(json_encode($xml));
if (Cache::has($key)) {
Log::info('收到重复请求,已过滤');
return '请求重复';
return 'success';
} else {
Cache::set($key, '', 3);
Cache::set($key, '', 10);
}
$xml = simplexml_load_string($xml);
if (empty($xml)) return '请求体为空';
if (empty($xml)) return 'success';
if ($appid) {
// 转发消息与事件推送请求给第三方
$forwardResult = Forward::run($request, $appid, 'app');

@ -31,6 +31,7 @@ Route::any('/wechat[/{appid}]', [app\wechat\controller\IndexController::class, '
// openapi
Route::group('/openapi', function () {
Route::get('/getToken', [app\wechat\controller\OpenApiController::class, 'getToken']);
Route::get('/getOfficialToken', [app\wechat\controller\OpenApiController::class, 'getOfficialToken']);
Route::get('/getTokenByStoreId', [app\wechat\controller\OpenApiController::class, 'getTokenByStoreId']);
})->middleware(
app\wechat\middleware\OpenApiMiddleware::class

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save