tourze/wechat-work-bundle
最新稳定版本:1.1.0
Composer 安装命令:
composer require tourze/wechat-work-bundle
包简介
企业微信集成能力模块
README 文档
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.
统计信息
- 总下载量: 3.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 17
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-22