tourze / wechat-mini-program-bundle
微信小程序核心模块与功能服务
Installs: 2 888
Dependents: 11
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/wechat-mini-program-bundle
Requires
- php: ^8.1
- ext-libxml: *
- ext-openssl: *
- ext-simplexml: *
- doctrine/data-fixtures: ^2.0
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/doctrine-fixtures-bundle: ^4.0
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- knplabs/knp-menu: ^3.7
- nesbot/carbon: ^2.72 || ^3
- psr/link: ^1.1|^2.0
- psr/log: ^3|^2|^1
- symfony/cache-contracts: ^3
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-client-contracts: ^2.5 | ^3.0
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/security-core: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/arrayable: 0.0.*
- tourze/backtrace-helper: 0.1.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-ip-bundle: 0.0.*
- tourze/doctrine-resolve-target-entity-bundle: 0.0.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/doctrine-track-bundle: 0.1.*
- tourze/doctrine-user-bundle: 0.0.*
- tourze/easy-admin-attribute: 0.1.*
- tourze/easy-admin-menu-bundle: 0.1.*
- tourze/enum-extra: 0.1.*
- tourze/http-client-bundle: 0.1.*
- tourze/json-rpc-core: 0.0.*
- tourze/json-rpc-log-bundle: 0.1.*
- tourze/symfony-cron-job-bundle: 0.1.*
- tourze/wechat-mini-program-appid-contracts: 0.0.*
- yiisoft/arrays: ^3
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
README
[
]
(https://packagist.org/packages/tourze/wechat-mini-program-bundle)
[
]
(https://packagist.org/packages/tourze/wechat-mini-program-bundle)
[
]
(https://packagist.org/packages/tourze/wechat-mini-program-bundle)
[
]
(https://github.com/tourze/php-monorepo/actions)
[
]
(https://codecov.io/gh/tourze/php-monorepo)
A Symfony bundle for integrating WeChat Mini Program functionality into your application.
Table of Contents
- Features
- Installation
- Configuration
- Usage
- Console Commands
- Services
- Entities
- Exceptions
- Advanced Usage
- Error Handling
- Security
- Dependencies
- Contributing
- License
- Documentation
Features
- WeChat Mini Program API client
- Account management for Mini Program accounts
- Access token automatic refresh and caching
- Launch options handling
- Console commands for debugging and maintenance
- Entity traits for Mini Program integration
- JSON-RPC procedures for API access
Installation
composer require tourze/wechat-mini-program-bundle
Configuration
Add the bundle to your bundles.php:
<?php return [ // ... other bundles WechatMiniProgramBundle\WechatMiniProgramBundle::class => ['all' => true], ];
Console Commands
Get Access Token
Retrieve access tokens for all valid Mini Program accounts:
php bin/console wechat-mini-program:get-access-token
This command is designed to run as a cron job every 20 minutes to pre-fetch access tokens, reducing frontend wait times.
Query RID Information
Query RID information for debugging purposes:
php bin/console wechat-mini-program:open-api:query-rid <account_id> <rid>
Reset API Frequency
Reset API call frequency using AppSecret:
php bin/console wechat-mini-program:reset-api-frequency [account_id]
Usage
Basic Client Usage
use WechatMiniProgramBundle\Service\Client; use WechatMiniProgramBundle\Request\YourRequest; // Inject the client public function __construct( private readonly Client $client, ) {} // Make API requests $request = new YourRequest(); $response = $this->client->request($request);
Account Management
use WechatMiniProgramBundle\Entity\Account; use WechatMiniProgramBundle\Repository\AccountRepository; // Get account repository public function __construct( private readonly AccountRepository $accountRepository, ) {} // Find valid accounts $accounts = $this->accountRepository->findBy(['valid' => true]);
Launch Options
Use the LaunchOptionsAware trait in your entities to handle Mini Program launch options:
use WechatMiniProgramBundle\Entity\LaunchOptionsAware; class YourEntity { use LaunchOptionsAware; // Your entity properties and methods }
Services
Client
The main API client for making requests to WeChat Mini Program APIs:
- Automatic access token handling
- Token refresh on expiration
- Exception handling for API errors
- Caching support
Account Service
Service for managing Mini Program accounts:
- Account validation
- Access token retrieval
- Account configuration management
Launch Option Helper
Helper service for processing Mini Program launch options:
- Query parameter parsing
- Launch option validation
- Path parsing and handling
Entities
Account
Represents a WeChat Mini Program account with:
- Basic account information (name, appId, appSecret)
- Token and encryption key storage
- Timestamps and blame tracking
- IP tracing capabilities
Exceptions
AccountNotFoundException: Thrown when a requested account cannot be foundWechatApiException: Thrown when WeChat API returns an errorDecryptException: Thrown when decryption operations fail
Advanced Usage
Custom API Requests
Create custom API requests by implementing the RequestInterface:
use HttpClientBundle\Request\RequestInterface; use WechatMiniProgramBundle\Request\WithAccountRequest; class CustomRequest extends WithAccountRequest implements RequestInterface { public function getPath(): string { return '/your/custom/endpoint'; } public function getMethod(): string { return 'POST'; } public function getBody(): array { return [ 'custom_param' => 'value', ]; } }
Environment Configuration
Configure different API endpoints for different environments:
# Disable specific WeChat API domains
WECHAT_MIN_PROGRAM_DISABLE_BASE_URL_api.weixin.qq.com=true
WECHAT_MIN_PROGRAM_DISABLE_BASE_URL_api2.weixin.qq.com=true
Error Handling
The bundle provides comprehensive error handling:
use WechatMiniProgramBundle\Exception\WechatApiException; use WechatMiniProgramBundle\Exception\AccountNotFoundException; try { $response = $this->client->request($request); } catch (WechatApiException $e) { // Handle WeChat API errors $errorCode = $e->getCode(); $errorMessage = $e->getMessage(); } catch (AccountNotFoundException $e) { // Handle missing account errors }
Security
Access Token Security
- Access tokens are automatically cached and refreshed
- Tokens are stored securely using Symfony's cache component
- AppSecret values should be stored as environment variables
Best Practices
- Always validate account credentials before making API calls
- Use HTTPS for all API communications
- Regularly rotate AppSecret values
- Monitor API call frequency to avoid rate limiting
Reporting Security Issues
If you discover a security vulnerability, please send an email to security@example.com. All security vulnerabilities will be promptly addressed.
Dependencies
This bundle requires the following packages:
symfony/framework-bundle: ^7.3- Core Symfony frameworkdoctrine/orm: ^3.0- Object-relational mappingdoctrine/doctrine-bundle: ^2.13- Doctrine integrationtourze/http-client-bundle: 0.1.*- HTTP client functionalitytourze/json-rpc-core: 0.0.*- JSON-RPC supporttourze/backtrace-helper: 0.1.*- Debug tracing utilitieseasycorp/easyadmin-bundle: ^4- Admin interface support
For a complete list of dependencies, see the composer.json file.
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This bundle is licensed under the MIT License. See the LICENSE file for details.
Documentation
For more information about WeChat Mini Program development, refer to the official documentation.