index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <div class="app-container">
  3. <div class="filter-container">
  4. <el-button
  5. class="filter-item"
  6. style="margin: 0 10px 20px 0; float: left;"
  7. type="success"
  8. icon="el-icon-circle-plus-outline"
  9. @click="handleCreate"
  10. >
  11. 新建
  12. </el-button>
  13. </div>
  14. <floating-window :able-hide="false" :class="{'is_fixed': isFixed}" >
  15. <el-dropdown @command="handleCommand">
  16. <img width="50px" height="50px" src="@/assets/navigation.png" alt="导航.png" />
  17. <el-dropdown-menu slot="dropdown">
  18. <el-dropdown-item command="detail">食物更新</el-dropdown-item>
  19. <el-dropdown-item command="nutrient">营养素关联</el-dropdown-item>
  20. <el-dropdown-item command="unit">单位管理</el-dropdown-item>
  21. <el-dropdown-item command="list">食物列表</el-dropdown-item>
  22. </el-dropdown-menu>
  23. </el-dropdown>
  24. </floating-window>
  25. <el-table
  26. :key="0"
  27. v-loading="listLoading"
  28. :data="list"
  29. border
  30. fit
  31. highlight-current-row
  32. style="width: 100%;"
  33. @row-click="showDialog"
  34. >
  35. <el-table-column type="index" label="序号" align="center" fixed width="60px" />
  36. <el-table-column label="规格名称" fixed align="center">
  37. <template slot-scope="{row}">
  38. <span>{{ row.name }}</span>
  39. </template>
  40. </el-table-column>
  41. <el-table-column label="图片" align="center" width="180">
  42. <template slot-scope="{row}">
  43. <el-image
  44. v-if="row.images && row.images.length > 0"
  45. style="width: 150px; height: 100px"
  46. :src="row.images[0]"
  47. fit="contain"
  48. />
  49. </template>
  50. </el-table-column>
  51. <el-table-column label="转换关系" align="center">
  52. <template slot-scope="{row}">
  53. <span v-if="row.quantity">{{ row.quantity + row.unit }}</span>
  54. <span v-else>无</span>
  55. </template>
  56. </el-table-column>
  57. <el-table-column label="推荐摄入量" align="center">
  58. <template slot-scope="{row}">
  59. <span>{{ row.inInit + row.inInitUnit }}</span>
  60. </template>
  61. </el-table-column>
  62. <el-table-column label="最小刻度" align="center" width="80px">
  63. <template slot-scope="{row}">
  64. <span>{{ row.minScale }}</span>
  65. </template>
  66. </el-table-column>
  67. <el-table-column label="最大刻度" align="center" width="80px">
  68. <template slot-scope="{row}">
  69. <span>{{ row.maxScale }}</span>
  70. </template>
  71. </el-table-column>
  72. <el-table-column label="卡尺每格刻度" align="center" width="120px">
  73. <template slot-scope="{row}">
  74. <span>{{ row.stepScale }}</span>
  75. </template>
  76. </el-table-column>
  77. <el-table-column label="创建时间" width="180px" align="center">
  78. <template slot-scope="{row}">
  79. <span>{{ row.createTime }}</span>
  80. </template>
  81. </el-table-column>
  82. <el-table-column label="更新时间" width="180px" align="center">
  83. <template slot-scope="{row}">
  84. <span>{{ row.updateTime }}</span>
  85. </template>
  86. </el-table-column>
  87. <el-table-column label="操作" align="center" class-name="small-padding" fixed="right" width="320">
  88. <template slot-scope="{row}">
  89. <el-button type="primary" size="mini" @click="handleUpdate(row)">
  90. 更新
  91. </el-button>
  92. <el-button type="primary" size="mini" @click="updateSort(row, 0)">
  93. 上移
  94. </el-button>
  95. <el-button type="primary" size="mini" @click="updateSort(row, 1)">
  96. 下移
  97. </el-button>
  98. <el-button size="mini" type="danger" @click="handleDelete(row)">
  99. 删除
  100. </el-button>
  101. </template>
  102. </el-table-column>
  103. </el-table>
  104. <el-dialog title="营养素计算" :visible.sync="dialogFormVisible">
  105. <span v-html="dialogHtml" />
  106. </el-dialog>
  107. </div>
  108. </template>
  109. <script>
  110. import { getFoodModifiers, removeFoodModifier, updateFoodModifierSort, convertFoodUnit, getNutrientList,
  111. getDetail, calculateNutrientsContent } from '@/api/food'
  112. import FloatingWindow from '@/components/FloatingWindow'
  113. export default {
  114. name: 'FoodModifierIndex',
  115. components: { FloatingWindow },
  116. data() {
  117. return {
  118. listLoading: false,
  119. list: [],
  120. food: {},
  121. foodId: '',
  122. dialogHtml: '',
  123. dialogFormVisible: false,
  124. isFixed: false,
  125. offsetTop: 0
  126. }
  127. },
  128. mounted() {
  129. window.addEventListener('scroll', this.initHeight);
  130. },
  131. //回调中移除监听
  132. destroyed() {
  133. window.removeEventListener('scroll', this.handleScroll)
  134. },
  135. created() {
  136. this.foodId = this.$route.params && this.$route.params.id
  137. this.fetchData()
  138. this.fetchFood()
  139. },
  140. methods: {
  141. handleCreate() {
  142. this.$router.push({ path: `/food/${this.foodId}/modifier/create` })
  143. },
  144. handleUpdate(row) {
  145. this.$router.push({ path: `/food/${this.foodId}/modifier/${row.id}/edit` })
  146. },
  147. handleDelete(row) {
  148. removeFoodModifier(this.foodId, row.id).then(res => {
  149. this.$notify.success('提交成功')
  150. this.fetchData()
  151. }).catch(res => {
  152. this.$message.error(res.data.message)
  153. this.fetchData()
  154. })
  155. },
  156. fetchFood() {
  157. getDetail(this.foodId).then(res => {
  158. this.food = res.data
  159. })
  160. },
  161. fetchData() {
  162. this.listLoading = true
  163. getFoodModifiers(this.foodId).then(res => {
  164. this.list = res.data.list
  165. this.listLoading = false
  166. }).catch(res => {
  167. this.$message.error('获取数据失败')
  168. this.listLoading = false
  169. })
  170. },
  171. updateSort(row, type) {
  172. updateFoodModifierSort(this.foodId, row.id, { type }).then(res => {
  173. this.$notify.success('提交成功')
  174. this.fetchData()
  175. }).catch(res => {
  176. this.$message.error(res.data.message)
  177. })
  178. },
  179. showDialog(row, column, event) {
  180. calculateNutrientsContent(row.foodId, { unit: row.inInitUnit, quantity: row.inInit }).then(res => {
  181. this.dialogFormVisible = true
  182. this.dialogHtml = res.data
  183. }).catch(res => {
  184. this.$message.error(res.data.message)
  185. })
  186. },
  187. initHeight() {
  188. // 设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离 (被卷曲的高度)
  189. const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
  190. //如果被卷曲的高度大于吸顶元素到顶端位置 的距离
  191. this.isFixed = scrollTop > this.offsetTop;
  192. },
  193. handleCommand(value) {
  194. let path = ''
  195. if (value === 'detail') {
  196. path = `/food/edit/${this.foodId}`
  197. } else if (value === 'unit') {
  198. path = `/food/${this.foodId}/unit`
  199. } else if (value === 'nutrient') {
  200. path = `/food/${this.foodId}/nutrient`
  201. } else if (value === 'modifier') {
  202. path = `/food/${this.foodId}/modifier`
  203. } else if (value === 'list') {
  204. path = `/food`
  205. }
  206. if (path) {
  207. this.$router.push({ path: path })
  208. }
  209. }
  210. }
  211. }
  212. </script>
  213. <style scoped>
  214. </style>