index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <template>
  2. <div class="app-container" v-loading="uploadLoading">
  3. <div class="filter-container">
  4. <el-row style="margin-bottom: 5px">
  5. <el-input
  6. v-model="listQuery.query"
  7. placeholder="请输入检索词"
  8. style="width: 60%;"
  9. class="filter-item"
  10. @keyup.enter.native="fetchData()"
  11. />
  12. <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="fetchData">
  13. 检索
  14. </el-button>
  15. <el-select v-model="listQuery.orderBy" style="width: 160px;margin-left: 10px;" class="filter-item" @change="fetchData">
  16. <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key" />
  17. </el-select>
  18. </el-row>
  19. <el-row>
  20. <el-button
  21. class="filter-item"
  22. style="margin: 0 10px 20px 0; float: right;"
  23. type="success"
  24. icon="el-icon-circle-plus-outline"
  25. @click="handleCreate"
  26. >
  27. 新建
  28. </el-button>
  29. <el-button
  30. class="filter-item"
  31. style="margin: 0 10px 20px 0; float: right;"
  32. type="primary"
  33. @click="handleImport"
  34. >
  35. 食物导入
  36. </el-button>
  37. </el-row>
  38. </div>
  39. <el-table
  40. :key="tableKey"
  41. v-loading="listLoading"
  42. :data="list"
  43. border
  44. fit
  45. highlight-current-row
  46. style="width: 100%;"
  47. >
  48. <el-table-column type="index" label="序号" align="center" fixed width="60px" />
  49. <el-table-column label="名称" align="center" fixed width="200px">
  50. <template slot-scope="{row}">
  51. <span>{{ row.name }}</span>
  52. </template>
  53. </el-table-column>
  54. <el-table-column label="图片" align="center" width="180">
  55. <template slot-scope="{row}">
  56. <el-image
  57. v-if="row.mainImage"
  58. style="width: 150px; height: 100px"
  59. :src="row.mainImage"
  60. fit="contain"
  61. />
  62. </template>
  63. </el-table-column>
  64. <el-table-column label="自定义ID" align="center" width="100px">
  65. <template slot-scope="{row}">
  66. <span>{{ row.id2 }}</span>
  67. </template>
  68. </el-table-column>
  69. <el-table-column label="描述" align="center" width="200">
  70. <template slot-scope="{row}">
  71. <span>{{ row.description }}</span>
  72. </template>
  73. </el-table-column>
  74. <el-table-column label="食物类型" align="center" width="80">
  75. <template slot-scope="{row}">
  76. <span>{{ row.foodType | foodTypeFilter }}</span>
  77. </template>
  78. </el-table-column>
  79. <el-table-column label="ep(%)" align="center" width="70px">
  80. <template slot-scope="{row}">
  81. <span>{{ row.ep }}</span>
  82. </template>
  83. </el-table-column>
  84. <el-table-column label="GI" align="center" width="70px">
  85. <template slot-scope="{row}">
  86. <span>{{ row.gi }}</span>
  87. </template>
  88. </el-table-column>
  89. <el-table-column label="GL" align="center" width="70px">
  90. <template slot-scope="{row}">
  91. <span>{{ row.gl ? row.gl : row.glCalcd }}</span>
  92. </template>
  93. </el-table-column>
  94. <el-table-column label="数据来源" align="center" width="100px">
  95. <template slot-scope="{row}">
  96. <span>{{ row.dataSource }}</span>
  97. </template>
  98. </el-table-column>
  99. <el-table-column label="创建时间" width="180px" align="center">
  100. <template slot-scope="{row}">
  101. <span>{{ row.createTime }}</span>
  102. </template>
  103. </el-table-column>
  104. <el-table-column label="更新时间" width="180px" align="center">
  105. <template slot-scope="{row}">
  106. <span>{{ row.updateTime }}</span>
  107. </template>
  108. </el-table-column>
  109. <el-table-column label="操作" align="center" class-name="small-padding" fixed="right" width="90">
  110. <template slot-scope="{row,$index}">
  111. <el-dropdown @command="handleCommand">
  112. <el-button size="small" @click.stop="{}" type="primary">
  113. 操作<i class="el-icon-arrow-down el-icon--right"></i>
  114. </el-button>
  115. <el-dropdown-menu slot="dropdown">
  116. <el-dropdown-item :command="{row: row, command: 'update'}">更新</el-dropdown-item>
  117. <el-dropdown-item :command="{row: row, command: 'copy'}">复制</el-dropdown-item>
  118. <el-dropdown-item :command="{row: row, command: 'manageNutrients'}">营养素关联</el-dropdown-item>
  119. <el-dropdown-item :command="{row: row, command: 'manageUnits'}">单位管理</el-dropdown-item>
  120. <el-dropdown-item :command="{row: row, command: 'manageModifiers'}">规格管理</el-dropdown-item>
  121. <el-dropdown-item :command="{row: row, command: 'delete'}">删除</el-dropdown-item>
  122. </el-dropdown-menu>
  123. </el-dropdown>
  124. </template>
  125. </el-table-column>
  126. </el-table>
  127. <pagination
  128. v-show="total>0"
  129. :total="total"
  130. :page.sync="listQuery.pageNum"
  131. :limit.sync="listQuery.pageSize"
  132. @pagination="fetchData"
  133. />
  134. <el-dialog title="食物导入" width="40%" :visible.sync="dialogFormVisible">
  135. <el-upload
  136. ref="upload"
  137. action=""
  138. :auto-upload="false"
  139. :multiple="false"
  140. drag
  141. :limit="1"
  142. :show-file-list="true"
  143. :on-change="fileChanged"
  144. >
  145. <i class="el-icon-upload"></i>
  146. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  147. </el-upload>
  148. <div slot="footer" >
  149. <el-button @click="dialogFormVisible = false">
  150. 取消
  151. </el-button>
  152. <el-button type="primary" @click="importFoods">
  153. 开始导入
  154. </el-button>
  155. </div>
  156. </el-dialog>
  157. <el-dialog title="复制食物" width="40%" :visible.sync="copyDialogVisible">
  158. <el-form ref="dialogForm" label-position="left" label-width="90px" style="width: 80%; margin-left:50px;">
  159. <el-form-item label="新食物名称: " prop="foodName">
  160. <el-input v-model="params.foodName" placeholder="请输入新食物名称" />
  161. </el-form-item>
  162. </el-form>
  163. <div slot="footer" >
  164. <el-button @click="copyDialogVisible = false">
  165. 取消
  166. </el-button>
  167. <el-button type="primary" @click="copyFood">
  168. 确认
  169. </el-button>
  170. </div>
  171. </el-dialog>
  172. </div>
  173. </template>
  174. <script>
  175. import Pagination from '@/components/Pagination'
  176. import { getList, remove, copyFromSimilarFood } from '@/api/food'
  177. import axios from 'axios'
  178. import { getToken } from '@/utils/auth'
  179. import { getFoodSourceByUrl } from '@/utils/food-utils'
  180. const foodTypes = { 0: '主材', 1: '辅材' }
  181. export default {
  182. name: 'FoodList',
  183. components: { Pagination },
  184. filters: {
  185. foodTypeFilter(value) {
  186. return foodTypes[value]
  187. }
  188. },
  189. data() {
  190. return {
  191. tableKey: 0,
  192. total: 0,
  193. dialogStatus: '',
  194. dialogFormVisible: false,
  195. sortOptions: [{ key: 0, label: '按创建时间倒序排列' }, { key: 1, label: '按ID顺序排列' }],
  196. list: [],
  197. listLoading: true,
  198. listQuery: {
  199. query: '',
  200. orderBy: 0,
  201. pageNum: 1,
  202. pageSize: 20
  203. },
  204. uploadFile: null,
  205. uploadLoading: false,
  206. copyDialogVisible: false,
  207. params: {},
  208. foodSource: ''
  209. }
  210. },
  211. created() {
  212. this.foodSource = getFoodSourceByUrl(this.$route.path)
  213. if (this.foodSource === 'ENTRY' || this.foodSource === '') {
  214. this.listQuery.orderBy = 0
  215. } else {
  216. this.listQuery.orderBy = 1
  217. }
  218. this.fetchData()
  219. },
  220. methods: {
  221. fetchData() {
  222. this.listLoading = true
  223. this.listQuery.foodSource = this.foodSource
  224. getList(this.listQuery).then(response => {
  225. this.list = response.data.list
  226. this.total = response.data.count
  227. this.listLoading = false
  228. })
  229. },
  230. handleCreate() {
  231. this.$router.push({ path: '/food/create' })
  232. },
  233. handleUpdate(row) {
  234. this.$router.push({ path: '/food/edit/' + row.id })
  235. },
  236. manageNutrients(row) {
  237. this.$router.push({ path: `/food/${row.id}/nutrient` })
  238. },
  239. manageUnits(row) {
  240. this.$router.push({ path: `/food/${row.id}/unit` })
  241. },
  242. manageModifiers(row) {
  243. this.$router.push({ path: `/food/${row.id}/modifier` })
  244. },
  245. handleDelete(row, index) {
  246. remove(row.id).then(res => {
  247. this.$notify({ title: '成功', message: '删除成功', type: 'success', duration: 2000 })
  248. this.list.splice(index, 1)
  249. this.fetchData()
  250. }).catch(res => {
  251. this.$message.error(res.data.message)
  252. })
  253. },
  254. handleImport() {
  255. if (this.$refs.upload) {
  256. this.$refs.upload.clearFiles()
  257. }
  258. this.uploadFile = null
  259. this.dialogFormVisible = true
  260. },
  261. importFoods() {
  262. const reqConfig = { headers: { 'Authorization': getToken(), 'Content-Type':'multipart/form-data' },
  263. timeout: 1000 * 60 * 3 }
  264. const importUrl = process.env.VUE_APP_BASE_API + '/api/foods/import'
  265. this.uploadLoading = true
  266. this.dialogFormVisible = false
  267. axios.post(importUrl, this.uploadFile, reqConfig).then(res => {
  268. this.$notify.success('导入成功')
  269. this.uploadLoading = false
  270. }).catch(res => {
  271. this.$message.error(res.response.data.message)
  272. this.uploadLoading = false
  273. })
  274. },
  275. fileChanged(file, fileList) {
  276. let fromData = new FormData()
  277. fromData.append('file', file.raw)
  278. this.uploadFile = fromData
  279. },
  280. handleCopy(data) {
  281. this.params = { foodId: data.id }
  282. this.copyDialogVisible = true
  283. },
  284. copyFood() {
  285. copyFromSimilarFood(this.params).then(res => {
  286. this.$notify.success('提交成功')
  287. this.$router.push({ path: `/food/edit/${res.data}`})
  288. }).catch(res => {
  289. this.$message.error(res.data.message)
  290. })
  291. },
  292. handleCommand(data) {
  293. switch (data.command) {
  294. case 'update':
  295. this.handleUpdate(data.row)
  296. break
  297. case 'copy':
  298. this.handleCopy(data.row)
  299. break
  300. case 'manageNutrients':
  301. this.manageNutrients(data.row)
  302. break
  303. case 'manageUnits':
  304. this.manageUnits(data.row)
  305. break
  306. case 'manageModifiers':
  307. this.manageModifiers(data.row)
  308. break
  309. case 'delete':
  310. this.handleDelete(data.row)
  311. break
  312. }
  313. }
  314. }
  315. }
  316. </script>
  317. <style scoped>
  318. </style>