Files
huangjingfen/pro_v3.5.1/crmeb/services/wechat/client/miniprogram/AuthClient.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

153 lines
4.9 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
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace crmeb\services\wechat\client\miniprogram;
use crmeb\services\wechat\client\BaseClient;
use crmeb\services\wechat\config\MiniProgramConfig;
use crmeb\services\wechat\util\AES;
use crmeb\services\wechat\WechatException;
use EasyWeChat\Kernel\HttpClient\AccessTokenAwareClient;
use EasyWeChat\Kernel\HttpClient\Response;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use think\annotation\Inject;
/**
* 授权
* Class AuthClient
* @author 等风来
* @email 136327134@qq.com
* @date 2023/9/15
* @package crmeb\services\wechat\client\miniprogram
*/
class AuthClient extends BaseClient
{
/**
* @var MiniProgramConfig
*/
protected MiniProgramConfig $config;
/**
* AuthClient constructor.
* @param AccessTokenAwareClient $api
*/
public function __construct(AccessTokenAwareClient $api)
{
parent::__construct($api);
$this->config = app()->make(MiniProgramConfig::class);
}
/**
* 使用code换取用户信息
* @param string $code
* @return Response|ResponseInterface
* @throws TransportExceptionInterface
* @author 等风来
* @email 136327134@qq.com
* @date 2023/9/15
*/
public function session(string $code): ResponseInterface|Response
{
$params = [
'appid' => $this->config->appId,
'secret' => $this->config->secret,
'js_code' => $code,
'grant_type' => 'authorization_code',
];
return $this->api->get('sns/jscode2session', $params);
}
/**
* 解密数据
* @param string $sessionKey
* @param string $iv
* @param string $encrypted
* @return array
* @author 等风来
* @email 136327134@qq.com
* @date 2023/9/15
*/
public function decryptData(string $sessionKey, string $iv, string $encrypted): array
{
$decrypted = AES::decrypt(
base64_decode($encrypted, false),
base64_decode($sessionKey, false),
base64_decode($iv, false)
);
$decrypted = json_decode($decrypted, true);
if (!$decrypted) {
throw new WechatException('The given payload is invalid.');
}
return $decrypted;
}
/**
* 获取小程序码:适用于需要的码数量极多,或仅临时使用的业务场景
* @param string $scene
* @param array $optional
* @return Response
* @throws TransportExceptionInterface
* @author 等风来
* @email 136327134@qq.com
* @date 2023/9/15
*/
public function getUnlimit(string $scene, array $optional = []): Response
{
$params = array_merge([
'scene' => $scene,
], $optional);
return $this->getStream('wxa/getwxacodeunlimit', $params);
}
/**
* 获取流文件
* @param string $endpoint
* @param array $params
* @return Response
* @throws TransportExceptionInterface
* @author 等风来
* @email 136327134@qq.com
* @date 2023/9/15
*/
protected function getStream(string $endpoint, array $params): Response
{
return $this->api->request('POST', $endpoint, ['json' => $params]);
}
/**
* 获取小程序短链
* @param string $path 通过 URL Link 进入的小程序页面路径,必须是已经发布的小程序存在的页面,不可携带 query 。path 为空时会跳转小程序主页
* @param string $query 通过 URL Link 进入小程序时的query最大1024个字符只支持数字大小写英文以及部分特殊字符!#$&'()*+,/:;=?@-._~%
* @param int $expire_type 默认值0.小程序 URL Link 失效类型失效时间0失效间隔天数1
* @param int $expire_time 到期失效的 URL Link 的失效时间,为 Unix 时间戳。生成的到期失效 URL Link 在该时间前有效。最长有效期为30天。expire_type 为 0 必填
* @param int $expire_interval 到期失效的URL Link的失效间隔天数。生成的到期失效URL Link在该间隔时间到达前有效。最长间隔天数为30天。expire_type 为 1 必填
* @throws TransportExceptionInterface User: liusl
* DateTime: 2024/11/23 下午2:46
*/
public function generateUrlLink(string $path, string $query, int $expire_type = 0, int $expire_time = 30 * 24 * 3600, int $expire_interval = 29)
{
$params = [
'path' => $path,
'query' => $query,
'expire_type' => $expire_type,
];
if ($expire_type == 0) {
$params['expire_time'] = time() + $expire_time;
} else {
$params['expire_interval'] = $expire_interval;
}
return $this->api->postJson('wxa/generate_urllink', $params);
}
}