cwg-complex-search.vue 20 KB

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