cwg-complex-search.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <template>
  2. <view class="complex-search">
  3. <!-- PC/平板端:直接显示表单 -->
  4. <cwg-match-media :min-width="991">
  5. <view v-if="!isMobile" class="search-bar search-form">
  6. <view v-for="(row, rowIndex) in formRows" :key="rowIndex" class="form-row">
  7. <view v-for="field in row" :key="field.key" class="form-item">
  8. <view class="search-bar">
  9. <!-- 根据字段类型渲染不同组件 -->
  10. <template v-if="field.type === 'input'">
  11. <uni-easyinput v-model="formData[field.key]" :placeholder="field.placeholder || '请输入'"
  12. clearable />
  13. </template>
  14. <template v-else-if="field.type === 'select'">
  15. <cwg-combox v-model:value="formData[field.key]" :options="field.options"
  16. :placeholder="field.placeholder || '请选择'" clearable @change="handleSearch" />
  17. </template>
  18. <template v-else-if="field.type === 'date'">
  19. <uni-datetime-picker v-model="formData[field.key]" type="date"
  20. :placeholder="field.placeholder || '请选择日期'" @change="handleDateChange" />
  21. </template>
  22. <template v-else-if="field.type === 'daterange'">
  23. <uni-datetime-picker v-model="formData[field.key]" type="daterange"
  24. :placeholder="field.placeholder || '请选择日期范围'" @change="handleDateChange" />
  25. </template>
  26. <template v-else-if="field.type === 'number'">
  27. <uni-easyinput v-model="formData[field.key]" type="number"
  28. :placeholder="field.placeholder || '请输入数字'" />
  29. </template>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="form-actions">
  34. <button class="reset-btn" @click="resetForm" v-t="'Documentary.tradingCenter.item4'" />
  35. <button class="search-btn" type="primary" @click="handleSearch" v-t="'Btn.Search'" />
  36. </view>
  37. </view>
  38. </cwg-match-media>
  39. <cwg-match-media :max-width="991">
  40. <!-- 移动端:只显示筛选按钮,点击弹出底部抽屉 -->
  41. <view class="mobile-filter">
  42. <view v-if="dateField" class="mobile-date-wrapper">
  43. <uni-datetime-picker v-model="formData[dateField.key]"
  44. :type="dateField.type === 'daterange' ? 'daterange' : 'date'"
  45. :placeholder="dateField.placeholder || (dateField.type === 'daterange' ? '选择日期范围' : '选择日期')"
  46. @change="handleDateChange" />
  47. </view>
  48. <button class="filter-chip" @click="openFilterPopup">
  49. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none"
  50. stroke="#141d22" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
  51. class="filter-icon">
  52. <path
  53. d="M4 4h16v2.172a2 2 0 0 1 -.586 1.414l-4.414 4.414v7l-6 2v-8.5l-4.48 -4.928a2 2 0 0 1 -.52 -1.345v-2.227z">
  54. </path>
  55. </svg>
  56. <text class="filter-label" v-t="'Documentary.tradingCenter.item3'" />
  57. </button>
  58. </view>
  59. </cwg-match-media>
  60. <cwg-popup v-model:visible="visible" type="center" :mask-click="false" :showFooters="true"
  61. :title="t('Documentary.tradingCenter.item3')">
  62. <scroll-view scroll-y class="drawer-content">
  63. <view v-for="field in nonDateField" :key="field.key" class="filter-item">
  64. <view class="label">{{ field.label }}</view>
  65. <view class="control">
  66. <template v-if="field.type === 'input'">
  67. <uni-easyinput v-model="tempFormData[field.key]" :placeholder="field.placeholder || '请输入'"
  68. clearable />
  69. </template>
  70. <template v-else-if="field.type === 'select'">
  71. <uni-data-select v-model:value="tempFormData[field.key]" :localdata="field.options"
  72. :placeholder="field.placeholder || '请选择'" clearable v-if="shouldUseSelect(field)" />
  73. <view class="chip-group" v-else>
  74. <view class="chip-list">
  75. <view v-for="opt in field.options" :key="opt.value" class="chip" :class="{
  76. 'chip-filled': tempFormData[field.key] === opt.value,
  77. 'chip-outlined': tempFormData[field.key] !== opt.value
  78. }" @click="selectChip(field.key, opt.value)">
  79. {{ opt.text }}
  80. </view>
  81. </view>
  82. </view>
  83. </template>
  84. <template v-else-if="field.type === 'number'">
  85. <uni-easyinput v-model="tempFormData[field.key]" type="number"
  86. :placeholder="field.placeholder || '请输入数字'" />
  87. </template>
  88. </view>
  89. </view>
  90. </scroll-view>
  91. <template #footer>
  92. <button class="reset-btn" @click="resetTempForm" v-t="'Documentary.tradingCenter.item4'" />
  93. <button class="search-btn" type="primary" @click="applyFilter" v-t="'Btn.Search'" />
  94. </template>
  95. </cwg-popup>
  96. </view>
  97. </template>
  98. <script setup>
  99. import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
  100. import { useI18n } from 'vue-i18n'
  101. const { t } = useI18n()
  102. const props = defineProps({
  103. // 字段配置列表,每个字段包含: key, label, type, options(可选), placeholder(可选)
  104. fields: {
  105. type: Array,
  106. required: true
  107. },
  108. // 每行显示的字段数(PC端),默认3个
  109. columns: {
  110. type: Number,
  111. default: 1
  112. },
  113. // 表单初始值 / v-model 绑定值
  114. modelValue: {
  115. type: Object,
  116. default: () => ({})
  117. }
  118. })
  119. const emit = defineEmits(['update:modelValue', 'search', 'reset'])
  120. // 获取日期字段的默认值(当前月范围或当天)
  121. const getDefaultDateValue = (field) => {
  122. const now = new Date()
  123. const year = now.getFullYear()
  124. const month = now.getMonth()
  125. const firstDay = new Date(year, month, 1)
  126. const lastDay = new Date(year, month + 1, 0)
  127. const formatDate = (date) => {
  128. const y = date.getFullYear()
  129. const m = String(date.getMonth() + 1).padStart(2, '0')
  130. const d = String(date.getDate()).padStart(2, '0')
  131. return `${y}-${m}-${d}`
  132. }
  133. if (field.type === 'daterange') {
  134. return [formatDate(firstDay), formatDate(lastDay)]
  135. } else if (field.type === 'date') {
  136. // 单日期默认设为今天(也可设为第一天,根据需求调整)
  137. return formatDate(now)
  138. }
  139. return ''
  140. }
  141. // 表单数据(PC端)
  142. const formData = ref({})
  143. // 移动端临时数据
  144. const tempFormData = ref({})
  145. // 移动端弹窗是否可见
  146. const visible = ref(false)
  147. // 将字段按列数分组(仅用于PC布局)
  148. const formRows = computed(() => {
  149. const rows = []
  150. for (let i = 0; i < props.fields.length; i += props.columns) {
  151. rows.push(props.fields.slice(i, i + props.columns))
  152. }
  153. return rows
  154. })
  155. const selectChip = (key, value) => {
  156. // 如果点击的是当前已选中的项,可以保持不变,也可以置空(根据需求)
  157. // 此处保持选中该项,不提供取消功能(符合常见筛选行为)
  158. tempFormData.value[key] = value
  159. }
  160. // 初始化表单数据
  161. const initFormData = () => {
  162. const initial = {}
  163. props.fields.forEach(field => {
  164. // 1. 优先使用外部传入的 modelValue(如果值不为 null 或 undefined)
  165. if (props.modelValue && props.modelValue[field.key] != null) {
  166. initial[field.key] = props.modelValue[field.key]
  167. }
  168. // 2. 其次使用字段配置的 defaultValue
  169. else if (field.defaultValue !== undefined) {
  170. initial[field.key] = field.defaultValue
  171. }
  172. // 3. 日期字段特殊处理:默认当前月(即使 modelValue 中存在但为 null/undefined)
  173. else if (field.type === 'date' || field.type === 'daterange') {
  174. initial[field.key] = getDefaultDateValue(field)
  175. }
  176. // 4. 其他字段默认为空字符串
  177. else {
  178. initial[field.key] = ''
  179. }
  180. })
  181. formData.value = initial
  182. tempFormData.value = JSON.parse(JSON.stringify(initial))
  183. }
  184. // 获取第一个日期或日期范围字段(用于移动端顶部快捷显示)
  185. const dateField = computed(() => {
  186. return props.fields.find(field => field.type === 'date' || field.type === 'daterange')
  187. })
  188. // 判断字段是否应该使用下拉选择器(用于排序和渲染)
  189. const shouldUseSelect = (field) => {
  190. if (!field.options) return false
  191. return field.options.length > 10 || field.isSelect === true
  192. }
  193. const handleDateChange = (value) => {
  194. // 日期变化时自动触发搜索
  195. handleSearch()
  196. }
  197. // 排序:输入框 + 下拉选择器 → 前面;Chip 选择器 → 后面
  198. const nonDateField = computed(() => {
  199. const filtered = props.fields.filter(field => field.type !== 'date' && field.type !== 'daterange')
  200. const priorityFields = []
  201. const otherFields = []
  202. filtered.forEach(field => {
  203. if (field.type === 'input' || field.type === 'number') {
  204. priorityFields.push(field)
  205. }
  206. else if (field.type === 'select') {
  207. if (shouldUseSelect(field)) {
  208. priorityFields.push(field)
  209. } else {
  210. otherFields.push(field)
  211. }
  212. }
  213. else {
  214. otherFields.push(field)
  215. }
  216. })
  217. return [...priorityFields, ...otherFields]
  218. })
  219. // 监听外部 modelValue 变化
  220. watch(() => props.modelValue, (newVal) => {
  221. if (newVal) {
  222. props.fields.forEach(field => {
  223. if (newVal[field.key] != null) {
  224. formData.value[field.key] = newVal[field.key]
  225. } else if (field.type === 'date' || field.type === 'daterange') {
  226. // 当日期字段为 null 或 undefined 时,使用默认值
  227. formData.value[field.key] = getDefaultDateValue(field)
  228. }
  229. })
  230. }
  231. }, { deep: true, immediate: true })
  232. // 监听内部 formData 变化,同步到外部
  233. watch(formData, (newVal) => {
  234. emit('update:modelValue', newVal)
  235. }, { deep: true })
  236. // 重置表单(清空所有字段)
  237. const resetForm = () => {
  238. const empty = {}
  239. props.fields.forEach(field => {
  240. if (field.defaultValue !== undefined) {
  241. empty[field.key] = field.defaultValue
  242. } else if (field.type === 'date' || field.type === 'daterange') {
  243. empty[field.key] = getDefaultDateValue(field)
  244. } else {
  245. empty[field.key] = ''
  246. }
  247. })
  248. formData.value = empty
  249. emit('reset', empty)
  250. }
  251. // 触发查询
  252. const handleSearch = () => {
  253. emit('search', { ...formData.value })
  254. }
  255. // 移动端:打开弹窗
  256. const openFilterPopup = () => {
  257. // 复制当前表单数据到临时对象
  258. tempFormData.value = JSON.parse(JSON.stringify(formData.value))
  259. visible.value = true
  260. }
  261. const closePopup = () => {
  262. visible.value = false
  263. }
  264. const resetTempForm = () => {
  265. const empty = {}
  266. props.fields.forEach(field => {
  267. if (field.defaultValue !== undefined) {
  268. empty[field.key] = field.defaultValue
  269. } else if (field.type === 'date' || field.type === 'daterange') {
  270. empty[field.key] = getDefaultDateValue(field)
  271. } else {
  272. empty[field.key] = ''
  273. }
  274. })
  275. tempFormData.value = empty
  276. }
  277. const applyFilter = () => {
  278. // 将临时数据同步到正式表单
  279. formData.value = JSON.parse(JSON.stringify(tempFormData.value))
  280. closePopup()
  281. handleSearch()
  282. }
  283. // 监听fields变化,重新初始化
  284. watch(() => props.fields, () => {
  285. initFormData()
  286. }, { deep: true, immediate: false })
  287. onMounted(() => {
  288. // 初始化表单数据
  289. initFormData()
  290. })
  291. </script>
  292. <style lang="scss" scoped>
  293. @import "@/uni.scss";
  294. .complex-search {
  295. width: 100%;
  296. }
  297. .search-bar {
  298. .cwg-combox,
  299. .uni-easyinput,
  300. .uni-date {
  301. width: px2rpx(240) !important;
  302. flex: none;
  303. }
  304. .uni-date {
  305. width: px2rpx(250) !important;
  306. flex: none;
  307. }
  308. .form-actions {
  309. display: flex;
  310. justify-content: flex-end;
  311. gap: px2rpx(10);
  312. .reset-btn,
  313. .search-btn {
  314. line-height: px2rpx(35);
  315. font-size: px2rpx(14);
  316. }
  317. }
  318. }
  319. .mobile-filter {
  320. margin-bottom: px2rpx(10);
  321. display: flex;
  322. align-items: center;
  323. justify-content: center;
  324. gap: px2rpx(12);
  325. .mobile-date-wrapper {
  326. flex: 1;
  327. }
  328. .filter-chip {
  329. display: inline-flex;
  330. align-items: center;
  331. justify-content: center;
  332. gap: px2rpx(12);
  333. background-color: #ffffff;
  334. border: 1px solid #e5e5e5;
  335. border-radius: px2rpx(4);
  336. padding: px2rpx(0) px2rpx(16);
  337. font-size: px2rpx(16);
  338. font-weight: 500;
  339. color: #141d22;
  340. line-height: px2rpx(35);
  341. transition: all 0.2s ease;
  342. box-shadow: none;
  343. .iconfont,
  344. .uni-icons {
  345. font-size: 32rpx;
  346. color: #141d22;
  347. }
  348. /* 按下反馈效果 */
  349. &:active {
  350. transform: scale(0.96);
  351. background-color: #f5f7fa;
  352. /* 轻量点击背景色 */
  353. border-color: #b9bfc7;
  354. }
  355. &::after {
  356. border: none;
  357. }
  358. }
  359. }
  360. /* 弹窗整体优化 */
  361. .drawer-content {
  362. padding: px2rpx(20);
  363. border-radius: px2rpx(16);
  364. box-sizing: border-box;
  365. .label {
  366. font-size: px2rpx(18);
  367. font-weight: 600;
  368. color: #2c3e50;
  369. margin-bottom: px2rpx(20);
  370. letter-spacing: px2rpx(1);
  371. }
  372. :deep(.uni-scroll-view-content) {
  373. display: flex;
  374. flex-direction: column;
  375. gap: px2rpx(20);
  376. }
  377. :deep(uni-button) {
  378. line-height: px2rpx(35);
  379. }
  380. /* ========== Chip 胶囊样式 (移动端筛选弹窗) ========== */
  381. .chip-group {
  382. margin-bottom: px2rpx(20);
  383. .chip-list {
  384. display: flex;
  385. flex-wrap: wrap;
  386. gap: px2rpx(16);
  387. }
  388. .chip {
  389. display: inline-flex;
  390. align-items: center;
  391. justify-content: center;
  392. padding: px2rpx(4) px2rpx(12);
  393. border-radius: px2rpx(64);
  394. font-size: px2rpx(14);
  395. font-weight: 500;
  396. transition: all 0.2s ease;
  397. background-color: #ffffff;
  398. border: px2rpx(1) solid #d1d5db;
  399. color: #141d22;
  400. cursor: pointer;
  401. &:active {
  402. transform: scale(0.96);
  403. }
  404. /* 填充样式(选中) */
  405. &.chip-filled {
  406. background-color: #007aff;
  407. border-color: #007aff;
  408. color: #ffffff;
  409. }
  410. /* 轮廓样式(未选中) */
  411. &.chip-outlined {
  412. background-color: #ffffff;
  413. border-color: #d1d5db;
  414. color: #141d22;
  415. }
  416. }
  417. }
  418. }
  419. </style>