add.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <div class="app-container">
  3. <van-form ref="formRef" @submit="onSubmit" required="auto">
  4. <h4 class="mb8 mt8">入库信息</h4>
  5. <van-cell-group>
  6. <van-field
  7. v-model="state.form.strainName"
  8. label="菌毒种名称"
  9. placeholder="请输入菌毒种名称"
  10. maxlength="255"
  11. :readonly="isDetail"
  12. :rules="isDetail ? [] : [{ required: true, message: '请输入菌毒种名称' }]"
  13. />
  14. <van-field
  15. v-model="state.form.preservationTemp"
  16. label="保存温度"
  17. placeholder="如:-80°C"
  18. maxlength="90"
  19. :readonly="isDetail"
  20. :rules="isDetail ? [] : [{ required: true, message: '请输入保存温度' }]"
  21. />
  22. <van-field
  23. v-model="state.form.storageLocation"
  24. label="存放地点"
  25. placeholder="请输入存放地点"
  26. maxlength="255"
  27. :readonly="isDetail"
  28. :rules="isDetail ? [] : [{ required: true, message: '请输入存放地点' }]"
  29. />
  30. <van-field
  31. v-model="state.form.inboundDate"
  32. label="入库日期"
  33. placeholder="请选择入库日期"
  34. :readonly="isDetail"
  35. :rules="isDetail ? [] : [{ required: true, message: '请选择入库日期' }]"
  36. @click="isDetail || (showDatePicker = true)"
  37. />
  38. <van-field
  39. v-model="state.form.inboundPerson"
  40. label="入库人"
  41. placeholder="当前用户"
  42. readonly
  43. />
  44. <van-field
  45. v-model="state.form.quantity"
  46. label="入库量"
  47. placeholder="如:10包"
  48. maxlength="255"
  49. :readonly="isDetail"
  50. :rules="isDetail ? [] : [{ required: true, message: '请输入入库量' }]"
  51. />
  52. </van-cell-group>
  53. <van-action-bar placeholder>
  54. <van-action-bar-icon icon="wap-home-o" text="首页" @click="router.push('/home')" />
  55. <van-action-bar-icon icon="revoke" text="返回" @click="router.push('/strain')" />
  56. <van-action-bar-button v-if="!isDetail" type="primary" :text="isEditMode ? '保存修改' : '立即提交'" :loading="submitting" native-type="submit" />
  57. </van-action-bar>
  58. </van-form>
  59. <van-popup v-model:show="showDatePicker" position="bottom">
  60. <van-date-picker v-model="currentDate" title="选择日期" @confirm="onConfirmDate" @cancel="showDatePicker = false" />
  61. </van-popup>
  62. </div>
  63. </template>
  64. <script lang="ts" setup>
  65. import to from 'await-to-js'
  66. import { computed, onMounted, reactive, ref } from 'vue'
  67. import { useRouter, useRoute } from 'vue-router'
  68. import { useStrainApi } from '/@/api/strain'
  69. import { useUserInfo } from '/@/stores/userInfo'
  70. import { formatDate } from '/@/utils/formatTime'
  71. import { showNotify } from 'vant'
  72. const router = useRouter()
  73. const route = useRoute()
  74. const strainApi = useStrainApi()
  75. const userStore = useUserInfo()
  76. const formRef = ref()
  77. const showDatePicker = ref(false)
  78. const currentDate = ref([new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()])
  79. const submitting = ref(false)
  80. const pageMode = computed(() => (route.query.mode as string) || 'add')
  81. const isDetail = computed(() => pageMode.value === 'detail')
  82. const isEditMode = computed(() => pageMode.value === 'edit')
  83. const state = reactive({
  84. form: {
  85. id: 0,
  86. strainName: '',
  87. preservationTemp: '',
  88. storageLocation: '',
  89. inboundDate: formatDate(new Date(), 'YYYY-mm-dd'),
  90. inboundPersonId: 0,
  91. inboundPerson: '',
  92. quantity: '',
  93. }
  94. })
  95. const onConfirmDate = () => {
  96. showDatePicker.value = false
  97. state.form.inboundDate = `${currentDate.value[0]}-${String(currentDate.value[1]).padStart(2, '0')}-${String(currentDate.value[2]).padStart(2, '0')}`
  98. }
  99. const initForm = () => {
  100. const u = userStore.userInfos
  101. state.form.inboundPersonId = u.id
  102. state.form.inboundPerson = u.nickName || ''
  103. state.form.inboundDate = formatDate(new Date(), 'YYYY-mm-dd')
  104. }
  105. const loadData = async (id: number) => {
  106. const [err, res]: any = await to(strainApi.getInboundById({ id }))
  107. if (err) return
  108. const d = res?.data
  109. if (d) {
  110. state.form = {
  111. id: d.id || 0,
  112. strainName: d.strainName || '',
  113. preservationTemp: d.preservationTemp || '',
  114. storageLocation: d.storageLocation || '',
  115. inboundDate: d.inboundDate ? d.inboundDate.slice(0, 10) : '',
  116. inboundPersonId: d.inboundPersonId || 0,
  117. inboundPerson: d.inboundPerson || '',
  118. quantity: d.quantity || '',
  119. }
  120. }
  121. }
  122. const onSubmit = async () => {
  123. const [errValid] = await to(formRef.value.validate())
  124. if (errValid) return
  125. submitting.value = true
  126. const params = JSON.parse(JSON.stringify(state.form))
  127. const post = isEditMode.value ? strainApi.updateInbound : strainApi.createInbound
  128. const [err]: any = await to(post(params))
  129. submitting.value = false
  130. if (err) return
  131. showNotify({ type: 'success', message: isEditMode.value ? '修改成功' : '新增成功' })
  132. setTimeout(() => {
  133. router.push('/strain')
  134. }, 1000)
  135. }
  136. onMounted(async () => {
  137. const id = route.query.id
  138. if (id) {
  139. await loadData(Number(id))
  140. } else {
  141. initForm()
  142. }
  143. })
  144. </script>
  145. <style lang="scss" scoped>
  146. .app-container {
  147. h4 {
  148. margin: 10px 0;
  149. height: 20px;
  150. line-height: 20px;
  151. padding-left: 16px;
  152. &::before {
  153. display: inline-block;
  154. content: '';
  155. background-color: #3c78e3;
  156. width: 4px;
  157. height: 20px;
  158. margin-right: 4px;
  159. vertical-align: top;
  160. }
  161. }
  162. }
  163. </style>