tourze / wechat-work-bundle
企业微信集成能力模块
Installs: 3 204
Dependents: 16
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/wechat-work-bundle
Requires
- php: ^8.1
- doctrine/collections: ^2.3
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- nesbot/carbon: ^2.72 || ^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-kernel: ^6.4
- symfony/routing: ^6.4
- symfony/serializer: ^6.4
- symfony/yaml: ^6.4 || ^7.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/enum-extra: 0.1.*
- tourze/http-client-bundle: 0.1.*
- tourze/symfony-cron-job-bundle: 0.1.*
- tourze/wechat-work-contracts: 0.0.*
- yiisoft/json: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
README
WeChatWork Bundle provides WeChatWork API integration capabilities for Symfony applications.
Table of Contents
- Dependencies
- Installation
- Features
- Configuration
- Quick Start
- Console Commands
- Advanced Usage
- Entity Description
- Security
- Reference Documentation
- License
Dependencies
This bundle requires:
- PHP 8.1 or higher
- Symfony 7.3 or higher
- Doctrine ORM 3.0 or higher
tourze/wechat-work-contractsfor interface definitionstourze/doctrine-timestamp-bundlefor timestamp handlingtourze/doctrine-track-bundlefor tracking changestourze/doctrine-user-bundlefor user-related functionalitytourze/doctrine-ip-bundlefor IP trackingtourze/doctrine-resolve-target-entity-bundlefor entity resolutiontourze/http-client-bundlefor HTTP client functionalitytourze/symfony-cron-job-bundlefor scheduled taskstourze/enum-extrafor enum utilitiesnesbot/carbonfor date/time handlingyiisoft/jsonfor JSON processing
Installation
composer require tourze/wechat-work-bundle
Features
- WeChatWork application management
- Automatic access token refresh
- Application information synchronization
- Enterprise information management
- Full Doctrine ORM support
- Scheduled task support
Configuration
Basic Configuration
Enable the bundle in your config/bundles.php:
<?php return [ // Other bundles... WechatWorkBundle\WechatWorkBundle::class => ['all' => true], ];
Database Configuration
Run migrations to create required tables:
php bin/console doctrine:migrations:migrate
Quick Start
1. Configure Enterprise Information
First, create a Corp entity and at least one Agent:
use WechatWorkBundle\Entity\Corp; use WechatWorkBundle\Entity\Agent; // Create enterprise $corp = new Corp(); $corp->setName('My Company'); $corp->setCorpId('your_corp_id'); // Create application $agent = new Agent(); $agent->setName('My App'); $agent->setAgentId('your_agent_id'); $agent->setSecret('your_agent_secret'); $agent->setCorp($corp); $entityManager->persist($corp); $entityManager->persist($agent); $entityManager->flush();
2. Using Services
use WechatWorkBundle\Service\WorkService; class MyService { public function __construct( private WorkService $workService ) {} public function sendMessage(): void { // The service automatically handles access token refresh $this->workService->refreshAgentAccessToken($agent); // Use the service to make API calls // Implementation depends on your specific needs } }
3. Automatic Access Token Refresh
The bundle automatically handles access token refresh. You can also manually refresh tokens:
php bin/console wechat-work:refresh-agent-access-token
Console Commands
Refresh Access Token
Refresh access tokens for all agents:
php bin/console wechat-work:refresh-agent-access-token
Synchronize Application Information
Synchronize application information from WeChatWork API:
php bin/console wechat-work:sync-agent-info
Advanced Usage
Custom Request Implementation
You can extend the WorkService to implement custom API requests:
use WechatWorkBundle\Service\WorkService; use WechatWorkBundle\Entity\Agent; class CustomWorkService extends WorkService { public function sendTextMessage(Agent $agent, string $content): array { $this->refreshAgentAccessToken($agent); $data = [ 'touser' => '@all', 'msgtype' => 'text', 'agentid' => $agent->getAgentId(), 'text' => [ 'content' => $content ] ]; return $this->request([ 'url' => $this->getBaseUrl() . '/cgi-bin/message/send', 'method' => 'POST', 'query' => ['access_token' => $agent->getAccessToken()], 'json' => $data ]); } }
Service Extension
Create custom services that utilize the WeChatWork API:
use WechatWorkBundle\Service\WorkService; use WechatWorkBundle\Repository\AgentRepository; class NotificationService { public function __construct( private WorkService $workService, private AgentRepository $agentRepository ) {} public function sendNotification(string $message): void { $agents = $this->agentRepository->findBy(['active' => true]); foreach ($agents as $agent) { // Send notification via each active agent $this->workService->refreshAgentAccessToken($agent); // Implement your notification logic here } } }
Entity Description
Corp (Enterprise)
Represents a WeChatWork enterprise:
name: Enterprise namecorpId: Unique enterprise identifieragents: Collection of associated applications
Agent (Application)
Represents a WeChatWork application within an enterprise:
name: Application nameagentId: Application identifiersecret: Application secret for API accessaccessToken: Current access token (auto-managed)accessTokenExpireTime: Token expiration timecorp: Associated enterprise
Security
Access Token Management
Access tokens are automatically managed by the bundle:
- Tokens are refreshed before expiration
- Failed refresh attempts are logged
- Tokens are stored securely in the database
Sensitive Data
Ensure proper protection of sensitive information:
- Agent secrets should be stored securely
- Access tokens are auto-generated and managed
- Consider encryption for sensitive database fields
Reference Documentation
License
This bundle is released under the MIT License. See the bundled LICENSE file for details.