index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. 新建
  22. </el-button>
  23. </div>
  24. <el-table
  25. key="0"
  26. v-loading="listLoading"
  27. :data="list"
  28. border
  29. fit
  30. highlight-current-row
  31. style="width: 100%;"
  32. @row-click="showDialog"
  33. >
  34. <el-table-column type="index" label="序号" align="center" fixed width="60px" />
  35. <el-table-column label="名称" fixed align="center">
  36. <template slot-scope="{row}">
  37. <span>{{ row.name }}</span>
  38. </template>
  39. </el-table-column>
  40. <el-table-column label="自定义ID" align="center" fixed width="80px">
  41. <template slot-scope="{row}">
  42. <span>{{ row.id2 }}</span>
  43. </template>
  44. </el-table-column>
  45. <el-table-column label="食谱封面图" align="center" width="200px">
  46. <template slot-scope="{row}">
  47. <el-image style="width: 180px; height: 100px" :src="row.coverPic" fit="contain" />
  48. </template>
  49. </el-table-column>
  50. <el-table-column label="描述" align="center">
  51. <template slot-scope="{row}">
  52. <span>{{ row.description }}</span>
  53. </template>
  54. </el-table-column>
  55. <el-table-column label="创建时间" width="180px" align="center">
  56. <template slot-scope="{row}">
  57. <span>{{ row.createTime }}</span>
  58. </template>
  59. </el-table-column>
  60. <el-table-column label="更新时间" width="180px" align="center">
  61. <template slot-scope="{row}">
  62. <span>{{ row.updateTime }}</span>
  63. </template>
  64. </el-table-column>
  65. <el-table-column label="操作" align="center" class-name="small-padding" fixed="right" width="360">
  66. <template slot-scope="{row,$index}">
  67. <el-button type="primary" size="mini" @click="handleCreateOrUpdate('edit', row)">
  68. 更新
  69. </el-button>
  70. <el-button type="primary" size="mini" @click="manageSteps(row)">
  71. 管理步骤
  72. </el-button>
  73. <el-button type="primary" size="mini" @click="manageFoods(row)">
  74. 管理食物
  75. </el-button>
  76. <el-button size="mini" type="danger" @click="handleDelete(row, $index)">
  77. 删除
  78. </el-button>
  79. </template>
  80. </el-table-column>
  81. </el-table>
  82. <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  83. <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
  84. <el-form ref="dataForm" :rules="rules" :model="params" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
  85. <el-form-item label="ID" prop="id">
  86. <el-input v-model="params.id2" placeholder="请输入自定义ID" />
  87. </el-form-item>
  88. <el-form-item label="名称" prop="name">
  89. <el-input v-model="params.name" placeholder="请输入名称" />
  90. </el-form-item>
  91. <el-form-item label="描述" prop="description">
  92. <el-input v-model="params.description" placeholder="请输入描述" type="textarea" :row="2" />
  93. </el-form-item>
  94. <el-form-item label="营养素图片" prop="coverPic">
  95. <single-image :value="params.coverPic" :show-preview="true" @success="updateFile" style="width: 100%" />
  96. </el-form-item>
  97. </el-form>
  98. <div slot="footer" class="dialog-footer">
  99. <el-button @click="dialogFormVisible = false">
  100. 取消
  101. </el-button>
  102. <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
  103. 提交
  104. </el-button>
  105. </div>
  106. </el-dialog>
  107. <el-dialog title="营养素计算" :visible.sync="contentDialogVisible">
  108. <span v-html="recipeContentHtml" />
  109. </el-dialog>
  110. </div>
  111. </template>
  112. <script>
  113. import { getRecipes, createRecipe, updateRecipe, removeRecipe, getRecipeReport } from '@/api/recipe'
  114. import Pagination from '@/components/Pagination'
  115. import SingleImage from '@/components/Upload/SingleImage'
  116. export default {
  117. name: 'Index',
  118. components: { Pagination, SingleImage },
  119. data() {
  120. return {
  121. listQuery: {
  122. query: '',
  123. pageNum: 1,
  124. pageSize: 20
  125. },
  126. listLoading: false,
  127. list: [],
  128. total: 0,
  129. dialogFormVisible: false,
  130. textMap: { 'create': '新建', 'edit': '更新' },
  131. dialogStatus: '',
  132. rules: {},
  133. params: {},
  134. recipeContentHtml: '',
  135. contentDialogVisible: false
  136. }
  137. },
  138. created() {
  139. this.fetchData()
  140. },
  141. methods: {
  142. fetchData() {
  143. this.listLoading = true
  144. getRecipes(this.listQuery).then(res => {
  145. this.list = res.data.list
  146. this.listLoading = false
  147. }).catch(res => {
  148. this.$message.error('获取数据失败')
  149. this.listLoading = false
  150. })
  151. },
  152. handleCreateOrUpdate(dialogStatus, row) {
  153. this.params = dialogStatus === 'create' ? {} : Object.assign({}, row)
  154. this.dialogStatus = dialogStatus
  155. this.dialogFormVisible = true
  156. },
  157. manageSteps(row) {
  158. this.$router.push({ path: `recipe/${row.id}/step` })
  159. },
  160. manageFoods(row) {
  161. this.$router.push({ path: `recipe/${row.id}/food` })
  162. },
  163. handleDelete(row) {
  164. removeRecipe(row.id).then(res => {
  165. this.$message.success('提交成功')
  166. this.fetchData()
  167. }).catch(res => {
  168. this.$message.error(res.data.message)
  169. this.fetchData()
  170. })
  171. },
  172. createData() {
  173. createRecipe(this.params).then(res => {
  174. this.$message.success('提交成功')
  175. this.dialogFormVisible = false
  176. this.fetchData()
  177. }).catch(res => {
  178. this.$message.error(res.data.message)
  179. this.fetchData()
  180. })
  181. },
  182. updateData() {
  183. updateRecipe(this.params.id, this.params).then(res => {
  184. this.$message.success('提交成功')
  185. this.dialogFormVisible = false
  186. this.fetchData()
  187. }).catch(res => {
  188. this.$message.error(res.data.message)
  189. this.fetchData()
  190. })
  191. },
  192. updateFile(data) {
  193. this.$set(this.params, 'coverPic', data.fileUrl)
  194. },
  195. showDialog(row) {
  196. getRecipeReport(row.id).then(res => {
  197. this.contentDialogVisible = true
  198. this.recipeContentHtml = res.data
  199. }).catch(res => {
  200. this.$message.error(res.data.message)
  201. })
  202. }
  203. }
  204. }
  205. </script>
  206. <style scoped>
  207. </style>