ChooseToneQuestion.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. } from '@/views/exercise_questions/data/chooseTone';
  160. import { GetStaticResources } from '@/api/app';
  161. export default {
  162. name: 'ChooseToneQuestion',
  163. components: {
  164. UploadAudio,
  165. SoundRecord,
  166. },
  167. mixins: [QuestionMixin],
  168. data() {
  169. return {
  170. toneList,
  171. changeOptionType,
  172. audioGenerationMethodList,
  173. toneTypeList,
  174. data: JSON.parse(JSON.stringify(ChooseToneData)),
  175. matically_pinyin_obj: {}, // 存放转成声调的拼音
  176. matically_pinyin_str: {}, // 存放转成声调的字符串
  177. res_arr: [],
  178. loading_list: [
  179. {
  180. loading: false,
  181. },
  182. {
  183. loading: false,
  184. },
  185. {
  186. loading: false,
  187. },
  188. ],
  189. };
  190. },
  191. watch: {
  192. 'data.option_list.length': {
  193. handler(val) {
  194. this.loading_list = [];
  195. for (let i = 0; i < val; i++) {
  196. let obj = {
  197. loading: false,
  198. };
  199. this.loading_list.push(obj);
  200. }
  201. this.handleChangeType();
  202. },
  203. deep: true,
  204. immediate: true,
  205. },
  206. },
  207. created() {},
  208. methods: {
  209. addOption() {
  210. this.data.option_list.push(getOption());
  211. },
  212. uploads(file_id, index) {
  213. this.data.option_list[index].audio_file_id = file_id;
  214. this.data.file_id_list.push(file_id);
  215. },
  216. deleteFiles(file_id, itemIndex) {
  217. this.data.option_list[itemIndex].audio_file_id = '';
  218. this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
  219. },
  220. /**
  221. * 删除音频 id
  222. * @param {number} i 选项下标
  223. */
  224. deleteAudio(i) {
  225. this.data.file_id_list.splice(this.data.file_id_list.indexOf(this.data.option_list[i].audio_file_id), 1);
  226. this.data.option_list[i].audio_file_id = '';
  227. },
  228. // 删除小题
  229. deleteOption(i, file_id) {
  230. this.$confirm('是否删除?', '提示', {
  231. confirmButtonText: '确定',
  232. cancelButtonText: '取消',
  233. type: 'warning',
  234. })
  235. .then(() => {
  236. this.data.option_list.splice(i, 1);
  237. this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
  238. this.loading_list.splice(i, 1);
  239. })
  240. .catch(() => {});
  241. },
  242. // 自动生成音频
  243. handleMatically(item, i) {
  244. if (item.content.trim()) {
  245. this.loading_list[i].loading = true;
  246. if (!this.matically_pinyin_obj[item.mark]) {
  247. this.handleItemAnswer(item);
  248. }
  249. let MethodName = 'tool-PinyinToVoiceFile';
  250. let data = {
  251. pinyin: this.matically_pinyin_obj[item.mark],
  252. };
  253. GetStaticResources(MethodName, data)
  254. .then((res) => {
  255. this.loading_list[i].loading = false;
  256. if (res.status === 1) {
  257. item.audio_file_id = res.file_id;
  258. this.data.file_id_list.push(res.file_id);
  259. }
  260. })
  261. .catch(() => {
  262. this.loading_list[i].loading = false;
  263. });
  264. }
  265. },
  266. handleReplaceTone(value, mark) {
  267. if (!value) return;
  268. value.split(/\s+/).forEach((item) => {
  269. this.handleValue(item);
  270. });
  271. this.matically_pinyin_obj[mark] = this.res_arr
  272. .map((item) =>
  273. item.map(({ number, con }) => (number && con ? addTone(Number(number), con) : number || con || '')),
  274. )
  275. .filter((item) => item.length > 0)
  276. .join(',');
  277. this.matically_pinyin_str[mark] = this.res_arr
  278. .map((item) =>
  279. item.map(({ number, con }) => (number && con ? addTone(Number(number), con) : number || con || '')),
  280. )
  281. .filter((item) => item.length > 0)
  282. .join(' ');
  283. },
  284. handleValue(valItem) {
  285. let numList = [];
  286. if (/[A-Za-zü]+\d/g.test(valItem)) {
  287. valItem.split('').forEach((item, i) => {
  288. if (/\d/.test(item)) {
  289. let numIndex = numList.length === 0 ? 0 : numList[numList.length - 1].index;
  290. let con = valItem.substring(numIndex, i).replace(/\d/g, '');
  291. numList.push({
  292. index: i,
  293. number: item,
  294. con,
  295. isTran: true,
  296. });
  297. }
  298. });
  299. } else {
  300. numList = [];
  301. }
  302. this.res_arr.push(numList.length === 0 ? [{ con: valItem }] : numList);
  303. },
  304. // 答案
  305. handleItemAnswer(item) {
  306. const index = this.data.answer.answer_list.findIndex((items) => items.mark === item.mark);
  307. let content = item.content.trim();
  308. const regex = /[\u4e00-\u9fa5]/g;
  309. item.content_hz = content.match(regex) ? content.match(regex).join('') : '';
  310. let content_arr = content.replace(regex, '').trim().split(' ');
  311. let select_item = '';
  312. let content_preview = '';
  313. this.res_arr = [];
  314. this.$set(this.matically_pinyin_obj, item.mark, []);
  315. this.$set(this.matically_pinyin_str, item.mark, '');
  316. content_arr.forEach((items, index) => {
  317. let items_trim = items.trim();
  318. if (items_trim) {
  319. let items_yuan = JSON.parse(JSON.stringify(items_trim)).replace(/0|1|2|3|4/, '');
  320. let indexs = items.search(/0|1|2|3|4/);
  321. if (this.data.property.answer_mode === 'select') {
  322. // 如果是选择声调 把声调放在拼音后面
  323. // select_item += `${items_yuan + items_trim.substring(indexs, indexs + 1)} `;
  324. select_item += `${items_trim.substring(indexs, indexs + 1)}${index === content_arr.length - 1 ? '' : ' '}`;
  325. } else if (this.data.property.answer_mode === 'label') {
  326. // 如果是标注声调 把声调放在对应字母后面
  327. select_item += `${items_trim}${index === content_arr.length - 1 ? '' : ' '}`;
  328. }
  329. content_preview += `${items_yuan} `;
  330. this.handleReplaceTone(items_yuan + items_trim.substring(indexs, indexs + 1), item.mark);
  331. }
  332. });
  333. if (index === -1) {
  334. let obj = {
  335. mark: item.mark,
  336. value: select_item.split(' '),
  337. };
  338. this.data.answer.answer_list.push(obj);
  339. } else {
  340. this.data.answer.answer_list[index].value = select_item.split(' ');
  341. }
  342. item.content_view = content_preview.trim().split(' ');
  343. // item.matically_pinyin = matically_pinyin.trim().split(' ').join(',');
  344. },
  345. // 改变类型
  346. handleChangeType() {
  347. this.data.option_list.forEach((item) => {
  348. this.handleItemAnswer(item);
  349. });
  350. },
  351. // 修改拼音
  352. changePinyin(item) {
  353. if (this.data.other.audio_generation_method === 'auto') {
  354. item.audio_file_id = '';
  355. }
  356. },
  357. /**
  358. * 智能识别
  359. * @param {String} text 识别数据
  360. */
  361. recognition(text) {
  362. let arr = this.recognitionCommon(text);
  363. this.data.option_list = arr.map((content) => getOption(content));
  364. this.data.option_list.forEach((item) => {
  365. this.handleItemAnswer(item);
  366. });
  367. },
  368. },
  369. };
  370. </script>
  371. <style lang="scss" scoped>
  372. .content {
  373. display: flex;
  374. flex-direction: column;
  375. .subtitle {
  376. margin: 8px 0;
  377. font-size: 14px;
  378. color: #4e5969;
  379. }
  380. .content-item {
  381. .upload-wrapper {
  382. width: 200px;
  383. margin-top: 0;
  384. overflow: hidden;
  385. }
  386. :deep .file-name {
  387. width: 205px;
  388. overflow: hidden;
  389. text-overflow: ellipsis;
  390. white-space: nowrap;
  391. }
  392. .auto-matically,
  393. .upload-audio-play {
  394. .audio-wrapper {
  395. // margin-right: 12px;
  396. :deep .audio-play {
  397. width: 16px;
  398. height: 16px;
  399. color: #000;
  400. background-color: initial;
  401. }
  402. :deep .audio-play.not-url {
  403. color: #a1a1a1;
  404. }
  405. :deep .voice-play {
  406. width: 16px;
  407. height: 16px;
  408. }
  409. }
  410. }
  411. .upload-audio-play {
  412. display: flex;
  413. flex-shrink: 0;
  414. align-items: center;
  415. width: 200px;
  416. background-color: $fill-color;
  417. border-radius: 2px;
  418. .upload-play {
  419. display: flex;
  420. column-gap: 8px;
  421. align-items: center;
  422. .svg-icon {
  423. cursor: pointer;
  424. }
  425. }
  426. }
  427. .auto-matically {
  428. display: flex;
  429. flex-shrink: 0;
  430. column-gap: 12px;
  431. align-items: center;
  432. width: 200px;
  433. padding: 5px 12px;
  434. background-color: $fill-color;
  435. border-radius: 2px;
  436. .auto-btn {
  437. font-size: 16px;
  438. font-weight: 400;
  439. line-height: 22px;
  440. color: #1d2129;
  441. cursor: pointer;
  442. }
  443. }
  444. .delete {
  445. flex-shrink: 0;
  446. width: 16px;
  447. height: 16px;
  448. }
  449. }
  450. }
  451. </style>