edit.vue 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <div style="padding: 20px">
  3. <h2 style="text-align: center">
  4. 游戏题目{{ questionId ? "编辑" : "添加" }}
  5. </h2>
  6. <el-form ref="form" :rules="rules" :model="form" label-width="90px">
  7. <el-form-item label="题目:" prop="title">
  8. <el-input v-model="form.title" placeholder="请输入实例名(必填)">
  9. <el-select slot="prepend" v-model="form.optType" style="width:100px" placeholder="请选择">
  10. <el-option label="单选题" value="1" />
  11. <el-option label="多选题" value="2" />
  12. <el-option label="判断题" value="3" />
  13. </el-select>
  14. </el-input>
  15. </el-form-item>
  16. <el-form-item v-if="questionId!==undefined && resultMode === 'examination'" label="正确选项:">
  17. <!-- filterable可搜索 multiple可多选 default-first-option回车可选-->
  18. <el-select
  19. v-model="form.rightAnswer"
  20. style="width:100%"
  21. multiple
  22. filterable
  23. default-first-option
  24. placeholder="请选择"
  25. >
  26. <el-option
  27. v-for="item in rightList"
  28. :key="item"
  29. :label="item"
  30. :value="item"
  31. />
  32. </el-select>
  33. </el-form-item>
  34. <el-form-item>
  35. <el-button type="primary" @click="onSubmit('form')">确定</el-button>
  36. <el-button @click="$router.go(-1)">返回</el-button>
  37. </el-form-item>
  38. </el-form>
  39. <el-card v-if="questionId!==undefined" class="box-card">
  40. <div slot="header" class="clearfix">
  41. <h3 style="float:left">答案列表</h3>
  42. <el-button
  43. type="primary"
  44. style="float: right"
  45. icon="el-icon-plus"
  46. @click="onAddAnswer"
  47. >添加特征</el-button>
  48. </div>
  49. <ul style="list-style-type: none;margin:10px 0 10px -40px">
  50. <li v-for="answer in answerList" :key="answer.answerId" class="answerli">
  51. <div style="flex:1;width: 100%;overflow: hidden;display: flex;" @click="handleEdit(answer)">
  52. <span style="width:45px">{{ answer.option }}</span>
  53. <span style="flex:1">{{ answer.content }}</span>
  54. </div>
  55. <el-popconfirm
  56. style="width:30px"
  57. icon="el-icon-info"
  58. icon-color="red"
  59. title="确定删除这个答案吗?"
  60. @onConfirm="handleDelete(answer)"
  61. >
  62. <el-button slot="reference" type="text" class="deleteQuestion" style="color:red">删除</el-button>
  63. </el-popconfirm>
  64. </li>
  65. </ul>
  66. </el-card>
  67. <QuestionDrawer
  68. :dialog="dialog"
  69. :question-id="questionId"
  70. :answer-id="answerId"
  71. :game-id="gameId"
  72. :result-mode="resultMode"
  73. @handleCloseDrawer="handleCloseDrawer"
  74. @handleEditDrawer="handleEditDrawer"
  75. />
  76. </div>
  77. </template>
  78. <script>
  79. import { saveQuestion, UpdateQuestion, getQuestionDetail } from '@/api/question'
  80. import { getAnswerList, deleteAnswer } from '@/api/answer'
  81. import QuestionDrawer from '@/components/Question/drawer.vue'
  82. export default {
  83. components: {
  84. QuestionDrawer
  85. },
  86. props: {
  87. gameId: String,
  88. questionId: String,
  89. resultMode: String
  90. },
  91. data() {
  92. return {
  93. rules: {
  94. title: [{ required: true, message: '请输入游戏名称', trigger: 'blur' }]
  95. },
  96. form: {
  97. title: undefined,
  98. optType: undefined,
  99. rightAnswer: undefined,
  100. gameId: undefined,
  101. questionId: undefined
  102. },
  103. // 答案列表(包括选项 内容 特征词列表)
  104. answerList: [],
  105. answerId: undefined,
  106. // 正确答案数据源
  107. rightList: [],
  108. dialog: false
  109. }
  110. },
  111. watch: {
  112. // 用于点击左边改变右边的页面
  113. questionId: function() {
  114. if (this.questionId) {
  115. this.rightList = []
  116. getQuestionDetail(this.questionId).then((res) => {
  117. this.form = res.data
  118. this.form.rightAnswer = res.data.rightAnswer?.split(',')
  119. })
  120. this.handleAnswerList()
  121. } else {
  122. this.form = {
  123. title: undefined,
  124. optType: undefined,
  125. gameId: undefined,
  126. rightAnswer: undefined,
  127. questionId: undefined
  128. }
  129. this.answerList = []
  130. this.rightList = []
  131. }
  132. }
  133. },
  134. methods: {
  135. // 添加答案
  136. onAddAnswer() {
  137. this.dialog = true
  138. this.answerId = undefined
  139. },
  140. // 编辑答案
  141. handleEdit(val) {
  142. this.dialog = true
  143. this.answerId = val.answerId
  144. },
  145. // 删除答案
  146. handleDelete(val) {
  147. deleteAnswer(val.answerId).then((res) => {
  148. this.$message('删除答案成功')
  149. this.handleAnswerList()
  150. })
  151. },
  152. // 关闭答案抽屉
  153. handleCloseDrawer() {
  154. this.dialog = false
  155. this.answerId = undefined
  156. },
  157. // 编辑答案抽屉成功重新查询列表
  158. handleEditDrawer(val) {
  159. this.handleCloseDrawer()
  160. this.handleAnswerList()
  161. },
  162. // 查询当前问题答案列表
  163. handleAnswerList() {
  164. getAnswerList({ questionId: this.questionId }).then((res) => {
  165. this.answerList = res.data.records
  166. // 给正确答按下拉菜单数据源赋值
  167. const list = []
  168. this.answerList.map(item => {
  169. list.push(item.option)
  170. })
  171. this.rightList = list
  172. })
  173. },
  174. // 转换特征数组数据格式
  175. handleToString(list) {
  176. const nameList = []
  177. list?.map(item => {
  178. nameList.push(item.name)
  179. })
  180. return nameList.toString()
  181. },
  182. onSubmit(form) {
  183. this.$refs[form].validate((valid) => {
  184. if (valid) {
  185. if (this.form.optType) {
  186. const data = { ...this.form }
  187. data.gameId = this.gameId
  188. if (this.questionId) {
  189. if (data.rightAnswer) {
  190. data.rightAnswer = data.rightAnswer.toString()
  191. }
  192. UpdateQuestion(data, this.questionId).then((res) => {
  193. this.$message('修改问题成功')
  194. // 告诉父页面实例表需要刷新并且关闭当前组件
  195. this.$emit('handleRefreshQuestion', true)
  196. })
  197. } else {
  198. saveQuestion(data).then((res) => {
  199. this.$message('添加问题成功')
  200. // 告诉父页面实例表需要刷新并且关闭当前组件
  201. this.$emit('handleRefreshQuestion', true)
  202. this.$emit('handleEditQuestion', res.data.questionId)
  203. })
  204. }
  205. } else {
  206. this.$message('请选择题型')
  207. }
  208. } else {
  209. return false
  210. }
  211. })
  212. }
  213. }
  214. }
  215. </script>
  216. <style scoped lang="scss">
  217. .answerli {
  218. width: 100%;
  219. overflow: hidden;
  220. display: flex;
  221. line-height:40px;
  222. background-color: white;
  223. border: 1px solid #f0f2f5;
  224. }
  225. .answerli:hover {
  226. background-color: rgb(230, 247, 255);
  227. }
  228. .answerli:nth-of-type(even) {
  229. background: #f0f2f5;
  230. }
  231. </style>