Files
huangjingfen/pro_v3.5.1/app/services/activity/live/LiveAnchorServices.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

200 lines
7.1 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
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;
}
}