init
- 框架初始化 - 安装插件 - 修复PHP8.4报错
This commit is contained in:
163
application/admin/controller/shopro/app/mplive/Goods.php
Normal file
163
application/admin/controller/shopro/app/mplive/Goods.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\app\mplive;
|
||||
|
||||
use app\admin\model\shopro\app\mplive\Goods as MpliveGoodsModel;
|
||||
|
||||
/**
|
||||
* 小程序直播商品管理
|
||||
*/
|
||||
class Goods extends Index
|
||||
{
|
||||
|
||||
// 直播间商品
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$model = new MpliveGoodsModel();
|
||||
$list = $model->sheepFilter()->paginate($this->request->param('list_rows', 10));
|
||||
|
||||
// 批量更新直播商品状态
|
||||
// $this->updateAuditStatusByGoods($list);
|
||||
|
||||
$this->success('获取成功', null, $list);
|
||||
}
|
||||
|
||||
// 直播间商品详情
|
||||
public function detail($id)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$goods = (new MpliveGoodsModel)->findOrFail($id);
|
||||
|
||||
$this->success('', null, $goods);
|
||||
}
|
||||
|
||||
// 创建直播间商品并提交审核
|
||||
public function add()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$params = $this->request->param();
|
||||
|
||||
$data = [
|
||||
"coverImgUrl" => $this->uploadMedia($params['cover_img_url']),
|
||||
"name" => $params['name'],
|
||||
"priceType" => $params['price_type'],
|
||||
"price" => $params['price'],
|
||||
"price2" => $params['price_type'] === 1 ? "" : $params['price2'], // priceType为2或3时必填
|
||||
"url" => $params['url'],
|
||||
];
|
||||
|
||||
$res = $this->wechat->broadcast->create($data);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
$params['id'] = $res['goodsId'];
|
||||
$params['audit_id'] = $res['auditId'];
|
||||
$params['audit_status'] = 1;
|
||||
|
||||
(new MpliveGoodsModel)->save($params);
|
||||
|
||||
$this->success("操作成功");
|
||||
}
|
||||
|
||||
// 直播商品审核
|
||||
public function audit($id)
|
||||
{
|
||||
$id = intval($id);
|
||||
$act = $this->request->param('act');
|
||||
|
||||
$goods = MpliveGoodsModel::where('id', $id)->find();
|
||||
if (!$goods) {
|
||||
error_stop('未找到该商品');
|
||||
}
|
||||
// 撤回审核
|
||||
if ($act === 'reset') {
|
||||
$auditId = $goods->audit_id;
|
||||
if ($auditId) {
|
||||
$res = $this->wechat->broadcast->resetAudit($auditId, $id);
|
||||
$this->catchLiveError($res);
|
||||
}
|
||||
}
|
||||
|
||||
// 重新审核
|
||||
if ($act === 'resubmit') {
|
||||
$res = $this->wechat->broadcast->resubmitAudit($id);
|
||||
$this->catchLiveError($res);
|
||||
$goods->audit_id = $res['auditId'];
|
||||
$goods->save();
|
||||
}
|
||||
|
||||
return $this->status($id);
|
||||
}
|
||||
|
||||
// 删除直播商品
|
||||
public function delete($id)
|
||||
{
|
||||
$id = intval($id);
|
||||
$res = $this->wechat->broadcast->delete($id);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
MpliveGoodsModel::where('id', $id)->delete();
|
||||
$this->success('操作成功');
|
||||
}
|
||||
|
||||
// 更新直播商品
|
||||
public function edit($id = null)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch('add');
|
||||
}
|
||||
|
||||
$params = $this->request->param();
|
||||
|
||||
$data = [
|
||||
'goodsId' => $id,
|
||||
"coverImgUrl" => $this->uploadMedia($params['cover_img_url']),
|
||||
"name" => $params['name'],
|
||||
"priceType" => $params['price_type'],
|
||||
"price" => $params['price'],
|
||||
"price2" => $params['price_type'] === 1 ? "" : $params['price2'], // priceType为2或3时必填
|
||||
"url" => $params['url'],
|
||||
];
|
||||
|
||||
$res = $this->wechat->broadcast->update($data);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
$goods = MpliveGoodsModel::where('id', $id)->find();
|
||||
$goods->save($data);
|
||||
|
||||
$this->success('操作成功');
|
||||
}
|
||||
|
||||
// 更新直播商品状态
|
||||
public function status($id)
|
||||
{
|
||||
$res = $this->wechat->broadcast->getGoodsWarehouse([$id]);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
$list = $res['goods'];
|
||||
|
||||
foreach ($list as $key => $goods) {
|
||||
$mpliveGoods = MpliveGoodsModel::where('id', $goods['goods_id'])->find();
|
||||
if ($mpliveGoods) {
|
||||
$mpliveGoods->audit_status = $goods['audit_status'];
|
||||
$mpliveGoods->third_party_tag = $goods['third_party_tag'];
|
||||
$mpliveGoods->save();
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('操作成功');
|
||||
}
|
||||
}
|
||||
81
application/admin/controller/shopro/app/mplive/Index.php
Normal file
81
application/admin/controller/shopro/app/mplive/Index.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\app\mplive;
|
||||
|
||||
use fast\Http;
|
||||
use app\admin\controller\shopro\Common;
|
||||
use app\admin\model\shopro\app\mplive\Room as MpliveRoomModel;
|
||||
use addons\shopro\library\mplive\ServiceProvider;
|
||||
use addons\shopro\facade\Wechat;
|
||||
|
||||
/**
|
||||
* 小程序直播
|
||||
*/
|
||||
class Index extends Common
|
||||
{
|
||||
protected $wechat;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->wechat = Wechat::miniProgram();
|
||||
|
||||
(new ServiceProvider())->register($this->wechat);
|
||||
}
|
||||
|
||||
// 上传临时素材
|
||||
protected function uploadMedia($path)
|
||||
{
|
||||
$filesystem = config('filesystem.default');
|
||||
if ($filesystem !== 'local' || is_url($path)) {
|
||||
$body = Http::get(cdnurl($path, true));
|
||||
$dir = RUNTIME_PATH . 'storage' . DS . 'temp';
|
||||
if (!is_dir($dir)) {
|
||||
@mkdir($dir, 0755, true);
|
||||
}
|
||||
$temp_path = $dir . $this->getBaseName($path);
|
||||
file_put_contents($temp_path, $body);
|
||||
} else {
|
||||
$temp_path = ROOT_PATH . 'public' . $path;
|
||||
}
|
||||
|
||||
if (!isset($temp_path) || empty($temp_path)) {
|
||||
error_stop("上传失败,不是一个有效的文件: " . $path);
|
||||
}
|
||||
|
||||
$media = $this->wechat->media;
|
||||
$res = $media->uploadImage($temp_path);
|
||||
@unlink($temp_path); // 删除临时文件
|
||||
if (isset($res['media_id'])) {
|
||||
return $res['media_id'];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
// 解析图片文件名
|
||||
private function getBaseName($path)
|
||||
{
|
||||
if (strpos($path, 'mmbiz.qpic.cn') !== false) {
|
||||
return DS . gen_random_str(8) . '.jpg';
|
||||
}
|
||||
|
||||
return basename($path);
|
||||
}
|
||||
|
||||
// 转义直播错误信息
|
||||
protected function catchLiveError($response)
|
||||
{
|
||||
if (!isset($response['errcode'])) {
|
||||
error_stop("未知错误");
|
||||
}
|
||||
|
||||
$errorMap = MpliveRoomModel::ERR_CODE;
|
||||
if (isset($response['errcode']) && ($response['errcode'] !== 0 && $response['errcode'] !== 1001)) {
|
||||
if (isset($errorMap[$response['errcode']])) {
|
||||
error_stop("{$errorMap[$response['errcode']]} [错误码: {$response['errcode']}]");
|
||||
} else {
|
||||
error_stop("{$response['errmsg']} [错误码: {$response['errcode']}]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
201
application/admin/controller/shopro/app/mplive/Room.php
Normal file
201
application/admin/controller/shopro/app/mplive/Room.php
Normal file
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\shopro\app\mplive;
|
||||
|
||||
use app\admin\model\shopro\app\mplive\Room as MpliveRoomModel;
|
||||
|
||||
/**
|
||||
* 小程序直播
|
||||
*/
|
||||
class Room extends Index
|
||||
{
|
||||
|
||||
protected $noNeedRight = ['select'];
|
||||
|
||||
// 直播间列表
|
||||
public function index()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$list = (new MpliveRoomModel)->sheepFilter()->select();
|
||||
|
||||
$this->success('', null, $list);
|
||||
}
|
||||
|
||||
// 直播间详情
|
||||
public function detail($id)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$room = (new MpliveRoomModel)->where('roomid', $id)->findOrFail();
|
||||
|
||||
$this->success('', null, $room);
|
||||
}
|
||||
|
||||
// 同步直播间列表
|
||||
public function sync()
|
||||
{
|
||||
$res = $this->wechat->broadcast->getRooms();
|
||||
$data = [];
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
MpliveRoomModel::where('roomid', '>', 0)->delete();
|
||||
foreach ($res['room_info'] as $room) {
|
||||
$room['status'] = $room['live_status'];
|
||||
$room['type'] = $room['live_type'];
|
||||
$data[] = $room;
|
||||
}
|
||||
|
||||
MpliveRoomModel::strict(false)->insertAll($data);
|
||||
$list = MpliveRoomModel::select();
|
||||
|
||||
$this->success('', null, $list);
|
||||
}
|
||||
|
||||
// 创建直播间
|
||||
public function add()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$params = $this->request->param();
|
||||
|
||||
$data = [
|
||||
'name' => $params['name'], // 房间名字
|
||||
'coverImg' => $this->uploadMedia($params['cover_img']), // 通过 uploadfile 上传,填写 mediaID
|
||||
'shareImg' => $this->uploadMedia($params['share_img']), //通过 uploadfile 上传,填写 mediaID
|
||||
'feedsImg' => $this->uploadMedia($params['feeds_img']), //通过 uploadfile 上传,填写 mediaID
|
||||
'startTime' => $params['start_time'], // 开始时间
|
||||
'endTime' => $params['end_time'], // 结束时间
|
||||
'anchorName' => $params['anchor_name'], // 主播昵称
|
||||
'anchorWechat' => $params['anchor_wechat'], // 主播微信号
|
||||
'subAnchorWechat' => $params['sub_anchor_wechat'], // 主播副号微信号
|
||||
'isFeedsPublic' => $params['is_feeds_public'], // 是否开启官方收录,1 开启,0 关闭
|
||||
'type' => $params['type'], // 直播类型,1 推流 0 手机直播
|
||||
'closeLike' => $params['close_like'], // 是否关闭点赞 1:关闭
|
||||
'closeGoods' => $params['close_goods'], // 是否关闭商品货架,1:关闭
|
||||
'closeComment' => $params['close_comment'], // 是否开启评论,1:关闭
|
||||
'closeReplay' => $params['close_replay'], // 是否关闭回放 1 关闭
|
||||
'closeKf' => $params['close_kf'], // 是否关闭客服,1 关闭
|
||||
];
|
||||
|
||||
$res = $this->wechat->broadcast->createLiveRoom($data);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
return $this->sync();
|
||||
}
|
||||
|
||||
// 更新直播间
|
||||
public function edit($id = null)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch('add');
|
||||
}
|
||||
|
||||
$params = $this->request->param();
|
||||
|
||||
$data = [
|
||||
'id' => $id,
|
||||
'name' => $params['name'], // 房间名字
|
||||
'coverImg' => $this->uploadMedia($params['cover_img']), // 通过 uploadfile 上传,填写 mediaID
|
||||
'shareImg' => $this->uploadMedia($params['share_img']), //通过 uploadfile 上传,填写 mediaID
|
||||
'feedsImg' => $this->uploadMedia($params['feeds_img']), //通过 uploadfile 上传,填写 mediaID
|
||||
'startTime' => $params['start_time'], // 开始时间
|
||||
'endTime' => $params['end_time'], // 结束时间
|
||||
'anchorName' => $params['anchor_name'], // 主播昵称
|
||||
'anchorWechat' => $params['anchor_wechat'], // 主播昵称
|
||||
'isFeedsPublic' => $params['is_feeds_public'], // 是否开启官方收录,1 开启,0 关闭
|
||||
'type' => $params['type'], // 直播类型,1 推流 0 手机直播
|
||||
'closeLike' => $params['close_like'], // 是否关闭点赞 1:关闭
|
||||
'closeGoods' => $params['close_goods'], // 是否关闭商品货架,1:关闭
|
||||
'closeComment' => $params['close_comment'], // 是否开启评论,1:关闭
|
||||
'closeReplay' => $params['close_replay'], // 是否关闭回放 1 关闭
|
||||
'closeKf' => $params['close_kf'], // 是否关闭客服,1 关闭
|
||||
];
|
||||
|
||||
$res = $this->wechat->broadcast->updateLiveRoom($data);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
return $this->sync();
|
||||
}
|
||||
|
||||
// 删除直播间
|
||||
public function delete($id)
|
||||
{
|
||||
$res = $this->wechat->broadcast->deleteLiveRoom([
|
||||
'id' => $id,
|
||||
]);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
MpliveRoomModel::where('roomid', $id)->delete();
|
||||
$this->success('操作成功');
|
||||
}
|
||||
|
||||
// 推流地址
|
||||
public function pushUrl($id)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$res = $this->wechat->broadcast->getPushUrl([
|
||||
'roomId' => $id
|
||||
]);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
$this->success('', null, ['pushAddr' => $res['pushAddr']]);
|
||||
}
|
||||
|
||||
// 分享二维码
|
||||
public function qrcode($id)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$res = $this->wechat->broadcast->getShareQrcode([
|
||||
'roomId' => $id
|
||||
]);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
$this->success('', null, ['pagePath' => $res['pagePath'], 'cdnUrl' => $res['cdnUrl']]);
|
||||
}
|
||||
|
||||
// 查看回放
|
||||
public function playback($id)
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$res = $this->wechat->broadcast->getPlaybacks((int)$id, (int)$start = 0, (int)$limit = 10);
|
||||
|
||||
$this->catchLiveError($res);
|
||||
|
||||
$data = $res['live_replay'];
|
||||
|
||||
$this->success('', null, $data);
|
||||
}
|
||||
|
||||
public function select()
|
||||
{
|
||||
if (!$this->request->isAjax()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
$list = (new MpliveRoomModel)->sheepFilter()->select();
|
||||
|
||||
$this->success('', null, $list);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user