cwg-complex-search.vue 19 KB

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