FoodDetail.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <div class="createPost-container">
  3. <el-form ref="postForm" :model="postForm" :rules="rules" label-position="right" label-width="90px" class="form-container">
  4. <div class="createPost-main-container">
  5. <el-form-item label="自定义ID:" style="margin-bottom: 40px;width: 60%" prop="id">
  6. <el-input v-model="postForm.id2" placeholder="请输入ID" />
  7. </el-form-item>
  8. <el-form-item label="名称:" prop="name" style="margin-bottom: 40px;width: 60%" required>
  9. <el-input v-model="postForm.name" placeholder="请输入名称" />
  10. </el-form-item>
  11. <el-form-item label="描述:" prop="description" style="margin-bottom: 30px;width: 40%">
  12. <el-input v-model="postForm.description" type="textarea" :rows="4" placeholder="请输入描述" />
  13. </el-form-item>
  14. <el-form-item label="食物类型:" prop="foodType" style="margin-bottom: 30px;width: 40%">
  15. <el-radio :label="0" v-model="postForm.foodType">主材</el-radio>
  16. <el-radio :label="1" v-model="postForm.foodType">辅材</el-radio>
  17. </el-form-item>
  18. <el-form-item label="ep:" prop="ep" style="margin-bottom: 40px;width: 60%">
  19. <el-input v-model="postForm.ep" placeholder="请输入ep" style="width: 40%" />
  20. </el-form-item>
  21. <el-form-item label="NV_spec:" prop="nvSpec" style="margin-bottom: 40px;width: 60%">
  22. <el-row :gutter="10">
  23. <el-col :span="8">
  24. <el-input v-model="postForm.nvSpec" placeholder="请输入nvSpec" />
  25. </el-col>
  26. <el-col :span="6">
  27. <el-autocomplete
  28. v-model="postForm.nvSpecUnitName"
  29. :fetch-suggestions="querySearchAsync"
  30. placeholder="请输入内容"
  31. ></el-autocomplete>
  32. </el-col>
  33. </el-row>
  34. </el-form-item>
  35. <el-form-item label="最小刻度:" prop="minScale" style="margin-bottom: 40px;width: 60%">
  36. <el-input v-model="postForm.minScale" placeholder="请输入最小刻度" />
  37. </el-form-item>
  38. <el-form-item label="最大刻度:" prop="maxScale" style="margin-bottom: 40px;width: 60%">
  39. <el-input v-model="postForm.maxScale" placeholder="请输入最大刻度" />
  40. </el-form-item>
  41. <el-form-item label="卡尺每格刻度:" prop="stepScale" style="margin-bottom: 40px;width: 60%">
  42. <el-input v-model="postForm.stepScale" placeholder="请输入卡尺每格刻度" />
  43. </el-form-item>
  44. <el-form-item label="图片列表:" prop="images" style="margin-bottom: 30px;width: 60%">
  45. <dropzone
  46. :id="mainImgDropzone"
  47. :default-img="postForm.images"
  48. @dropzone-removedFile="removeFile"
  49. @dropzone-success="addFile"
  50. />
  51. </el-form-item>
  52. <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
  53. 保存
  54. </el-button>
  55. <el-button v-loading="loading" style="margin-left: 10px;" type="success" v-if="!isEdit" @click="handleDialog">
  56. 保存并从模板导入营养素
  57. </el-button>
  58. </div>
  59. </el-form>
  60. <el-dialog title="从模板导入营养素" :visible.sync="dialogFormVisible">
  61. <el-form ref="dialogForm" label-position="left" label-width="80px" style="width: 400px; margin-left:50px;">
  62. <el-form-item label="模板:" prop="templateId">
  63. <el-select
  64. v-model="templateId"
  65. class="filter-item"
  66. filterable
  67. remote
  68. reserve-keyword
  69. placeholder="请输入模板关键词"
  70. :loading="templateLoading"
  71. :remote-method="queryTemplates"
  72. >
  73. <el-option v-for="item in templates" :key="`template${item.id}`" :label="item.name" :value="item.id" />
  74. </el-select>
  75. </el-form-item>
  76. </el-form>
  77. <div slot="footer" class="dialog-footer">
  78. <el-button @click="dialogFormVisible = false">
  79. 取消
  80. </el-button>
  81. <el-button type="primary" @click="submitFromTemplate">
  82. 提交
  83. </el-button>
  84. </div>
  85. </el-dialog>
  86. </div>
  87. </template>
  88. <script>
  89. import { create, getDetail, update } from '@/api/food'
  90. import Dropzone from '@/components/Dropzone'
  91. import { getList as getUnits } from '@/api/unit'
  92. import { getNutrientTemplates } from '@/api/nutrientTemplate'
  93. export default {
  94. name: "FoodDetail",
  95. components: { Dropzone },
  96. props: {
  97. isEdit: {
  98. type: Boolean,
  99. default: false
  100. }
  101. },
  102. data() {
  103. return {
  104. mainImgDropzone: 'mainImgDropzone',
  105. nutrientImgDropzone: 'nutrientImgDropzone',
  106. postForm: { id2: '', name: '', img: '', description: ''},
  107. loading: false,
  108. rules: {
  109. name: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
  110. },
  111. foodId: '',
  112. unitLoading: false,
  113. dialogFormVisible: false,
  114. templateLoading:false,
  115. templates: [],
  116. templateId: ''
  117. }
  118. },
  119. created() {
  120. if (this.isEdit) {
  121. this.foodId = this.$route.params && this.$route.params.id
  122. this.fetchData(this.foodId)
  123. }
  124. },
  125. methods: {
  126. fetchData(id) {
  127. getDetail(id).then(res => {
  128. this.postForm = res.data
  129. })
  130. },
  131. submitForm() {
  132. this.loading = true
  133. this.$refs['postForm'].validate((valid) => {
  134. if (valid) {
  135. if (this.isEdit) {
  136. this.updateData()
  137. } else {
  138. this.createData()
  139. }
  140. } else {
  141. this.$notify({ title: '失败', message: '必填字段为空', type: 'error', duration: 2000 })
  142. this.loading = false
  143. }
  144. })
  145. },
  146. createData() {
  147. create(this.postForm).then(res => {
  148. this.$message.success('创建成功')
  149. this.loading = false
  150. this.$router.push({ path: '/food' })
  151. }).catch(error => {
  152. this.$message.error(res.data.message)
  153. this.loading = false
  154. })
  155. },
  156. updateData() {
  157. update(this.foodId, this.postForm).then(res => {
  158. this.$message.success('更新成功')
  159. this.loading = false
  160. this.$router.push({ path: '/food' })
  161. }).catch(error => {
  162. this.$message.error(res.data.message)
  163. this.loading = false
  164. })
  165. },
  166. removeFile(file) {
  167. let imgs = this.postForm.images
  168. let imgIndex = -1
  169. for (let i = 0; i < imgs.length; i++) {
  170. if (imgs[i] === file.fileUrl) {
  171. imgIndex = i
  172. break
  173. }
  174. }
  175. imgs.splice(imgIndex, 1)
  176. },
  177. addFile(file) {
  178. if (file.dropzoneId === this.mainImgDropzone) {
  179. if (this.postForm.images) {
  180. this.postForm.images.push(file.fileUrl)
  181. } else {
  182. this.postForm.images = [file.fileUrl]
  183. }
  184. }
  185. },
  186. queryTemplates(query) {
  187. getNutrientTemplates({ query }).then(res => {
  188. this.templates = res.data.list
  189. }).catch(() => {
  190. this.templates = []
  191. })
  192. },
  193. handleDialog() {
  194. this.templateId = ''
  195. this.dialogFormVisible = true
  196. },
  197. submitFromTemplate() {
  198. this.postForm.templateId = this.templateId
  199. this.submitForm()
  200. },
  201. querySearchAsync(query, cb) {
  202. getUnits({ query }).then(res => {
  203. cb(res.data.list.map(item => { return { value: item.name } }))
  204. }).catch(() => {
  205. cb([])
  206. })
  207. }
  208. }
  209. }
  210. </script>
  211. <style scoped lang="scss">
  212. .createPost-container {
  213. position: relative;
  214. .createPost-main-container {
  215. padding: 40px 45px 20px 50px;
  216. .postInfo-container {
  217. position: relative;
  218. margin-bottom: 10px;
  219. .postInfo-container-item {
  220. float: left;
  221. }
  222. }
  223. }
  224. .word-counter {
  225. width: 40px;
  226. position: absolute;
  227. right: 10px;
  228. top: 0px;
  229. }
  230. }
  231. </style>