docs/src/5.x/mini-program/union.md
小程序联盟推广功能允许开发者管理推广计划,通过推广获得佣金收益。
$union = $app->union;
创建新的推广计划:
$result = $union->createPromotion('推广计划名称');
参数说明:
promotionSourceName string 推广计划名称返回结果:
{
"errcode": 0,
"errmsg": "ok",
"promotionSourcePid": "10000123",
"promotionSourceName": "推广计划名称"
}
删除指定的推广计划:
$result = $union->deletePromotion('10000123', '推广计划名称');
参数说明:
promotionSourcePid string 推广计划IDpromotionSourceName string 推广计划名称更新推广计划信息:
$result = $union->updatePromotion('10000123', '新的推广计划名称');
参数说明:
promotionSourcePid string 推广计划IDpromotionSourceName string 新的推广计划名称获取所有推广计划:
$result = $union->getPromotions();
返回结果:
{
"errcode": 0,
"errmsg": "ok",
"promotionList": [
{
"promotionSourcePid": "10000123",
"promotionSourceName": "推广计划1",
"createTime": 1635724800,
"status": 1
}
]
}
将商品添加到推广计划:
$result = $union->addProduct('10000123', [
'productId' => 'product_001',
'commissionRate' => 1500 // 佣金比例,单位为万分之一,1500表示15%
]);
从推广计划中移除商品:
$result = $union->removeProduct('10000123', 'product_001');
获取推广计划下的所有商品:
$result = $union->getProducts('10000123', $page, $pageSize);
参数说明:
promotionSourcePid string 推广计划IDpage int 页码,从1开始pageSize int 每页数量,最大50查询推广产生的订单:
$result = $union->getOrders([
'promotionSourcePid' => '10000123',
'startTime' => strtotime('-30 days'),
'endTime' => time(),
'page' => 1,
'pageSize' => 20
]);
返回结果:
{
"errcode": 0,
"errmsg": "ok",
"orderList": [
{
"orderId": "order_123456",
"productId": "product_001",
"productName": "商品名称",
"orderAmount": 10000,
"commissionAmount": 1500,
"orderTime": 1635724800,
"status": 2,
"buyerOpenid": "buyer_openid"
}
],
"totalCount": 156
}
查询佣金收益明细:
$result = $union->getCommissions([
'promotionSourcePid' => '10000123',
'startTime' => strtotime('-30 days'),
'endTime' => time(),
'page' => 1,
'pageSize' => 20
]);
返回结果:
{
"errcode": 0,
"errmsg": "ok",
"commissionList": [
{
"orderId": "order_123456",
"commissionAmount": 1500,
"commissionTime": 1635724800,
"status": 1,
"settleTime": 1635811200
}
],
"totalAmount": 15000,
"totalCount": 10
}
$result = $union->getOverview('10000123', [
'startTime' => strtotime('-30 days'),
'endTime' => time()
]);
返回结果:
{
"errcode": 0,
"errmsg": "ok",
"data": {
"clickCount": 1520,
"orderCount": 156,
"orderAmount": 1560000,
"commissionAmount": 234000,
"conversionRate": 10.26
}
}
$result = $union->getTrend('10000123', [
'startTime' => strtotime('-7 days'),
'endTime' => time(),
'granularity' => 'day' // day, hour
]);
use EasyWeChat\Factory;
$config = [
'app_id' => 'your-app-id',
'secret' => 'your-app-secret',
// ...
];
$app = Factory::miniProgram($config);
$union = $app->union;
// 1. 创建推广计划
$promotion = $union->createPromotion('春季促销推广');
if ($promotion['errcode'] === 0) {
$pid = $promotion['promotionSourcePid'];
echo "推广计划创建成功,ID: {$pid}\n";
// 2. 添加推广商品
$addProduct = $union->addProduct($pid, [
'productId' => 'spring_product_001',
'commissionRate' => 2000 // 20%佣金
]);
if ($addProduct['errcode'] === 0) {
echo "商品添加成功\n";
// 3. 获取推广链接
$promotionUrl = "https://your-mini-program.com?pid={$pid}&product_id=spring_product_001";
echo "推广链接: {$promotionUrl}\n";
// 4. 查询推广数据
sleep(1); // 模拟等待一段时间后查询
$overview = $union->getOverview($pid, [
'startTime' => strtotime('-1 day'),
'endTime' => time()
]);
if ($overview['errcode'] === 0) {
$data = $overview['data'];
echo "点击数: {$data['clickCount']}\n";
echo "订单数: {$data['orderCount']}\n";
echo "订单金额: " . ($data['orderAmount'] / 100) . "元\n";
echo "佣金收益: " . ($data['commissionAmount'] / 100) . "元\n";
echo "转化率: {$data['conversionRate']}%\n";
}
}
}
// 查询待结算佣金
$commissions = $union->getCommissions([
'promotionSourcePid' => $pid,
'status' => 0, // 0:待结算 1:已结算
'page' => 1,
'pageSize' => 50
]);
if ($commissions['errcode'] === 0) {
$totalPendingCommission = 0;
foreach ($commissions['commissionList'] as $commission) {
$totalPendingCommission += $commission['commissionAmount'];
}
echo "待结算佣金总额: " . ($totalPendingCommission / 100) . "元\n";
echo "待结算订单数: " . count($commissions['commissionList']) . "\n";
}
// 获取最近7天的推广趋势
$trend = $union->getTrend($pid, [
'startTime' => strtotime('-7 days'),
'endTime' => time(),
'granularity' => 'day'
]);
if ($trend['errcode'] === 0) {
echo "最近7天推广趋势:\n";
foreach ($trend['data'] as $dayData) {
$date = date('Y-m-d', $dayData['timestamp']);
echo "{$date}: 点击{$dayData['clickCount']}次, 订单{$dayData['orderCount']}个\n";
}
}
// 计算推广ROI
$overview = $union->getOverview($pid, [
'startTime' => strtotime('-30 days'),
'endTime' => time()
]);
if ($overview['errcode'] === 0) {
$data = $overview['data'];
$roi = $data['orderAmount'] > 0 ? ($data['commissionAmount'] / $data['orderAmount']) * 100 : 0;
echo "推广ROI: {$roi}%\n";
}
| 错误码 | 说明 |
|---|---|
| 0 | 成功 |
| -1 | 系统繁忙 |
| 40001 | 获取access_token时AppSecret错误 |
| 40013 | 不合法的AppID |
| 41001 | 缺少access_token参数 |
| 45009 | 接口调用超过限额 |
| 48001 | api功能未授权 |