ChooseToneQuestion.vue 13 KB

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