tourze/wechat-official-account-contracts

微信公众号用户接口定义和合约规范

Installs: 752

Dependents: 3

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/tourze/wechat-official-account-contracts

0.0.1 2025-05-16 15:15 UTC

This package is auto-updated.

Last update: 2025-10-31 05:59:56 UTC


README

PHP Version License Build Status Code Coverage

English | 中文

WeChat Official Account contracts and interfaces for PHP applications.

Installation

composer require tourze/wechat-official-account-contracts

Quick Start

This package provides essential interfaces for WeChat Official Account integration:

<?php

use Tourze\WechatOfficialAccountContracts\UserInterface;
use Tourze\WechatOfficialAccountContracts\UserLoaderInterface;
use Tourze\WechatOfficialAccountContracts\OfficialAccountInterface;

// Implement user interface
class MyUser implements UserInterface
{
    public function getOpenId(): string
    {
        return 'user_open_id';
    }

    public function getUnionId(): ?string
    {
        return 'user_union_id';
    }

    public function getAvatarUrl(): ?string
    {
        return 'https://example.com/avatar.jpg';
    }

    public function getOfficialAccount(): ?OfficialAccountInterface
    {
        return new MyOfficialAccount();
    }
}

// Implement user loader interface
class MyUserLoader implements UserLoaderInterface
{
    public function loadUserByOpenId(string $openId): ?UserInterface
    {
        // Load user by OpenID
        return new MyUser();
    }

    public function loadUserByUnionId(string $unionId): ?UserInterface
    {
        // Load user by UnionID
        return new MyUser();
    }

    public function syncUserByOpenId(OfficialAccountInterface $officialAccount, string $openId): ?UserInterface
    {
        // Sync user data from WeChat API
        return new MyUser();
    }
}

Interfaces

UserInterface

Represents a WeChat user with the following methods:

  • getOpenId(): Get user's OpenID
  • getUnionId(): Get user's UnionID (optional)
  • getAvatarUrl(): Get user's avatar URL (optional)
  • getOfficialAccount(): Get associated official account (optional)

UserLoaderInterface

Provides methods to load and sync WeChat users:

  • loadUserByOpenId(string $openId): Load user by OpenID
  • loadUserByUnionId(string $unionId): Load user by UnionID
  • syncUserByOpenId(OfficialAccountInterface $officialAccount, string $openId): Sync user data

OfficialAccountInterface

Represents a WeChat Official Account:

  • getAppId(): Get the App ID of the official account (optional)

License

MIT License. See LICENSE for details.