index.vue 12 KB

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