cwg-complex-search.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <template>
  2. <view class="complex-search">
  3. <!-- PC/平板端:直接显示表单 -->
  4. <cwg-match-media :min-width="991">
  5. <view class="search-bar search-form">
  6. <view v-for="(row, rowIndex) in displayRows" :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="field.clearable || false"
  17. @change="(e) => field.onChange?.(e) || handleSearch()" />
  18. </template>
  19. <template v-else-if="field.type === 'date'">
  20. <uni-datetime-picker :start="dateLimit.start" :end="dateLimit.end"
  21. v-model="formData[field.key]" type="date"
  22. :placeholder="field.placeholder || '请选择日期'" @change="handleDateChange" />
  23. </template>
  24. <template v-else-if="field.type === 'daterange'">
  25. <uni-datetime-picker :start="dateLimit.start" :end="dateLimit.end"
  26. v-model="formData[field.key]" type="daterange"
  27. :placeholder="field.placeholder || '请选择日期范围'" @change="handleDateChange" />
  28. </template>
  29. <template v-else-if="field.type === 'picker'">
  30. <uni-data-picker style="min-width: 100px" v-model="formData[field.key]"
  31. :localdata="field.options" :popup-title="field.popupTitle || t('State.All')"
  32. :map="field.map || { value: 'value', text: 'label' }"
  33. @change="(e) => field.onChange?.(e)"
  34. @nodeclick="(node) => field.onNodeClick?.(node)" />
  35. </template>
  36. <template v-else-if="field.type === 'number'">
  37. <uni-easyinput v-model="formData[field.key]" type="number"
  38. :placeholder="field.placeholder || '请输入数字'" />
  39. </template>
  40. </view>
  41. </view>
  42. </view>
  43. <view class="form-actions">
  44. <button v-if="hasMoreFields" class="search-btn" @click="toggleShowAll">
  45. <text>{{ showAllFields ? t('Btn.Hide') : t('Btn.More') }}</text>
  46. </button>
  47. <button class="search-btn" @click="handleSearch" v-t="'Btn.Search'" />
  48. </view>
  49. </view>
  50. </cwg-match-media>
  51. <cwg-match-media :max-width="991">
  52. <!-- 移动端:只显示筛选按钮,点击弹出底部抽屉 -->
  53. <view class="mobile-filter">
  54. <view v-if="dateField" class="mobile-date-wrapper">
  55. <uni-datetime-picker :start="dateLimit.start" :end="dateLimit.end" v-model="formData[dateField.key]"
  56. :type="dateField.type === 'daterange' ? 'daterange' : 'date'"
  57. :placeholder="dateField.placeholder || (dateField.type === 'daterange' ? '选择日期范围' : '选择日期')"
  58. @change="handleDateChange" />
  59. </view>
  60. <view v-else class="mobile-date-wrapper"></view>
  61. <button class="filter-chip" @click="openFilterPopup">
  62. <cwg-icon name="cwg-filter" :size="14" color="#141d22" />
  63. <text class="filter-label" v-t="'Documentary.tradingCenter.item3'" />
  64. </button>
  65. </view>
  66. </cwg-match-media>
  67. <cwg-popup v-model:visible="visible" type="center" :mask-click="false" :showFooters="true"
  68. :title="t('Documentary.tradingCenter.item3')" class="search-dialog">
  69. <scroll-view scroll-y class="drawer-content" style="overflow: visible;">
  70. <view v-for="field in nonDateField" :key="field.key" class="filter-item">
  71. <view class="label">{{ field.label }}</view>
  72. <view class="control">
  73. <template v-if="field.type === 'input'">
  74. <uni-easyinput v-model="tempFormData[field.key]" :placeholder="field.placeholder || '请输入'"
  75. clearable />
  76. </template>
  77. <template v-else-if="field.type === 'select'">
  78. <cwg-combox v-model:value="tempFormData[field.key]" :options="field.options"
  79. :placeholder="field.placeholder || '请选择'" :clearable="false" :placement="'bottom'"
  80. v-if="shouldUseSelect(field)" />
  81. <view class="chip-group" v-else>
  82. <view class="chip-list">
  83. <view v-for="opt in field.options" :key="opt.value" class="chip" :class="{
  84. 'chip-filled': tempFormData[field.key] === opt.value,
  85. 'chip-outlined': tempFormData[field.key] !== opt.value
  86. }" @click="selectChip(field.key, opt.value)">
  87. {{ opt.text }}
  88. </view>
  89. </view>
  90. </view>
  91. </template>
  92. <template v-else-if="field.type === 'number'">
  93. <uni-easyinput v-model="tempFormData[field.key]" type="number"
  94. :placeholder="field.placeholder || '请输入数字'" clearable />
  95. </template>
  96. <template v-else-if="field.type === 'picker'">
  97. <uni-data-picker v-model="tempFormData[field.key]" :localdata="field.options"
  98. :popup-title="field.popupTitle || t('State.All')"
  99. :map="field.map || { value: 'value', text: 'label' }"
  100. @change="(e) => field.onChange?.(e)" @nodeclick="(node) => field.onNodeClick?.(node)" />
  101. </template>
  102. </view>
  103. <view v-if="nonDateField.length == 1" style="height: 25vh;" />
  104. </view>
  105. </scroll-view>
  106. <template #footer>
  107. <!-- <button class="reset-btn" @click="resetTempForm" v-t="'Documentary.tradingCenter.item4'" /> -->
  108. <button class="search-btn" @click="applyFilter" v-t="'Btn.Search'" />
  109. </template>
  110. </cwg-popup>
  111. </view>
  112. </template>
  113. <script setup>
  114. import { ref, computed, watch, onMounted, nextTick, onUnmounted } from 'vue'
  115. import { useI18n } from 'vue-i18n'
  116. const { t } = useI18n()
  117. const props = defineProps({
  118. // 字段配置列表,每个字段包含: key, label, type, options(可选), placeholder(可选)
  119. fields: {
  120. type: Array,
  121. required: true
  122. },
  123. // 每行显示的字段数(PC端),默认3个
  124. columns: {
  125. type: Number,
  126. default: 1
  127. },
  128. // 不设置默认数据
  129. noData: {
  130. type: Boolean,
  131. default: false
  132. },
  133. // 表单初始值 / v-model 绑定值
  134. modelValue: {
  135. type: Object,
  136. default: () => ({})
  137. }
  138. })
  139. const emit = defineEmits(['update:modelValue', 'search', 'reset'])
  140. // 时间选择器限制:今天,近一年
  141. const dateLimit = computed(() => {
  142. const now = new Date()
  143. // 结束日期 = 今天
  144. const endDate = new Date(now)
  145. // 开始日期 = 今天 - 365天
  146. const startDate = new Date(now)
  147. startDate.setDate(startDate.getDate() - 365)
  148. const format = (d) => {
  149. const y = d.getFullYear()
  150. const m = String(d.getMonth() + 1).padStart(2, '0')
  151. const d2 = String(d.getDate()).padStart(2, '0')
  152. return `${y}-${m}-${d2}`
  153. }
  154. return {
  155. start: '',
  156. end: format(endDate)
  157. }
  158. })
  159. // 获取日期字段的默认值(当前月范围:月初 ~ 今天)
  160. const getDefaultDateValue = (field) => {
  161. const now = new Date()
  162. const year = now.getFullYear()
  163. const month = now.getMonth()
  164. // 本月1号
  165. const firstDay = new Date(year, month, 1)
  166. // 今天
  167. const today = new Date()
  168. const formatDate = (date) => {
  169. const y = date.getFullYear()
  170. const m = String(date.getMonth() + 1).padStart(2, '0')
  171. const d = String(date.getDate()).padStart(2, '0')
  172. return `${y}-${m}-${d}`
  173. }
  174. if (field.type === 'daterange') {
  175. return [formatDate(firstDay), formatDate(today)]
  176. } else if (field.type === 'date') {
  177. return formatDate(today)
  178. }
  179. return ''
  180. }
  181. // 表单数据(PC端)
  182. const formData = ref({})
  183. // 移动端临时数据
  184. const tempFormData = ref({})
  185. // 移动端弹窗是否可见
  186. const visible = ref(false)
  187. // 是否显示所有字段
  188. const showAllFields = ref(false)
  189. // 将字段按列数分组(仅用于PC布局)
  190. const formRows = computed(() => {
  191. const rows = []
  192. for (let i = 0; i < props.fields.length; i += props.columns) {
  193. rows.push(props.fields.slice(i, i + props.columns))
  194. }
  195. return rows
  196. })
  197. const winWidth = computed(() => {
  198. console.log(window.innerWidth)
  199. return window.innerWidth
  200. })
  201. // 显示的行(根据 showAllFields 决定)
  202. const displayRows = computed(() => {
  203. if (showAllFields.value) {
  204. return formRows.value
  205. }
  206. // 默认只显示第一行
  207. return winWidth.value>1500 ?formRows.value.slice(0,4):formRows.value.slice(0,3)
  208. })
  209. // 是否有更多字段可以展开
  210. const hasMoreFields = computed(() => {
  211. return formRows.value.length > (winWidth.value > 1500 ? 4 : 3)
  212. })
  213. // 切换显示/隐藏状态
  214. const toggleShowAll = () => {
  215. showAllFields.value = !showAllFields.value
  216. }
  217. const selectChip = (key, value) => {
  218. // 如果点击的是当前已选中的项,可以保持不变,也可以置空(根据需求)
  219. // 此处保持选中该项,不提供取消功能(符合常见筛选行为)
  220. tempFormData.value[key] = value
  221. }
  222. // 初始化表单数据
  223. const initFormData = () => {
  224. const initial = {}
  225. if (!props.noData) {
  226. props.fields.forEach(field => {
  227. // 1. 优先使用外部传入的 modelValue(如果值不为 null 或 undefined)
  228. if (props.modelValue && props.modelValue[field.key] != null) {
  229. initial[field.key] = props.modelValue[field.key]
  230. }
  231. // 2. 其次使用字段配置的 defaultValue
  232. else if (field.defaultValue !== undefined) {
  233. initial[field.key] = field.defaultValue
  234. return
  235. }
  236. // 3. 日期字段特殊处理:默认当前月(即使 modelValue 中存在但为 null/undefined)
  237. else if (field.type === 'date' || field.type === 'daterange') {
  238. initial[field.key] = getDefaultDateValue(field)
  239. }
  240. // 4. select 类型字段如果没有默认值,默认置空,不强制选第一个
  241. else if (field.type === 'select') {
  242. initial[field.key] = null
  243. }
  244. // 5. 其他字段默认为空字符串
  245. else {
  246. initial[field.key] = ''
  247. }
  248. })
  249. }
  250. formData.value = initial
  251. tempFormData.value = JSON.parse(JSON.stringify(initial))
  252. handleSearch()
  253. }
  254. // 获取第一个日期或日期范围字段(用于移动端顶部快捷显示)
  255. const dateField = computed(() => {
  256. return props.fields.find(field => field.type === 'date' || field.type === 'daterange')
  257. })
  258. // 判断字段是否应该使用下拉选择器(用于排序和渲染)
  259. const shouldUseSelect = (field) => {
  260. if (!field.options) return false
  261. return field.options.length > 10 || field.isSelect === true
  262. }
  263. const handleDateChange = (value) => {
  264. formData.value[dateField.value.key] = value
  265. tempFormData.value[dateField.value.key] = value
  266. // 日期变化时自动触发搜索
  267. handleSearch()
  268. }
  269. // 排序:输入框 + 下拉选择器 → 前面;Chip 选择器 → 后面
  270. const nonDateField = computed(() => {
  271. const filtered = props.fields.filter(field => field.type !== 'date' && field.type !== 'daterange')
  272. const priorityFields = []
  273. const otherFields = []
  274. filtered.forEach(field => {
  275. if (field.type === 'input' || field.type === 'number') {
  276. priorityFields.push(field)
  277. }
  278. else if (field.type === 'select') {
  279. if (shouldUseSelect(field)) {
  280. priorityFields.push(field)
  281. } else {
  282. otherFields.push(field)
  283. }
  284. }
  285. else if (field.type === 'picker') {
  286. priorityFields.push(field)
  287. }
  288. else {
  289. otherFields.push(field)
  290. }
  291. })
  292. return [...priorityFields, ...otherFields]
  293. })
  294. const clonePlain = (val) => JSON.parse(JSON.stringify(val ?? {}))
  295. const isEqual = (a, b) => JSON.stringify(a ?? {}) === JSON.stringify(b ?? {})
  296. // 监听外部 modelValue 变化
  297. watch(() => props.modelValue, (newVal) => {
  298. if (!newVal) return
  299. const incoming = clonePlain(newVal)
  300. const next = { ...formData.value }
  301. props.fields.forEach(field => {
  302. if (incoming[field.key] != null) {
  303. next[field.key] = incoming[field.key]
  304. } else if (field.type === 'date' || field.type === 'daterange') {
  305. next[field.key] = getDefaultDateValue(field)
  306. }
  307. })
  308. if (isEqual(next, formData.value)) return
  309. formData.value = next
  310. tempFormData.value = clonePlain(next)
  311. }, { deep: true, immediate: true })
  312. // 监听内部 formData 变化,同步到外部
  313. watch(formData, (newVal) => {
  314. const outgoing = clonePlain(newVal)
  315. if (isEqual(outgoing, props.modelValue)) return
  316. emit('update:modelValue', outgoing)
  317. }, { deep: true })
  318. // 重置表单(清空所有字段)
  319. const resetForm = () => {
  320. const empty = {}
  321. props.fields.forEach(field => {
  322. // 有默认值 → 用默认值
  323. if (field.defaultValue !== undefined) {
  324. empty[field.key] = field.defaultValue
  325. }
  326. // 日期字段 → 用默认日期
  327. else if (field.type === 'date' || field.type === 'daterange') {
  328. empty[field.key] = getDefaultDateValue(field)
  329. }
  330. // select 类型字段如果没有默认值,置空
  331. else if (field.type === 'select') {
  332. empty[field.key] = null
  333. }
  334. // 无默认值 → 清空
  335. else {
  336. empty[field.key] = ''
  337. }
  338. })
  339. formData.value = empty
  340. tempFormData.value = JSON.parse(JSON.stringify(empty)) // 👈 加这行
  341. nextTick(() => {
  342. tempFormData.value = { ...empty }
  343. formData.value = { ...empty }
  344. nextTick(() => {
  345. if (dateField.value) {
  346. handleDateChange(formData.value[dateField.value.key])
  347. }
  348. })
  349. })
  350. emit('reset', empty)
  351. }
  352. // 触发查询
  353. const handleSearch = () => {
  354. emit('search', { ...formData.value })
  355. }
  356. // 移动端:打开弹窗
  357. const openFilterPopup = () => {
  358. // 复制当前表单数据到临时对象
  359. tempFormData.value = JSON.parse(JSON.stringify(formData.value))
  360. visible.value = true
  361. }
  362. const closePopup = () => {
  363. visible.value = false
  364. }
  365. const resetTempForm = () => {
  366. const empty = {}
  367. props.fields.forEach(field => {
  368. // 有默认值 → 用默认值
  369. if (field.defaultValue !== undefined) {
  370. empty[field.key] = field.defaultValue
  371. }
  372. // 日期字段 → 用默认日期
  373. else if (field.type === 'date' || field.type === 'daterange') {
  374. empty[field.key] = getDefaultDateValue(field)
  375. }
  376. // select 类型字段如果没有默认值,置空
  377. else if (field.type === 'select') {
  378. empty[field.key] = null
  379. }
  380. // 无默认值 → 清空
  381. else {
  382. empty[field.key] = ''
  383. }
  384. })
  385. tempFormData.value = empty
  386. formData.value = JSON.parse(JSON.stringify(empty)) // 👈 加这行
  387. // 👇 强制日期组件刷新选中状态
  388. nextTick(() => {
  389. formData.value = { ...empty }
  390. tempFormData.value = { ...empty }
  391. nextTick(() => {
  392. if (dateField.value) {
  393. handleDateChange(formData.value[dateField.value.key])
  394. }
  395. handleSearch()
  396. })
  397. })
  398. }
  399. const applyFilter = () => {
  400. // 将临时数据同步到正式表单
  401. formData.value = JSON.parse(JSON.stringify(tempFormData.value))
  402. closePopup()
  403. handleSearch()
  404. }
  405. // 监听fields变化,重新初始化
  406. watch(() => props.fields, () => {
  407. initFormData()
  408. }, { deep: true, immediate: false })
  409. onMounted(() => {
  410. // 初始化表单数据
  411. initFormData()
  412. })
  413. </script>
  414. <style lang="scss" scoped>
  415. @import "@/uni.scss";
  416. .complex-search {
  417. width: 100%;
  418. }
  419. .search-bar {
  420. .cwg-combox,
  421. .uni-easyinput{
  422. width: px2rpx(240) !important;
  423. flex: none;
  424. }
  425. :deep(.uni-easyinput__content) {
  426. height: px2rpx(35) !important;
  427. min-height: px2rpx(35) !important;
  428. }
  429. :deep(.uni-easyinput__content-input) {
  430. height: px2rpx(35) !important;
  431. }
  432. .uni-date {
  433. width: px2rpx(250) !important;
  434. flex: none;
  435. }
  436. .form-actions {
  437. display: flex;
  438. justify-content: flex-end;
  439. align-items: center;
  440. gap: px2rpx(10);
  441. .reset-btn,
  442. .search-btn {
  443. background-color: var(--btn-color);
  444. height: px2rpx(35);
  445. min-height: px2rpx(35);
  446. line-height: px2rpx(35);
  447. padding: 0 px2rpx(16);
  448. margin: 0;
  449. box-sizing: border-box;
  450. font-size: px2rpx(14);
  451. }
  452. .toggle-btn {
  453. display: inline-flex;
  454. align-items: center;
  455. gap: px2rpx(4);
  456. background: transparent;
  457. border: none;
  458. color: #666;
  459. font-size: px2rpx(14);
  460. line-height: px2rpx(35);
  461. padding: 0 px2rpx(12);
  462. cursor: pointer;
  463. &::after {
  464. border: none;
  465. }
  466. &:active {
  467. opacity: 0.7;
  468. }
  469. }
  470. }
  471. }
  472. .mobile-filter {
  473. margin-bottom: px2rpx(10);
  474. display: flex;
  475. align-items: center;
  476. justify-content: center;
  477. gap: px2rpx(12);
  478. .mobile-date-wrapper {
  479. flex: 1;
  480. }
  481. .filter-chip {
  482. display: inline-flex;
  483. align-items: center;
  484. justify-content: center;
  485. gap: px2rpx(12);
  486. background-color: #ffffff;
  487. border: 1px solid #e5e5e5;
  488. border-radius: px2rpx(4);
  489. padding: px2rpx(0) px2rpx(8);
  490. font-size: px2rpx(16);
  491. font-weight: 500;
  492. color: var(--bs-emphasis-color);
  493. line-height: px2rpx(35);
  494. transition: all 0.2s ease;
  495. box-shadow: none;
  496. .iconfont,
  497. .uni-icons {
  498. font-size: 32rpx;
  499. color: var(--bs-emphasis-color);
  500. }
  501. /* 按下反馈效果 */
  502. &:active {
  503. transform: scale(0.96);
  504. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  505. /* 轻量点击背景色 */
  506. border-color: #b9bfc7;
  507. }
  508. &::after {
  509. border: none;
  510. }
  511. }
  512. }
  513. /* 弹窗整体优化 */
  514. .drawer-content {
  515. padding: px2rpx(20);
  516. border-radius: px2rpx(16);
  517. //min-height: 40vh;
  518. box-sizing: border-box;
  519. .label {
  520. font-size: px2rpx(18);
  521. font-weight: 600;
  522. color: #2c3e50;
  523. margin-bottom: px2rpx(20);
  524. letter-spacing: px2rpx(1);
  525. }
  526. :deep(.uni-scroll-view) {
  527. //min-height: px2rpx(200);
  528. }
  529. :deep(.uni-scroll-view-content) {
  530. display: flex;
  531. flex-direction: column;
  532. gap: px2rpx(20);
  533. overflow: visible;
  534. }
  535. :deep(uni-button) {
  536. line-height: px2rpx(35);
  537. }
  538. /* ========== Chip 胶囊样式 (移动端筛选弹窗) ========== */
  539. .chip-group {
  540. margin-bottom: px2rpx(20);
  541. .chip-list {
  542. display: flex;
  543. flex-wrap: wrap;
  544. gap: px2rpx(16);
  545. }
  546. .chip {
  547. display: inline-flex;
  548. align-items: center;
  549. justify-content: center;
  550. padding: px2rpx(4) px2rpx(12);
  551. border-radius: px2rpx(64);
  552. font-size: px2rpx(14);
  553. font-weight: 500;
  554. transition: all 0.2s ease;
  555. background-color: #ffffff;
  556. border: px2rpx(1) solid #d1d5db;
  557. color: var(--bs-emphasis-color);
  558. cursor: pointer;
  559. &:active {
  560. transform: scale(0.96);
  561. }
  562. /* 填充样式(选中) */
  563. &.chip-filled {
  564. background-color: #cf1322;
  565. border-color: #cf1322;
  566. color: #ffffff;
  567. }
  568. /* 轮廓样式(未选中) */
  569. &.chip-outlined {
  570. background-color: #ffffff;
  571. border-color: #d1d5db;
  572. color: var(--bs-emphasis-color);
  573. }
  574. }
  575. }
  576. }
  577. .search-dialog{
  578. .search-btn{
  579. color: var(--btn-color);
  580. }
  581. }
  582. </style>