RepeatQuestion.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <!-- 听说训练 -->
  2. <template>
  3. <QuestionBase>
  4. <template #content>
  5. <div class="stem">
  6. <RichText v-model="data.stem" :font-size="18" placeholder="输入题干" />
  7. <RichText
  8. v-if="isEnable(data.property.is_enable_description)"
  9. v-model="data.description"
  10. placeholder="输入提示"
  11. />
  12. </div>
  13. <div class="content">
  14. <label class="title-little">内容</label>
  15. <ul>
  16. <li v-for="(item, i) in data.option_list" :key="i" class="content-item repeat-option">
  17. <span class="question-number" title="双击切换序号类型" @dblclick="changeOptionType(data)">
  18. {{ computedQuestionNumber(i, data.option_number_show_mode) }}
  19. </span>
  20. <div class="option-content">
  21. <RichText
  22. v-model="item.content"
  23. class="repeat-richtext"
  24. placeholder="拼音间用空格隔开,如: ni3 hao3"
  25. :inline="true"
  26. @handleRichTextBlur="handleRichTextBlur"
  27. />
  28. </div>
  29. <UploadAudio
  30. v-if="data.other.audio_generation_method === 'upload'"
  31. :key="item.audio_file_id || i"
  32. :file-id="item.audio_file_id"
  33. :item-index="i"
  34. :show-upload="!item.audio_file_id"
  35. @upload="uploads"
  36. @deleteFile="deleteFiles"
  37. />
  38. <div v-else-if="data.other.audio_generation_method === 'auto'" class="auto-matically">
  39. <AudioPlay v-if="item.audio_file_id" :file-id="item.audio_file_id" theme-color="gray" />
  40. <span
  41. v-loading="loading_list[i] ? loading_list[i].loading : false"
  42. class="auto-btn"
  43. @click="handleMatically(item, i)"
  44. >{{ item.audio_file_id ? '已生成' : '自动生成' }}</span
  45. >
  46. </div>
  47. <SoundRecord v-else :wav-blob.sync="item.audio_file_id" />
  48. <SvgIcon icon-class="delete" class="delete pointer" @click="deleteOption(i, item.audio_file_id)" />
  49. </li>
  50. </ul>
  51. </div>
  52. <div class="footer">
  53. <span class="add-option" @click="addOption">
  54. <SvgIcon icon-class="add-circle" size="14" /> <span>增加选项</span>
  55. </span>
  56. </div>
  57. </template>
  58. <template #property>
  59. <el-form :model="data.property" label-width="72px" label-position="left">
  60. <el-form-item label="题号">
  61. <el-input v-model="data.property.question_number" />
  62. </el-form-item>
  63. <el-form-item>
  64. <el-radio
  65. v-for="{ value, label } in questionNumberTypeList"
  66. :key="value"
  67. v-model="data.other.question_number_type"
  68. :label="value"
  69. >
  70. {{ label }}
  71. </el-radio>
  72. </el-form-item>
  73. <el-form-item label="题干题号">
  74. <el-select v-model="data.property.stem_question_number_font_size">
  75. <el-option v-for="item in fontSizeList" :key="item" :label="item" :value="item" />
  76. </el-select>
  77. </el-form-item>
  78. <el-form-item label="提示">
  79. <el-radio
  80. v-for="{ value, label } in switchOption"
  81. :key="value"
  82. v-model="data.property.is_enable_description"
  83. :label="value"
  84. >
  85. {{ label }}
  86. </el-radio>
  87. </el-form-item>
  88. <el-form-item label="分值">
  89. <el-radio
  90. v-for="{ value, label } in scoreTypeList"
  91. :key="value"
  92. v-model="data.property.score_type"
  93. :label="value"
  94. >
  95. {{ label }}
  96. </el-radio>
  97. </el-form-item>
  98. <el-form-item>
  99. <el-input-number
  100. v-model="data.property.score"
  101. :min="0"
  102. :step="data.property.score_type === scoreTypeList[0].value ? 1 : 0.1"
  103. />
  104. </el-form-item>
  105. <el-form-item label="音频">
  106. <el-radio
  107. v-for="{ value, label } in audioGenerationMethodList"
  108. :key="value"
  109. v-model="data.other.audio_generation_method"
  110. :label="value"
  111. >
  112. {{ label }}
  113. </el-radio>
  114. </el-form-item>
  115. </el-form>
  116. </template>
  117. </QuestionBase>
  118. </template>
  119. <script>
  120. import UploadAudio from '../common/UploadAudio.vue';
  121. import QuestionMixin from '../common/QuestionMixin.js';
  122. import SoundRecord from '../common/SoundRecord.vue';
  123. import { selectTypeList, changeOptionType, addTone } from '@/views/exercise_questions/data/common';
  124. import { repeatData, getOption, audioGenerationMethodList } from '@/views/exercise_questions/data/repeat';
  125. import { GetStaticResources } from '@/api/app';
  126. export default {
  127. name: 'RepeatQuestion',
  128. components: {
  129. UploadAudio,
  130. SoundRecord,
  131. },
  132. mixins: [QuestionMixin],
  133. data() {
  134. return {
  135. selectTypeList,
  136. audioGenerationMethodList,
  137. changeOptionType,
  138. data: JSON.parse(JSON.stringify(repeatData)),
  139. loading_list: [
  140. {
  141. loading: false,
  142. },
  143. {
  144. loading: false,
  145. },
  146. {
  147. loading: false,
  148. },
  149. ],
  150. matically_pinyin_obj: {},
  151. };
  152. },
  153. watch: {
  154. 'data.option_list.length': {
  155. handler(val) {
  156. this.loading_list = [];
  157. for (let i = 0; i < val; i++) {
  158. let obj = {
  159. loading: false,
  160. };
  161. this.loading_list.push(obj);
  162. }
  163. },
  164. deep: true,
  165. immediate: true,
  166. },
  167. },
  168. methods: {
  169. /**
  170. * 智能识别
  171. * @param {String} text 识别数据
  172. */
  173. recognition(text) {
  174. let arr = this.recognitionCommon(text);
  175. this.data.option_list = arr.map((content) => getOption(content));
  176. },
  177. addOption() {
  178. this.data.option_list.push(getOption());
  179. },
  180. uploads(file_id, index) {
  181. this.data.option_list[index].audio_file_id = file_id;
  182. this.data.file_id_list.push(file_id);
  183. },
  184. deleteFiles(file_id, itemIndex) {
  185. this.data.option_list[itemIndex].audio_file_id = '';
  186. this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
  187. },
  188. // 删除小题
  189. deleteOption(i, file_id) {
  190. this.$confirm('是否删除?', '提示', {
  191. confirmButtonText: '确定',
  192. cancelButtonText: '取消',
  193. type: 'warning',
  194. })
  195. .then(() => {
  196. this.data.option_list.splice(i, 1);
  197. this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
  198. this.loading_list.splice(i, 1);
  199. })
  200. .catch(() => {});
  201. },
  202. handleReplaceTone(value, mark) {
  203. if (!value) return;
  204. value.split(/\s+/).forEach((item) => {
  205. this.handleValue(item);
  206. });
  207. this.matically_pinyin_obj[mark] = this.res_arr
  208. .map((item) =>
  209. item.map(({ number, con }) => (number && con ? addTone(Number(number), con) : number || con || '')),
  210. )
  211. .filter((item) => item.length > 0)
  212. .join(' ');
  213. },
  214. handleValue(valItem) {
  215. let numList = [];
  216. if (/[A-Za-zü]+\d/g.test(valItem)) {
  217. valItem.split('').forEach((item, i) => {
  218. if (/\d/.test(item)) {
  219. let con = valItem.replace(/\d/g, '');
  220. numList.push({
  221. index: i,
  222. number: item,
  223. con,
  224. isTran: true,
  225. });
  226. }
  227. });
  228. } else {
  229. numList = [];
  230. }
  231. this.res_arr.push(numList.length === 0 ? [{ con: valItem }] : numList);
  232. },
  233. handleItemPinyin(content, mark) {
  234. let content_arr = content.trim().split(' ');
  235. this.res_arr = [];
  236. this.$set(this.matically_pinyin_obj, mark, []);
  237. content_arr.forEach((items, index) => {
  238. let items_trim = items.trim();
  239. if (items_trim) {
  240. this.handleReplaceTone(items_trim, mark);
  241. }
  242. });
  243. },
  244. // 转成带声调的拼音
  245. handleRichTextBlur() {
  246. let rich_rext_arr = document.getElementsByClassName('repeat-richtext');
  247. if (rich_rext_arr) {
  248. for (let i = 0; i < rich_rext_arr.length; i++) {
  249. let content = rich_rext_arr[i].innerText;
  250. let index = content.search(/0|1|2|3|4/);
  251. if (index > -1) {
  252. this.handleItemPinyin(content, this.data.option_list[i].mark);
  253. setTimeout(() => {
  254. document.getElementsByClassName('repeat-richtext')[i].innerText =
  255. this.matically_pinyin_obj[this.data.option_list[i].mark];
  256. this.data.option_list[i].content = this.matically_pinyin_obj[this.data.option_list[i].mark];
  257. }, 100);
  258. }
  259. }
  260. }
  261. },
  262. // 自动生成音频
  263. handleMatically(item, i) {
  264. if (
  265. document.getElementsByClassName('repeat-richtext') &&
  266. document.getElementsByClassName('repeat-richtext')[i] &&
  267. document.getElementsByClassName('repeat-richtext')[i].innerText
  268. ) {
  269. this.loading_list[i].loading = true;
  270. let MethodName = 'tool-PinyinToVoiceFile';
  271. let data = {
  272. pinyin: document.getElementsByClassName('repeat-richtext')[i].innerText.trim().split(' ').join(','),
  273. };
  274. GetStaticResources(MethodName, data)
  275. .then((res) => {
  276. this.loading_list[i].loading = false;
  277. if (res.status === 1) {
  278. item.audio_file_id = res.file_id;
  279. this.data.file_id_list.push(res.file_id);
  280. }
  281. })
  282. .catch(() => {
  283. this.loading_list[i].loading = false;
  284. });
  285. }
  286. },
  287. },
  288. };
  289. </script>
  290. <style lang="scss" scoped>
  291. .repeat-option {
  292. :deep .upload-wrapper {
  293. margin-top: 0;
  294. }
  295. :deep .file-name {
  296. width: 205px;
  297. overflow: hidden;
  298. text-overflow: ellipsis;
  299. white-space: nowrap;
  300. }
  301. .auto-matically {
  302. display: flex;
  303. flex-shrink: 0;
  304. align-items: center;
  305. width: 233px;
  306. padding: 5px 12px;
  307. background-color: $fill-color;
  308. border-radius: 2px;
  309. .audio-wrapper {
  310. margin-right: 12px;
  311. :deep .audio-play {
  312. width: 16px;
  313. height: 16px;
  314. color: #000;
  315. background-color: initial;
  316. }
  317. :deep .audio-play.not-url {
  318. color: #a1a1a1;
  319. }
  320. :deep .voice-play {
  321. width: 16px;
  322. height: 16px;
  323. }
  324. }
  325. .auto-btn {
  326. font-size: 14px;
  327. font-weight: 400;
  328. line-height: 22px;
  329. color: #1d2129;
  330. cursor: pointer;
  331. }
  332. }
  333. .delete {
  334. flex-shrink: 0;
  335. }
  336. }
  337. </style>