定制 tourze/wechat-mini-program-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/wechat-mini-program-bundle

Composer 安装命令:

composer require tourze/wechat-mini-program-bundle

包简介

微信小程序核心模块与功能服务

README 文档

README

English | 中文

PHP Version [Latest Version] (https://packagist.org/packages/tourze/wechat-mini-program-bundle) [Total Downloads] (https://packagist.org/packages/tourze/wechat-mini-program-bundle) [License] (https://packagist.org/packages/tourze/wechat-mini-program-bundle) [Build Status] (https://github.com/tourze/php-monorepo/actions) [Code Coverage] (https://codecov.io/gh/tourze/php-monorepo)

A Symfony bundle for integrating WeChat Mini Program functionality into your application.

Table of Contents

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 found
  • WechatApiException: Thrown when WeChat API returns an error
  • DecryptException: 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 framework
  • doctrine/orm: ^3.0 - Object-relational mapping
  • doctrine/doctrine-bundle: ^2.13 - Doctrine integration
  • tourze/http-client-bundle: 0.1.* - HTTP client functionality
  • tourze/json-rpc-core: 0.0.* - JSON-RPC support
  • tourze/backtrace-helper: 0.1.* - Debug tracing utilities
  • easycorp/easyadmin-bundle: ^4 - Admin interface support

For a complete list of dependencies, see the composer.json file.

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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.

tourze/wechat-mini-program-bundle 适用场景与选型建议

tourze/wechat-mini-program-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.54k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tourze/wechat-mini-program-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tourze/wechat-mini-program-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 tourze/wechat-mini-program-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 18
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-28