Initial commit: queue workspace
Made-with: Cursor
This commit is contained in:
204
pro_v3.5.1/app/services/activity/live/LiveAnchorServices.php
Normal file
204
pro_v3.5.1/app/services/activity/live/LiveAnchorServices.php
Normal file
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\services\activity\live;
|
||||
|
||||
|
||||
use app\dao\activity\live\LiveAnchorDao;
|
||||
use app\jobs\activity\AutoUpdateLiveJob;
|
||||
use app\services\BaseServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\services\CacheService;
|
||||
use crmeb\services\FormBuilder as Form;
|
||||
use crmeb\services\wechat\MiniProgram;
|
||||
use FormBuilder\Factory\Iview;
|
||||
use think\annotation\Inject;
|
||||
use think\facade\Route as Url;
|
||||
|
||||
|
||||
/**
|
||||
* 主播
|
||||
* Class LiveAnchorServices
|
||||
* @package app\services\activity\live
|
||||
* @mixin LiveAnchorDao
|
||||
*/
|
||||
class LiveAnchorServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var LiveAnchorDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected LiveAnchorDao $dao;
|
||||
|
||||
/**
|
||||
* 获取某个主播
|
||||
* @param int $id
|
||||
* @return array|\think\Model|null
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getLiveAnchor(int $id)
|
||||
{
|
||||
return $this->dao->get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主播列表
|
||||
* @param array $where 查询条件
|
||||
* @return array
|
||||
*/
|
||||
public function getList(array $where)
|
||||
{
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$where['is_del'] = 0;
|
||||
$list = $this->dao->getList($where, '*', $page, $limit);
|
||||
$count = $this->dao->count($where);
|
||||
return compact('count', 'list');
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加修改标签表单
|
||||
* @param int $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function add(int $id)
|
||||
{
|
||||
$anchor = $this->getLiveAnchor($id);
|
||||
$field = array();
|
||||
if (!$anchor) {
|
||||
$title = '添加主播';
|
||||
$field[] = Form::input('name', '主播名称:', '')->maxlength(20)->required('请填写名称');
|
||||
$field[] = Form::input('wechat', '主播微信号:', '')->maxlength(32)->required('请填写微信号');
|
||||
$field[] = Form::input('phone', '主播手机号:', '')->maxlength(20)->required('请填写手机号');
|
||||
$field[] = Form::frameImage('cover_img', '主播图像:', Url::buildUrl('admin/widget.images/index', array('fodder' => 'cover_img')), '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择图像'));
|
||||
} else {
|
||||
$title = '修改主播';
|
||||
$field[] = Form::hidden('id', $anchor->getData('id'));
|
||||
$field[] = Form::input('name', '主播名称:', $anchor->getData('name'))->maxlength(20)->required('请填写名称');
|
||||
$field[] = Form::input('wechat', '主播微信号:', $anchor->getData('wechat'))->maxlength(32)->required('请填写微信号');
|
||||
$field[] = Form::input('phone', '主播手机号:', $anchor->getData('phone'))->maxlength(20)->required('请填写手机号');
|
||||
$field[] = Form::frameImage('cover_img', '主播图像:', Url::buildUrl('admin/widget.images/index', array('fodder' => 'cover_img')), $anchor->getData('cover_img'))->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择图像'));
|
||||
}
|
||||
return create_form($title, $field, $this->url('/live/anchor/save'), 'POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存标签表单数据
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(int $id, array $data)
|
||||
{
|
||||
$liveAnchor = $this->dao->get(['wechat' => $data['wechat']]);
|
||||
if (!MiniProgram::getRoleList(2, 0, 30, $data['wechat'])) {
|
||||
return ['auth' => true];
|
||||
}
|
||||
if ($id) {
|
||||
if ($liveAnchor && $id != $liveAnchor['id']) {
|
||||
throw new AdminException('该主播已经存在');
|
||||
}
|
||||
if ($this->dao->update($id, $data)) {
|
||||
return true;
|
||||
} else {
|
||||
throw new AdminException('修改失败或者您没有修改什么!');
|
||||
}
|
||||
} else {
|
||||
unset($data['id']);
|
||||
if ($liveAnchor) {
|
||||
throw new AdminException('该主播已经存在');
|
||||
}
|
||||
if ($this->dao->save($data)) {
|
||||
return true;
|
||||
} else {
|
||||
throw new AdminException('添加失败!');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param $id
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delAnchor(int $id)
|
||||
{
|
||||
if ($anchor = $this->getLiveAnchor($id)) {
|
||||
if (!$this->dao->update($id, ['is_del' => 1])) {
|
||||
throw new AdminException('删除失败,请稍候再试!');
|
||||
}
|
||||
/** @var LiveRoomServices $liveRoom */
|
||||
$liveRoom = app()->make(LiveRoomServices::class);
|
||||
$room = $liveRoom->get(['anchor_wechat' => $anchor['wechat'], 'is_del' => 0], ['id']);
|
||||
if ($room) {
|
||||
$liveRoom->delete((int)$room->id);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否显示
|
||||
* @param int $id
|
||||
* @param $is_show
|
||||
* @return mixed
|
||||
*/
|
||||
public function setShow(int $id, $is_show)
|
||||
{
|
||||
if (!$this->getLiveAnchor($id))
|
||||
throw new AdminException('数据不存在');
|
||||
if ($this->dao->update($id, ['is_show' => $is_show])) {
|
||||
return $is_show == 1 ? '显示成功' : '隐藏成功';
|
||||
} else {
|
||||
throw new AdminException($is_show == 1 ? '显示失败' : '隐藏失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步微信主播列表
|
||||
* @param bool $is_job 是否为定时任务调用
|
||||
* @return bool
|
||||
*/
|
||||
public function syncAnchor($is_job = false)
|
||||
{
|
||||
$key = md5('Live_sync_status');
|
||||
$res = CacheService::redisHandler()->get($key);
|
||||
if (!$res || $is_job) {
|
||||
$start = 0;
|
||||
$limit = 30;
|
||||
$data = $dataAll = [];
|
||||
$anchors = $this->dao->getColumn([], 'id,wechat', 'wechat');
|
||||
do {
|
||||
$wxAnchor = MiniProgram::getRoleList(2, $start, $limit);
|
||||
foreach ($wxAnchor as $anchor) {
|
||||
if (isset($anchors[$anchor['username']])) {
|
||||
$this->dao->update($anchors[$anchor['username']]['id'], ['cover_img' => $anchor['headingimg'], 'name' => $anchor['nickname']]);
|
||||
} else {
|
||||
$data['name'] = $anchor['nickname'];
|
||||
$data['wechat'] = $anchor['username'];
|
||||
$data['cover_img'] = $anchor['headingimg'];
|
||||
$data['add_time'] = $anchor['updateTimestamp'] ?? time();
|
||||
$dataAll[] = $data;
|
||||
}
|
||||
}
|
||||
$start++;
|
||||
} while (count($wxAnchor) >= $limit);
|
||||
if ($dataAll) {
|
||||
$this->dao->saveAll($dataAll);
|
||||
}
|
||||
if (!$is_job) AutoUpdateLiveJob::dispatchSece(120);
|
||||
CacheService::redisHandler()->set($key, 1, 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
281
pro_v3.5.1/app/services/activity/live/LiveGoodsServices.php
Normal file
281
pro_v3.5.1/app/services/activity/live/LiveGoodsServices.php
Normal file
@@ -0,0 +1,281 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\services\activity\live;
|
||||
|
||||
|
||||
use app\dao\activity\live\LiveGoodsDao;
|
||||
use app\services\BaseServices;
|
||||
use app\services\product\product\StoreProductServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\services\DownloadImageService;
|
||||
use crmeb\services\wechat\MiniProgram;
|
||||
use crmeb\services\wechat\OfficialAccount;
|
||||
use think\annotation\Inject;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 直播商品
|
||||
* Class LiveGoodsServices
|
||||
* @package app\services\activity\live
|
||||
* @mixin LiveGoodsDao
|
||||
*/
|
||||
class LiveGoodsServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var LiveGoodsDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected LiveGoodsDao $dao;
|
||||
|
||||
/**
|
||||
* 获取直播商品列表
|
||||
* @param array $where 查询条件
|
||||
* @return array
|
||||
*/
|
||||
public function getList(array $where)
|
||||
{
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$where['is_del'] = 0;
|
||||
$list = $this->dao->getList($where, '*', ['product'], $page, $limit);
|
||||
$count = $this->dao->count($where);
|
||||
return compact('count', 'list');
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建直播商品(验证商品有效性)
|
||||
* @param array $product_ids 商品ID数组
|
||||
* @return array
|
||||
* @throws AdminException
|
||||
*/
|
||||
public function create(array $product_ids)
|
||||
{
|
||||
/** @var StoreProductServices $product */
|
||||
$productServices = app()->make(StoreProductServices::class);
|
||||
$products = $productServices->getColumn([['id', 'IN', $product_ids], ['is_del', '=', 0], ['is_show', '=', 1]], 'id,image,store_name,price,price as cost_price,stock', 'id');
|
||||
if (count($product_ids) != count($products)) {
|
||||
throw new AdminException('已选商品中包括已下架或移入回收站商品');
|
||||
}
|
||||
$checkGoods = $this->dao->getCount([['product_id', 'IN', $product_ids], ['is_del', '=', 0], ['audit_status', '<>', 3]]);
|
||||
if ($checkGoods > 0) {
|
||||
throw new AdminException('其中有商品已经添加');
|
||||
}
|
||||
return array_merge($products);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return bool|mixed
|
||||
* @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function add(array $goods_info)
|
||||
{
|
||||
$product_ids = array_column($goods_info, 'id');
|
||||
$this->create($product_ids);
|
||||
/** @var DownloadImageService $download */
|
||||
$download = app()->make(DownloadImageService::class);
|
||||
$dataAll = $data = [];
|
||||
$time = time();
|
||||
foreach ($goods_info as $product) {
|
||||
$data = [
|
||||
'product_id' => $product['id'],
|
||||
'name' => substrUTf8($product['store_name'], 12, 'UTF-8', ''),
|
||||
'cover_img' => $product['image'] ?? '',
|
||||
'price_type' => 1,
|
||||
'cost_price' => $product['cost_price'] ?? 0.00,
|
||||
'price' => $product['price'] ?? 0.00,
|
||||
'url' => 'pages/goods_details/index?id=' . $product['id'],
|
||||
'sort' => $product['sort'] ?? 0,
|
||||
'add_time' => $time
|
||||
];
|
||||
try {
|
||||
$path = root_path() . 'public' . $download->thumb(true)->downloadImage($data['cover_img'])['path'];
|
||||
$coverImgUrl = MiniProgram::temporaryUpload($path)['media_id'];
|
||||
@unlink($path);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('添加直播商品图片错误,原因:' . $e->getMessage());
|
||||
$coverImgUrl = $data['cover_img'];
|
||||
}
|
||||
$res = MiniProgram::addGoods($coverImgUrl, $data['name'], $data['price_type'], $data['url'], floatval($data['price']));
|
||||
$data['goods_id'] = $res['goodsId'];
|
||||
$data['audit_id'] = $res['auditId'];
|
||||
$data['audit_status'] = 1;
|
||||
$dataAll[] = $data;
|
||||
}
|
||||
if (!$goods = $this->dao->saveAll($dataAll)) {
|
||||
throw new AdminException('添加商品失败');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步商品
|
||||
* @return bool
|
||||
* @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
|
||||
*/
|
||||
public function syncGoods()
|
||||
{
|
||||
$liveGoods = $this->dao->getColumn([['goods_id', '>', 0]], '*', 'id');
|
||||
if ($liveGoods) {
|
||||
foreach ($liveGoods as $good) {
|
||||
$path = root_path() . 'public' . app()->make(DownloadImageService::class)->thumb(true)->downloadImage($good['cover_img'])['path'];
|
||||
$materialInfo = MiniProgram::temporaryUpload($path);
|
||||
$coverImgUrl = $materialInfo['media_id'];
|
||||
@unlink($path);
|
||||
$res = MiniProgram::addGoods($coverImgUrl, $good['name'], $good['price_type'], $good['url'], floatval($good['price']));
|
||||
$data['goods_id'] = $res['goodsId'];
|
||||
$data['audit_id'] = $res['auditId'];
|
||||
$data['audit_status'] = 1;
|
||||
if (!$this->dao->update($good['id'], $data, 'id')) {
|
||||
throw new AdminException('同步失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信小程序创建直播商品
|
||||
* @param mixed $goods 商品信息
|
||||
* @return array
|
||||
* @throws AdminException
|
||||
*/
|
||||
public function wxCreate($goods)
|
||||
{
|
||||
if ($goods['goods_id'])
|
||||
throw new AdminException('商品已创建');
|
||||
|
||||
$goods = $goods->toArray();
|
||||
$path = root_path() . 'public' . app()->make(DownloadImageService::class)->thumb(true)->downloadImage($goods['cover_img'])['path'];
|
||||
|
||||
$url = 'pages/goods_details/index?id=' . $goods['product_id'];
|
||||
$coverImgUrl = MiniProgram::temporaryUpload($path)['media_id'];
|
||||
@unlink($path);
|
||||
return MiniProgram::addGoods($coverImgUrl, $goods['name'], 1, $url, floatval($goods['price']));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置直播商品显示状态
|
||||
* @param int $id 商品ID
|
||||
* @param int $is_show 显示状态
|
||||
* @return string
|
||||
* @throws AdminException
|
||||
*/
|
||||
public function isShow(int $id, $is_show)
|
||||
{
|
||||
$goods = $this->dao->get(['id' => $id, 'audit_status' => 2]);
|
||||
if (!$goods) {
|
||||
throw new AdminException('审核中或审核失败不允许此操作');
|
||||
}
|
||||
$this->dao->update($id, ['is_show' => $is_show]);
|
||||
return $is_show == 1 ? '显示成功' : '隐藏成功';
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新提交审核
|
||||
* @param int $id
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function audit(int $id)
|
||||
{
|
||||
$goods = $this->dao->get($id);
|
||||
if (!$goods) {
|
||||
throw new AdminException('数据不存在');
|
||||
}
|
||||
if ($goods['audit_status'] != 0) {
|
||||
throw new AdminException('在审核中或已经审核通过');
|
||||
}
|
||||
if (!$this->dao->update($id, ['audit_status' => 1])) {
|
||||
throw new AdminException('修改审核状态失败');
|
||||
}
|
||||
return MiniProgram::auditGoods((int)$goods['good_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 撤回审核
|
||||
* @param int $id
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function resetAudit(int $id)
|
||||
{
|
||||
$goods = $this->dao->get($id);
|
||||
if (!$goods) {
|
||||
throw new AdminException('数据不存在');
|
||||
}
|
||||
if ($goods['audit_status'] == 0) {
|
||||
return true;
|
||||
}
|
||||
if ($goods['audit_status'] != 1) {
|
||||
throw new AdminException('审核通过或失败');
|
||||
}
|
||||
if (!$this->dao->update($id, ['audit_status' => 0])) {
|
||||
throw new AdminException('修改审核状态失败');
|
||||
}
|
||||
return MiniProgram::resetauditGoods((int)$goods['good_id'], $goods['audit_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品
|
||||
* @param int $id
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function delete(int $id)
|
||||
{
|
||||
$goods = $this->dao->get(['id' => $id, 'is_del' => 0]);
|
||||
if ($goods) {
|
||||
if (in_array($goods['audit_status'], [0, 1])) {
|
||||
throw new AdminException('商品审核中,无法删除');
|
||||
}
|
||||
if (!$this->dao->update($id, ['is_del' => 1])) {
|
||||
throw new AdminException('删除失败');
|
||||
}
|
||||
if (MiniProgram::deleteGoods((int)$goods->goods_id)) {
|
||||
/** @var LiveRoomGoodsServices $liveRoomGoods */
|
||||
$liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
|
||||
$liveRoomGoods->delete(['live_goods_id' => $id]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步直播商品审核状态
|
||||
* @return bool
|
||||
*/
|
||||
public function syncGoodStatus()
|
||||
{
|
||||
$goodsIds = $this->dao->goodsStatusAll();
|
||||
if (!count($goodsIds)) return true;
|
||||
$res = MiniProgram::getGooodsInfo(array_keys($goodsIds));
|
||||
foreach ($res as $item) {
|
||||
if (isset($goodsIds[$item['goods_id']]) && $item['audit_status'] != $goodsIds[$item['goods_id']]) {
|
||||
$data = ['audit_status' => $item['audit_status']];
|
||||
// 同步商品审核状态
|
||||
$this->dao->update((int)$goodsIds[$item['goods_id']]['id'], $data);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\services\activity\live;
|
||||
|
||||
|
||||
use app\dao\activity\live\LiveRoomGoodsDao;
|
||||
use app\services\BaseServices;
|
||||
use think\annotation\Inject;
|
||||
|
||||
/**
|
||||
* 直播间关联商品
|
||||
* Class LiveRoomGoodsServices
|
||||
* @package app\services\activity\live
|
||||
* @mixin LiveRoomGoodsDao
|
||||
*/
|
||||
class LiveRoomGoodsServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var LiveRoomGoodsDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected LiveRoomGoodsDao $dao;
|
||||
}
|
||||
327
pro_v3.5.1/app/services/activity/live/LiveRoomServices.php
Normal file
327
pro_v3.5.1/app/services/activity/live/LiveRoomServices.php
Normal file
@@ -0,0 +1,327 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\services\activity\live;
|
||||
|
||||
|
||||
use app\dao\activity\live\LiveRoomDao;
|
||||
use app\services\BaseServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\services\DownloadImageService;
|
||||
use crmeb\services\wechat\MiniProgram;
|
||||
use think\annotation\Inject;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 直播间
|
||||
* Class LiveRoomServices
|
||||
* @package app\services\activity\live
|
||||
* @mixin LiveRoomDao
|
||||
*/
|
||||
class LiveRoomServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var LiveRoomDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected LiveRoomDao $dao;
|
||||
|
||||
/**
|
||||
* 获取直播间列表(后台)
|
||||
* @param array $where 查询条件
|
||||
* @return array
|
||||
*/
|
||||
public function getList(array $where)
|
||||
{
|
||||
$where['is_del'] = 0;
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$list = $this->dao->getList($where, '*', [], $page, $limit);
|
||||
$count = $this->dao->count($where);
|
||||
return compact('count', 'list');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播间列表
|
||||
* @param array $where
|
||||
* @param int $limit
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function userList(array $where, int $limit = 0)
|
||||
{
|
||||
if ($limit) {
|
||||
$page = 0;
|
||||
} else {
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
}
|
||||
$where['is_show'] = 1;
|
||||
$where['is_del'] = 0;
|
||||
$list = $this->dao->getList($where, '*', ['roomGoods.goods', 'anchor'], $page, $limit);
|
||||
foreach ($list as &$item) {
|
||||
$item['roomid'] = $item['room_id'];
|
||||
$item['goods'] = [];
|
||||
$item['show_time'] = date('m/d H:i', strtotime($item['start_time']));
|
||||
if (isset($item['roomGoods']) && $item['roomGoods']) {
|
||||
$item['goods'] = array_column($item['roomGoods'], 'goods');
|
||||
}
|
||||
if (in_array($item['live_status'], [105, 106])) {
|
||||
$item['live_status'] = 101;
|
||||
}
|
||||
if (in_array($item['live_status'], [104, 107])) {
|
||||
$item['live_status'] = 103;
|
||||
}
|
||||
unset($item['roomGoods']);
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播间回放列表
|
||||
* @param int $id 直播间ID
|
||||
* @return array
|
||||
*/
|
||||
public function getPlaybacks(int $id)
|
||||
{
|
||||
$room = $this->dao->get(['id' => $id, 'is_del' => 0]);
|
||||
if (!$room) {
|
||||
throw new AdminException('数据不存在');
|
||||
}
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
return MiniProgram::getLivePlayback($room['room_id'], $page, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加直播间
|
||||
* @param array $data 直播间数据
|
||||
* @return bool
|
||||
*/
|
||||
public function add(array $data)
|
||||
{
|
||||
/** @var LiveAnchorServices $anchorServices */
|
||||
$anchorServices = app()->make(LiveAnchorServices::class);
|
||||
$anchor = $anchorServices->get(['wechat' => $data['anchor_wechat']]);
|
||||
if (!$anchor) {
|
||||
throw new AdminException('该主播不存在');
|
||||
}
|
||||
$data['start_time'] = strtotime($data['start_time']);
|
||||
$data['end_time'] = strtotime($data['end_time']);
|
||||
$time = time() + 600;
|
||||
$time6 = time() + 180 * 24 * 3600;
|
||||
if ($data['start_time'] < $time || $data['start_time'] > $time6) {
|
||||
throw new AdminException('开播时间需要在当前时间的10分钟后 并且 开始时间不能在 6 个月后');
|
||||
}
|
||||
$t = $data['end_time'] - $data['start_time'];
|
||||
if ($t < 1800 || $t > 24 * 3600) {
|
||||
throw new AdminException('开播时间和结束时间间隔不得短于30分钟,不得超过24小时');
|
||||
}
|
||||
$data['anchor_name'] = $data['anchor_name'] ?? $anchor['name'];
|
||||
$data['add_time'] = time();
|
||||
$wxRoom = $this->wxCreate($data);
|
||||
$data['room_id'] = $wxRoom['roomId'];
|
||||
$data['status'] = 2;
|
||||
|
||||
if (!$this->dao->save($data)) {
|
||||
throw new AdminException('添加直播间数据失败');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 审核直播间
|
||||
* @param int $id 直播间ID
|
||||
* @param int $status 审核状态
|
||||
* @param string $msg 拒绝原因
|
||||
* @return void
|
||||
*/
|
||||
public function apply($id, $status, $msg = '')
|
||||
{
|
||||
$room = $this->dao->get($id);
|
||||
if (!$room) {
|
||||
throw new AdminException('数据不存在');
|
||||
}
|
||||
$room->status = $status;
|
||||
if ($status == -1)
|
||||
$room->error_msg = $msg;
|
||||
else {
|
||||
$room->room_id = $this->wxCreate($room)['roomId'];
|
||||
$room->status = 2;
|
||||
}
|
||||
$room->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建微信直播间
|
||||
* @param mixed $room 直播间数据
|
||||
* @return array
|
||||
*/
|
||||
public function wxCreate($room)
|
||||
{
|
||||
try {
|
||||
$coverImg = root_path() . 'public' . app()->make(DownloadImageService::class)->downloadImage($room['cover_img'])['path'];
|
||||
$shareImg = root_path() . 'public' . app()->make(DownloadImageService::class)->downloadImage($room['share_img'])['path'];
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('添加直播间封面图出错误,原因:' . $e->getMessage());
|
||||
$coverImg = root_path() . 'public' . $room['cover_img'];
|
||||
$shareImg = root_path() . 'public' . $room['share_img'];
|
||||
}
|
||||
$data = [
|
||||
'startTime' => is_string($room['start_time']) ? strtotime($room['start_time']) : $room['start_time'],
|
||||
'endTime' => is_string($room['end_time']) ? strtotime($room['end_time']) : $room['end_time'],
|
||||
'name' => $room['name'],
|
||||
'anchorName' => $room['anchor_name'],
|
||||
'anchorWechat' => $room['anchor_wechat'],
|
||||
'screenType' => $room['screen_type'],
|
||||
'closeGoods' => $room['close_goods'] == 1 ? 0 : 1,
|
||||
'closeLike' => $room['close_like'] == 1 ? 0 : 1,
|
||||
'closeComment' => $room['close_comment'] == 1 ? 0 : 1,
|
||||
'closeReplay' => $room['replay_status'] == 1 ? 0 : 1,
|
||||
'type' => $room['type'],
|
||||
'coverImg' => MiniProgram::temporaryUpload($coverImg)['media_id'],
|
||||
'shareImg' => MiniProgram::temporaryUpload($shareImg)['media_id'],
|
||||
'closeKf' => 1
|
||||
];
|
||||
$data['feedsImg'] = $data['coverImg'];
|
||||
@unlink($coverImg);
|
||||
@unlink($shareImg);
|
||||
return MiniProgram::createLiveRoom($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置直播间显示状态
|
||||
* @param int $id 直播间ID
|
||||
* @param int $is_show 显示状态
|
||||
* @return string
|
||||
*/
|
||||
public function isShow(int $id, $is_show)
|
||||
{
|
||||
$this->dao->update($id, ['is_show' => $is_show]);
|
||||
return $is_show == 1 ? '显示成功' : '隐藏成功';
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除直播间
|
||||
* @param int $id 直播间ID
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(int $id)
|
||||
{
|
||||
$room = $this->dao->get(['id' => $id, 'is_del' => 0]);
|
||||
if ($room) {
|
||||
if (!$this->dao->update($id, ['is_del' => 1])) {
|
||||
throw new AdminException('删除失败');
|
||||
}
|
||||
/** @var LiveRoomGoodsServices $liveRoomGoods */
|
||||
$liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
|
||||
$liveRoomGoods->delete(['live_room_id' => $id]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置直播间备注
|
||||
* @param int $id 直播间ID
|
||||
* @param string $mark 备注内容
|
||||
* @return bool
|
||||
*/
|
||||
public function mark($id, $mark)
|
||||
{
|
||||
return $this->dao->update($id, compact('mark'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 直播间添加商品
|
||||
* @param $room_id
|
||||
* @param array $ids
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function exportGoods(int $room_id, array $ids)
|
||||
{
|
||||
$liveGoodsServices = app()->make(LiveGoodsServices::class);
|
||||
if (count($ids) != count($goods = $liveGoodsServices->goodsList($ids)))
|
||||
throw new AdminException('请选择正确的直播商品');
|
||||
if (!$room = $this->dao->validRoom($room_id))
|
||||
throw new AdminException('直播间状态有误');
|
||||
$data = [];
|
||||
/** @var LiveRoomGoodsServices $liveRoomGoodsServices */
|
||||
$liveRoomGoodsServices = app()->make(LiveRoomGoodsServices::class);
|
||||
//查询已经关联的
|
||||
$roomGoods = $liveRoomGoodsServices->getColumn(['live_room_id' => $room_id], 'live_goods_id', 'Live_goods_id');
|
||||
$goods_ids = [];
|
||||
foreach ($goods as $key => $item) {
|
||||
if (isset($roomGoods[$item['id']])) {
|
||||
unset($goods[$key]);
|
||||
} else {
|
||||
$goods_ids[] = $item['goods_id'];
|
||||
$data[] = [
|
||||
'live_room_id' => $room_id,
|
||||
'live_goods_id' => $item['id']
|
||||
];
|
||||
}
|
||||
}
|
||||
if ($goods_ids) {
|
||||
$liveRoomGoodsServices->saveAll($data);
|
||||
return MiniProgram::roomAddGoods($room['room_id'], $goods_ids);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步直播间状态
|
||||
* @return bool
|
||||
*/
|
||||
public function syncRoomStatus()
|
||||
{
|
||||
$start = 0;
|
||||
$limit = 50;
|
||||
$data = $dataAll = [];
|
||||
$rooms = $this->dao->getColumn([], 'id,room_id,live_status', 'room_id');
|
||||
$roomIds = [];
|
||||
do {
|
||||
$wxRooms = MiniProgram::getLiveInfo($start, $limit);
|
||||
foreach ($wxRooms as $room) {
|
||||
if (in_array($room['roomid'], $roomIds)) {
|
||||
continue;
|
||||
}
|
||||
$roomIds[] = $room['roomid'];
|
||||
if ($rooms && isset($rooms[$room['roomid']])) {
|
||||
if ($room['live_status'] != $rooms[$room['roomid']]['live_status']) {
|
||||
$this->dao->update($rooms[$room['roomid']]['id'], ['live_status' => $room['live_status']]);
|
||||
}
|
||||
} else {
|
||||
$data['name'] = $room['name'];
|
||||
$data['room_id'] = $room['roomid'];
|
||||
$data['cover_img'] = $room['cover_img'];
|
||||
$data['share_img'] = $room['share_img'];
|
||||
$data['live_status'] = $room['live_status'];
|
||||
$data['start_time'] = $room['start_time'];
|
||||
$data['end_time'] = $room['end_time'];
|
||||
$data['anchor_name'] = $room['anchor_name'];
|
||||
$dataAll[] = $data;
|
||||
}
|
||||
}
|
||||
$start += $limit;
|
||||
} while (count($wxRooms) >= $limit);
|
||||
if ($dataAll) {
|
||||
$this->dao->saveAll($dataAll);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user