barrage.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <template>
  2. <div class="app-container live-page">
  3. <div class="live-page__header">
  4. <h2 class="live-page__title">弹幕任务</h2>
  5. </div>
  6. <div class="live-panel live-panel--table">
  7. <el-row :gutter="10" class="mb8">
  8. <el-col :span="1.5">
  9. <el-button
  10. type="primary" plain icon="el-icon-refresh" size="mini" @click="handleReload" v-hasPermi="['live:task:add']"
  11. >刷新</el-button>
  12. </el-col>
  13. <el-col :span="1.5">
  14. <el-button
  15. :disabled="isViewOnly"
  16. type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['live:task:add']"
  17. >新增</el-button>
  18. </el-col>
  19. <el-col :span="1.5">
  20. <el-button
  21. type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple || isViewOnly" @click="handleDelete" v-hasPermi="['live:task:remove']"
  22. >删除</el-button>
  23. </el-col>
  24. <el-col :span="1.5">
  25. <el-button
  26. :disabled="isViewOnly"
  27. type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['live:task:export']"
  28. >导出</el-button>
  29. </el-col>
  30. <el-col :span="1.5">
  31. <el-button
  32. :disabled="isViewOnly"
  33. type="success" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleImport" v-hasPermi="['live:task:export']"
  34. >导入</el-button>
  35. </el-col>
  36. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  37. </el-row>
  38. <el-table border v-loading="loading" :data="taskList" @selection-change="handleSelectionChange" class="live-table">
  39. <el-table-column type="selection" width="55" align="center" />
  40. <el-table-column label="编号" align="center" prop="id" />
  41. <el-table-column label="人物名称" align="center" prop="taskName" />
  42. <el-table-column label="弹幕内容" align="center" prop="content" />
  43. <el-table-column label="触发类型" align="center" prop="triggerType" :formatter="triggerTypeFormatter" />
  44. <el-table-column label="触发时间" align="center" prop="triggerValue" :formatter="triggerValueFormatter" />
  45. <!-- <el-table-column label="完成状态" align="center" prop="finishStatus" :formatter="finishStatusFormatter" />-->
  46. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  47. <template slot-scope="scope">
  48. <el-button
  49. :disabled="isViewOnly"
  50. size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['live:task:edit']"
  51. >修改</el-button>
  52. <el-button
  53. :disabled="isViewOnly"
  54. size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['live:task:remove']"
  55. >删除</el-button>
  56. </template>
  57. </el-table-column>
  58. </el-table>
  59. <pagination
  60. v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"
  61. />
  62. <!-- 添加或修改直播间自动化任务配置对话框 -->
  63. </div>
  64. <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  65. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  66. <el-form-item label="直播间ID" prop="liveId" v-show="false">
  67. <el-input :disabled="liveAbled" v-model="form.liveId" placeholder="请输入直播间ID" />
  68. </el-form-item>
  69. <el-form-item label="人物名称" prop="taskName">
  70. <el-input v-model="form.taskName" placeholder="请输入人物名称" />
  71. </el-form-item>
  72. <el-form-item label="弹幕内容" prop="content">
  73. <el-input v-model="form.content" placeholder="请输入弹幕内容" />
  74. </el-form-item>
  75. <el-form-item label="触发时间" prop="content">
  76. <el-time-picker
  77. default-value="2025-01-01 00:00:00"
  78. v-model="form.triggerValue"
  79. :picker-options="{
  80. selectableRange: '00:00:00 - 23:59:59'
  81. }"
  82. placeholder="任意时间点">
  83. </el-time-picker>
  84. </el-form-item>
  85. <!-- <el-form-item label="触发类型" prop="triggerType">-->
  86. <!-- <el-select v-model="form.triggerType" placeholder="请选择触发类型">-->
  87. <!-- <el-option label="请选择字典生成" value="" />-->
  88. <!-- </el-select>-->
  89. <!-- </el-form-item>-->
  90. <!-- <el-form-item label="触发值" prop="triggerValue">-->
  91. <!-- <el-input v-model="form.triggerValue" placeholder="请输入触发值" />-->
  92. <!-- </el-form-item>-->
  93. <!-- <el-form-item label="任务内容">-->
  94. <!-- <editor v-model="form.content" :min-height="192"/>-->
  95. <!-- </el-form-item>-->
  96. <el-form-item label="状态">
  97. <el-radio-group v-model="form.status">
  98. <el-radio :label="1">启用</el-radio>
  99. <el-radio :label="0">禁用</el-radio>
  100. </el-radio-group>
  101. </el-form-item>
  102. </el-form>
  103. <div slot="footer" class="dialog-footer">
  104. <el-button type="primary" @click="submitForm">确 定</el-button>
  105. <el-button @click="cancel">取 消</el-button>
  106. </div>
  107. </el-dialog>
  108. <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
  109. <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '' " :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
  110. <i class="el-icon-upload"></i>
  111. <div class="el-upload__text">
  112. 将文件拖到此处,或
  113. <em>点击上传</em>
  114. </div>
  115. <div class="el-upload__tip" slot="tip">
  116. <el-link type="info" style="font-size:12px;float:right" @click="importTemplate">下载模板</el-link>
  117. </div>
  118. <div class="el-upload__tip" style="color:red" slot="tip">提示:仅允许导入“xls”或“xlsx”格式文件!</div>
  119. </el-upload>
  120. <div slot="footer" class="dialog-footer">
  121. <el-button type="primary" @click="submitFileForm">确 定</el-button>
  122. <el-button @click="upload.open = false">取 消</el-button>
  123. </div>
  124. </el-dialog>
  125. </div>
  126. </template>
  127. <script>
  128. import { listTaskBarrage, getTask, delTask, addTask, updateTask, exportTaskBarrage,importTemplate } from "@/api/live/task";
  129. import { getToken } from "@/utils/auth";
  130. export default {
  131. props:{
  132. isViewOnly: {
  133. type: Boolean,
  134. default: false,
  135. }
  136. },
  137. name: "Barrage",
  138. data() {
  139. return {
  140. // 遮罩层
  141. loading: true,
  142. // 导出遮罩层
  143. exportLoading: false,
  144. // 选中数组
  145. ids: [],
  146. // 非单个禁用
  147. single: true,
  148. // 非多个禁用
  149. multiple: true,
  150. // 显示搜索条件
  151. showSearch: true,
  152. // 总条数
  153. total: 0,
  154. liveAbled: false,
  155. // 直播间自动化任务配置表格数据
  156. taskList: [],
  157. // 弹出层标题
  158. title: "",
  159. // 是否显示弹出层
  160. open: false,
  161. // 查询参数
  162. queryParams: {
  163. pageNum: 1,
  164. pageSize: 10,
  165. liveId: null,
  166. taskName: null,
  167. taskType: null,
  168. triggerType: null,
  169. triggerValue: null,
  170. content: null,
  171. status: null,
  172. createdTime: null,
  173. updatedTime: null
  174. },
  175. // 表单参数
  176. form: {},
  177. // 表单校验
  178. rules: {
  179. liveId: [
  180. { required: true, message: "直播间ID不能为空", trigger: "blur" }
  181. ],
  182. taskName: [
  183. { required: true, message: "人物名称不能为空", trigger: "blur" }
  184. ],
  185. taskType: [
  186. { required: true, message: "任务类型:1-定时推送卡片商品 2-定时发送红包 ", trigger: "change" }
  187. ],
  188. content: [
  189. { required: true, message: "任务内容不能为空", trigger: "blur" }
  190. ],
  191. triggerType: [
  192. { required: true, message: "触发类型:相对直播开始时间不能为空", trigger: "change" }
  193. ],
  194. triggerValue: [
  195. { required: true, message: "触发值:绝对时间用yyyy-MM-dd HH:mm:ss,相对时间用分钟数不能为空", trigger: "blur" }
  196. ],
  197. },
  198. // 用户导入参数
  199. upload: {
  200. // 是否显示弹出层(用户导入)
  201. open: false,
  202. // 弹出层标题(用户导入)
  203. title: "",
  204. // 是否禁用上传
  205. isUploading: false,
  206. // 是否更新已经存在的用户数据
  207. updateSupport: 0,
  208. // 设置上传的请求头部
  209. headers: { Authorization: "Bearer " + getToken() },
  210. // 上传的地址
  211. url: process.env.VUE_APP_BASE_API + "/live/task/importData",
  212. },
  213. liveId: null,
  214. socket: null,
  215. isLoading: false, // 是否正在加载
  216. hasMore: true, // 是否还有更多数据
  217. };
  218. },
  219. watch: {
  220. // 监听路由的 query 参数变化
  221. '$route.query': {
  222. handler(newQuery) {
  223. if (this.$route.params.liveId) {
  224. this.liveId = this.$route.params.liveId;
  225. }else {
  226. this.liveId = this.$route.query.liveId;
  227. }
  228. if(this.liveId == null) {
  229. return;
  230. }
  231. this.liveAbled = true
  232. this.queryParams.liveId = this.liveId;
  233. this.upload.url = process.env.VUE_APP_BASE_API + "/live/task/importData?liveId=" + this.liveId;
  234. this.getList();
  235. },
  236. // 初始化时立即执行一次
  237. immediate: true
  238. }
  239. },
  240. created() {
  241. this.socket = (this.$store.state.liveWs[String(this.liveId)] || this.$store.state.liveWs[this.liveId])
  242. },
  243. methods: {
  244. triggerTypeFormatter(row, column, value){
  245. if (!value) return '--'; // 空值处理
  246. switch (value) {
  247. case 1:
  248. return "相对直播开始时间";
  249. case 2:
  250. return "相对时间";
  251. default:
  252. return "--";
  253. }
  254. },
  255. triggerValueFormatter(row, column, value) {
  256. if (!value) return '--'; // 空值处理
  257. // 创建日期对象(兼容时间戳和字符串)
  258. let date;
  259. if (typeof value === 'number') {
  260. // 处理时间戳(注意:如果是10位时间戳需要乘以1000)
  261. date = new Date(value.toString().length === 10 ? value * 1000 : value);
  262. } else if (typeof value === 'string') {
  263. // 处理字符串格式(尝试直接转换)
  264. date = new Date(value);
  265. } else {
  266. return '格式错误';
  267. }
  268. // 检查日期是否有效
  269. if (isNaN(date.getTime())) {
  270. return '无效时间';
  271. }
  272. // 格式化日期为 "yyyy-MM-dd HH:mm:ss"
  273. const hours = String(date.getHours()).padStart(2, '0');
  274. const minutes = String(date.getMinutes()).padStart(2, '0');
  275. const seconds = String(date.getSeconds()).padStart(2, '0');
  276. return `${hours}:${minutes}:${seconds}`;
  277. },
  278. /** 查询直播间自动化任务配置列表 */
  279. getList() {
  280. if(this.liveId == null) {
  281. this.$message.error("页面错误,请联系管理员");
  282. return;
  283. }
  284. this.loading = true;
  285. listTaskBarrage(this.queryParams).then(res => {
  286. if(res.rows.length > 0) {
  287. this.taskList = res.rows;
  288. }
  289. this.total = res.total;
  290. this.loading = false;
  291. });
  292. },
  293. // 取消按钮
  294. cancel() {
  295. this.open = false;
  296. this.reset();
  297. },
  298. // 表单重置
  299. reset() {
  300. this.form = {
  301. id: null,
  302. liveId: this.liveId,
  303. taskName: null,
  304. taskType: null,
  305. triggerType: null,
  306. triggerValue: null,
  307. content: null,
  308. status: 1,
  309. createdTime: null,
  310. updatedTime: null
  311. };
  312. this.resetForm("form");
  313. },
  314. /** 搜索按钮操作 */
  315. handleQuery() {
  316. this.queryParams.pageNum = 1;
  317. this.getList();
  318. },
  319. /** 重置按钮操作 */
  320. resetQuery() {
  321. this.resetForm("queryForm");
  322. this.handleQuery();
  323. },
  324. // 多选框选中数据
  325. handleSelectionChange(selection) {
  326. this.ids = selection.map(item => item.id)
  327. this.single = selection.length!==1
  328. this.multiple = !selection.length
  329. },
  330. /** 新增按钮操作 */
  331. handleAdd() {
  332. this.reset();
  333. this.open = true;
  334. this.title = "添加直播间弹幕脚本";
  335. },
  336. handleReload() {
  337. this.getList()
  338. },
  339. /** 修改按钮操作 */
  340. async handleUpdate(row) {
  341. this.reset();
  342. this.form = row;
  343. this.open = true;
  344. this.title = "修改直播间弹幕脚本";
  345. },
  346. /** 提交按钮 */
  347. submitForm() {
  348. this.form.liveId = this.liveId;
  349. if(this.liveId == null) {
  350. this.msgError("请选择直播间");
  351. return;
  352. }
  353. this.$refs["form"].validate(valid => {
  354. if (valid) {
  355. this.form.taskType = 3
  356. if (this.form.id != null) {
  357. updateTask(this.form).then(response => {
  358. this.msgSuccess("修改成功");
  359. this.open = false;
  360. this.getList();
  361. });
  362. } else {
  363. addTask(this.form).then(response => {
  364. this.msgSuccess("新增成功");
  365. this.open = false;
  366. this.getList();
  367. });
  368. }
  369. }
  370. });
  371. },
  372. /** 删除按钮操作 */
  373. handleDelete(row) {
  374. const ids = row.id || this.ids;
  375. this.$confirm('是否确认删除直播间弹幕脚本配置编号为"' + ids + '"的数据项?', "警告", {
  376. confirmButtonText: "确定",
  377. cancelButtonText: "取消",
  378. type: "warning"
  379. }).then(function() {
  380. return delTask(ids);
  381. }).then(() => {
  382. this.getList();
  383. this.msgSuccess("删除成功");
  384. const msg={
  385. cmd:'delAutoTask',
  386. data:row.absValue,
  387. liveId:this.liveId,
  388. userType:1
  389. }
  390. this.socket.send(JSON.stringify( msg))
  391. }).catch(() => {});
  392. },
  393. /** 导出按钮操作 */
  394. handleExport() {
  395. const queryParams = this.queryParams;
  396. this.$confirm('是否确认导出所有直播间弹幕脚本数据项?', "警告", {
  397. confirmButtonText: "确定",
  398. cancelButtonText: "取消",
  399. type: "warning"
  400. }).then(() => {
  401. this.exportLoading = true;
  402. return exportTaskBarrage(queryParams);
  403. }).then(response => {
  404. this.download(response.msg);
  405. this.exportLoading = false;
  406. }).catch(() => {});
  407. },
  408. handleImport() {
  409. this.upload.title = "弹幕脚本导入";
  410. this.upload.open = true;
  411. },
  412. // 文件上传中处理
  413. handleFileUploadProgress(event, file, fileList) {
  414. this.upload.isUploading = true;
  415. },
  416. // 文件上传成功处理
  417. handleFileSuccess(response, file, fileList) {
  418. this.upload.open = false;
  419. this.upload.isUploading = false;
  420. this.$refs.upload.clearFiles();
  421. this.importMsgOpen=true;
  422. this.importMsg=response.msg
  423. this.getList();
  424. },
  425. // 提交上传文件
  426. submitFileForm() {
  427. if (!this.liveId) {
  428. this.$message.error("错误直播间,请联系管理员处理");
  429. return ;
  430. }
  431. this.$refs.upload.submit();
  432. },
  433. /** 下载模板操作 */
  434. importTemplate() {
  435. importTemplate().then((response) => {
  436. this.download(response.msg);
  437. });
  438. },
  439. }
  440. };
  441. </script>
  442. <style scoped>
  443. .loading-indicator {
  444. padding: 10px;
  445. text-align: center;
  446. color: #606266;
  447. font-size: 12px;
  448. display: flex;
  449. align-items: center;
  450. justify-content: center;
  451. }
  452. .loading-indicator .el-icon-loading {
  453. margin-right: 5px;
  454. animation: rotate 1s linear infinite;
  455. }
  456. .no-more {
  457. padding: 10px;
  458. text-align: center;
  459. color: #909399;
  460. font-size: 12px;
  461. }
  462. @keyframes rotate {
  463. from { transform: rotate(0deg); }
  464. to { transform: rotate(360deg); }
  465. }
  466. </style>