code refactor

This commit is contained in:
MaysWind
2025-01-05 23:54:35 +08:00
parent ad1eec7d47
commit f56bef40d8
4 changed files with 28 additions and 20 deletions
+5 -5
View File
@@ -24,7 +24,7 @@ import { type Ref, ref, computed, watch, useTemplateRef } from 'vue';
interface PinCode { interface PinCode {
value: string; value: string;
inputType: string; inputType: string;
inputTimer: number | null; inputTimer: unknown | null;
focused: boolean; focused: boolean;
} }
@@ -129,7 +129,7 @@ function setInputType(index: number): void {
} }
function setFocus(index: number): void { function setFocus(index: number): void {
if (pinCodeInputs.value[index]) { if (pinCodeInputs.value && pinCodeInputs.value[index]) {
pinCodeInputs.value[index].focus(); pinCodeInputs.value[index].focus();
pinCodeInputs.value[index].select(); pinCodeInputs.value[index].select();
} }
@@ -239,13 +239,13 @@ function onPaste(index: number, event: ClipboardEvent): void {
event.preventDefault(); event.preventDefault();
} }
function onInput(index: number, event: InputEvent): void { function onInput(index: number, event: Event | { target: { value: string }, preventDefault: () => void }): void {
if (!event.target.value) { if (!event.target || !(event.target as { value: string }).value) {
event.preventDefault(); event.preventDefault();
return; return;
} }
autoFillText(index, event.target.value); autoFillText(index, (event.target as { value: string }).value);
event.preventDefault(); event.preventDefault();
} }
+12 -12
View File
@@ -38,23 +38,23 @@ const titleContent: Ref<string> = ref(props.title || tt('global.app.title'));
const textContent: Ref<string> = ref(props.text || ''); const textContent: Ref<string> = ref(props.text || '');
const finalColor: Ref<string> = ref(props.color || 'primary'); const finalColor: Ref<string> = ref(props.color || 'primary');
let resolveFunc: (value: T | PromiseLike<T>) => void = null; let resolveFunc: ((value?: unknown) => void) | null = null;
let rejectFunc: (reason?: unknown) => void = null; let rejectFunc: ((reason?: unknown) => void) | null = null;
function open(title: string, text: string, options: Record<string, unknown>) { function open(titleOrText: string, textOrOptions: string | Record<string, unknown>, options: Record<string, unknown>) {
showState.value = true; showState.value = true;
if (isString(text)) { if (isString(textOrOptions)) { // second parameter is text
titleContent.value = tt(title, options); titleContent.value = tt(titleOrText, options);
textContent.value = tt(text, options); textContent.value = tt(textOrOptions, options);
} else { } else { // second parameter is options
options = text; const actualOptions = textOrOptions as Record<string, unknown>;
titleContent.value = tt('global.app.title'); titleContent.value = tt('global.app.title');
textContent.value = tt(title, options); textContent.value = tt(titleOrText, actualOptions);
} }
if (options && options.color) { if (options && isString(options.color)) {
finalColor.value = options.color || 'primary'; finalColor.value = (options.color as string) || 'primary';
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -81,7 +81,7 @@ function cancel(): void {
emit('update:show', false); emit('update:show', false);
} }
watch(() => showState, (newValue) => { watch(showState, newValue => {
emit('update:show', newValue); emit('update:show', newValue);
}); });
+7 -1
View File
@@ -11,6 +11,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { type Ref, ref, watch } from 'vue'; import { type Ref, ref, watch } from 'vue';
import { isObject } from '@/lib/common.ts';
import { useI18n } from '@/lib/i18n.js'; import { useI18n } from '@/lib/i18n.js';
const emit = defineEmits<{ const emit = defineEmits<{
@@ -29,7 +30,12 @@ function showMessage(message: string, options: Record<string, unknown>): void {
function showError(error: string | { message: string }): void { function showError(error: string | { message: string }): void {
showState.value = true; showState.value = true;
messageContent.value = te(error.message || error);
if (isObject(error) && (error as { message: string }).message) {
messageContent.value = te((error as { message: string }).message);
} else {
messageContent.value = te(error);
}
} }
watch(showState, (newValue) => { watch(showState, (newValue) => {
+4 -2
View File
@@ -32,10 +32,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed, useTemplateRef } from 'vue'; import { computed, useTemplateRef } from 'vue';
import type MapView from '@/components/common/MapView.vue'; import MapView from '@/components/common/MapView.vue';
import type { MapPosition } from '@/lib/map/base.ts'; import type { MapPosition } from '@/lib/map/base.ts';
type MapViewType = InstanceType<typeof MapView>;
const props = defineProps<{ const props = defineProps<{
modelValue?: MapPosition; modelValue?: MapPosition;
show: boolean; show: boolean;
@@ -46,7 +48,7 @@ const emit = defineEmits<{
(e: 'update:show', value: boolean): void (e: 'update:show', value: boolean): void
}>(); }>();
const map = useTemplateRef<MapView>('map'); const map = useTemplateRef<MapViewType>('map');
const geoLocation = computed<MapPosition | undefined>({ const geoLocation = computed<MapPosition | undefined>({
get: () => { get: () => {