barrage.vue 16 KB

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