index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <div class="app-container">
  3. <div class="filter-container">
  4. <el-input
  5. v-model="listQuery.query"
  6. placeholder="请输入检索词"
  7. style="width: 60%;"
  8. class="filter-item"
  9. @keyup.enter.native="fetchData()"
  10. />
  11. <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="fetchData">
  12. 检索
  13. </el-button>
  14. <el-button
  15. class="filter-item"
  16. style="margin: 0 10px 20px 0; float: right;"
  17. type="success"
  18. icon="el-icon-circle-plus-outline"
  19. @click="handleCreateOrUpdate('create')">
  20. 新建
  21. </el-button>
  22. </div>
  23. <el-table
  24. :key="tableKey"
  25. v-loading="listLoading"
  26. :data="list"
  27. border
  28. fit
  29. highlight-current-row
  30. ref="table"
  31. >
  32. <el-table-column type="index" label="序号" align="center" width="60" />
  33. <el-table-column label="名称" align="center" width="150">
  34. <template slot-scope="{row}">
  35. <span>{{ row.name }}</span>
  36. </template>
  37. </el-table-column>
  38. <el-table-column label="描述" align="center" width="300">
  39. <template slot-scope="{row}">
  40. <span>{{ row.description }}</span>
  41. </template>
  42. </el-table-column>
  43. <el-table-column label="创建人" align="center" width="150">
  44. <template slot-scope="{row}">
  45. <span>{{ row.userName }}</span>
  46. </template>
  47. </el-table-column>
  48. <el-table-column label="创建时间" width="180px" align="center">
  49. <template slot-scope="{row}">
  50. <span>{{ row.createTime }}</span>
  51. </template>
  52. </el-table-column>
  53. <el-table-column label="更新时间" width="180px" align="center">
  54. <template slot-scope="{row}">
  55. <span>{{ row.updateTime }}</span>
  56. </template>
  57. </el-table-column>
  58. <el-table-column label="操作" align="center">
  59. <template slot-scope="{row,$index}">
  60. <el-button type="primary" size="mini" @click="handleCreateOrUpdate('update', row)" :disabled="!canUpdate(row.userId)">
  61. 更新
  62. </el-button>
  63. <el-button type="primary" size="mini" @click="manageNutrients(row)" :disabled="!canUpdate(row.userId)">
  64. 管理营养素
  65. </el-button>
  66. <el-button type="primary" size="mini" @click="updateSort(row, 0)" :disabled="!canUpdate(row.userId)">
  67. 上移
  68. </el-button>
  69. <el-button type="primary" size="mini" @click="updateSort(row, 1)" :disabled="!canUpdate(row.userId)">
  70. 下移
  71. </el-button>
  72. <el-button type="primary" size="mini" @click="updateSort(row, 2)" :disabled="!canUpdate(row.userId)">
  73. 置顶
  74. </el-button>
  75. <el-button size="mini" type="danger" @click="handleDelete(row, $index)" :disabled="!canUpdate(row.userId)">
  76. 删除
  77. </el-button>
  78. </template>
  79. </el-table-column>
  80. </el-table>
  81. <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  82. <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
  83. <el-form ref="dataForm" :rules="rules" :model="params" label-position="left" label-width="200px" style="width: 400px; margin-left:50px;">
  84. <el-form-item label="名称" prop="name">
  85. <el-input v-model="params.name" placeholder="请输入名称" />
  86. </el-form-item>
  87. <el-form-item label="描述" prop="description">
  88. <el-input v-model="params.description" placeholder="请输入描述" type="textarea" :rows="2" />
  89. </el-form-item>
  90. </el-form>
  91. <div slot="footer" class="dialog-footer">
  92. <el-button @click="dialogFormVisible = false">
  93. 取消
  94. </el-button>
  95. <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
  96. 提交
  97. </el-button>
  98. </div>
  99. </el-dialog>
  100. </div>
  101. </template>
  102. <script>
  103. import store from '@/store'
  104. import Pagination from '@/components/Pagination'
  105. import { getNutrientTemplates, createTemplate, updateSort, updateTemplate, removeTemplate } from '@/api/nutrientTemplate'
  106. import {create} from "@/api/nutrient";
  107. export default {
  108. name: 'NutrientTemplateList',
  109. components: { Pagination },
  110. created() {
  111. this.fetchData()
  112. },
  113. data() {
  114. return {
  115. tableKey: 0,
  116. listQuery: {
  117. query: '',
  118. pageNum: 1,
  119. pageSize: 20
  120. },
  121. listLoading: false,
  122. list: [],
  123. total: 0,
  124. textMap: {
  125. update: '更新',
  126. create: '新建'
  127. },
  128. dialogStatus: '',
  129. dialogFormVisible: false,
  130. rules: {},
  131. params: {}
  132. }
  133. },
  134. methods: {
  135. fetchData() {
  136. this.listLoading = true
  137. getNutrientTemplates(this.listQuery).then(res => {
  138. this.list = res.data.list
  139. this.total = res.data.count
  140. this.listLoading = false
  141. }).catch(res => {
  142. this.$message.error("获取数据失败")
  143. this.listLoading = false
  144. })
  145. },
  146. handleCreateOrUpdate(type, row) {
  147. this.dialogStatus = type
  148. this.params = type === 'create' ? {} : row
  149. this.dialogFormVisible = true
  150. },
  151. updateSort(row, type) {
  152. updateSort(row.id, { type }).then(res => {
  153. this.fetchData()
  154. this.$message.success("提交成功")
  155. }).catch(res => {
  156. this.fetchData()
  157. this.$message.error(res.data.message)
  158. })
  159. },
  160. handleDelete(row, index) {
  161. removeTemplate(row.id).then(res => {
  162. this.fetchData()
  163. this.$message.success("提交成功")
  164. }).catch(res => {
  165. this.fetchData()
  166. this.$message.error(res.data.message)
  167. })
  168. },
  169. createData() {
  170. if (this.params) {
  171. createTemplate(this.params).then(res => {
  172. this.$notify({ title: '成功', message: '提交成功', type: 'success', duration: 2000 })
  173. this.fetchData()
  174. this.dialogFormVisible = false
  175. }).catch(error => {
  176. this.$notify({ title: '失败', message: error.response.data.message, type: 'error', duration: 2000 })
  177. this.fetchData()
  178. })
  179. }
  180. },
  181. updateData() {
  182. if (this.params) {
  183. updateTemplate(this.params.id, this.params).then(res => {
  184. this.$notify({ title: '成功', message: '提交成功', type: 'success', duration: 2000 })
  185. this.fetchData()
  186. this.dialogFormVisible = false
  187. }).catch(error => {
  188. this.$notify({ title: '失败', message: error.response.data.message, type: 'error', duration: 2000 })
  189. this.fetchData()
  190. })
  191. }
  192. },
  193. canUpdate(userId) {
  194. return store.getters.isAdmin || store.getters.userId === userId
  195. },
  196. manageNutrients(row) {
  197. this.$router.push({ path: `nutrient-template/${row.id}/detail` })
  198. }
  199. }
  200. }
  201. </script>
  202. <style scoped>
  203. </style>