ChooseToneQuestion.vue 11 KB

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