//MiniProgram::getAssessToken()方法是获取assesstoken
use fast\Http;
class LiveService
{
//访问接口
public static function request($url, $params ,$encode = true,$option=[])
{
if ($encode) {
$params = json_encode($params);
}
$res = Http::post($url,$params,$option);
return json_decode($res,true);
}
//获取直播间列表
public static function getLiveList($start = 0, $limit = 20, $action = '',$room_id = 0)
{
$url = 'https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=' . MiniProgram::getAssessToken();
$params = [
'start'=>$start,
'limit'=>$limit,
];
if (!empty($action)){
$params['action'] = $action;
$params['room_id'] = $room_id;
}
return self::request($url,$params);
}
/**
* 删除直播间
* @param $roomid
* @return mixed
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public static function del($roomid)
{
$url = 'https://api.weixin.qq.com/wxaapi/broadcast/room/deleteroom?access_token=' . MiniProgram::getAssessToken();
$params = ['id'=>intval($roomid)];
return self::request($url,$params,true,[CURLOPT_HTTPHEADER=>['Content-Type: application/json; charset=utf-8']]);
}
/**
* 上传素材
* @param $file
* @param string $type
* @return mixed
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public static function uploadMedia($file,$type='image')
{
$url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='. MiniProgram::getAssessToken() . '&type=' . $type;
$params = ['media'=> new \CURLFile($file) ];
$res = Http::post($url,$params);
return json_decode($res,true);
}
/**
* 新增商品
* @param $goodsId
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \think\exception\DbException
*/
public static function addGoods($goodsId)
{
$goods = \app\admin\model\goods\Goods::get($goodsId);
//上传图片
$goodsImage = ROOT_PATH . 'public' . DS . $goods['goods_image'];
$uploadResult = self::uploadMedia($goodsImage);
if (!empty($uploadResult['errcode'])){
exception($uploadResult['errmsg']);
}
$addData = [
'goodsInfo'=>[
'coverImgUrl'=>$uploadResult['media_id'],
'name'=>$goods['live_title'],
'priceType'=>1,
'price'=>$goods['deposit'],
// 'url'=>'pages/goods/goodsDetail?id='.$goodsId,]
'url'=>'yb_shopv2/native/tabbar1/index?id='.$goodsId,]
];
// echo 'https://api.weixin.qq.com/wxaapi/broadcast/goods/add?access_token=' . MiniProgram::getAssessToken() .PHP_EOL;
// die(json_encode($addData));
$addUrl = 'https://api.weixin.qq.com/wxaapi/broadcast/goods/add?access_token=' . MiniProgram::getAssessToken();
// $addres = Http::post($addUrl,json_encode($addData),[CURLOPT_HTTPHEADER=>['Content-Type: application/json; charset=utf-8']]);
// $addres = json_decode($addres,true);
$addres = self::request($addUrl,$addData,true,[CURLOPT_HTTPHEADER=>['Content-Type: application/json; charset=utf-8']]);
if (!empty($addres['errcode'])){
exception($addres['errmsg']);
}
$liveGoodsData = [
'goods_id'=>$goodsId,
'audit_id'=>$addres['auditId'],
'wx_goodsid'=>$addres['goodsId'],
'goods_image'=>$goods['goods_image'],
'live_price'=>$goods['deposit'],
'live_status'=>0
];
\app\admin\model\live\Goods::create($liveGoodsData);
}
/**
* 同步商品状态 命令行执行
* @param array $liveGoodsIds 最大长度20
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \think\exception\DbException
*/
public static function syncGoods(array $liveGoodsIds)
{
$url = 'https://api.weixin.qq.com/wxa/business/getgoodswarehouse?access_token=' . MiniProgram::getAssessToken();
// $liveGoodsIds = \app\admin\model\live\Goods::where(['goods_id'=>['in',$ids]])->column('wx_goodsid');
$params = ['goods_ids'=>$liveGoodsIds];
$res = self::request($url,$params);
if (!empty($res['errcode'])){
exception($res['errmsg']);
}
foreach ($res['goods'] as $good){
$g = \app\admin\model\live\Goods::get(['wx_goodsid'=>$good['goods_id']]);
if (!empty($g)){
$g->live_status = $good['audit_status'];
$g->save();
}
}
}
/**
* 重新提交审核
* @param $goodsId
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \think\exception\DbException
*/
public static function audit($goodsId)
{
$url = 'https://api.weixin.qq.com/wxaapi/broadcast/goods/audit?access_token='. MiniProgram::getAssessToken();
$goods = \app\admin\model\live\Goods::get(['goods_id'=>$goodsId]);
if (!empty($goods)) {
$params = ['goodsId'=>$goods['wx_goodsid']];
$res = self::request($url,$params,true,[CURLOPT_HTTPHEADER=>['Content-Type: application/json; charset=utf-8']]);
if (!empty($res['errcode'])){
exception($res['errmsg']);
}
}else{
exception('商品不存在');
}
}
/**
* 删除直播间商品
* @param $goodsId
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \think\exception\DbException
*/
public static function delGoods($goodsId)
{
$url = 'https://api.weixin.qq.com/wxaapi/broadcast/goods/delete?access_token='. MiniProgram::getAssessToken();
$goods = \app\admin\model\live\Goods::get(['goods_id'=>$goodsId]);
if (!empty($goods)) {
$params = ['goodsId'=>$goods['wx_goodsid']];
$res = self::request($url,$params,true,[CURLOPT_HTTPHEADER=>['Content-Type: application/json; charset=utf-8']]);
if (!empty($res['errcode'])){
exception($res['errmsg']);
}
$goods->delete();
}
}
}
包含功能:
- 直播间列表
- 删除直播间
- 上传素材
- 新增商品
- 获取商品状态
- 重新提交审核
- 删除商品