初始上传
This commit is contained in:
198
addon/live/model/Goods.php
Executable file
198
addon/live/model/Goods.php
Executable file
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\live\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Cron;
|
||||
use app\model\upload\Upload;
|
||||
|
||||
class Goods extends BaseModel
|
||||
{
|
||||
private $liveStatus = [
|
||||
0 => '未审核',
|
||||
1 => '审核中',
|
||||
2 => '审核通过',
|
||||
3 => '审核驳回'
|
||||
];
|
||||
|
||||
/**
|
||||
* 获取直播间列表
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getGoodsPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [])
|
||||
{
|
||||
$data = model('weapp_goods')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join);
|
||||
if (!empty($data[ 'list' ])) {
|
||||
foreach ($data[ 'list' ] as $k => $item) {
|
||||
$data[ 'list' ][ $k ][ 'status_name' ] = $this->liveStatus[ $item[ 'status' ] ] ?? '';
|
||||
}
|
||||
}
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步商品库商品
|
||||
*/
|
||||
public function syncGoods($start, $limit, $site_id, $status = 2)
|
||||
{
|
||||
$live = new Live($site_id);
|
||||
$result = $live->getGoodsList($start, $limit, $status);
|
||||
if ($result[ 'code' ] < 0) return $result;
|
||||
|
||||
if (!empty($result[ 'data' ][ 'goods' ])) {
|
||||
$upload = new Upload($site_id, 'shop');
|
||||
$upload->setPath('upload/live/goods/');
|
||||
$goodsID_arr = [];
|
||||
foreach ($result[ 'data' ][ 'goods' ] as $item) {
|
||||
$goodsID_arr[] = $item[ 'goodsId' ];
|
||||
preg_match("/(pages\/goods\/detail\?sku_id=)(\d*)$/", $item[ 'url' ], $matches);
|
||||
$data = [
|
||||
'site_id' => $site_id,
|
||||
'goods_id' => $item[ 'goodsId' ],
|
||||
'name' => $item[ 'name' ],
|
||||
'price' => $item[ 'price' ],
|
||||
'status' => $status,
|
||||
'url' => $item[ 'url' ],
|
||||
'sku_id' => $matches[ 2 ] ?? 0,
|
||||
'third_party_tag' => $item[ 'thirdPartyTag' ]
|
||||
];
|
||||
$room_info = model('weapp_goods')->getInfo([ [ 'goods_id', '=', $item[ 'goodsId' ] ], [ 'site_id', '=', $site_id ] ], 'id');
|
||||
if (empty($room_info)) {
|
||||
if (is_url($item[ 'coverImgUrl' ])) {
|
||||
$pull_result = $upload->remotePull($item[ 'coverImgUrl' ]);
|
||||
$pull_result = $pull_result[ 'data' ];
|
||||
if (isset($pull_result[ 'pic_path' ]) && !empty($pull_result[ 'pic_path' ])) {
|
||||
$data[ 'cover_img' ] = $pull_result[ 'pic_path' ];
|
||||
} else {
|
||||
$data[ 'cover_img' ] = $item[ 'coverImgUrl' ];
|
||||
}
|
||||
}
|
||||
model('weapp_goods')->add($data);
|
||||
} else {
|
||||
model('weapp_goods')->update($data, [ [ 'id', '=', $room_info[ 'id' ] ] ]);
|
||||
}
|
||||
}
|
||||
|
||||
$list_id_arr = model('weapp_goods')->getList([ [ 'site_id', '=', $site_id ] ]);
|
||||
foreach ($list_id_arr as $key => $val) {
|
||||
if (!in_array($val[ 'goods_id' ], $goodsID_arr)) {
|
||||
model('weapp_goods')->delete([ 'goods_id' => $val[ 'goods_id' ] ]);
|
||||
}
|
||||
}
|
||||
$total_page = ceil($result[ 'data' ][ 'total' ] / $limit);
|
||||
return $this->success([ 'page' => $start, 'total_page' => $total_page ]);
|
||||
} else {
|
||||
return $this->success([ 'page' => $start, 'total_page' => 1 ]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加商品
|
||||
* @param $param
|
||||
*/
|
||||
public function addGoods($param)
|
||||
{
|
||||
if (!preg_match("/(pages\/goods\/detail\?sku_id=)(\d*)$/", $param[ 'url' ], $matches)) {
|
||||
return $this->error('', '商品链接格式不正确');
|
||||
}
|
||||
$live = new Live($param[ 'site_id' ]);
|
||||
if (is_url($param[ 'goods_pic' ])) {
|
||||
$upload = new Upload($param[ 'site_id' ]);
|
||||
$goods_pic = $upload->setPath("common/temp/" . date("Ymd") . '/')->remotePull($param[ 'goods_pic' ])[ 'data' ][ 'pic_path' ] ?? '';
|
||||
$result = $live->addImageMedia($goods_pic);
|
||||
} else {
|
||||
$result = $live->addImageMedia($param[ 'goods_pic' ]);
|
||||
}
|
||||
if ($result[ 'code' ] < 0) return $result;
|
||||
|
||||
$audit = [
|
||||
'goodsInfo' => [
|
||||
'coverImgUrl' => $result[ 'data' ][ 'media_id' ],
|
||||
'name' => $param[ 'name' ],
|
||||
'priceType' => $param[ 'price_type' ],
|
||||
'price' => $param[ 'price' ],
|
||||
'price2' => $param[ 'price2' ],
|
||||
'url' => $param[ 'url' ]
|
||||
]
|
||||
];
|
||||
if ($param[ 'price_type' ] == 1) unset($audit[ 'goodsInfo' ][ 'price2' ]);
|
||||
$result = $live->addGoodsAudit($audit);
|
||||
if ($result[ 'code' ] < 0) return $result;
|
||||
|
||||
$data = [
|
||||
'site_id' => $param[ 'site_id' ],
|
||||
'goods_id' => $result[ 'data' ][ 'goodsId' ],
|
||||
'name' => $param[ 'name' ],
|
||||
'cover_img' => $param[ 'goods_pic' ],
|
||||
'price' => $param[ 'price' ],
|
||||
'status' => 1,
|
||||
'url' => $param[ 'url' ],
|
||||
'audit_id' => $result[ 'data' ][ 'auditId' ],
|
||||
'sku_id' => $matches[ 2 ],
|
||||
'third_party_tag' => 2
|
||||
];
|
||||
$result = model('weapp_goods')->add($data);
|
||||
|
||||
$cron_info = model("cron")->getInfo([ [ 'event', '=', 'LiveGoodsStatus' ], [ 'relate_id', '=', $param[ 'site_id' ] ] ]);
|
||||
if (empty($cron_info)) {
|
||||
$cron = new Cron();
|
||||
$cron->addCron(2, 10, '小程序商品获取审核状态', 'LiveGoodsStatus', time(), $param[ 'site_id' ]);
|
||||
}
|
||||
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品
|
||||
* @param $id
|
||||
* @param $site_id
|
||||
*/
|
||||
public function deleteGoods($id, $site_id)
|
||||
{
|
||||
$info = model('weapp_goods')->getInfo([ [ 'site_id', '=', $site_id ], [ 'id', '=', $id ] ], 'goods_id');
|
||||
if (empty($info)) return $this->error('', '未获取到商品信息');
|
||||
|
||||
$live = new Live($site_id);
|
||||
$result = $live->deleteGoods($info[ 'goods_id' ]);
|
||||
if ($result[ 'code' ] < 0) return $result;
|
||||
|
||||
$res = model('weapp_goods')->delete([ [ 'site_id', '=', $site_id ], [ 'id', '=', $id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播商品审核状态
|
||||
* @param $id
|
||||
*/
|
||||
public function getGoodsAuditStatus($site_id)
|
||||
{
|
||||
$prefix = config("database")[ "connections" ][ "mysql" ][ "prefix" ];
|
||||
$data = model('weapp_goods')->query("SELECT GROUP_CONCAT(goods_id) as goods_id FROM {$prefix}weapp_goods WHERE site_id = {$site_id} AND status = 1");
|
||||
if (isset($data[ 0 ]) && isset($data[ 0 ][ 'goods_id' ]) && !empty($data[ 0 ][ 'goods_id' ])) {
|
||||
$live = new Live($site_id);
|
||||
$result = $live->getGoodsStatus(explode(',', $data[ 0 ][ 'goods_id' ]));
|
||||
if ($result[ 'code' ] < 0) return $result;
|
||||
|
||||
foreach ($result[ 'data' ] as $item) {
|
||||
if ($item[ 'audit_status' ] != 1) {
|
||||
model('weapp_goods')->update([ 'status' => $item[ 'audit_status' ] ], [ [ 'site_id', '=', $site_id ], [ 'goods_id', '=', $item[ 'goods_id' ] ] ]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
298
addon/live/model/Live.php
Executable file
298
addon/live/model/Live.php
Executable file
@@ -0,0 +1,298 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\live\model;
|
||||
|
||||
use addon\weapp\model\Config as WeappConfigModel;
|
||||
use addon\wxoplatform\model\Config as WxOplatformConfigModel;
|
||||
use app\model\BaseModel;
|
||||
use EasyWeChat\Factory;
|
||||
|
||||
class Live extends BaseModel
|
||||
{
|
||||
private $app;
|
||||
|
||||
/**
|
||||
* 微信直播间接口错误码
|
||||
* @var array
|
||||
*/
|
||||
private $room_error = [
|
||||
1003 => '商品id不存在',
|
||||
47001 => '入参格式不符合规范',
|
||||
200002 => '入参错误',
|
||||
300001 => '禁止创建/更新商品 或 禁止编辑&更新房间',
|
||||
300002 => '名称长度不符合规则',
|
||||
300006 => '图片上传失败',
|
||||
300022 => '此房间号不存在',
|
||||
300023 => '房间状态 拦截(当前房间状态不允许此操作)',
|
||||
300024 => '商品不存在',
|
||||
300025 => '商品审核未通过',
|
||||
300026 => '房间商品数量已经满额',
|
||||
300027 => '导入商品失败',
|
||||
300028 => '房间名称违规',
|
||||
300029 => '主播昵称违规',
|
||||
300030 => '主播微信号不合法',
|
||||
300031 => '直播间封面图不合规',
|
||||
300032 => '直播间分享图违规',
|
||||
300033 => '添加商品超过直播间上限',
|
||||
300034 => '主播微信昵称长度不符合要求',
|
||||
300035 => '主播微信号不存在',
|
||||
300036 => '主播微信号未实名认证',
|
||||
];
|
||||
|
||||
/**
|
||||
* 微信直播商品接口错误码
|
||||
* @var array
|
||||
*/
|
||||
private $goods_error = [
|
||||
300001 => '商品创建功能被封禁',
|
||||
300002 => '名称长度不符合规则',
|
||||
300003 => '价格输入不合规',
|
||||
300004 => '商品名称存在违规违法内容',
|
||||
300005 => '商品图片存在违规违法内容',
|
||||
300006 => '图片上传失败',
|
||||
300007 => '线上小程序版本不存在该链接',
|
||||
300008 => '添加商品失败',
|
||||
300009 => '商品审核撤回失败',
|
||||
300010 => '商品审核状态不对',
|
||||
300011 => 'API不允许操作非API创建的商品',
|
||||
300012 => '没有提审额度(每天500次提审额度)',
|
||||
300013 => '提审失败',
|
||||
300014 => '审核中,无法删除',
|
||||
300017 => '商品未提审',
|
||||
300021 => '商品添加成功,审核失败',
|
||||
];
|
||||
|
||||
|
||||
public function __construct($site_id = 0)
|
||||
{
|
||||
//微信小程序配置
|
||||
$weapp_config_model = new WeappConfigModel();
|
||||
$weapp_config = $weapp_config_model->getWeappConfig($site_id)[ "data" ][ "value" ];
|
||||
|
||||
if (isset($weapp_config[ 'is_authopen' ]) && addon_is_exit('wxoplatform')) {
|
||||
$plateform_config_model = new WxOplatformConfigModel();
|
||||
$plateform_config = $plateform_config_model->getOplatformConfig()[ "data" ][ "value" ];
|
||||
|
||||
$config = [
|
||||
'app_id' => $plateform_config[ "appid" ] ?? '',
|
||||
'secret' => $plateform_config[ "secret" ] ?? '',
|
||||
'token' => $plateform_config[ "token" ] ?? '',
|
||||
'aes_key' => $plateform_config[ "aes_key" ] ?? '',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'permission' => 0777,
|
||||
'file' => 'runtime/log/wechat/oplatform.logs',
|
||||
],
|
||||
];
|
||||
$open_platform = Factory::openPlatform($config);
|
||||
$this->app = $open_platform->miniProgram($weapp_config[ 'authorizer_appid' ], $weapp_config[ 'authorizer_refresh_token' ]);
|
||||
} else {
|
||||
$config = [
|
||||
'app_id' => $weapp_config[ "appid" ] ?? '',
|
||||
'secret' => $weapp_config[ "appsecret" ] ?? '',
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'permission' => 0777,
|
||||
'file' => 'runtime/log/wechat/easywechat.logs',
|
||||
],
|
||||
];
|
||||
$this->app = Factory::miniProgram($config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加永久图片素材
|
||||
* @param $path
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function addImageMedia($path)
|
||||
{
|
||||
try {
|
||||
$result = $this->app->media->uploadImage($path);
|
||||
if (isset($result[ 'errcode' ])) {
|
||||
return $this->error('', '图片上传失败');
|
||||
} else {
|
||||
return $this->success($result);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播间列表
|
||||
* @param int $start
|
||||
* @param int $limit
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function getRoomList($start = 0, $limit = PAGE_LIST_ROWS)
|
||||
{
|
||||
try {
|
||||
$result = $this->app->live->room->getLiveInfo([ 'start' => $start, 'limit' => $limit ]);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
|
||||
return $this->success([ 'list' => $result[ 'room_info' ], 'total' => $result[ 'total' ] ]);
|
||||
} else {
|
||||
return $this->error('', $this->room_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取回放视频
|
||||
* @param $room_id
|
||||
* @param int $start
|
||||
* @param int $limit
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function getPlaybackInfo($room_id, $start = 0, $limit = PAGE_LIST_ROWS)
|
||||
{
|
||||
try {
|
||||
$data = [
|
||||
'action' => 'get_replay',
|
||||
'room_id' => $room_id,
|
||||
'start' => $start,
|
||||
'limit' => $limit
|
||||
];
|
||||
$result = $this->app->live->room->getLiveInfo($data);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
|
||||
return $this->success([ 'list' => $result[ 'live_replay' ], 'total' => $result[ 'total' ] ]);
|
||||
} else {
|
||||
return $this->error('', $this->room_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建直播间
|
||||
* @param $param
|
||||
*/
|
||||
public function createRoom($param)
|
||||
{
|
||||
try {
|
||||
$result = $this->app->live->room->create($param);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
|
||||
return $this->success($result);
|
||||
} else {
|
||||
return $this->error('', $this->room_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 给直播间添加商品
|
||||
* @param int $room_id
|
||||
* @param array $goods_ids
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function roomAddGoods(int $room_id, array $goods_ids)
|
||||
{
|
||||
try {
|
||||
$result = $this->app->live->room->addGoods([ 'roomId' => $room_id, 'ids' => $goods_ids ]);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
|
||||
return $this->success($result);
|
||||
} else {
|
||||
return $this->error('', $this->room_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品列表
|
||||
* @param int $start
|
||||
* @param int $limit
|
||||
* @param int $status
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function getGoodsList($start = 0, $limit = PAGE_LIST_ROWS, $status = 2)
|
||||
{
|
||||
try {
|
||||
$result = $this->app->live->goods->getGoodsList([ 'offset' => $start, 'limit' => $limit, 'status' => $status ]);
|
||||
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
|
||||
return $this->success($result);
|
||||
} else {
|
||||
return $this->error('', $this->goods_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加商品审核
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
public function addGoodsAudit($param)
|
||||
{
|
||||
try {
|
||||
$result = $this->app->live->goods->add($param);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
|
||||
return $this->success($result);
|
||||
} else {
|
||||
return $this->error('', $this->goods_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品
|
||||
*/
|
||||
public function deleteGoods($goods_id)
|
||||
{
|
||||
try {
|
||||
$result = $this->app->live->goods->delete([ 'goodsId' => $goods_id ]);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
|
||||
return $this->success($result);
|
||||
} else {
|
||||
return $this->error('', $this->goods_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品状态
|
||||
* @param array $goods_ids
|
||||
* @return array
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function getGoodsStatus(array $goods_ids)
|
||||
{
|
||||
try {
|
||||
$result = $this->app->live->goods->getStatus([ 'goods_ids' => $goods_ids ]);
|
||||
if (isset($result[ 'errcode' ]) && $result[ 'errcode' ] == 0) {
|
||||
return $this->success($result[ 'goods' ]);
|
||||
} else {
|
||||
return $this->error('', $this->goods_error[ abs($result[ 'errcode' ]) ] ?? $result[ 'errmsg' ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
214
addon/live/model/Room.php
Executable file
214
addon/live/model/Room.php
Executable file
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\live\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Cron;
|
||||
use app\model\upload\Upload;
|
||||
|
||||
class Room extends BaseModel
|
||||
{
|
||||
private $liveStatus = [
|
||||
'101' => '直播中',
|
||||
'102' => '未开始',
|
||||
'103' => '已结束',
|
||||
'104' => '禁播',
|
||||
'105' => '暂停中',
|
||||
'106' => '异常',
|
||||
'107' => '已过期',
|
||||
];
|
||||
|
||||
/**
|
||||
* 创建直播间
|
||||
* @param $data
|
||||
* @param $site_id
|
||||
*/
|
||||
public function createRoom($data, $site_id)
|
||||
{
|
||||
$live = new Live($site_id);
|
||||
$res = $live->createRoom($data);
|
||||
if ($res[ 'code' ] == 0) {
|
||||
$this->syncLiveRoom(0, 1, $site_id);
|
||||
|
||||
$cron_info = model("cron")->getInfo([ [ 'event', '=', 'LiveRoomStatus' ], [ 'relate_id', '=', $site_id ] ]);
|
||||
if (empty($cron_info)) {
|
||||
$cron = new Cron();
|
||||
$cron->addCron(2, 10, '轮询小程序直播状态', 'LiveRoomStatus', time(), $site_id);
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑直播间信息
|
||||
* @param array $data
|
||||
* @param array $where
|
||||
*/
|
||||
public function updateRoomInfo($data = [], $where = [])
|
||||
{
|
||||
$res = model('weapp_live_room')->update($data, $where);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播间列表
|
||||
* @param array $condition
|
||||
* @param bool $field
|
||||
* @param string $order
|
||||
* @param int $page
|
||||
* @param int $list_rows
|
||||
* @param string $alias
|
||||
* @param array $join
|
||||
* @return array
|
||||
*/
|
||||
public function getRoomPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [])
|
||||
{
|
||||
$data = model('weapp_live_room')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join);
|
||||
if (!empty($data[ 'list' ])) {
|
||||
foreach ($data[ 'list' ] as $k => $item) {
|
||||
$data[ 'list' ][ $k ][ 'status_name' ] = $this->liveStatus[ $item[ 'live_status' ] ] ?? '';
|
||||
if (isset($item[ 'goods' ])) $data[ 'list' ][ $k ][ 'goods' ] = json_decode($item[ 'goods' ], true);
|
||||
}
|
||||
}
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步直播间列表
|
||||
*/
|
||||
public function syncLiveRoom($start, $limit, $site_id)
|
||||
{
|
||||
$live = new Live($site_id);
|
||||
$result = $live->getRoomList($start, $limit);
|
||||
if ($result[ 'code' ] < 0) return $result;
|
||||
|
||||
if (!empty($result[ 'data' ][ 'list' ])) {
|
||||
$upload = new Upload($site_id, 'shop');
|
||||
$upload->setPath('upload/live/room/');
|
||||
foreach ($result[ 'data' ][ 'list' ] as $item) {
|
||||
$data = [
|
||||
'site_id' => $site_id,
|
||||
'roomid' => $item[ 'roomid' ],
|
||||
'name' => $item[ 'name' ],
|
||||
'cover_img' => $item[ 'cover_img' ],
|
||||
'start_time' => $item[ 'start_time' ],
|
||||
'end_time' => $item[ 'end_time' ],
|
||||
'anchor_name' => $item[ 'anchor_name' ],
|
||||
'goods' => json_encode($item[ 'goods' ], JSON_UNESCAPED_UNICODE),
|
||||
'live_status' => $item[ 'live_status' ],
|
||||
];
|
||||
$room_info = model('weapp_live_room')->getInfo([ [ 'roomid', '=', $item[ 'roomid' ] ], [ 'site_id', '=', $site_id ] ], 'id');
|
||||
if (empty($room_info)) {
|
||||
if (is_url($item[ 'share_img' ])) {
|
||||
$pull_result = $upload->remotePull($item[ 'share_img' ]);
|
||||
$pull_result = $pull_result[ 'data' ];
|
||||
if (isset($pull_result[ 'pic_path' ]) && !empty($pull_result[ 'pic_path' ])) {
|
||||
$data[ 'share_img' ] = $pull_result[ 'pic_path' ];
|
||||
}
|
||||
}
|
||||
model('weapp_live_room')->add($data);
|
||||
} else {
|
||||
$data = [
|
||||
// 'goods' => json_encode($item[ 'goods' ], JSON_UNESCAPED_UNICODE),
|
||||
'live_status' => $item[ 'live_status' ],
|
||||
];
|
||||
model('weapp_live_room')->update($data, [ [ 'id', '=', $room_info[ 'id' ] ] ]);
|
||||
}
|
||||
}
|
||||
$total_page = ceil($result[ 'data' ][ 'total' ] / $limit);
|
||||
return $this->success([ 'page' => $start, 'total_page' => $total_page ]);
|
||||
} else {
|
||||
return $this->success([ 'page' => $start, 'total_page' => 1 ]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播间信息
|
||||
*/
|
||||
public function getRoomInfo($condition = [], $field = '*')
|
||||
{
|
||||
$data = model('weapp_live_room')->getInfo($condition, $field);
|
||||
if (!empty($data)) {
|
||||
$data[ 'status_name' ] = $this->liveStatus[ $data[ 'live_status' ] ] ?? '';
|
||||
if (isset($data[ 'goods' ]) && !empty($data[ 'goods' ])) $data[ 'goods' ] = json_decode($data[ 'goods' ], true);
|
||||
}
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加商品到直播间
|
||||
* @param $site_id
|
||||
* @param $room_id
|
||||
* @param $data
|
||||
*/
|
||||
public function addGoods($site_id, $room_id, $data)
|
||||
{
|
||||
if (empty($data)) return $this->error('', '请先选择要添加的商品');
|
||||
$room_info = model('weapp_live_room')->getInfo([ [ 'site_id', '=', $site_id ], [ 'roomid', '=', $room_id ] ], 'goods');
|
||||
if (empty($room_info)) return $this->error('', '未查找到直播间信息');
|
||||
|
||||
$data = json_decode($data, true);
|
||||
|
||||
$goods_ids = [];
|
||||
$goods_data = [];
|
||||
|
||||
foreach ($data as $item) {
|
||||
$goods_ids[] = $item['goods_id'];
|
||||
$goods_data[] = [
|
||||
'name' => $item['name'],
|
||||
'cover_img' => $item['cover_img'],
|
||||
'url' => $item['url'],
|
||||
'price' => $item['price']
|
||||
];
|
||||
}
|
||||
|
||||
$live = new Live($site_id);
|
||||
$result = $live->roomAddGoods($room_id, $goods_ids);
|
||||
if ($result[ 'code' ] < 0) return $result;
|
||||
|
||||
if (!empty($room_info[ 'goods' ])) {
|
||||
$room_goods = json_decode($room_info[ 'goods' ], true);
|
||||
$goods_data = array_merge($room_goods, $goods_data);
|
||||
}
|
||||
|
||||
$res = model('weapp_live_room')->update([ 'goods' => json_encode($goods_data, JSON_UNESCAPED_UNICODE) ], [ [ 'site_id', '=', $site_id ], [ 'roomid', '=', $room_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 轮询更新直播间状态
|
||||
* @param $site_id
|
||||
*/
|
||||
public function updateRoomStatus($site_id)
|
||||
{
|
||||
$count = model('weapp_live_room')->getCount([ [ 'site_id', '=', $site_id ], [ 'live_status', 'in', [ '101', '102', '105' ] ] ]);
|
||||
if ($count) {
|
||||
$start = 0;
|
||||
$result = $this->syncLiveRoom($start, 20, $site_id);
|
||||
if (isset($result[ 'code' ]) && $result[ 'code' ] == 0 && $result[ 'total_page' ] > 1) {
|
||||
for ($i = 1; $i < $result[ 'data' ]; $i++) {
|
||||
$this->syncLiveRoom($i, 20, $site_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除直播间
|
||||
* @param $site_id
|
||||
* @param $room_ids
|
||||
*/
|
||||
public function deleteRoom($site_id, $room_ids)
|
||||
{
|
||||
$res = model('weapp_live_room')->delete([ [ 'site_id', '=', $site_id ], [ 'id', 'in', $room_ids ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user