ChooseToneQuestion.vue 14 KB

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