tourze / wechat-mini-program-share-bundle
微信小程序分享功能包,支持邀请码生成、分享统计和用户追踪
Installs: 49
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/wechat-mini-program-share-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
- hashids/hashids: ^5.0
- monolog/monolog: ^3.1
- nesbot/carbon: ^2.72 || ^3
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/event-dispatcher-contracts: ^2.5 | ^3
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/routing: ^6.4
- symfony/security-bundle: ^6.4
- symfony/security-core: ^6.4
- symfony/serializer: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/doctrine-async-bundle: 0.1.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-ip-bundle: 0.0.*
- tourze/doctrine-snowflake-bundle: 0.1.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/doctrine-user-bundle: 0.0.*
- tourze/easy-admin-attribute: 0.1.*
- tourze/json-rpc-cache-bundle: 0.1.*
- tourze/json-rpc-core: 0.0.*
- tourze/wechat-mini-program-auth-bundle: 0.0.*
- tourze/wechat-mini-program-bundle: 0.1.*
- yiisoft/arrays: ^3
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-11-01 19:31:11 UTC
README
A Symfony bundle for handling WeChat Mini Program sharing functionality, including share code generation, visit tracking, and invitation management.
Table of Contents
Features
- Share Code Management: Generate and manage mini program share codes
- Visit Tracking: Track user visits through shared links
- Invitation System: Handle user invitations and track referral relationships
- Analytics: Comprehensive logging and analytics for sharing behavior
- Event-Driven: Symfony event system integration for extensibility
Installation
composer require tourze/wechat-mini-program-share-bundle
Configuration
Add the bundle to your bundles.php:
return [ // ... WechatMiniProgramShareBundle\WechatMiniProgramShareBundle::class => ['all' => true], ];
Quick Start
1. Generate Share Configuration
Use the GetWechatMiniProgramPageShareConfig procedure to generate share paths with tracking parameters:
use WechatMiniProgramShareBundle\Procedure\GetWechatMiniProgramPageShareConfig; $procedure = new GetWechatMiniProgramPageShareConfig($hashids, $security); $procedure->path = '/pages/product/detail'; $procedure->params = ['id' => 123]; $procedure->config = [ 'title' => 'Product Title', 'path' => '/pages/product/detail?id=123' ]; $shareConfig = $procedure->execute();
2. Handle Share Code Information
Use the GetWechatMiniProgramShareCodeInfo procedure to process share codes:
use WechatMiniProgramShareBundle\Procedure\GetWechatMiniProgramShareCodeInfo; $procedure = new GetWechatMiniProgramShareCodeInfo( $codeRepository, $doctrineService, $security, $logger ); $procedure->id = 'share-code-id'; $result = $procedure->execute(); // Returns redirect information for the mini program
3. Track Invitations
The bundle automatically tracks user invitations through the InviteVisitSubscriber:
// The subscriber listens to CodeToSessionResponseEvent // and automatically creates InviteVisitLog entries
Entities
ShareCode
Represents a share code with validation and tracking information.
ShareVisitLog
Tracks visits through share codes.
InviteVisitLog
Records invitation relationships between users.
ShareTicketLog
Logs share ticket operations.
Events
InviteUserEvent
Dispatched when a user is invited through sharing.
use WechatMiniProgramShareBundle\Event\InviteUserEvent; // Listen to the event public function onInviteUser(InviteUserEvent $event): void { $log = $event->getInviteVisitLog(); // Handle invitation logic }
Advanced Usage
Custom Event Listeners
You can create custom event listeners to extend the sharing functionality:
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; use WechatMiniProgramShareBundle\Event\InviteUserEvent; #[AsEventListener] class CustomInviteListener { public function onInviteUser(InviteUserEvent $event): void { $log = $event->getInviteVisitLog(); // Custom logic for handling invitations if ($log->isNewUser()) { // Send welcome message $this->sendWelcomeMessage($log->getVisitUser()); } // Award points to the inviter $this->awardInvitationPoints($log->getShareUser()); } }
Custom Share Code Validation
Implement custom validation logic for share codes:
use WechatMiniProgramShareBundle\Entity\ShareCode; use Symfony\Component\Validator\Context\ExecutionContextInterface; class CustomShareCodeValidator { public function validate(ShareCode $shareCode, ExecutionContextInterface $context): void { if (!$this->isValidDomain($shareCode->getLinkUrl())) { $context->buildViolation('Invalid domain for share URL') ->atPath('linkUrl') ->addViolation(); } } }
Extending Share Analytics
Create custom analytics by listening to share events:
use WechatMiniProgramShareBundle\Entity\ShareVisitLog; use Doctrine\ORM\Event\PostPersistEventArgs; class ShareAnalyticsListener { public function postPersist(PostPersistEventArgs $args): void { $entity = $args->getObject(); if ($entity instanceof ShareVisitLog) { // Send analytics data to external service $this->analyticsService->track('share_visit', [ 'code_id' => $entity->getCode()->getId(), 'user_id' => $entity->getUser()?->getId(), 'timestamp' => $entity->getCreateTime(), ]); } } }
Requirements
- PHP 8.1+
- Symfony 7.3+
- Doctrine ORM 3.0+
- WeChat Mini Program Bundle (tourze/wechat-mini-program-bundle)
- WeChat Mini Program Auth Bundle (tourze/wechat-mini-program-auth-bundle)
- Hashids PHP 5.0+
License
MIT