barrage.vue 15 KB

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