@ysgroup/ui 0.1.48 → 0.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ysgroup/ui",
3
- "version": "0.1.48",
3
+ "version": "0.1.50",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -25,7 +25,7 @@
25
25
  "access": "public"
26
26
  },
27
27
  "devDependencies": {
28
- "@ysgroup/contracts": "^0.1.50",
28
+ "@ysgroup/contracts": "^0.1.51",
29
29
  "@ysgroup/core": "^0.1.28",
30
30
  "@ysgroup/theme": "^0.3.1",
31
31
  "docx-preview": "^0.4.1"
@@ -0,0 +1,236 @@
1
+ <!-- YsForcedPasswordChange —— 登录后强制修改密码的整屏拦截页。
2
+ 登录回执的 user.must_change_password 为真时(密码不是本人设的:初始密码、管理员重置后、没有本人改密记录)
3
+ 由 App.vue 代替主界面显示:不能关闭,只能改密码或退出登录。服务端(ys_base)同时只放行改密和退出,
4
+ 绕过这个界面也调不动业务事件。改密请求经本系统转给 Auth,成功后 Auth 撤销该账号全部会话,
5
+ 父组件回到登录页提示用新密码登录。
6
+ 不用 YsDialog:它没有「不可关闭」的形态,这里是登录后的拦截页而不是普通弹窗。 -->
7
+ <script setup lang="ts">
8
+ import { computed, nextTick, reactive, ref } from "vue";
9
+ import type { FormInstance, FormRules } from "element-plus";
10
+ import { formatError } from "@ysgroup/core";
11
+
12
+ const props = defineProps<{
13
+ /** 系统名称,显示在标题上方。 */
14
+ systemName: string;
15
+ /** 称呼:主体名称或账号。 */
16
+ username: string;
17
+ /** user.password_change_reason:initial / reset / unknown,其他值按 unknown。 */
18
+ reason?: string;
19
+ /** 改密码:通常是 store.changeMyPassword;失败时抛错,错误文字显示在本页。 */
20
+ changePassword: (currentPassword: string, newPassword: string) => Promise<void>;
21
+ }>();
22
+ const emit = defineEmits<{ logout: [] }>();
23
+
24
+ const REASONS: Record<string, string> = {
25
+ initial: "您正在使用管理员为您设置的初始密码。",
26
+ reset: "管理员已为您重置密码,您当前使用的仍是管理员设置的密码。",
27
+ unknown: "系统中没有您本人修改密码的记录,您的密码可能已长期未更新。",
28
+ };
29
+ const reasonText = computed(() => REASONS[props.reason ?? ""] ?? REASONS.unknown);
30
+
31
+ const form = reactive({ current: "", next: "", confirm: "" });
32
+ const formRef = ref<FormInstance>();
33
+ const submitting = ref(false);
34
+ const error = ref("");
35
+
36
+ /** 新密码的问题,与 Auth 的强度规则一致;没问题返回空串。 */
37
+ function passwordProblem(password: string): string {
38
+ if (password.length < 8) return "新密码至少 8 位";
39
+ if (!/[A-Z]/.test(password)) return "新密码须至少包含一个大写字母";
40
+ if (!/[a-z]/.test(password)) return "新密码须至少包含一个小写字母";
41
+ if (!/\d/.test(password)) return "新密码须至少包含一个数字";
42
+ if (password === form.current) return "新密码不能与当前密码相同";
43
+ return "";
44
+ }
45
+
46
+ type Check = (rule: unknown, value: string, callback: (error?: Error) => void) => void;
47
+ const strong: Check = (_rule, value, callback) => {
48
+ const problem = passwordProblem(String(value ?? ""));
49
+ callback(problem ? new Error(problem) : undefined);
50
+ };
51
+ const same: Check = (_rule, value, callback) =>
52
+ callback(value === form.next ? undefined : new Error("两次输入的新密码不一致"));
53
+ const rules: FormRules = {
54
+ current: [{ required: true, message: "请输入当前密码", trigger: "blur" }],
55
+ next: [
56
+ { required: true, message: "请输入新密码", trigger: "blur" },
57
+ { validator: strong, trigger: "blur" },
58
+ ],
59
+ confirm: [
60
+ { required: true, message: "请再次输入新密码", trigger: "blur" },
61
+ { validator: same, trigger: "blur" },
62
+ ],
63
+ };
64
+
65
+ /** 提交前再完整查一遍(不只靠表单项的失焦校验),问题写在顶部。 */
66
+ function formProblem(): string {
67
+ if (!form.current || !form.next || !form.confirm) return "请完整填写密码信息";
68
+ return passwordProblem(form.next) || (form.next === form.confirm ? "" : "两次输入的新密码不一致");
69
+ }
70
+
71
+ async function submit() {
72
+ if (submitting.value) return;
73
+ error.value = "";
74
+ const valid = await formRef.value?.validate().catch(() => false);
75
+ const problem = formProblem();
76
+ if (!valid || problem) {
77
+ error.value = problem;
78
+ return;
79
+ }
80
+ submitting.value = true;
81
+ try {
82
+ await props.changePassword(form.current, form.next);
83
+ } catch (e) {
84
+ error.value = formatError(e, "修改密码失败,请稍后再试");
85
+ form.current = "";
86
+ void nextTick(() => formRef.value?.clearValidate("current"));
87
+ } finally {
88
+ submitting.value = false;
89
+ }
90
+ }
91
+ </script>
92
+
93
+ <template>
94
+ <div class="ys-forced-password">
95
+ <section
96
+ class="ys-forced-password__card"
97
+ role="dialog"
98
+ aria-modal="true"
99
+ aria-labelledby="ys-forced-password-title"
100
+ >
101
+ <header>
102
+ <p class="ys-forced-password__system">{{ systemName }}</p>
103
+ <h2 id="ys-forced-password-title">为了您的账号安全,请先修改密码</h2>
104
+ </header>
105
+ <div class="ys-forced-password__intro">
106
+ <p>
107
+ <b>{{ username }}</b
108
+ >,您好。{{ reasonText }}
109
+ </p>
110
+ <p>
111
+ 为防止账号被他人冒用,请设置一个只有您自己知道的新密码,完成后即可继续使用。
112
+ </p>
113
+ </div>
114
+
115
+ <el-alert
116
+ v-if="error"
117
+ :title="error"
118
+ type="error"
119
+ :closable="false"
120
+ show-icon
121
+ class="ys-forced-password__error"
122
+ />
123
+
124
+ <el-form
125
+ ref="formRef"
126
+ :model="form"
127
+ :rules="rules"
128
+ label-position="top"
129
+ :disabled="submitting"
130
+ @submit.prevent="submit"
131
+ >
132
+ <el-form-item label="当前密码" prop="current">
133
+ <el-input
134
+ v-model="form.current"
135
+ type="password"
136
+ show-password
137
+ autocomplete="current-password"
138
+ />
139
+ </el-form-item>
140
+ <el-form-item label="新密码" prop="next">
141
+ <el-input
142
+ v-model="form.next"
143
+ type="password"
144
+ show-password
145
+ autocomplete="new-password"
146
+ />
147
+ </el-form-item>
148
+ <el-form-item label="确认新密码" prop="confirm">
149
+ <el-input
150
+ v-model="form.confirm"
151
+ type="password"
152
+ show-password
153
+ autocomplete="new-password"
154
+ @keyup.enter="submit"
155
+ />
156
+ </el-form-item>
157
+ </el-form>
158
+
159
+ <ul class="ys-forced-password__tips">
160
+ <li>
161
+ 新密码至少 8 位,需同时包含大写字母、小写字母和数字,且不能与当前密码相同;
162
+ </li>
163
+ <li>请勿使用生日、手机号或与其他网站相同的密码;</li>
164
+ <li>修改成功后需要用新密码重新登录,各业务系统统一使用这个新密码。</li>
165
+ </ul>
166
+
167
+ <footer>
168
+ <el-button text :disabled="submitting" @click="emit('logout')"
169
+ >退出登录</el-button
170
+ >
171
+ <el-button type="primary" :loading="submitting" @click="submit"
172
+ >修改密码</el-button
173
+ >
174
+ </footer>
175
+ <p class="ys-forced-password__help">如忘记当前密码,请联系管理员重置。</p>
176
+ </section>
177
+ </div>
178
+ </template>
179
+
180
+ <style scoped>
181
+ .ys-forced-password {
182
+ position: fixed;
183
+ inset: 0;
184
+ z-index: 2000;
185
+ display: flex;
186
+ align-items: center;
187
+ justify-content: center;
188
+ padding: 24px;
189
+ overflow: auto;
190
+ background: var(--ys-color-bg-2, var(--el-bg-color-page));
191
+ }
192
+ .ys-forced-password__card {
193
+ width: min(480px, 100%);
194
+ padding: 28px 32px 20px;
195
+ border: 1px solid var(--el-border-color-lighter);
196
+ border-radius: 12px;
197
+ background: var(--el-bg-color);
198
+ box-shadow: var(--el-box-shadow-light);
199
+ }
200
+ .ys-forced-password__system {
201
+ margin: 0 0 6px;
202
+ color: var(--el-text-color-secondary);
203
+ font-size: 13px;
204
+ }
205
+ .ys-forced-password h2 {
206
+ margin: 0 0 14px;
207
+ font-size: 20px;
208
+ color: var(--el-text-color-primary);
209
+ }
210
+ .ys-forced-password__intro p {
211
+ margin: 0 0 8px;
212
+ line-height: 1.7;
213
+ color: var(--el-text-color-regular);
214
+ }
215
+ .ys-forced-password__error {
216
+ margin: 8px 0 12px;
217
+ }
218
+ .ys-forced-password__tips {
219
+ margin: 4px 0 16px;
220
+ padding-left: 18px;
221
+ color: var(--el-text-color-secondary);
222
+ font-size: 12px;
223
+ line-height: 1.8;
224
+ }
225
+ .ys-forced-password footer {
226
+ display: flex;
227
+ justify-content: space-between;
228
+ align-items: center;
229
+ }
230
+ .ys-forced-password__help {
231
+ margin: 12px 0 0;
232
+ color: var(--el-text-color-secondary);
233
+ font-size: 12px;
234
+ text-align: center;
235
+ }
236
+ </style>
@@ -82,6 +82,10 @@ defineSlots<{
82
82
 
83
83
  /** 收起动画结束后再恢复全部选项,收起过程中列表不跳。 */
84
84
  const CLOSE_RESET_MS = 300;
85
+ /** 下拉最宽 560px(不超出视口),选项文字一般完整显示;更长的才省略、悬停看全。 */
86
+ const POPPER_MAX_WIDTH = 560;
87
+ /** 选项左右内边距、名称与说明的间距、滚动条,按 Element Plus 下拉选项的样式估算。 */
88
+ const OPTION_CHROME = 20 + 32 + 12 + 10;
85
89
 
86
90
  function fieldOf(option: T, key: string): unknown {
87
91
  return option !== null && typeof option === "object" ? (option as Record<string, unknown>)[key] : option;
@@ -148,9 +152,45 @@ function OptionText(scope: { entry: Entry }): VNode[] {
148
152
  return nodes;
149
153
  }
150
154
 
155
+ /*
156
+ * 虚拟滚动的下拉(el-select-v2)默认和输入框一样宽,名称和右侧说明会被截断。
157
+ * 打开时按全部选项「名称 + 说明」的文字宽度算出下拉宽度:至少和输入框一样宽,最多 POPPER_MAX_WIDTH。
158
+ * 普通下拉(el-select)本来就按内容撑开,不用算。
159
+ */
160
+ const popperWidth = ref<number | undefined>(undefined);
161
+ let measured: { entries: Entry[]; font: string; width: number } | undefined;
162
+ function contentWidth(font: string, hintFont: string): number {
163
+ if (measured && measured.entries === entries.value && measured.font === font) return measured.width;
164
+ const ctx = typeof document === "undefined" ? null : document.createElement("canvas").getContext("2d");
165
+ if (!ctx) return 0;
166
+ let width = 0;
167
+ for (const entry of entries.value) {
168
+ ctx.font = font;
169
+ let line = ctx.measureText(entry.label).width;
170
+ if (entry.hint) {
171
+ ctx.font = hintFont;
172
+ line += ctx.measureText(entry.hint).width;
173
+ }
174
+ width = Math.max(width, line);
175
+ }
176
+ measured = { entries: entries.value, font, width: width + OPTION_CHROME };
177
+ return measured.width;
178
+ }
179
+ function fitPopper() {
180
+ const el = (inner.value as { $el?: HTMLElement } | null)?.$el;
181
+ if (!virtual.value || !el || typeof window === "undefined") return;
182
+ const style = getComputedStyle(el);
183
+ const family = style.fontFamily || "sans-serif";
184
+ const content = contentWidth(`14px ${family}`, `12px ${family}`);
185
+ if (!content || !el.offsetWidth) return;
186
+ const limit = Math.min(POPPER_MAX_WIDTH, window.innerWidth - 24);
187
+ popperWidth.value = Math.round(Math.max(el.offsetWidth, Math.min(content, limit)));
188
+ }
189
+
151
190
  let resetTimer: ReturnType<typeof setTimeout> | undefined;
152
191
  function onVisibleChange(visible: boolean) {
153
192
  clearTimeout(resetTimer);
193
+ if (visible) fitPopper();
154
194
  if (!visible) resetTimer = setTimeout(reset, CLOSE_RESET_MS);
155
195
  emit("visible-change", visible);
156
196
  }
@@ -181,6 +221,7 @@ defineExpose({
181
221
  :no-data-text="emptyText"
182
222
  no-match-text="无匹配数据"
183
223
  :popper-class="popper"
224
+ :fit-input-width="popperWidth ?? true"
184
225
  @update:model-value="emit('update:modelValue', $event)"
185
226
  @change="emit('change', $event)"
186
227
  @visible-change="onVisibleChange"
package/src/index.ts CHANGED
@@ -23,6 +23,7 @@ export { default as YsPageHeader } from "./components/YsPageHeader.vue";
23
23
  export { default as YsLoginPage } from "./components/YsLoginPage.vue";
24
24
  export { default as YsServiceStatus } from "./components/YsServiceStatus.vue";
25
25
  export { default as YsSettingsCenterDialog } from "./components/YsSettingsCenterDialog.vue";
26
+ export { default as YsForcedPasswordChange } from "./components/YsForcedPasswordChange.vue";
26
27
  export { default as YsPortalGrid } from "./components/YsPortalGrid.vue";
27
28
  export { default as YsRoleTag } from "./components/YsRoleTag.vue";
28
29
  export { default as YsAmount } from "./components/YsAmount.vue";