index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <z-paging ref="paging" v-model="list" bgColor="#fff" @query="query" :auto="false" safe-area-inset-bottom>
  3. <template #top>
  4. <view class="">
  5. <up-navbar :title="title" @leftClick="navigateBackOrHome()" :fixed="false"></up-navbar>
  6. <view class="pd-20">
  7. <ut-search ref="searchRef" v-model="form.medicineName" @search="changeSeach" @change="changeSeach" margin="0" :border="false" :placeholder="placeholder" bgColor="#f7f7f7" height="86rpx" borderRadius="16rpx"></ut-search>
  8. </view>
  9. </view>
  10. </template>
  11. <template v-if="list.length == 0 && !form.medicineName.trim()">
  12. <view class="pd-20">
  13. <view class="f-s-32 c-333 f-w-5 mb-20">已配置的主营品种</view>
  14. <!-- speLable 组件使用示例 -->
  15. <view class="d-flex a-c f-w-w">
  16. <spe-lable class="mr-20 mb-20" v-for="(item, index) in speArray" :key="index" :text="item?.medicineName" size="30rpx" :id="item?.id" @close="() => handleLabelClose(item?.id)" />
  17. </view>
  18. <ut-empty class="mg-at" v-if="speArray.length == 0">
  19. <view class="d-flex a-c j-c f-s-28 c-ccc">暂未配置单位主营品种 </view>
  20. <view class="d-flex a-c j-c f-s-28 c-ccc">点击上方搜索框搜索后添加~</view>
  21. </ut-empty>
  22. </view>
  23. </template>
  24. <template v-for="(item, index) in list" :key="index">
  25. <spe-list :text="item?.medicineName" :searchText="form.medicineName" :check="item?.isChoice" @update:check="(value) => handleCheckChange(value, item?.medicineCode)"> </spe-list>
  26. </template>
  27. <template #empty>
  28. <ut-empty class="mg-at">
  29. <view class="d-flex a-c j-c f-s-28 c-ccc">暂未搜索到该品种</view>
  30. </ut-empty>
  31. </template>
  32. <template #bottom v-if="form.medicineName.trim()">
  33. <view class="base-bottom-wrap pd-20 pb-0">
  34. <up-button type="primary" @click="subMit">确认</up-button>
  35. </view>
  36. </template>
  37. </z-paging>
  38. <ut-confirm-dialog v-model:show="showDeleteDialog" title="删除确认" :confirmText="'删除'" :cancelText="'取消'" @confirm="handleDeleteConfirm" @cancel="handleDeleteCancel">
  39. <text>确定要删除这个品种吗?删除后不可恢复。</text>
  40. </ut-confirm-dialog>
  41. </template>
  42. <script setup lang="ts">
  43. import { useClientRequest } from '@/utils/request';
  44. import speLable from '../models/speLable.vue';
  45. import SpeList from '../models/speList.vue';
  46. interface ListItem {
  47. rows: SpeArrayItem[];
  48. code: number;
  49. msg: string;
  50. total: number;
  51. }
  52. //定义speArray
  53. interface SpeArrayItem {
  54. id: number;
  55. abbreviation?: string;
  56. createdTime: string | null;
  57. creator: string | null;
  58. delFlag: number;
  59. genusLatinName: string;
  60. genusName: string;
  61. isChoice: string; // 或改为 boolean
  62. latinName: string;
  63. medicineName: string;
  64. modifier: string | null;
  65. sourceType: number;
  66. updateTime: string | null;
  67. medicineCode?: string | null;
  68. }
  69. const list = ref<SpeArrayItem[]>([]);
  70. const paging = ref();
  71. const title = ref('配置单位主营品种');
  72. const placeholder = ref('请搜索药材品种名称选择添加');
  73. const form = ref({
  74. medicineName: '',
  75. });
  76. const speArray = ref<any[]>([]);
  77. // 删除确认相关状态
  78. const showDeleteDialog = ref(false);
  79. const currentDeleteId = ref<number | null>(null);
  80. const getSpecies = async () => {
  81. const res = await useClientRequest.get('/plt-api/app/cpyVariety/list');
  82. if (res.code === 200) {
  83. speArray.value = res.data;
  84. speArray.value.forEach((item) => {
  85. item.isChoice = '1';
  86. });
  87. }
  88. };
  89. // 处理标签关闭事件
  90. const handleLabelClose = (id: string | number) => {
  91. currentDeleteId.value = id as number;
  92. showDeleteDialog.value = true;
  93. };
  94. const handleCheckChange = (newCheckValue: string, medicineCode: string | null) => {
  95. // 根据ID找到对应的列表项并更新
  96. const item = list.value.find((item) => item.medicineCode === medicineCode);
  97. if (item) {
  98. item.isChoice = newCheckValue;
  99. }
  100. speArray.value = speArray.value.filter((item) => item.medicineCode !== medicineCode);
  101. console.log(speArray.value, 'speArray.value');
  102. };
  103. const query = async (pageNum: number, pageSize: number) => {
  104. const params = {
  105. pageNum,
  106. pageSize,
  107. ...form.value,
  108. };
  109. if (!params.medicineName?.trim()) {
  110. // 处理空、null、undefined、纯空格的情况
  111. return;
  112. }
  113. // const res = await cpyList(params);
  114. const res: ListItem = await useClientRequest.get<ListItem>('/plt-api/app/medicine/pageList', params);
  115. const { rows } = res;
  116. // 给列表项添加 isChoice 属性
  117. paging.value.complete(rows);
  118. };
  119. const changeSeach = () => {
  120. paging.value.reload();
  121. };
  122. const onRefresh = () => {
  123. paging.value.reload();
  124. };
  125. const subMit = async () => {
  126. // 方法1:使用 Set 避免重复,更高效
  127. const existingIds = new Set(speArray.value.map((speItem) => speItem.medicineCode));
  128. list.value.forEach((item) => {
  129. console.log(item);
  130. if (item.isChoice === '1' && !existingIds.has(item.medicineCode)) {
  131. speArray.value.push({ medicineName: item?.medicineName, medicineCode: item?.medicineCode, isChoice: '1' });
  132. }
  133. });
  134. console.log(speArray.value, 'speArray.value');
  135. // 直接使用过滤后的 speArray 中的所有 id
  136. await useClientRequest.post('/plt-api/app/cpyVariety/add', {
  137. medicineCodes: speArray.value.map((item) => item.medicineCode),
  138. });
  139. form.value.medicineName = '';
  140. onRefresh();
  141. getSpecies();
  142. };
  143. // 处理删除确认
  144. const handleDeleteConfirm = async () => {
  145. if (currentDeleteId.value !== null) {
  146. await useClientRequest.get(`/plt-api/app/cpyVariety/delById/${currentDeleteId.value}`);
  147. getSpecies();
  148. }
  149. showDeleteDialog.value = false;
  150. currentDeleteId.value = null;
  151. };
  152. // 处理删除取消
  153. const handleDeleteCancel = () => {
  154. showDeleteDialog.value = false;
  155. currentDeleteId.value = null;
  156. };
  157. onMounted(() => {
  158. getSpecies();
  159. });
  160. </script>