ChooseToneQuestion.vue 11 KB

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