wui-components-v2 1.2.48 → 1.2.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
  import { defineEmits, defineOptions, defineProps, reactive, ref } from 'vue'
3
3
  import { useRouter } from 'uni-mini-router'
4
4
  import { useGlobalToast } from '../../composables/useGlobalToast'
5
- import { getUserConfig, login } from '../../api/login'
5
+ import { getKaptchaToken, getUserConfig, login } from '../../api/login'
6
6
  import { useUser } from '../../composables/useUser'
7
7
  import { useTabbar } from '../../composables/useTabbar'
8
8
  import type { TabbarItem } from '../../composables/useTabbar'
@@ -30,11 +30,46 @@ const model = reactive<{
30
30
  name: string
31
31
  password: string
32
32
  remember: boolean
33
+ kaptchaText: string
33
34
  }>({
34
35
  name: uni.getStorageSync('USER_NAME') || '',
35
36
  password: uni.getStorageSync('USER_PASSWORD') || '',
36
37
  remember: uni.getStorageSync('REMEMBER') || false,
38
+ kaptchaText: '',
37
39
  })
40
+ // 是否显示验证码
41
+ const showCaptcha = ref(false)
42
+ // 验证码token
43
+ const kaptchaToken = ref('')
44
+ // 验证码图片
45
+ const captchaImg = ref('')
46
+ // 是否正在加载验证码
47
+ const captchaLoading = ref(false)
48
+
49
+ // 获取验证码
50
+ async function handleGetCaptcha() {
51
+ try {
52
+ captchaLoading.value = true
53
+ // 调用 /v3/auth/kaptcha 接口获取验证码
54
+ const res = await getKaptchaToken()
55
+ // 接口返回:{ img: base64图片, kaptchaToken: 验证码token, status: 'success' }
56
+ const data = res?.data || res
57
+ if (data.status === 'success' || data.kaptchaToken) {
58
+ kaptchaToken.value = data.kaptchaToken
59
+ captchaImg.value = data.img
60
+ showCaptcha.value = true
61
+ // 清空之前输入的验证码
62
+ model.kaptchaText = ''
63
+ } else {
64
+ toast.error({ msg: data.message || '验证码获取失败,请重试' })
65
+ }
66
+ } catch (error: any) {
67
+ console.log(error, '获取验证码失败')
68
+ toast.error({ msg: '验证码获取失败,请重试' })
69
+ } finally {
70
+ captchaLoading.value = false
71
+ }
72
+ }
38
73
  const { setTabbarItemActive } = useTabbar(props.tabbarItems)
39
74
  const form = ref()
40
75
 
@@ -46,8 +81,13 @@ function handleSubmit() {
46
81
  if (valid) {
47
82
  try {
48
83
  subloading.value = true
49
- // 登录
50
- const res = await login(model.name, model.password)
84
+ // 登录(如果已显示验证码则携带验证码信息)
85
+ const res = await login(
86
+ model.name,
87
+ model.password,
88
+ showCaptcha.value ? kaptchaToken.value : '',
89
+ showCaptcha.value ? model.kaptchaText : ''
90
+ )
51
91
 
52
92
  if (res.token) {
53
93
  // 保存token
@@ -79,10 +119,14 @@ function handleSubmit() {
79
119
  toast.success({ msg: '登录成功!' })
80
120
  }
81
121
  } else {
82
- toast.error({ msg: res.message })
122
+ toast.error({ msg: res.message || res.msg || '账号或密码错误' })
123
+ // 账号或密码错误时,显示并获取验证码
124
+ handleGetCaptcha()
83
125
  }
84
126
  } catch (error: any) {
85
- console.log(error, 'error')
127
+ // 登录接口返回 status: 'error' 时,core/index.ts 会 reject 整个 res
128
+ // 该函数内部已通过 uni.showToast 提示错误,这里仅触发验证码显示
129
+ handleGetCaptcha()
86
130
  }
87
131
  } else {
88
132
  toast.error({ msg: '请填写登录信息' })
@@ -122,10 +166,37 @@ function handleSubmit() {
122
166
  placeholder="请输入密码"
123
167
  :rules="[{ required: true, message: '请填写密码' }]"
124
168
  />
125
- <view class="p-4 pb-0 pt-0 dark:bg-[#1b1b1e]">
126
- <wd-checkbox v-model="model.remember" shape="square">记住密码</wd-checkbox>
127
- </view>
128
169
  </wd-cell-group>
170
+ <!-- 验证码(账号或密码错误时显示) -->
171
+ <wd-cell-group v-if="showCaptcha" border>
172
+ <wd-input
173
+ v-model="model.kaptchaText"
174
+ label="验证码"
175
+ label-width="100px"
176
+ prop="kaptchaText"
177
+ clearable
178
+ placeholder="请输入验证码"
179
+ :rules="[{ required: true, message: '请填写验证码' }]"
180
+ >
181
+ <template #suffix>
182
+ <view class="flex items-center justify-center" @click="handleGetCaptcha">
183
+ <image
184
+ v-if="captchaImg"
185
+ :src="captchaImg"
186
+ class="h-32px w-90px"
187
+ mode="aspectFit"
188
+ />
189
+ <wd-button v-else size="small" :round="false" :loading="captchaLoading">
190
+ 获取验证码
191
+ </wd-button>
192
+ </view>
193
+ </template>
194
+ </wd-input>
195
+ </wd-cell-group>
196
+ <!-- 记住密码 -->
197
+ <view class="p-4 pb-0 pt-0 dark:bg-[#1b1b1e]">
198
+ <wd-checkbox v-model="model.remember" shape="square">记住密码</wd-checkbox>
199
+ </view>
129
200
  </view>
130
201
  <view class="p-4">
131
202
  <wd-button block :loading="subloading" :round="false" @click="handleSubmit">登录</wd-button>
@@ -21,7 +21,12 @@ const theme = computed(() => themeStore.theme)
21
21
  const show = ref(false)
22
22
  const search = ref('')
23
23
  const formControlRef = ref()
24
- const entity = ref({})
24
+ // 外置 preferred 筛选字段的 form-control 实例
25
+ const preferredFormControlRef = ref()
26
+ // 外置 preferred 表单的刷新标识:弹窗提交后递增,强制外置 form-control 重挂载读取最新 entity
27
+ const preferredRefreshKey = ref(0)
28
+ // 共享实体:外置 preferred 字段与弹窗筛选字段通过它进行双向同步
29
+ const entity = ref<{ [key: string]: any }>({})
25
30
  // 判断字段实际渲染的控件类型是否为纯文本输入(优先 extControlType,回退 controlType)
26
31
  const isTextCriterion = (item: Fields) => ControlTypeSupportor.getControlType(item) === 'text'
27
32
  // 用于 wd-search 的搜索字段:primaryCriterion 存在时直接使用,否则取 criteria 中第一个文本类型的筛选项
@@ -33,79 +38,140 @@ const searchCriterion = computed(() => {
33
38
  })
34
39
  // 仅当 searchCriterion 存在时显示搜索框
35
40
  const showSearch = computed(() => searchCriterion.value !== null)
41
+ // tab 筛选字段(split2TabCriterias)的 sourceId,用于从普通筛选中排除
42
+ const split2TabSourceIds = computed(() => props.split2TabCriterias?.map(item => item.sourceId) || [])
43
+ // preferred=true 的字段外置到搜索框上方展示(排除 tab 筛选字段,以及已作为搜索框的 searchCriterion,避免重复展示)
44
+ const preferredCriteria = computed(() => {
45
+ const searchSourceId = showSearch.value ? searchCriterion.value?.sourceId : undefined
46
+ return props.criteria.filter(
47
+ item =>
48
+ item.preferred === true && !split2TabSourceIds.value.includes(item.sourceId) && item.sourceId !== searchSourceId
49
+ )
50
+ })
36
51
  const filterCriteria = computed(() => {
37
- const criteriaSourceIds = props.split2TabCriterias?.map(item => item.sourceId) || []
38
52
  // 仅当搜索框真正显示时,才把 searchCriterion 从筛选弹窗中排除;
39
53
  // 否则(extControlType 存在)该字段应回落到筛选弹窗中参与筛选
40
54
  const searchSourceId = showSearch.value ? searchCriterion.value?.sourceId : undefined
41
- if (!searchSourceId) return props.criteria.filter(item => !criteriaSourceIds.includes(item.sourceId))
42
- return props.criteria.filter(item => ![searchSourceId, ...criteriaSourceIds].includes(item.sourceId))
55
+ // 弹窗筛选需保留 preferred 字段(与外置字段双向绑定),因此不排除 preferredCriteria
56
+ if (!searchSourceId) return props.criteria.filter(item => !split2TabSourceIds.value.includes(item.sourceId))
57
+ return props.criteria.filter(item => ![searchSourceId, ...split2TabSourceIds.value].includes(item.sourceId))
43
58
  })
59
+ // 把外部/弹窗表单返回的数据归一化后再写入 entity:
60
+ // 数组值统一转成逗号分隔字符串,避免弹窗 initFormData 对数组调用 split 报错导致空白
61
+ function normalizeForEntity(data: { [key: string]: any }) {
62
+ const result: { [key: string]: any } = {}
63
+ for (const key in data) {
64
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
65
+ const val = data[key]
66
+ result[key] = Array.isArray(val) ? val.join(',') : val
67
+ }
68
+ }
69
+ return result
70
+ }
71
+ // 打开筛选弹窗前,先把外置 preferred 字段的当前值同步进 entity,
72
+ // 再打开弹窗(await 保证先同步完,弹窗 form-control 重挂载时能读取到最新值,避免竞态空白)
73
+ async function handleShowPopup() {
74
+ if (preferredFormControlRef.value?.submit) {
75
+ try {
76
+ const pdata = await preferredFormControlRef.value.submit()
77
+ Object.assign(entity.value, normalizeForEntity(pdata))
78
+ } catch {
79
+ // 校验失败(如必填未填)时仍保留原 entity,不阻塞弹窗打开
80
+ }
81
+ }
82
+ console.log('[Search] handleShowPopup entity =', entity.value)
83
+ show.value = true
84
+ }
44
85
  function handleClose() {
45
86
  show.value = false
46
87
  }
88
+ // 把合并后的数据对象(fdata)拼接为查询条件字符串
89
+ function buildQueryData(fdata: { [key: string]: any }) {
90
+ let data = ''
91
+ if (showSearch.value && searchCriterion.value?.sourceId) {
92
+ data = `c_${searchCriterion.value.sourceId}=${search.value}`
93
+ }
94
+ filterCriteria.value.forEach(item => {
95
+ const element = fdata[item.sourceId]
96
+ // 空值(空字符串、undefined、null、空数组)仍拼接 `c_${sourceId}=` 空参数,
97
+ // 让父组件识别「该字段已被用户清空」,从而跳过 defaultCriteriaValue 的默认值兜底
98
+ if (element === undefined || element === null || element === '') {
99
+ data = `${data}&c_${item.sourceId}=`
100
+ return
101
+ }
102
+ if (Array.isArray(element) && element.length === 0) {
103
+ data = `${data}&c_${item.sourceId}=`
104
+ return
105
+ }
106
+ // 跳过用户未修改的默认值(时间字段常被自动填充当前时间,不应作为查询条件)
107
+ if (item.transDefaultValue && String(element) === String(item.transDefaultValue)) return
108
+ if (item.extControlType === 'relselectvalue') {
109
+ const elements = Array.isArray(element) ? element : [element]
110
+ data = `${data}&c_${item.sourceId}=${elements
111
+ .map((item: string) => {
112
+ return item.split('@R@')[1]
113
+ })
114
+ .join('@,@')}`
115
+ } else if (item.extControlType === 'multiselect') {
116
+ const elements = Array.isArray(element) ? element : [element]
117
+ elements.forEach((subitem: string) => {
118
+ data = `${data}&c_${item.sourceId}=${subitem}`
119
+ })
120
+ } else {
121
+ data = `${data}&c_${item.sourceId}=${element}`
122
+ }
123
+ })
124
+ return data
125
+ }
126
+ // 对外触发提交(点击搜索/确定时使用)
127
+ function emitSubmit(fdata: { [key: string]: any }) {
128
+ console.log('查询条件', buildQueryData(fdata))
129
+ emits('submit', buildQueryData(fdata))
130
+ handleClose()
131
+ }
47
132
  function handleSubmit() {
48
133
  if (!formControlRef.value) {
49
134
  if (!showSearch.value || !searchCriterion.value?.sourceId) return
50
- const data = `c_${searchCriterion.value.sourceId}=${search.value}`
51
- emits('submit', data)
52
- handleClose()
135
+ emitSubmit({ [searchCriterion.value.sourceId]: search.value })
53
136
  return
54
137
  }
55
138
  formControlRef.value.submit().then((fdata: any) => {
56
- entity.value = {
57
- ...fdata,
58
- ...(showSearch.value && searchCriterion.value?.sourceId
59
- ? { [searchCriterion.value.sourceId]: search.value }
60
- : {}),
61
- }
62
- let data = ''
63
-
139
+ // 双向绑定:弹窗内提交的值(含 preferred 字段)写回 entity,供外置字段同步;
140
+ // 数组值归一化为字符串,避免后续 initFormData 对数组 split 报错
141
+ Object.assign(entity.value, normalizeForEntity(fdata))
64
142
  if (showSearch.value && searchCriterion.value?.sourceId) {
65
- data = `c_${searchCriterion.value.sourceId}=${search.value}`
143
+ entity.value[searchCriterion.value.sourceId] = search.value
66
144
  }
67
-
68
- filterCriteria.value.forEach(item => {
69
- const element = fdata[item.sourceId]
70
- // 空值(空字符串、undefined、null、空数组)仍拼接 `c_${sourceId}=` 空参数,
71
- // 让父组件识别「该字段已被用户清空」,从而跳过 defaultCriteriaValue 的默认值兜底
72
- if (element === undefined || element === null || element === '') {
73
- data = `${data}&c_${item.sourceId}=`
74
- return
75
- }
76
- if (Array.isArray(element) && element.length === 0) {
77
- data = `${data}&c_${item.sourceId}=`
78
- return
79
- }
80
- // 跳过用户未修改的默认值(时间字段常被自动填充当前时间,不应作为查询条件)
81
- if (item.transDefaultValue && String(element) === String(item.transDefaultValue)) return
82
- if (item.extControlType === 'relselectvalue') {
83
- const elements = Array.isArray(element) ? element : [element]
84
- data = `${data}&c_${item.sourceId}=${elements
85
- .map((item: string) => {
86
- return item.split('@R@')[1]
87
- })
88
- .join('@,@')}`
89
- } else if (item.extControlType === 'multiselect') {
90
- const elements = Array.isArray(element) ? element : [element]
91
- elements.forEach((subitem: string) => {
92
- data = `${data}&c_${item.sourceId}=${subitem}`
93
- })
94
- } else {
95
- data = `${data}&c_${item.sourceId}=${element}`
96
- }
97
- })
98
-
99
- console.log('查询条件', data)
100
-
101
- emits('submit', data)
102
- handleClose()
145
+ // 递增刷新标识,强制外置 preferred 表单重挂载并读取最新 entity(弹窗 → 外置同步)
146
+ preferredRefreshKey.value++
147
+ emitSubmit(fdata)
103
148
  })
104
149
  }
105
150
  </script>
106
151
 
107
152
  <template>
108
153
  <wd-config-provider :theme="theme">
154
+ <!-- preferred=true 的筛选项外置在搜索框上方,常驻展示;与弹窗中的同名筛选双向绑定 -->
155
+ <view v-if="preferredCriteria.length" class="w-full box-border pt-2">
156
+ <formControl
157
+ ref="preferredFormControlRef"
158
+ :key="`preferred-${preferredRefreshKey}`"
159
+ :field-group="{
160
+ fields: preferredCriteria,
161
+ readOnly: false,
162
+ displayConfig: [],
163
+ id: '',
164
+ buttons: [],
165
+ title: '',
166
+ type: '',
167
+ pointSourceId: '',
168
+ mstrucId: '',
169
+ relationNames: [],
170
+ }"
171
+ :entity="entity"
172
+ :enum-column="enumColumn"
173
+ />
174
+ </view>
109
175
  <view v-if="showSearch" class="w-full">
110
176
  <wd-search
111
177
  v-model="search"
@@ -120,14 +186,14 @@ function handleSubmit() {
120
186
  name="filter"
121
187
  size="17px"
122
188
  class="text-gray-500 dark:text-[var(--wot-dark-color3)]"
123
- @click="show = true"
189
+ @click="handleShowPopup"
124
190
  />
125
191
  </view>
126
192
  </template>
127
193
  </wd-search>
128
194
  </view>
129
195
  <view v-else-if="filterCriteria.length" class="px-[8px] mt-2">
130
- <wd-button block custom-class="search-filter-btn" @click="show = true">
196
+ <wd-button block custom-class="search-filter-btn" @click="handleShowPopup">
131
197
  <wd-icon name="filter" size="17px" class="mr-1" />
132
198
  筛选
133
199
  </wd-button>
@@ -135,7 +201,9 @@ function handleSubmit() {
135
201
  <wd-popup v-model="show" position="bottom" custom-class="h-80vh" closable @close="handleClose">
136
202
  <view class="relative box-border h-full p-1 bg-white dark:bg-[var(--wot-dark-background)]">
137
203
  <view class="h-34px flex items-center justify-center text-black dark:text-[var(--wot-dark-color)]">筛选</view>
204
+ <!-- v-if="show" 保证每次打开弹窗都重新挂载,读取最新 entity(含外置 preferred 值),实现双向绑定 -->
138
205
  <formControl
206
+ v-if="show"
139
207
  ref="formControlRef"
140
208
  :field-group="{
141
209
  fields: filterCriteria,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wui-components-v2",
3
- "version": "1.2.48",
3
+ "version": "1.2.50",
4
4
  "description": "wui 组件库",
5
5
  "author": "wgxshh",
6
6
  "license": "MIT",
package/type.ts CHANGED
@@ -151,6 +151,8 @@ export interface Fields extends Columns {
151
151
  min?: string
152
152
  comparator?: Comparator
153
153
  rules?: FieldRule | FieldRule[]
154
+ // 为 true 时该筛选项会外置到搜索框上方展示(同时仍保留在筛选弹窗中,并与弹窗双向绑定)
155
+ preferred?: boolean
154
156
  }
155
157
 
156
158
  // 编辑按钮配置