LiveAnchorServices.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\services\BaseServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\CacheService;
  17. use app\jobs\LiveJob;
  18. use crmeb\services\FormBuilder as Form;
  19. use crmeb\services\app\MiniProgramService;
  20. use FormBuilder\components\Validate;
  21. use think\facade\Route as Url;
  22. /**
  23. * Class LiveGoodsServices
  24. * @package app\services\activity\live
  25. */
  26. class LiveAnchorServices extends BaseServices
  27. {
  28. /**
  29. * LiveAnchorServices constructor.
  30. * @param LiveAnchorDao $dao
  31. */
  32. public function __construct(LiveAnchorDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 获取某个主播
  38. * @param int $id
  39. * @return array|\think\Model|null
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function getLiveAnchor(int $id)
  45. {
  46. return $this->dao->get($id);
  47. }
  48. public function getList(array $where)
  49. {
  50. [$page, $limit] = $this->getPageValue();
  51. $where['is_del'] = 0;
  52. $list = $this->dao->getList($where, '*', $page, $limit);
  53. $count = $this->dao->count($where);
  54. return compact('count', 'list');
  55. }
  56. /**
  57. * 添加修改标签表单
  58. * @param int $id
  59. * @return mixed
  60. */
  61. public function add(int $id)
  62. {
  63. $anchor = $this->getLiveAnchor($id);
  64. $field = array();
  65. if (!$anchor) {
  66. $title = '添加主播';
  67. $field[] = Form::input('name', '主播名称', '')->maxlength(20)->required('请填写名称');
  68. $field[] = Form::input('wechat', '主播微信号', '')->maxlength(32)->required('请填写微信号');
  69. $field[] = Form::input('phone', '主播手机号', '')->maxlength(20)->required('请填写手机号');
  70. $field[] = Form::frameImage('cover_img', '主播图像', Url::buildUrl(config('app.admin_prefix', 'admin') . '/widget.images/index', array('fodder' => 'cover_img')), '')->icon('el-icon-picture-outline')->width('950px')->height('560px')->Props(['footer' => false])->appendValidate(Validate::str()->required('请选择图像'));
  71. } else {
  72. $title = '修改主播';
  73. $field[] = Form::hidden('id', $anchor->getData('id'));
  74. $field[] = Form::input('name', '主播名称', $anchor->getData('name'))->maxlength(20)->required('请填写名称');
  75. $field[] = Form::input('wechat', '主播微信号', $anchor->getData('wechat'))->maxlength(32)->required('请填写微信号');
  76. $field[] = Form::input('phone', '主播手机号', $anchor->getData('phone'))->maxlength(20)->required('请填写手机号');
  77. $field[] = Form::frameImage('cover_img', '主播图像', Url::buildUrl(config('app.admin_prefix', 'admin') . '/widget.images/index', array('fodder' => 'cover_img')), $anchor->getData('cover_img'))->icon('el-icon-picture-outline')->width('950px')->height('560px')->Props(['footer' => false])->appendValidate(Validate::str()->required('请选择图像'));
  78. }
  79. return create_form($title, $field, $this->url('/live/anchor/save'), 'POST');
  80. }
  81. /**
  82. * 保存标签表单数据
  83. * @param int $id
  84. * @param array $data
  85. * @return mixed
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function save(int $id, array $data)
  91. {
  92. $liveAnchor = $this->dao->get(['wechat' => $data['wechat'], 'is_del' => 0]);
  93. if (!MiniProgramService::getRoleList(2, 0, 30, $data['wechat'])) {
  94. throw new AdminException(400426);
  95. }
  96. if ($id) {
  97. if ($liveAnchor && $id != $liveAnchor['id']) {
  98. throw new AdminException(400425);
  99. }
  100. if ($this->dao->update($id, $data)) {
  101. return true;
  102. } else {
  103. throw new AdminException(100007);
  104. }
  105. } else {
  106. unset($data['id']);
  107. if ($liveAnchor) {
  108. throw new AdminException(400425);
  109. }
  110. if ($this->dao->save($data)) {
  111. return true;
  112. } else {
  113. throw new AdminException(100022);
  114. }
  115. }
  116. }
  117. /**
  118. * 删除
  119. * @param $id
  120. * @throws \Exception
  121. */
  122. public function delAnchor(int $id)
  123. {
  124. if ($anchor = $this->getLiveAnchor($id)) {
  125. if (!$this->dao->update($id, ['is_del' => 1])) {
  126. throw new AdminException(100008);
  127. }
  128. /** @var LiveRoomServices $liveRoom */
  129. $liveRoom = app()->make(LiveRoomServices::class);
  130. $room = $liveRoom->get(['anchor_wechat' => $anchor['wechat'], 'is_del' => 0], ['id']);
  131. if ($room) {
  132. $liveRoom->delete((int)$room->id);
  133. }
  134. }
  135. return true;
  136. }
  137. /**
  138. * 设置是否显示
  139. * @param int $id
  140. * @param $is_show
  141. * @return mixed
  142. */
  143. public function setShow(int $id, $is_show)
  144. {
  145. if (!$this->getLiveAnchor($id))
  146. throw new AdminException(100026);
  147. if ($this->dao->update($id, ['is_show' => $is_show])) {
  148. return true;
  149. } else {
  150. throw new AdminException(100015);
  151. }
  152. }
  153. public function syncAnchor($is_job = false)
  154. {
  155. $key = md5('Live_sync_status');
  156. $res = CacheService::get($key);
  157. if (!$res || $is_job) {
  158. $start = 0;
  159. $limit = 30;
  160. $data = $dataAll = [];
  161. $anchors = $this->dao->getColumn([], 'id,wechat', 'wechat');
  162. do {
  163. $wxAnchor = MiniProgramService::getRoleList(2, $start, $limit);
  164. foreach ($wxAnchor as $anchor) {
  165. if (isset($anchors[$anchor['username']])) {
  166. $this->dao->update($anchors[$anchor['username']]['id'], ['cover_img' => $anchor['headingimg'], 'name' => $anchor['nickname']]);
  167. } else {
  168. $data['name'] = $anchor['nickname'];
  169. $data['wechat'] = $anchor['username'];
  170. $data['cover_img'] = $anchor['headingimg'];
  171. $data['add_time'] = $anchor['updateTimestamp'] ?? time();
  172. $dataAll[] = $data;
  173. }
  174. }
  175. $start++;
  176. } while (count($wxAnchor) >= $limit);
  177. if ($dataAll) {
  178. $this->dao->saveAll($dataAll);
  179. }
  180. //支付成功后发送消息
  181. if (!$is_job) LiveJob::dispatchSecs(120);
  182. CacheService::set($key, 1, 0);
  183. }
  184. return true;
  185. }
  186. }