Sentence.vue 13 KB

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