cwg-complex-search.vue 19 KB

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