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