ChineseQuestion.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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="title-little">题目:</label>
  14. <ul>
  15. <li
  16. v-for="(item, i) in data.option_list"
  17. :key="i"
  18. v-loading="loading_list[i] ? loading_list[i].loadings : false"
  19. class="content-item"
  20. >
  21. <el-input v-model="item.content" :placeholder="'输入汉字或词汇'" @blur="handleChineseStrokes(item, i)" />
  22. <el-input
  23. v-model="item.pinyin"
  24. :placeholder="'拼音间用空格隔开'"
  25. @blur="handleSplitPy(item)"
  26. @change="changePinyin(item)"
  27. />
  28. <UploadAudio
  29. v-if="data.other.audio_generation_method === 'upload'"
  30. :key="item.audio_file_id || i"
  31. :file-id="item.audio_file_id"
  32. :item-index="i"
  33. :show-upload="!item.audio_file_id"
  34. @upload="uploads"
  35. @deleteFile="deleteFiles"
  36. />
  37. <div v-else-if="data.other.audio_generation_method === 'auto'" class="auto-matically">
  38. <AudioPlay v-if="item.audio_file_id" :file-id="item.audio_file_id" theme-color="gray" />
  39. <span
  40. v-loading="loading_list[i] ? loading_list[i].loading : false"
  41. class="auto-btn"
  42. @click="handleMatically(item, i)"
  43. >{{ item.audio_file_id ? '已生成' : '自动生成' }}</span
  44. >
  45. </div>
  46. <SoundRecord v-else :wav-blob.sync="item.audio_file_id" />
  47. <el-input v-model="item.definition" placeholder="输入释义" />
  48. <el-input v-model="item.collocation" placeholder="输入搭配" />
  49. <SvgIcon icon-class="delete" class="delete pointer" @click="deleteOption(i, item.audio_file_id)" />
  50. </li>
  51. </ul>
  52. </div>
  53. <div class="footer">
  54. <span class="add-option" @click="addOption">
  55. <SvgIcon icon-class="add-circle" size="14" /> <span>增加汉字</span>
  56. </span>
  57. </div>
  58. </template>
  59. <template #property>
  60. <el-form :model="data.property" label-width="72px" label-position="left">
  61. <el-form-item label="题号">
  62. <el-input v-model="data.property.question_number" />
  63. </el-form-item>
  64. <el-form-item>
  65. <el-radio
  66. v-for="{ value, label } in questionNumberTypeList"
  67. :key="value"
  68. v-model="data.other.question_number_type"
  69. :label="value"
  70. >
  71. {{ label }}
  72. </el-radio>
  73. </el-form-item>
  74. <el-form-item label="题干题号">
  75. <el-select v-model="data.property.stem_question_number_font_size">
  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>
  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 learnTypeList"
  109. :key="value"
  110. v-model="data.property.learn_type"
  111. :label="value"
  112. >
  113. {{ label }}
  114. </el-radio>
  115. </el-form-item>
  116. <el-form-item label="田字格组">
  117. <el-input
  118. v-model="data.property.tian_number"
  119. type="number"
  120. @blur="data.property.tian_number = handleInputNumber(data.property.tian_number)"
  121. />
  122. </el-form-item>
  123. <el-form-item label="音频">
  124. <el-radio
  125. v-for="{ value, label } in audioGenerationMethodList"
  126. :key="value"
  127. v-model="data.other.audio_generation_method"
  128. :label="value"
  129. >
  130. {{ label }}
  131. </el-radio>
  132. </el-form-item>
  133. </el-form>
  134. </template>
  135. </QuestionBase>
  136. </template>
  137. <script>
  138. import QuestionMixin from '../common/QuestionMixin.js';
  139. import UploadAudio from '../common/UploadAudio.vue';
  140. import SoundRecord from '../common/SoundRecord.vue';
  141. import { GetStaticResources } from '@/api/app';
  142. import { changeOptionType, handleInputNumber, addTone } from '@/views/exercise_questions/data/common';
  143. import { getRandomNumber } from '@/utils/index';
  144. import {
  145. chineseData,
  146. learnTypeList,
  147. audioGenerationMethodList,
  148. getOption,
  149. } from '@/views/exercise_questions/data/chinese';
  150. export default {
  151. name: 'ChineseQuestion',
  152. components: {
  153. UploadAudio,
  154. SoundRecord,
  155. },
  156. mixins: [QuestionMixin],
  157. data() {
  158. return {
  159. learnTypeList,
  160. audioGenerationMethodList,
  161. data: JSON.parse(JSON.stringify(chineseData)),
  162. changeOptionType,
  163. handleInputNumber,
  164. loading_list: [
  165. {
  166. loading: false,
  167. loadings: false,
  168. },
  169. {
  170. loading: false,
  171. loadings: false,
  172. },
  173. {
  174. loading: false,
  175. loadings: false,
  176. },
  177. ],
  178. matically_pinyin_obj: {}, // 存放转成声调的拼音
  179. };
  180. },
  181. watch: {
  182. 'data.option_list.length': {
  183. handler(val) {
  184. this.loading_list = [];
  185. for (let i = 0; i < val; i++) {
  186. let obj = {
  187. loading: false,
  188. loadings: false,
  189. };
  190. this.loading_list.push(obj);
  191. }
  192. },
  193. deep: true,
  194. immediate: true,
  195. },
  196. },
  197. mounted() {},
  198. methods: {
  199. addOption() {
  200. this.data.option_list.push(getOption());
  201. this.loading_list.push({
  202. loading: false,
  203. loadings: false,
  204. });
  205. },
  206. uploads(file_id, index) {
  207. this.data.option_list[index].audio_file_id = file_id;
  208. this.data.file_id_list.push(file_id);
  209. },
  210. deleteFiles(file_id, itemIndex) {
  211. this.data.option_list[itemIndex].audio_file_id = '';
  212. this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
  213. },
  214. // 删除小题
  215. deleteOption(i, file_id) {
  216. this.$confirm('是否删除?', '提示', {
  217. confirmButtonText: '确定',
  218. cancelButtonText: '取消',
  219. type: 'warning',
  220. })
  221. .then(() => {
  222. this.data.option_list.splice(i, 1);
  223. this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
  224. this.loading_list.splice(i, 1);
  225. })
  226. .catch(() => {});
  227. },
  228. // 自动生成音频
  229. handleMatically(item, i) {
  230. if (item.pinyin.trim()) {
  231. this.loading_list[i].loading = true;
  232. let MethodName = 'tool-PinyinToVoiceFile';
  233. let data = {
  234. pinyin: item.pinyin.trim().split(' ').join(','),
  235. };
  236. GetStaticResources(MethodName, data)
  237. .then((res) => {
  238. this.loading_list[i].loading = false;
  239. if (res.status === 1) {
  240. item.audio_file_id = res.file_id;
  241. this.data.file_id_list.push(res.file_id);
  242. }
  243. })
  244. .catch(() => {
  245. this.loading_list[i].loading = false;
  246. });
  247. }
  248. },
  249. // 生成汉字
  250. handleChineseStrokes(item, i) {
  251. if (item.content.trim()) {
  252. this.loading_list[i].loadings = true;
  253. let content_arr = item.content.trim().split('');
  254. let content_arrs = [];
  255. let content_arr_strokes = [];
  256. content_arr.forEach((itemc) => {
  257. if (itemc.trim()) {
  258. content_arrs.push(itemc.trim());
  259. }
  260. });
  261. content_arrs.forEach((itemc, indexc) => {
  262. content_arr_strokes.push(null);
  263. let MethodName = 'hz_resource_manager-GetHZStrokesContent';
  264. let data = {
  265. hz: itemc,
  266. };
  267. GetStaticResources(MethodName, data).then((res) => {
  268. let obj = {
  269. hz: itemc.trim(),
  270. strokes: res,
  271. };
  272. content_arr_strokes[indexc] = obj;
  273. });
  274. });
  275. this.loading_list[i].loadings = false;
  276. item.hz_strokes_list = content_arr_strokes;
  277. }
  278. },
  279. // 切割拼音
  280. handleSplitPy(item) {
  281. let index = item.pinyin.search(/0|1|2|3|4/);
  282. if (index > -1) {
  283. this.handleItemPinyin(item.pinyin, item.mark);
  284. setTimeout(() => {
  285. item.pinyin = this.matically_pinyin_obj[item.mark];
  286. }, 100);
  287. }
  288. let pinyin_list = item.pinyin.trim().split(' ');
  289. item.pinyin_item_list = [];
  290. pinyin_list.forEach((itemp) => {
  291. let obj = {
  292. pinyin_item: itemp,
  293. };
  294. item.pinyin_item_list.push(obj);
  295. });
  296. },
  297. handleReplaceTone(value, mark) {
  298. if (!value) return;
  299. value.split(/\s+/).forEach((item) => {
  300. this.handleValue(item);
  301. });
  302. this.matically_pinyin_obj[mark] = this.res_arr
  303. .map((item) =>
  304. item.map(({ number, con }) => (number && con ? addTone(Number(number), con) : number || con || '')),
  305. )
  306. .filter((item) => item.length > 0)
  307. .join(' ');
  308. },
  309. handleValue(valItem) {
  310. let numList = [];
  311. if (/[A-Za-zü]+\d/g.test(valItem)) {
  312. valItem.split('').forEach((item, i) => {
  313. if (/\d/.test(item)) {
  314. let con = valItem.replace(/\d/g, '');
  315. numList.push({
  316. index: i,
  317. number: item,
  318. con,
  319. isTran: true,
  320. });
  321. }
  322. });
  323. } else {
  324. numList = [];
  325. }
  326. this.res_arr.push(numList.length === 0 ? [{ con: valItem }] : numList);
  327. },
  328. handleItemPinyin(content, mark) {
  329. let content_arr = content.trim().split(' ');
  330. this.res_arr = [];
  331. this.$set(this.matically_pinyin_obj, mark, []);
  332. content_arr.forEach((items, index) => {
  333. let items_trim = items.trim();
  334. if (items_trim) {
  335. this.handleReplaceTone(items_trim, mark);
  336. }
  337. });
  338. },
  339. // 修改拼音
  340. changePinyin(item) {
  341. if (this.data.other.audio_generation_method === 'auto') {
  342. item.audio_file_id = '';
  343. }
  344. },
  345. /**
  346. * 智能识别
  347. * @param {String} text 识别数据
  348. */
  349. recognition(text) {
  350. let arr = text
  351. .split(/[\r\n]/)
  352. .map((item) => item.trim())
  353. .filter((item) => item);
  354. if (arr.length > 0) {
  355. this.data.stem = arr[0];
  356. this.data.option_list = [];
  357. arr.slice(1).map((content, index) => {
  358. let content_item = content.split('&&');
  359. this.data.option_list.push({
  360. content: content_item[0] ? content_item[0] : '',
  361. mark: getRandomNumber(),
  362. audio_file_id: '',
  363. pinyin: content_item[1] ? content_item[1] : '',
  364. pinyin_item_list: [],
  365. definition: content_item[2] ? content_item[2] : '',
  366. collocation: content_item[3] ? content_item[3] : '',
  367. hz_strokes_list: [],
  368. });
  369. this.handleSplitPy(this.data.option_list[index]);
  370. this.handleChineseStrokes(this.data.option_list[index], index);
  371. });
  372. }
  373. },
  374. },
  375. };
  376. </script>
  377. <style lang="scss" scoped>
  378. .content {
  379. display: flex;
  380. flex-direction: column;
  381. .content-item {
  382. .upload-wrapper {
  383. margin-top: 0;
  384. }
  385. :deep .file-name {
  386. width: 205px;
  387. overflow: hidden;
  388. text-overflow: ellipsis;
  389. white-space: nowrap;
  390. }
  391. .auto-matically {
  392. display: flex;
  393. flex-shrink: 0;
  394. align-items: center;
  395. width: 233px;
  396. padding: 5px 12px;
  397. background-color: $fill-color;
  398. border-radius: 2px;
  399. .audio-wrapper {
  400. margin-right: 12px;
  401. :deep .audio-play {
  402. width: 16px;
  403. height: 16px;
  404. color: #000;
  405. background-color: initial;
  406. }
  407. :deep .audio-play.not-url {
  408. color: #a1a1a1;
  409. }
  410. :deep .voice-play {
  411. width: 16px;
  412. height: 16px;
  413. }
  414. }
  415. .auto-btn {
  416. font-size: 14px;
  417. font-weight: 400;
  418. line-height: 22px;
  419. color: #1d2129;
  420. cursor: pointer;
  421. }
  422. }
  423. .delete {
  424. flex-shrink: 0;
  425. width: 16px;
  426. height: 16px;
  427. }
  428. }
  429. }
  430. </style>