ChooseToneQuestion.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <template>
  2. <QuestionBase>
  3. <template #content>
  4. <div class="stem">
  5. <RichText v-model="data.stem" :font-size="18" placeholder="输入题干" />
  6. <RichText
  7. v-if="isEnable(data.property.is_enable_description)"
  8. v-model="data.description"
  9. placeholder="输入提示"
  10. />
  11. </div>
  12. <div class="content">
  13. <label class="subtitle">内容</label>
  14. <ul>
  15. <li v-for="(item, i) in data.option_list" :key="i" class="content-item">
  16. <span class="question-number" title="双击切换序号类型" @dblclick="changeOptionType(data)">
  17. {{ computedQuestionNumber(i, data.option_number_show_mode) }}
  18. </span>
  19. <el-input v-model="item.character" :placeholder="'汉字'" style="flex-shrink: 0; width: 80px" />
  20. <el-input
  21. v-model="item.content"
  22. placeholder="拼音间用空格隔开,如:ni3 hao3"
  23. @blur="handleItemAnswer(item)"
  24. @change="changePinyin(item)"
  25. />
  26. <el-input
  27. v-model="matically_pinyin_str[item.mark]"
  28. :placeholder="'拼音预览'"
  29. :readonly="true"
  30. style="width: 200px"
  31. />
  32. <div v-if="item.audio_file_id">
  33. <SoundRecord :wav-blob.sync="item.audio_file_id" />
  34. </div>
  35. <template v-else>
  36. <div :class="['upload-audio-play']">
  37. <UploadAudio
  38. v-if="data.other.audio_generation_method === 'upload'"
  39. :key="item.audio_file_id || i"
  40. :file-id="item.audio_file_id"
  41. :item-index="i"
  42. :show-upload="!item.audio_file_id"
  43. @upload="uploads"
  44. @deleteFile="deleteFiles"
  45. />
  46. <div
  47. v-else-if="data.other.audio_generation_method === 'auto'"
  48. v-loading="loading_list[i] ? loading_list[i].loading : false"
  49. class="auto-matically"
  50. @click="handleMatically(item, i)"
  51. >
  52. <SvgIcon icon-class="voiceprint-line" class="record" />
  53. <span class="auto-btn">{{ item.audio_file_id ? '已生成' : '生成音频' }}</span>
  54. </div>
  55. <SoundRecord v-else :wav-blob.sync="item.audio_file_id" />
  56. </div>
  57. </template>
  58. <SvgIcon icon-class="delete" class="delete pointer" @click="deleteOption(i, item.audio_file_id)" />
  59. </li>
  60. </ul>
  61. </div>
  62. <div class="footer">
  63. <span class="add-option" @click="addOption">
  64. <SvgIcon icon-class="add-circle" size="14" /> <span>增加选项</span>
  65. </span>
  66. </div>
  67. <div v-if="isEnable(data.property.is_enable_analysis)" class="analysis">
  68. <div class="analysis-title">解析:</div>
  69. <RichText v-model="data.analysis" :is-border="true" :font-size="14" placeholder="输入解析" />
  70. </div>
  71. </template>
  72. <template #property>
  73. <el-form :model="data.property" label-width="72px" label-position="left">
  74. <el-form-item label="题号">
  75. <el-input v-model="data.property.question_number" />
  76. </el-form-item>
  77. <el-form-item>
  78. <el-radio
  79. v-for="{ value, label } in questionNumberTypeList"
  80. :key="value"
  81. v-model="data.other.question_number_type"
  82. :label="value"
  83. >
  84. {{ label }}
  85. </el-radio>
  86. </el-form-item>
  87. <el-form-item label="题干题号">
  88. <el-select v-model="data.property.stem_question_number_font_size">
  89. <el-option v-for="item in fontSizeList" :key="item" :label="item" :value="item" />
  90. </el-select>
  91. </el-form-item>
  92. <el-form-item label="选项题号">
  93. <el-select v-model="data.property.option_question_number_font_size">
  94. <el-option v-for="item in fontSizeList" :key="item" :label="item" :value="item" />
  95. </el-select>
  96. </el-form-item>
  97. <el-form-item label="提示">
  98. <el-radio
  99. v-for="{ value, label } in switchOption"
  100. :key="value"
  101. v-model="data.property.is_enable_description"
  102. :label="value"
  103. >
  104. {{ label }}
  105. </el-radio>
  106. </el-form-item>
  107. <el-form-item label="解析">
  108. <el-radio
  109. v-for="{ value, label } in switchOption"
  110. :key="value"
  111. v-model="data.property.is_enable_analysis"
  112. :label="value"
  113. >
  114. {{ label }}
  115. </el-radio>
  116. </el-form-item>
  117. <el-form-item label="分值">
  118. <el-radio
  119. v-for="{ value, label } in scoreTypeList"
  120. :key="value"
  121. v-model="data.property.score_type"
  122. :label="value"
  123. >
  124. {{ label }}
  125. </el-radio>
  126. </el-form-item>
  127. <el-form-item>
  128. <el-input-number
  129. v-model="data.property.score"
  130. :min="0"
  131. :step="data.property.score_type === scoreTypeList[0].value ? 1 : 0.1"
  132. />
  133. </el-form-item>
  134. <el-form-item label="类型">
  135. <el-radio
  136. v-for="{ value, label } in toneTypeList"
  137. :key="value"
  138. v-model="data.property.answer_mode"
  139. :label="value"
  140. @change="handleChangeType"
  141. >
  142. {{ label }}
  143. </el-radio>
  144. </el-form-item>
  145. <el-form-item label="音频">
  146. <el-radio
  147. v-for="{ value, label } in audioGenerationMethodList"
  148. :key="value"
  149. v-model="data.other.audio_generation_method"
  150. :label="value"
  151. >
  152. {{ label }}
  153. </el-radio>
  154. </el-form-item>
  155. </el-form>
  156. </template>
  157. </QuestionBase>
  158. </template>
  159. <script>
  160. import QuestionMixin from '@/views/exercise_questions/create/components/common/QuestionMixin';
  161. import UploadAudio from '../common/UploadAudio.vue';
  162. import SoundRecord from '../common/SoundRecord.vue';
  163. import { changeOptionType, addTone } from '@/views/exercise_questions/data/common';
  164. import {
  165. getOption,
  166. ChooseToneData,
  167. audioGenerationMethodList,
  168. toneList,
  169. toneTypeList,
  170. analysisRecognitionChooseToneData,
  171. } from '@/views/exercise_questions/data/chooseTone';
  172. import { GetStaticResources } from '@/api/app';
  173. export default {
  174. name: 'ChooseToneQuestion',
  175. components: {
  176. UploadAudio,
  177. SoundRecord,
  178. },
  179. mixins: [QuestionMixin],
  180. data() {
  181. return {
  182. toneList,
  183. changeOptionType,
  184. audioGenerationMethodList,
  185. toneTypeList,
  186. data: JSON.parse(JSON.stringify(ChooseToneData)),
  187. matically_pinyin_obj: {}, // 存放转成声调的拼音
  188. matically_pinyin_str: {}, // 存放转成声调的字符串
  189. res_arr: [],
  190. loading_list: [
  191. {
  192. loading: false,
  193. },
  194. {
  195. loading: false,
  196. },
  197. {
  198. loading: false,
  199. },
  200. ],
  201. };
  202. },
  203. watch: {
  204. 'data.option_list.length': {
  205. handler(val) {
  206. this.loading_list = [];
  207. for (let i = 0; i < val; i++) {
  208. let obj = {
  209. loading: false,
  210. };
  211. this.loading_list.push(obj);
  212. }
  213. this.handleChangeType();
  214. },
  215. deep: true,
  216. immediate: true,
  217. },
  218. 'data.option_list': {
  219. handler(val) {
  220. if (!val) return;
  221. // 删除答案中不存在的选项
  222. this.data.answer.answer_list.forEach((item, i) => {
  223. if (!val.find((li) => li.mark === item.mark)) {
  224. this.data.answer.answer_list.splice(i, 1);
  225. }
  226. });
  227. },
  228. },
  229. },
  230. created() {},
  231. methods: {
  232. addOption() {
  233. this.data.option_list.push(getOption());
  234. },
  235. uploads(file_id, index) {
  236. this.data.option_list[index].audio_file_id = file_id;
  237. this.data.file_id_list.push(file_id);
  238. },
  239. deleteFiles(file_id, itemIndex) {
  240. this.data.option_list[itemIndex].audio_file_id = '';
  241. this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
  242. },
  243. /**
  244. * 删除音频 id
  245. * @param {number} i 选项下标
  246. */
  247. deleteAudio(i) {
  248. this.data.file_id_list.splice(this.data.file_id_list.indexOf(this.data.option_list[i].audio_file_id), 1);
  249. this.data.option_list[i].audio_file_id = '';
  250. },
  251. // 删除小题
  252. deleteOption(i, file_id) {
  253. this.$confirm('是否删除?', '提示', {
  254. confirmButtonText: '确定',
  255. cancelButtonText: '取消',
  256. type: 'warning',
  257. })
  258. .then(() => {
  259. this.data.option_list.splice(i, 1);
  260. this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
  261. this.loading_list.splice(i, 1);
  262. })
  263. .catch(() => {});
  264. },
  265. // 自动生成音频
  266. handleMatically(item, i) {
  267. let MethodName = 'tool-TextToVoiceFile';
  268. let data = {};
  269. if (item.character.trim() || item.content_hz) {
  270. data = {
  271. text: item.character.trim() || item.content_hz,
  272. };
  273. } else if (item.content.trim()) {
  274. if (!this.matically_pinyin_obj[item.mark]) {
  275. this.handleItemAnswer(item);
  276. }
  277. MethodName = 'tool-PinyinToVoiceFile';
  278. data = {
  279. pinyin: this.matically_pinyin_obj[item.mark],
  280. };
  281. }
  282. this.loading_list[i].loading = true;
  283. GetStaticResources(MethodName, data)
  284. .then((res) => {
  285. this.loading_list[i].loading = false;
  286. if (res.status === 1) {
  287. item.audio_file_id = res.file_id;
  288. this.data.file_id_list.push(res.file_id);
  289. }
  290. })
  291. .catch(() => {
  292. this.loading_list[i].loading = false;
  293. });
  294. },
  295. handleReplaceTone(value, mark, itemIndex) {
  296. if (!value) return;
  297. value.split(/\s+/).forEach((item) => {
  298. this.handleValue(item);
  299. });
  300. this.matically_pinyin_obj[mark] = this.res_arr
  301. .map((item) =>
  302. item.map(({ number, con }) => (number && con ? addTone(Number(number), con) : number || con || '')),
  303. )
  304. .filter((item) => item.length > 0)
  305. .join(',');
  306. this.matically_pinyin_str[mark] = this.res_arr
  307. .map((item) =>
  308. item.map(({ number, con }) => (number && con ? addTone(Number(number), con) : number || con || '')),
  309. )
  310. .filter((item) => item.length > 0)
  311. .join(' ');
  312. if (this.matically_pinyin_str[mark].indexOf(',') > -1) {
  313. this.$message.warning('输入的拼音有误,请重新输入');
  314. this.matically_pinyin_obj[mark] = [];
  315. this.matically_pinyin_str[mark] = '';
  316. this.data.option_list[itemIndex].content = '';
  317. }
  318. },
  319. handleValue(valItem) {
  320. let numList = [];
  321. if (/[A-Za-zü]+\d/g.test(valItem)) {
  322. valItem.split('').forEach((item, i) => {
  323. if (/\d/.test(item)) {
  324. let numIndex = numList.length === 0 ? 0 : numList[numList.length - 1].index;
  325. let con = valItem.substring(numIndex, i).replace(/\d/g, '');
  326. numList.push({
  327. index: i,
  328. number: item,
  329. con,
  330. isTran: true,
  331. });
  332. }
  333. });
  334. } else {
  335. numList = [];
  336. }
  337. this.res_arr.push(numList.length === 0 ? [{ con: valItem }] : numList);
  338. },
  339. // 答案
  340. handleItemAnswer(item) {
  341. const itemIndex = this.data.answer.answer_list.findIndex((items) => items.mark === item.mark);
  342. let content = item.content.trim();
  343. const regex = /[\u4e00-\u9fa5]/g;
  344. item.content_hz = content.match(regex) ? content.match(regex).join('') : '';
  345. let content_arr = content.replace(regex, '').trim().split(' ');
  346. let select_item = '';
  347. let content_preview = '';
  348. this.res_arr = [];
  349. this.$set(this.matically_pinyin_obj, item.mark, []);
  350. this.$set(this.matically_pinyin_str, item.mark, '');
  351. content_arr.forEach((items, index) => {
  352. let items_trim = items.trim();
  353. if (items_trim) {
  354. let items_yuan = JSON.parse(JSON.stringify(items_trim)).replace(/0|1|2|3|4/, '');
  355. let indexs = items.search(/0|1|2|3|4/);
  356. if (this.data.property.answer_mode === 'select') {
  357. // 如果是选择声调 把声调放在拼音后面
  358. // select_item += `${items_yuan + items_trim.substring(indexs, indexs + 1)} `;
  359. select_item += `${items_trim.substring(indexs, indexs + 1)}${index === content_arr.length - 1 ? '' : ' '}`;
  360. } else if (this.data.property.answer_mode === 'label') {
  361. // 如果是标注声调 把声调放在对应字母后面
  362. // select_item += `${items_trim}${index === content_arr.length - 1 ? '' : ' '}`;
  363. }
  364. content_preview += `${items_yuan}${index === content_arr.length - 1 ? '' : ' '}`;
  365. this.handleReplaceTone(items_yuan + items_trim.substring(indexs, indexs + 1), item.mark, itemIndex);
  366. }
  367. });
  368. if (this.data.property.answer_mode === 'label') {
  369. content_preview.split(' ').forEach((items, index) => {
  370. let items_trim = items.trim();
  371. if (items_trim) {
  372. let indexs = content_arr[index].search(/0|1|2|3|4/);
  373. let items_tone = this.matically_pinyin_str[item.mark].split(' ')[index].split('');
  374. for (let i = 0; i < items_trim.length; i++) {
  375. if (items_trim[i] === items_tone[i]) {
  376. select_item += `${items_trim[i]}`;
  377. } else {
  378. select_item += `${items_trim[i]}${content_arr[index].substring(indexs, indexs + 1)}`;
  379. }
  380. }
  381. select_item += `${index === content_preview.split(' ').length - 1 ? '' : ' '}`;
  382. }
  383. });
  384. }
  385. if (itemIndex === -1) {
  386. let obj = {
  387. mark: item.mark,
  388. value: select_item.split(' '),
  389. };
  390. this.data.answer.answer_list.push(obj);
  391. } else {
  392. this.data.answer.answer_list[itemIndex].value = select_item.split(' ');
  393. }
  394. item.content_view = content_preview.trim().split(' ');
  395. // item.matically_pinyin = matically_pinyin.trim().split(' ').join(',');
  396. },
  397. // 改变类型
  398. handleChangeType() {
  399. this.data.option_list.forEach((item) => {
  400. this.handleItemAnswer(item);
  401. });
  402. },
  403. // 修改拼音
  404. changePinyin(item) {
  405. if (this.data.other.audio_generation_method === 'auto') {
  406. item.audio_file_id = '';
  407. }
  408. },
  409. /**
  410. * 智能识别
  411. * @param {string} text 识别数据
  412. */
  413. recognition(text) {
  414. let arr = this.recognitionCommon(text);
  415. let obj = analysisRecognitionChooseToneData(arr);
  416. this.recognitionCommonSetObj(obj);
  417. this.data.option_list.forEach((item) => {
  418. this.handleItemAnswer(item);
  419. });
  420. },
  421. },
  422. };
  423. </script>
  424. <style lang="scss" scoped>
  425. .content {
  426. display: flex;
  427. flex-direction: column;
  428. .subtitle {
  429. margin: 8px 0;
  430. font-size: 14px;
  431. color: $font-light-color;
  432. }
  433. .content-item {
  434. .upload-wrapper {
  435. width: 200px;
  436. margin-top: 0;
  437. overflow: hidden;
  438. }
  439. :deep .file-name {
  440. width: 205px;
  441. overflow: hidden;
  442. text-overflow: ellipsis;
  443. white-space: nowrap;
  444. }
  445. .auto-matically,
  446. .upload-audio-play {
  447. .audio-wrapper {
  448. // margin-right: 12px;
  449. :deep .audio-play {
  450. width: 16px;
  451. height: 16px;
  452. color: #000;
  453. background-color: initial;
  454. }
  455. :deep .audio-play.not-url {
  456. color: #a1a1a1;
  457. }
  458. :deep .voice-play {
  459. width: 16px;
  460. height: 16px;
  461. }
  462. }
  463. }
  464. .upload-audio-play {
  465. display: flex;
  466. flex-shrink: 0;
  467. align-items: center;
  468. width: 200px;
  469. background-color: $fill-color;
  470. border-radius: 2px;
  471. .upload-play {
  472. display: flex;
  473. column-gap: 8px;
  474. align-items: center;
  475. .svg-icon {
  476. cursor: pointer;
  477. }
  478. }
  479. }
  480. .auto-matically {
  481. display: flex;
  482. flex-shrink: 0;
  483. column-gap: 12px;
  484. align-items: center;
  485. width: 200px;
  486. padding: 5px 12px;
  487. background-color: $fill-color;
  488. border-radius: 2px;
  489. .auto-btn {
  490. font-size: 16px;
  491. font-weight: 400;
  492. line-height: 22px;
  493. color: #1d2129;
  494. cursor: pointer;
  495. }
  496. }
  497. .delete {
  498. flex-shrink: 0;
  499. width: 16px;
  500. height: 16px;
  501. }
  502. }
  503. }
  504. </style>