index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <z-paging ref="paging" v-model="list" bgColor="#f7f7f7" @query="query" safe-area-inset-bottom>
  3. <template #top>
  4. <ut-navbar title="添加管理记录" :fixed="false">
  5. <template #left><view class=""></view></template>
  6. </ut-navbar>
  7. </template>
  8. <view class="pt-24">
  9. <up-form class="p-rtv" labelPosition="top" :model="form" :rules="rules" labelWidth="auto" ref="upFormRef">
  10. <view class="startline-title pl-24 ml-24 mb-16">管理记录信息</view>
  11. <view class="bg-#fff pd-24 mb-20">
  12. <!-- 操作日期 -->
  13. <view class="h-1" id="operationDatepppp"></view>
  14. <up-form-item :borderBottom="false" label="操作日期" required prop="operationDate">
  15. <ut-datetime-picker v-model="form.operationDate.startDate" mode="datetime" dateFields="day">
  16. <view class="d-flex mr-20">
  17. <up-input v-model="form.operationDate.startDate" placeholder="请选择操作开始时间" border="bottom" :customStyle="{ paddingLeft: '0rpx' }"></up-input>
  18. <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill" style="margin-left: -20rpx"></up-icon>
  19. </view>
  20. </ut-datetime-picker>
  21. <ut-datetime-picker v-model="form.operationDate.endDate" mode="datetime" dateFields="day">
  22. <view class="d-flex">
  23. <up-input v-model="form.operationDate.endDate" placeholder="请选择操作结束时间" border="bottom" :customStyle="{ paddingLeft: '0rpx' }"></up-input>
  24. <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill" style="margin-left: -20rpx"></up-icon>
  25. </view>
  26. </ut-datetime-picker>
  27. </up-form-item>
  28. <!-- 操作类型 -->
  29. <ut-action-sheet v-model="form.operationType" :tabs="operationTypeOptions" title="选择操作类型">
  30. <up-form-item borderBottom label="操作类型" required prop="operationType" id="operationTypepppp">
  31. <view v-if="form.operationType" class="f-s-30 c-333 f-w-5 flex1">{{ selectDictLabel(operationTypeOptions, form.operationType) }}</view>
  32. <view v-else class="f-s-30 c-ccc f-w-4 flex1">请选择操作类型</view>
  33. <template #right>
  34. <up-icon size="22rpx" color="#2A6D52" name="arrow-down-fill"></up-icon>
  35. </template>
  36. </up-form-item>
  37. </ut-action-sheet>
  38. <up-form-item :borderBottom="false" label="种养殖所在基地" required prop="baseId" id="baseIdpppp">
  39. <view v-if="!deawerData" class="w-100% d-flex a-c j-c pd-24 b-radius bg-#FBFDFB border-#AFDDBB" @click="openDrawer">
  40. <view class=""></view>
  41. <view class="f-s-34 c-primary">请选择种养殖所在的基地</view>
  42. </view>
  43. <view class="w-100%" v-else>
  44. <Baseinfo :modeValue="deawerData" @close="handleBaseinfoClose" :baseType="form.taskType" />
  45. </view>
  46. </up-form-item>
  47. <!-- 操作方式 -->
  48. <up-form-item borderBottom label="操作方式" required prop="operationMethod" id="operationMethodpppp">
  49. <up-input v-model="form.operationMethod" placeholder="请输入操作方式" border="none"></up-input>
  50. </up-form-item>
  51. <!-- 记录人 -->
  52. <up-form-item borderBottom label="记录人" required prop="recorder" id="recorderpppp">
  53. <up-input v-model="form.recorder" placeholder="请输入记录人" border="none"></up-input>
  54. </up-form-item>
  55. <!-- 是否使用物料 -->
  56. <up-form-item borderBottom label="是否使用物料" required prop="useMaterial" id="useMaterialpppp">
  57. <up-radio-group v-model="form.useMaterial">
  58. <up-radio :customStyle="{ marginRight: '60rpx' }" v-for="(item, index) in yes_no" :key="index" :label="item.label" :name="item.value"></up-radio>
  59. </up-radio-group>
  60. </up-form-item>
  61. <!-- 备注 -->
  62. <up-form-item borderBottom label="备注" prop="remark" id="remarkpppp">
  63. <up-input v-model="form.remark" placeholder="请输入备注" border="none"></up-input>
  64. </up-form-item>
  65. </view>
  66. </up-form>
  67. </view>
  68. <template #bottom>
  69. <view class="pd-24 d-flex j-c gap-20 base-bottom-wrap">
  70. <up-button type="warning" @click="saveDraft()">暂存</up-button>
  71. <up-button type="primary" @click="save()">提交</up-button>
  72. </view>
  73. </template>
  74. </z-paging>
  75. </template>
  76. <script setup lang="ts">
  77. import { ref, reactive, computed, getCurrentInstance, type ComponentInternalInstance } from 'vue';
  78. import { useClientRequest } from '@/utils/request';
  79. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  80. const { yes_no } = toRefs<any>(proxy?.useDict('yes_no'));
  81. const deawerData = ref();
  82. // 表单数据
  83. const form = ref({
  84. operationDate: {
  85. startDate: null as string | null,
  86. endDate: null as string | null,
  87. },
  88. operationType: null as string | null,
  89. operationMethod: '',
  90. recorder: '',
  91. useMaterial: '0' as '0' | '1',
  92. remark: '',
  93. });
  94. // 操作类型选项(示例,可以根据实际需求调整)
  95. const operationTypeOptions = ref([
  96. { label: '浇水', value: '1' },
  97. { label: '施肥', value: '2' },
  98. { label: '除草', value: '3' },
  99. { label: '病虫害防治', value: '4' },
  100. { label: '修剪', value: '5' },
  101. { label: '其他', value: '6' },
  102. ]);
  103. // 自定义校验函数:操作日期校验
  104. const validateOperationDate = (rule: any, value: any, callback: any) => {
  105. if (!value) {
  106. callback(new Error('请选择操作日期'));
  107. return;
  108. }
  109. const { startDate, endDate } = value;
  110. if (!startDate) {
  111. callback(new Error('请选择操作开始时间'));
  112. return;
  113. }
  114. if (!endDate) {
  115. callback(new Error('请选择操作结束时间'));
  116. return;
  117. }
  118. const start = new Date(startDate);
  119. const end = new Date(endDate);
  120. if (end <= start) {
  121. callback(new Error('操作结束时间必须晚于开始时间'));
  122. return;
  123. }
  124. callback();
  125. };
  126. // 表单验证规则
  127. const rules = reactive({
  128. operationDate: [{ validator: validateOperationDate, trigger: 'blur' }],
  129. operationType: [{ required: true, message: '请选择操作类型' }],
  130. operationMethod: [{ required: true, message: '请输入操作方式' }],
  131. recorder: [{ required: true, message: '请输入记录人' }],
  132. useMaterial: [{ required: true, message: '请选择是否使用物料' }],
  133. remark: [{ required: false, message: '请输入备注' }],
  134. });
  135. // 字典标签选择函数(从 port-create 复制)
  136. const selectDictLabel = (dict: any[], value: any) => {
  137. if (!dict || dict.length === 0) return '';
  138. const item = dict.find((item) => item.value === value);
  139. return item ? item.label : '';
  140. };
  141. const paging = ref();
  142. const list = ref();
  143. const upFormRef = ref<any>();
  144. const query = async (pageNo: number, pageSize: number) => {
  145. const res = await useClientRequest.get('/plt-api/app/plantationTask/list', {
  146. pageNo,
  147. pageSize,
  148. });
  149. // return res.data;
  150. };
  151. const handleBaseinfoClose = () => {
  152. deawerData.value = null;
  153. };
  154. // 提交表单
  155. const save = () => {
  156. uni.$u.debounce(
  157. async () => {
  158. try {
  159. console.log('开始提交管理记录');
  160. await upFormRef.value?.validate();
  161. console.log('校验完成');
  162. const params = {
  163. ...form.value,
  164. startDate: form.value.operationDate.startDate,
  165. endDate: form.value.operationDate.endDate,
  166. };
  167. console.log('提交参数:', params);
  168. // 这里需要根据实际 API 进行调整
  169. const res = await useClientRequest.post('/plt-api/app/managementRecord/save', params);
  170. if (res.code == 200) {
  171. uni.showToast({
  172. title: '提交成功',
  173. icon: 'success',
  174. duration: 2000,
  175. });
  176. setTimeout(() => {
  177. uni.navigateBack();
  178. }, 1000);
  179. }
  180. } catch (error: any) {
  181. console.log('表单验证错误:', error);
  182. // 滚动到第一个错误字段
  183. if (error && error[0]?.field) {
  184. const firstErrorField = error[0].field + 'pppp';
  185. paging.value?.scrollIntoViewById(firstErrorField, 30, true);
  186. }
  187. return;
  188. }
  189. },
  190. 1000,
  191. true,
  192. );
  193. };
  194. // 暂存
  195. const saveDraft = () => {
  196. uni.$u.debounce(
  197. async () => {
  198. try {
  199. const params = {
  200. ...form.value,
  201. startDate: form.value.operationDate.startDate,
  202. endDate: form.value.operationDate.endDate,
  203. };
  204. console.log('暂存参数:', params);
  205. // 这里需要根据实际 API 进行调整
  206. const res = await useClientRequest.post('/plt-api/app/managementRecord/draft', params);
  207. if (res.code == 200) {
  208. uni.showToast({
  209. title: '暂存成功',
  210. icon: 'success',
  211. duration: 2000,
  212. });
  213. setTimeout(() => {
  214. uni.navigateBack();
  215. }, 1000);
  216. }
  217. } catch (error: any) {
  218. console.log('暂存错误:', error);
  219. }
  220. },
  221. 1000,
  222. true,
  223. );
  224. };
  225. </script>