ChooseToneQuestion.vue 12 KB

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