Sentence.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <!-- -->
  2. <template>
  3. <div class="Big-Book-sentence" v-if="curQue">
  4. <div
  5. class="Big-Book-Single-content"
  6. style="margin-left: 20px; margin-top: 20px"
  7. >
  8. <div class="adult-book-input-item">
  9. <span class="adult-book-lable">标题:</span>
  10. <el-input
  11. class="adult-book-input"
  12. type="textarea"
  13. autosize
  14. placeholder="请输入标题"
  15. v-model="curQue.title"
  16. @blur="onBlur(curQue, 'title')"
  17. ></el-input>
  18. </div>
  19. <template v-if="type == 'sentence_listen_read_chs'">
  20. <div class="adult-book-input-item">
  21. <span class="adult-book-lable">题目音频:</span>
  22. <Upload
  23. :changeFillId="changeMp3"
  24. :datafileList="curQue.mp3_list"
  25. :filleNumber="mp3Number"
  26. :uploadType="'mp3'"
  27. :handleMp3Base64="handleChange"
  28. />
  29. </div>
  30. </template>
  31. <div
  32. class="Big-Book-main"
  33. v-for="(item, index) in curQue.option"
  34. :key="item + index"
  35. style="margin-bottom: 20px"
  36. >
  37. <SentenceModule
  38. :curQueItem="item"
  39. :curQue="curQue"
  40. :deleteOptionOne="deleteOptionOne"
  41. :index="index"
  42. :type="type"
  43. :fn_list="curQue.fn_list"
  44. />
  45. </div>
  46. <div class="Big-Book-addrole">
  47. <div class="addoption" @click="addOption">添加一个</div>
  48. </div>
  49. <div class="lrc-box" v-if="type != 'recordHZ_inputPY_chs'">
  50. <div
  51. v-if="this.curQue.wordTime && this.curQue.wordTime.length > 0"
  52. class="lrc-box"
  53. >
  54. <span>已有字幕时间节点</span>
  55. <el-button type="text" @click="againWordTime">重新生成</el-button>
  56. </div>
  57. <template v-else>
  58. <el-button v-if="!isWordTime" size="medium" @click="createWordTime"
  59. >自动生成字幕节点</el-button
  60. >
  61. <p v-else>字幕节点生成中...请等待</p>
  62. </template>
  63. </div>
  64. </div>
  65. </div>
  66. </template>
  67. <script>
  68. import SentenceModule from "../common/SentenceModule.vue";
  69. import Upload from "../common/Upload";
  70. const Base64 = require("js-base64").Base64;
  71. import {
  72. segSentences,
  73. BatchSegContent,
  74. createPinyin,
  75. prepareTranscribe,
  76. getWordTime,
  77. } from "@/api/ajax";
  78. export default {
  79. name: "Single",
  80. props: ["curQue", "fn_data", "changeCurQue", "type"],
  81. components: {
  82. SentenceModule,
  83. Upload,
  84. },
  85. data() {
  86. return {
  87. checkList: [],
  88. mp3Number: 1,
  89. form: {
  90. stem: {
  91. con: "",
  92. pinyin: "",
  93. english: "",
  94. highlight: "",
  95. underline: "",
  96. img_url: [],
  97. mp3_url: [],
  98. },
  99. },
  100. data_structure: {
  101. type: "recordHZ_inputPY_chs",
  102. name: "读汉字写拼音",
  103. title: "",
  104. option: [
  105. {
  106. number: "",
  107. con: "",
  108. answer: "",
  109. IsRecord: false,
  110. mp3_url: [],
  111. font:"",
  112. },
  113. ],
  114. },
  115. data_structure2: {
  116. type: "inputItem_chs",
  117. name: "输入选项",
  118. title: "",
  119. option: [
  120. {
  121. number: "",
  122. con: "",
  123. answer: "",
  124. detail: {
  125. pyPosition: "top", //top 拼音在上面;bottom 拼音在下面
  126. sentence: "", //句子
  127. segList: [], //分词结果
  128. seg_words: "",
  129. wordsList: [],
  130. },
  131. },
  132. ],
  133. },
  134. data_structure_sentence: {
  135. type: "sentence_chs",
  136. name: "句子模板",
  137. title: "",
  138. mp3_list: [],
  139. taskId: "",
  140. wordTime: [],
  141. detail: [],
  142. option: [
  143. {
  144. mp3_list: [],
  145. number: "",
  146. font: "",
  147. detail: {
  148. pyPosition: "top", //top 拼音在上面;bottom 拼音在下面
  149. sentence: "", //句子
  150. segList: [], //分词结果
  151. seg_words: "",
  152. wordsList: [],
  153. },
  154. en: "", //英文
  155. answer: [""],
  156. judge: true,
  157. },
  158. ],
  159. fn_list: [
  160. {
  161. type: "sentence_record_chs",
  162. name: "录音",
  163. isFn: false,
  164. },
  165. {
  166. type: "sentence_input_chs",
  167. name: "输入",
  168. isFn: false,
  169. },
  170. {
  171. type: "sentence_judge_chs",
  172. name: "判断",
  173. isFn: false,
  174. },
  175. ],
  176. },
  177. isWordTime: false,
  178. };
  179. },
  180. computed: {},
  181. watch: {},
  182. //方法集合
  183. methods: {
  184. createWordTime() {
  185. this.curQue.detail = [];
  186. this.curQue.option.forEach((item) => {
  187. this.curQue.detail.push(item.detail);
  188. });
  189. if (this.curQue.taskId) {
  190. let verseList = [];
  191. let numberArr = [];
  192. this.curQue.detail.forEach((item, i) => {
  193. numberArr.push(this.numberToChinese(i + 1));
  194. });
  195. for (let i = 0; i < this.curQue.detail.length; i++) {
  196. verseList = verseList.concat(
  197. `${numberArr[i]}` + this.curQue.detail[i].sentence
  198. );
  199. }
  200. if (verseList.length > 0) {
  201. this.isWordTime = true;
  202. let data = {
  203. taskId: this.curQue.taskId,
  204. verseList: JSON.stringify(verseList),
  205. };
  206. getWordTime(data).then((res) => {
  207. this.curQue.wordTime = res.data.result;
  208. this.isWordTime = false;
  209. });
  210. }
  211. } else {
  212. this.$message.warning("请先上传音频");
  213. }
  214. },
  215. againWordTime() {
  216. this.isWordTime = false;
  217. this.$set(this.curQue, "wordTime", []);
  218. },
  219. numberToChinese(num) {
  220. var AA = new Array(
  221. "零",
  222. "一",
  223. "二",
  224. "三",
  225. "四",
  226. "五",
  227. "六",
  228. "七",
  229. "八",
  230. "九",
  231. "十"
  232. );
  233. var BB = new Array("", "十", "百", "仟", "萬", "億", "点", "");
  234. var a = ("" + num).replace(/(^0*)/g, "").split("."),
  235. k = 0,
  236. re = "";
  237. for (var i = a[0].length - 1; i >= 0; i--) {
  238. switch (k) {
  239. case 0:
  240. re = BB[7] + re;
  241. break;
  242. case 4:
  243. if (
  244. !new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$").test(a[0])
  245. )
  246. re = BB[4] + re;
  247. break;
  248. case 8:
  249. re = BB[5] + re;
  250. BB[7] = BB[5];
  251. k = 0;
  252. break;
  253. }
  254. if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0)
  255. re = AA[0] + re;
  256. if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re;
  257. k++;
  258. }
  259. if (a.length > 1) {
  260. // 加上小数部分(如果有小数部分)
  261. re += BB[6];
  262. for (var i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
  263. }
  264. if (re == "一十") re = "十";
  265. if (re.match(/^一/) && re.length == 3) re = re.replace("一", "");
  266. return re;
  267. },
  268. // 上传音频文件
  269. handleChange(file, fileList) {
  270. let _this = this;
  271. _this.getBase64(file.raw).then((res) => {
  272. let base_res = res.split("base64,");
  273. let data = {
  274. fileName: file.raw.name,
  275. speechBase64: base_res[1],
  276. };
  277. prepareTranscribe(data).then((res) => {
  278. _this.$set(_this.curQue, "taskId", res.data.taskId);
  279. });
  280. });
  281. },
  282. getBase64(file) {
  283. return new Promise(function (resolve, reject) {
  284. let reader = new FileReader();
  285. let imgResult = "";
  286. reader.readAsDataURL(file);
  287. reader.onload = function () {
  288. imgResult = reader.result;
  289. };
  290. reader.onerror = function (error) {
  291. reject(error);
  292. };
  293. reader.onloadend = function () {
  294. resolve(imgResult);
  295. };
  296. });
  297. },
  298. onBlur(item, field) {
  299. item[field] = item[field] ? item[field].trim() : "";
  300. },
  301. // 修改正确选项中得某一个为正确答案
  302. changAnswer(index) {
  303. this.curQue.option.forEach((item, i) => {
  304. if (index == i) {
  305. this.curQue.correct[0].input[index] = item.Answer;
  306. }
  307. });
  308. },
  309. // 删除其中一个选项
  310. deleteOptionOne(index) {
  311. if (this.curQue.option.length <= 1) {
  312. this.$message.warning("至少要保留1个选项");
  313. return;
  314. }
  315. this.curQue.option.splice(index, 1);
  316. },
  317. // 新增选项
  318. addOption() {
  319. let obj;
  320. if (this.type == "recordHZ_inputPY_chs") {
  321. obj = JSON.parse(JSON.stringify(this.data_structure.option[0]));
  322. } else if (this.type == "inputItem_chs") {
  323. obj = JSON.parse(JSON.stringify(this.data_structure2.option[0]));
  324. } else if (this.type.indexOf("sentence") > -1) {
  325. obj = JSON.parse(
  326. JSON.stringify(this.data_structure_sentence.option[0])
  327. );
  328. }
  329. this.curQue.option.push(obj);
  330. },
  331. // 更多配置选择
  332. handleCheckedFnChange(value) {
  333. // let fn_list = JSON.parse(JSON.stringify(this.curQue.fn_list));
  334. // this.curQue.fn_list = fn_list.map((item) => {
  335. // if (value.indexOf(item.name) > -1) {
  336. // this.checkList.push(item.name);
  337. // item.isFn = true;
  338. // } else {
  339. // item.isFn = false;
  340. // this.curQue.con.forEach((it) => {
  341. // it.annotation = "";
  342. // });
  343. // }
  344. // return item;
  345. // });
  346. },
  347. //处理数据
  348. handleStructure(type) {
  349. let data = JSON.parse(JSON.stringify(this.data_structure_sentence));
  350. data.fn_list.map((item) => {
  351. if (item.type == type) {
  352. item.isFn = true;
  353. }
  354. if (type == "sentence_input_record_chs") {
  355. if (
  356. item.type == "sentence_record_chs" ||
  357. item.type == "sentence_input_chs"
  358. ) {
  359. item.isFn = true;
  360. }
  361. }
  362. if (
  363. type == "recordHZ_inputPY_chs" ||
  364. type == "sentence_listen_read_chs"
  365. ) {
  366. if (item.type == "sentence_record_chs") {
  367. item.isFn = true;
  368. }
  369. }
  370. return item;
  371. });
  372. return data;
  373. },
  374. initcurQue() {
  375. let data = null;
  376. if (this.type == "recordHZ_inputPY_chs") {
  377. // data = JSON.parse(JSON.stringify(this.data_structure));
  378. data = this.handleStructure(this.type);
  379. } else if (this.type == "inputItem_chs") {
  380. data = JSON.parse(JSON.stringify(this.data_structure2));
  381. } else if (this.type.indexOf("sentence_") > -1) {
  382. data = this.handleStructure(this.type);
  383. }
  384. console.log(data);
  385. this.changeCurQue(data);
  386. },
  387. changeMp3(fileList) {
  388. const articleImgList = JSON.parse(JSON.stringify(fileList));
  389. const articleImgRes = [];
  390. articleImgList.forEach((item) => {
  391. if (item.response) {
  392. const obj = {
  393. name: item.name,
  394. url: item.response.file_info_list[0].file_url,
  395. id: "[FID##" + item.response.file_info_list[0].file_id + "##FID]",
  396. media_duration: item.response.file_info_list[0].media_duration, //音频时长
  397. };
  398. articleImgRes.push(obj);
  399. }
  400. });
  401. this.curQue.mp3_list = JSON.parse(JSON.stringify(articleImgRes));
  402. },
  403. },
  404. //生命周期 - 创建完成(可以访问当前this实例)
  405. created() {
  406. console.log("句子控件的录入");
  407. if (!this.curQue) {
  408. this.initcurQue();
  409. }
  410. },
  411. //生命周期 - 挂载完成(可以访问DOM元素)
  412. mounted() {},
  413. beforeCreate() {}, //生命周期 - 创建之前
  414. beforeMount() {}, //生命周期 - 挂载之前
  415. beforeUpdate() {}, //生命周期 - 更新之前
  416. updated() {}, //生命周期 - 更新之后
  417. beforeDestroy() {}, //生命周期 - 销毁之前
  418. destroyed() {}, //生命周期 - 销毁完成
  419. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  420. };
  421. </script>
  422. <style lang='scss' scope>
  423. //@import url(); 引入公共css类
  424. .Big-Book-sentence {
  425. &-content {
  426. &.m {
  427. display: flex;
  428. justify-content: flex-start;
  429. align-items: flex-start;
  430. }
  431. .Big-Book-title {
  432. font-size: 16px;
  433. line-height: 40px;
  434. color: #000;
  435. margin-right: 15px;
  436. }
  437. .Big-Book-main {
  438. > div {
  439. margin-bottom: 10px;
  440. &.Big-Book-pinyin {
  441. display: flex;
  442. justify-content: flex-start;
  443. align-items: center;
  444. }
  445. }
  446. }
  447. }
  448. .Big-Book-addrole {
  449. > div {
  450. width: 300px;
  451. height: 40px;
  452. background: #f3f3f3;
  453. border: 1px dashed rgba(0, 0, 0, 0.15);
  454. box-sizing: border-box;
  455. border-radius: 4px;
  456. text-align: center;
  457. line-height: 40px;
  458. cursor: pointer;
  459. }
  460. }
  461. .Big-Book-more {
  462. .Big-Book-more-text {
  463. position: relative;
  464. text-align: center;
  465. }
  466. .Big-Book-more-text:before,
  467. .Big-Book-more-text:after {
  468. position: absolute;
  469. background: #ccc;
  470. content: "";
  471. height: 1px;
  472. top: 50%;
  473. width: 45%;
  474. }
  475. .Big-Book-more-text:before {
  476. left: 10px;
  477. }
  478. .Big-Book-more-text:after {
  479. right: 10px;
  480. }
  481. .Big-Book-more-main {
  482. display: flex;
  483. > :not(:nth-child(1)) {
  484. margin-left: 30px;
  485. }
  486. }
  487. }
  488. .Big-Book-con {
  489. display: flex;
  490. align-items: center;
  491. }
  492. }
  493. .lrc-box {
  494. display: flex;
  495. justify-content: flex-start;
  496. align-items: center;
  497. > span {
  498. font-size: 14px;
  499. margin-right: 16px;
  500. }
  501. }
  502. </style>
  503. <style lang="scss">
  504. </style>