LiveAnchorServices.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\live;
  13. use app\dao\activity\live\LiveAnchorDao;
  14. use app\jobs\activity\AutoUpdateLiveJob;
  15. use app\services\BaseServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\CacheService;
  18. use crmeb\services\FormBuilder as Form;
  19. use crmeb\services\wechat\MiniProgram;
  20. use FormBuilder\Factory\Iview;
  21. use think\facade\Route as Url;
  22. /**
  23. * 主播
  24. * Class LiveAnchorServices
  25. * @package app\services\activity\live
  26. * @mixin LiveAnchorDao
  27. */
  28. class LiveAnchorServices extends BaseServices
  29. {
  30. /**
  31. * LiveAnchorServices constructor.
  32. * @param LiveAnchorDao $dao
  33. */
  34. public function __construct(LiveAnchorDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * 获取某个主播
  40. * @param int $id
  41. * @return array|\think\Model|null
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function getLiveAnchor(int $id)
  47. {
  48. return $this->dao->get($id);
  49. }
  50. public function getList(array $where)
  51. {
  52. [$page, $limit] = $this->getPageValue();
  53. $where['is_del'] = 0;
  54. $list = $this->dao->getList($where, '*', $page, $limit);
  55. $count = $this->dao->count($where);
  56. return compact('count', 'list');
  57. }
  58. /**
  59. * 添加修改标签表单
  60. * @param int $id
  61. * @return mixed
  62. */
  63. public function add(int $id)
  64. {
  65. $anchor = $this->getLiveAnchor($id);
  66. $field = array();
  67. if (!$anchor) {
  68. $title = '添加主播';
  69. $field[] = Form::input('name', '主播名称', '')->maxlength(20)->required('请填写名称');
  70. $field[] = Form::input('wechat', '主播微信号', '')->maxlength(32)->required('请填写微信号');
  71. $field[] = Form::input('phone', '主播手机号', '')->maxlength(20)->required('请填写手机号');
  72. $field[] = Form::frameImage('cover_img', '主播图像', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'cover_img')), '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择图像'));
  73. } else {
  74. $title = '修改主播';
  75. $field[] = Form::hidden('id', $anchor->getData('id'));
  76. $field[] = Form::input('name', '主播名称', $anchor->getData('name'))->maxlength(20)->required('请填写名称');
  77. $field[] = Form::input('wechat', '主播微信号', $anchor->getData('wechat'))->maxlength(32)->required('请填写微信号');
  78. $field[] = Form::input('phone', '主播手机号', $anchor->getData('phone'))->maxlength(20)->required('请填写手机号');
  79. $field[] = Form::frameImage('cover_img', '主播图像', Url::buildUrl(config('admin.admin_prefix') . '/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('请选择图像'));
  80. }
  81. return create_form($title, $field, $this->url('/live/anchor/save'), 'POST');
  82. }
  83. /**
  84. * 保存标签表单数据
  85. * @param int $id
  86. * @param array $data
  87. * @return mixed
  88. */
  89. public function save(int $id, array $data)
  90. {
  91. $liveAnchor = $this->dao->get(['wechat' => $data['wechat']]);
  92. if (!MiniProgram::getRoleList(2, 0, 30, $data['wechat'])) {
  93. return ['auth' => true];
  94. }
  95. if ($id) {
  96. if ($liveAnchor && $id != $liveAnchor['id']) {
  97. throw new AdminException('该主播已经存在');
  98. }
  99. if ($this->dao->update($id, $data)) {
  100. return true;
  101. } else {
  102. throw new AdminException('修改失败或者您没有修改什么!');
  103. }
  104. } else {
  105. unset($data['id']);
  106. if ($liveAnchor) {
  107. throw new AdminException('该主播已经存在');
  108. }
  109. if ($this->dao->save($data)) {
  110. return true;
  111. } else {
  112. throw new AdminException('添加失败!');
  113. }
  114. }
  115. }
  116. /**
  117. * 删除
  118. * @param $id
  119. * @throws \Exception
  120. */
  121. public function delAnchor(int $id)
  122. {
  123. if ($anchor = $this->getLiveAnchor($id)) {
  124. if (!$this->dao->update($id, ['is_del' => 1])) {
  125. throw new AdminException('删除失败,请稍候再试!');
  126. }
  127. /** @var LiveRoomServices $liveRoom */
  128. $liveRoom = app()->make(LiveRoomServices::class);
  129. $room = $liveRoom->get(['anchor_wechat' => $anchor['wechat'], 'is_del' => 0], ['id']);
  130. if ($room) {
  131. $liveRoom->delete((int)$room->id);
  132. }
  133. }
  134. return true;
  135. }
  136. /**
  137. * 设置是否显示
  138. * @param int $id
  139. * @param $is_show
  140. * @return mixed
  141. */
  142. public function setShow(int $id, $is_show)
  143. {
  144. if (!$this->getLiveAnchor($id))
  145. throw new AdminException('数据不存在');
  146. if ($this->dao->update($id, ['is_show' => $is_show])) {
  147. return $is_show == 1 ? '显示成功' : '隐藏成功';
  148. } else {
  149. throw new AdminException($is_show == 1 ? '显示失败' : '隐藏失败');
  150. }
  151. }
  152. public function syncAnchor($is_job = false)
  153. {
  154. $key = md5('Live_sync_status');
  155. $res = CacheService::redisHandler()->get($key);
  156. if (!$res || $is_job) {
  157. $start = 0;
  158. $limit = 30;
  159. $data = $dataAll = [];
  160. $anchors = $this->dao->getColumn([], 'id,wechat', 'wechat');
  161. do {
  162. $wxAnchor = MiniProgram::getRoleList(2, $start, $limit);
  163. foreach ($wxAnchor as $anchor) {
  164. if (isset($anchors[$anchor['username']])) {
  165. $this->dao->update($anchors[$anchor['username']]['id'], ['cover_img' => $anchor['headingimg'], 'name' => $anchor['nickname']]);
  166. } else {
  167. $data['name'] = $anchor['nickname'];
  168. $data['wechat'] = $anchor['username'];
  169. $data['cover_img'] = $anchor['headingimg'];
  170. $data['add_time'] = $anchor['updateTimestamp'] ?? time();
  171. $dataAll[] = $data;
  172. }
  173. }
  174. $start++;
  175. } while (count($wxAnchor) >= $limit);
  176. if ($dataAll) {
  177. $this->dao->saveAll($dataAll);
  178. }
  179. if (!$is_job) AutoUpdateLiveJob::dispatchSece(120);
  180. CacheService::redisHandler()->set($key, 1, 0);
  181. }
  182. return true;
  183. }
  184. }