index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <z-paging ref="paging" v-model="list" paging-class="paging-btm-shadow" bgColor="#f7f7f7" safe-area-inset-bottom @query="query">
  3. <template #top>
  4. <ut-navbar leftText="请选择任务所使用的种子信息" :fixed="false"></ut-navbar>
  5. </template>
  6. <view class="d-flex a-c pd-24 pb-0 bg-#f7f7f7">
  7. <view class="min-w-170 flex1">
  8. <ut-action-sheet v-model="form.instoreType" :tabs="[{ label: '全部', value: '' }, ...pt_fresh_instore_type]" @change="changeSeach" title="选择原料类型">
  9. <view class="d-flex search-select-item a-c">
  10. <view class="flex1 ov-hd f-s-28 c-333 text-center f-w-5 w-s-no">{{ selectDictLabel(pt_fresh_instore_type, form.instoreType) || '全部' }}</view>
  11. <up-icon size="24rpx" color="#333" name="arrow-down-fill" class="mr-5"></up-icon>
  12. </view>
  13. </ut-action-sheet>
  14. </view>
  15. <view class="h-86 pl-20 w-100%">
  16. <ut-search ref="searchRef" v-model="form.keyword" @search="changeSeach" @change="changeSeach" margin="0" :border="false" placeholder="搜批次号、品种名、基地名" bgColor="#fff" height="86rpx" borderRadius="10rpx"></ut-search>
  17. </view>
  18. </view>
  19. <template v-for="(item, index) in list" :key="index">
  20. <info-card :item="item" :selected="selectedItems?.id === item.id" @click="toggleSelection(item)" />
  21. </template>
  22. <template #bottom>
  23. <view v-if="selectedItems" class="bg-#EBF6EE c-primary f-s-24 pd-10 pl-24"> 已选择种源:{{ selectedItems.variety }} </view>
  24. <view class="pd-20 d-flex">
  25. <up-button type="primary" @click="open">确认选择</up-button>
  26. </view>
  27. </template>
  28. </z-paging>
  29. <up-popup v-if="show" :show="show" :round="10" mode="bottom" @close="close" @open="open" closeable>
  30. <view>
  31. <view class="d-flex a-c pd-24">
  32. <view class="f-s-34 f-w-5 c-#333">关联种源信息</view>
  33. <view class="bg-#37A954 radius-10 pd4-10-20-10-20 d-flex a-c" style="width: max-content">
  34. <up-icon name="plus" color="#fff" size="24rpx"></up-icon>
  35. <view class="c-#fff f-s-22">添加种源关联信息</view>
  36. </view>
  37. </view>
  38. <view class="pd-24">
  39. <up-form class="p-rtv" labelPosition="top" :model="formDate" :rules="rules" labelWidth="auto" ref="upFormRef">
  40. <up-form-item :borderBottom="false" prop="inputs" id="inputspppp">
  41. <view class="d-flex flex-cln" style="width: 100%">
  42. <souceinfo v-if="selectedItems" :data="selectedItems" :index="0" v-model:inputs="formDate.inputs[0]" @close="handleSourceClose" />
  43. </view>
  44. </up-form-item>
  45. </up-form>
  46. </view>
  47. <view class="pd-20 d-flex">
  48. <up-button type="primary" @click="saveSeedInfo">确认添加</up-button>
  49. </view>
  50. </view>
  51. </up-popup>
  52. </template>
  53. <script setup lang="ts">
  54. import { useClientRequest } from '@/utils/request';
  55. import InfoCard from './models/info-card.vue';
  56. import Souceinfo from './models/souceinfo.vue';
  57. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  58. const { pt_fresh_instore_type } = toRefs<any>(proxy?.useDict('pt_fresh_instore_type'));
  59. interface SeedItem {
  60. id: number | string;
  61. [key: string]: any;
  62. }
  63. const list = ref<SeedItem[]>([]);
  64. const paging = ref();
  65. const form = ref({
  66. keyword: '',
  67. instoreType: '',
  68. storageType: '4',
  69. part: '',
  70. notIncludePart: '',
  71. });
  72. //表单
  73. const formDate = ref({
  74. inputs: [] as any,
  75. });
  76. // 自定义inputs校验函数
  77. const validateInputs = (rule: any, value: any, callback: any) => {
  78. // value 应该是 inputs 数组
  79. if (!value || !Array.isArray(value) || value.length === 0) {
  80. callback(new Error('请至少添加一个种源并填写用量'));
  81. return;
  82. }
  83. // 检查每个物料
  84. for (const item of value) {
  85. if (!item || typeof item !== 'object') {
  86. callback(new Error('种源数据格式错误'));
  87. return;
  88. }
  89. const { storageId, inputAmount } = item;
  90. console.log(item, 'item');
  91. if (!storageId) {
  92. callback(new Error('种源ID缺失'));
  93. return;
  94. }
  95. if (inputAmount === undefined || inputAmount === null || inputAmount === '') {
  96. callback(new Error('请填写种源用量'));
  97. return;
  98. }
  99. const amounts = Number(inputAmount);
  100. if (isNaN(amounts) || amounts <= 0) {
  101. callback(new Error('种源用量必须大于0'));
  102. return;
  103. }
  104. }
  105. callback();
  106. };
  107. // 表单验证规则
  108. const rules = ref({
  109. inputs: [{ validator: validateInputs, trigger: 'blur' }],
  110. });
  111. // 单选模式:存储当前选中的单个项目
  112. const selectedItems = ref<SeedItem | null>(null);
  113. const query = async (pageNo: number, pageSize: number) => {
  114. const params = {
  115. pageNo,
  116. pageSize,
  117. ...form.value,
  118. };
  119. const res = await useClientRequest.get('/plt-api/app/storage/list', params);
  120. const { rows } = res;
  121. paging.value.complete(rows);
  122. };
  123. const changeSeach = () => {
  124. paging.value.reload();
  125. };
  126. const toggleSelection = (item: SeedItem) => {
  127. if (selectedItems.value?.id === item.id) {
  128. // 如果点击的是已选项,则取消选择
  129. selectedItems.value = null;
  130. formDate.value.inputs = [];
  131. } else {
  132. // 选择新项(自动取消之前的选择)
  133. selectedItems.value = item;
  134. formDate.value.inputs = [
  135. {
  136. storageId: item.id,
  137. inputAmount: '',
  138. variety: item?.variety,
  139. },
  140. ];
  141. }
  142. console.log('当前选中的种子:', selectedItems.value);
  143. console.log('表单数据:', formDate.value.inputs);
  144. };
  145. // 创建响应式数据
  146. const show = ref(false);
  147. // 定义方法
  148. const open = () => {
  149. // 打开逻辑,比如设置 show 为 true
  150. show.value = true;
  151. // 获取当前选中的单个对象
  152. console.log('选中的项目:', selectedItems.value);
  153. };
  154. const close = () => {
  155. // 关闭逻辑,设置 show 为 false
  156. show.value = false;
  157. // console.log('close');
  158. };
  159. // 处理种源信息关闭事件(单选模式:直接取消选择)
  160. const handleSourceClose = (index: number) => {
  161. selectedItems.value = null;
  162. formDate.value.inputs = [];
  163. };
  164. const upFormRef = ref();
  165. //校验表单然后提交
  166. const saveSeedInfo = async () => {
  167. uni.$u.debounce(
  168. async () => {
  169. try {
  170. console.log('开始校验');
  171. console.log(formDate.value, 'formDate.value');
  172. await upFormRef.value?.validate();
  173. console.log('校验完成');
  174. const params = {
  175. processId: processId.value,
  176. inputList: formDate.value.inputs,
  177. };
  178. console.log('提交参数:', params);
  179. // 这里需要根据实际 API 进行调整
  180. const res = await useClientRequest.post('/plt-api/app/processInputMaterial/batchInput', params);
  181. if (res.code == 200) {
  182. uni.showToast({
  183. title: '提交成功',
  184. icon: 'success',
  185. duration: 2000,
  186. });
  187. setTimeout(() => {
  188. //发送emit
  189. uni.$emit(`updatesupervise-${processId.value}`, res?.data);
  190. uni.navigateBack({
  191. delta: 1,
  192. });
  193. }, 1000);
  194. }
  195. } catch (error: any) {
  196. console.log('表单验证错误:', error);
  197. // 滚动到第一个错误字段
  198. if (error && error[0]?.field) {
  199. const firstErrorField = error[0].field + 'pppp';
  200. paging.value?.scrollIntoViewById(firstErrorField, 30, true);
  201. }
  202. return;
  203. }
  204. },
  205. 1000,
  206. true,
  207. );
  208. };
  209. const processId = ref('');
  210. onLoad((options: any) => {
  211. processId.value = options?.id;
  212. if (+options?.processMedType == 1) {
  213. form.value.part = 'PL-FR-05';
  214. } else if (+options?.processMedType == 2) {
  215. form.value.notIncludePart = 'PL-FR-05';
  216. }
  217. });
  218. //卸载监听
  219. onUnload(() => {
  220. uni.$off(`updatesupervise-${processId.value}`);
  221. });
  222. </script>
  223. <style lang="scss" scoped>
  224. .search-select-item {
  225. height: 86rpx;
  226. background-color: #fff;
  227. border-radius: 10rpx;
  228. box-sizing: border-box;
  229. padding: 12rpx;
  230. }
  231. </style>