Files

255 lines
8.5 KiB
PHP

<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace TuzhiEasyWeChat\Channels\Sharer;
use TuzhiEasyWeChat\Kernel\BaseClient;
use TuzhiEasyWeChat\Kernel\Exceptions\InvalidArgumentException;
/**
* Class Client.
*
* @author mingyoung <mingyoungcheung@gmail.com>
*/
class Client extends BaseClient
{
/**
* 获取绑定的分享员列表
* 该接口用于获取已绑定的分享员列表
* 店铺分享员在店铺管理页邀请;普通分享员通过邀请分享员接口邀请。
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
*
*
* @param $page 分页参数,页数
* @param $pageSize 每页数量(默认10,不超过30)
* @param $sharer_type 分享员类型
*
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getSharerList($sharer_type ,$page=1,$pageSize=10)
{
$params = [
'sharer_type' => $sharer_type,
'page'=>$page
];
if($pageSize){
$params['page_size'] = $pageSize;
}
return $this->httpPostJson('channels/ec/sharer/get_sharer_list', $params);
}
/**
* 获取分享员订单列表
* 该接口用于获取分享员订单列表
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
*
* @param $page 分页参数,页数
* @param $pageSize 每页数量(默认10,不超过30)
* @param $openid string 否 分享员openid
* @param $share_scene number 否 分享场景,枚举值详情请参考ShareScene @link https://developers.weixin.qq.com/doc/channels/API/sharer/get_sharer_order_list.html#a_ShareScene
* @param $start_time number 否 订单创建开始时间
* @param $end_time number 否 订单创建结束时间
*
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getSharerOrderList($page=1,$pageSize=10,$openid='',$start_time=0,$end_time=0,$share_scene='')
{
$params = [
'page'=>$page
];
if($pageSize){
$params['page_size'] = $pageSize;
}
if($openid){
$params['openid'] = $openid;
}
if($share_scene){
$params['share_scene'] = $share_scene;
}
if($start_time){
$params['start_time'] = $start_time;
}
if($end_time){
$params['end_time'] = $end_time;
}
return $this->httpPostJson('channels/ec/sharer/get_sharer_order_list', $params);
}
/**
* 邀请分享员
* 可通过此接口邀请小店普通分享员
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
* @param $username 邀请的用户微信号,非必填。若填写则生成的二维码为一人一码,仅允许该微信号用户扫码绑定;若不填写则生成的二维码为多人一码,允许多人扫码绑定
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function bind($username='')
{
$params = [];
if($username){
$params['username'] = $username;
}
$data = $this->getStream('channels/ec/sharer/bind', $params);
return $data;
}
/**
* 解绑分享员
* 该接口用于解除已绑定的小店普通分享员
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
* @param $openid_list array|string 需要解绑的分享员openid列表
*
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function unbind($openid_list)
{
$params = [
'openid_list' => $openid_list
];
return $this->httpPostJson('channels/ec/sharer/unbind', $params);
}
/**
* 获取绑定的分享员
* 可通过该接口获取绑定的分享员
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
* @param $openid string 分享员openid(填openid与username其中一个即可)
* @param $username string 分享员微信号(填openid与username其中一个即可)
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function searchSharer($openid='',$username='')
{
$params = [
'openid' => $openid
];
if($username){
$params = [
'username'=>$username
];
}
return $this->httpPostJson('channels/ec/sharer/search_sharer', $params);
}
/**
* 获取分享员专属的商品H5短链
* 可通过该接口获取分享员专属的商品H5短链
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
* @param $product_id 商品ID
* @param $openid string 是 分享员openid
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getSharerProductH5url($product_id,$openid)
{
$params = [
'product_id' => $product_id,
'openid'=>$openid
];
return $this->httpPostJson('channels/ec/sharer/get_sharer_product_h5url', $params);
}
/**
* 获取分享员专属的商品口令
* 可通过该接口获取分享员专属的商品口令
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
* @param $product_id 商品ID
* @param $openid string 是 分享员openid
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getSharerProductTaglink($product_id,$openid)
{
$params = [
'product_id' => $product_id,
'openid'=>$openid
];
return $this->httpPostJson('channels/ec/sharer/get_sharer_product_taglink', $params);
}
/**
* 获取分享员专属的商品二维码
* 可通过该接口获取分享员专属的商品二维码
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
* @param $product_id 商品ID
* @param $openid string 是 分享员openid
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getSharerProductQrcode($product_id,$openid)
{
$params = [
'product_id' => $product_id,
'openid'=>$openid
];
return $this->httpPostJson('channels/ec/sharer/get_sharer_product_qrcode', $params);
}
/**
* Get stream.
*
* @param string $endpoint
* @param array $params
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function getStream(string $endpoint, array $params)
{
// 发送原始请求,获取响应
$response = $this->requestRaw($endpoint, 'POST', ['json' => $params]);
// 获取原始响应内容
$rawResponse = $response->getBody()->getContents();
// 使用正则表达式移除 qrcode_img 字段及其内容
$pattern = '/"qrcode_img":"(.*?)",/';
$modifiedResponse = preg_replace($pattern, '', $rawResponse);
// 解析修改后的 JSON 字符串
$decodedResponse = json_decode($modifiedResponse, true);
// 检查 JSON 解码错误
if (json_last_error() === JSON_ERROR_NONE) {
return $decodedResponse;
} else {
echo "Failed to decode JSON response: " . json_last_error_msg();
}
return $this->castResponseToType($response, $this->app['config']->get('response_type'));
}
}