add.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.usageDate"
  16. label="领用日期"
  17. placeholder="请选择领用日期"
  18. :readonly="isDetail"
  19. :rules="isDetail ? [] : [{ required: true, message: '请选择领用日期' }]"
  20. @click="isDetail || (showDatePicker = true)"
  21. />
  22. <van-field
  23. v-model="state.form.usagePerson"
  24. label="领用人"
  25. placeholder="当前用户"
  26. readonly
  27. />
  28. <van-field
  29. v-model="state.form.usageQuantity"
  30. label="领用数量"
  31. placeholder="如:6包"
  32. maxlength="255"
  33. :readonly="isDetail"
  34. :rules="isDetail ? [] : [{ required: true, message: '请输入领用数量' }]"
  35. />
  36. <van-field
  37. v-model="state.form.purpose"
  38. label="用途"
  39. placeholder="请输入用途"
  40. type="textarea"
  41. rows="2"
  42. autosize
  43. maxlength="2048"
  44. :readonly="isDetail"
  45. />
  46. <van-field
  47. v-model="state.form.remainingQuantity"
  48. label="剩余量"
  49. placeholder="如:46包"
  50. maxlength="255"
  51. :readonly="isDetail"
  52. />
  53. </van-cell-group>
  54. <van-action-bar placeholder>
  55. <van-action-bar-icon icon="wap-home-o" text="首页" @click="router.push('/home')" />
  56. <van-action-bar-icon icon="revoke" text="返回" @click="router.push('/strain')" />
  57. <van-action-bar-button v-if="!isDetail" type="primary" text="立即提交" :loading="submitting" native-type="submit" />
  58. </van-action-bar>
  59. </van-form>
  60. <van-popup v-model:show="showDatePicker" position="bottom">
  61. <van-date-picker v-model="currentDate" title="选择日期" @confirm="onConfirmDate" @cancel="showDatePicker = false" />
  62. </van-popup>
  63. </div>
  64. </template>
  65. <script lang="ts" setup>
  66. import to from 'await-to-js'
  67. import { computed, onMounted, reactive, ref } from 'vue'
  68. import { useRouter, useRoute } from 'vue-router'
  69. import { useStrainApi } from '/@/api/strain'
  70. import { useUserInfo } from '/@/stores/userInfo'
  71. import { formatDate } from '/@/utils/formatTime'
  72. import { showNotify } from 'vant'
  73. const router = useRouter()
  74. const route = useRoute()
  75. const strainApi = useStrainApi()
  76. const userStore = useUserInfo()
  77. const formRef = ref()
  78. const showDatePicker = ref(false)
  79. const currentDate = ref([new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()])
  80. const submitting = ref(false)
  81. const pageMode = computed(() => (route.query.mode as string) || 'add')
  82. const isDetail = computed(() => pageMode.value === 'detail')
  83. const state = reactive({
  84. form: {
  85. id: 0,
  86. strainName: '',
  87. usageDate: formatDate(new Date(), 'YYYY-mm-dd'),
  88. usagePersonId: 0,
  89. usagePerson: '',
  90. usageQuantity: '',
  91. purpose: '',
  92. remainingQuantity: '',
  93. }
  94. })
  95. const onConfirmDate = () => {
  96. showDatePicker.value = false
  97. state.form.usageDate = `${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.usagePersonId = u.id
  102. state.form.usagePerson = u.nickName || ''
  103. state.form.usageDate = formatDate(new Date(), 'YYYY-mm-dd')
  104. }
  105. const loadData = async (id: number) => {
  106. const [err, res]: any = await to(strainApi.getUsageById({ 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. usageDate: d.usageDate || '',
  114. usagePersonId: d.usagePersonId || 0,
  115. usagePerson: d.usagePerson || '',
  116. usageQuantity: d.usageQuantity || '',
  117. purpose: d.purpose || '',
  118. remainingQuantity: d.remainingQuantity || '',
  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 [err]: any = await to(strainApi.createUsage(params))
  128. submitting.value = false
  129. if (err) return
  130. showNotify({ type: 'success', message: '新增成功' })
  131. setTimeout(() => {
  132. router.push('/strain')
  133. }, 1000)
  134. }
  135. onMounted(async () => {
  136. const id = route.query.id
  137. if (id) {
  138. await loadData(Number(id))
  139. } else {
  140. initForm()
  141. }
  142. })
  143. </script>
  144. <style lang="scss" scoped>
  145. .app-container {
  146. h4 {
  147. margin: 10px 0;
  148. height: 20px;
  149. line-height: 20px;
  150. padding-left: 16px;
  151. &::before {
  152. display: inline-block;
  153. content: '';
  154. background-color: #3c78e3;
  155. width: 4px;
  156. height: 20px;
  157. margin-right: 4px;
  158. vertical-align: top;
  159. }
  160. }
  161. }
  162. </style>