This commit is contained in:
dyhj625 2025-04-04 12:56:53 +09:00
parent a27eda7124
commit 819a82cd89

View File

@ -38,7 +38,7 @@
:value="form.color"
@update:data="handleColorUpdate"
/>
<span v-if="colorDuplicated" class="text-red-500 invalid-feedback mt-1 d-block">
<span v-if="colorDuplicated" class="text-danger invalid-feedback mt-1 d-block">
이미 사용 중인 컬러입니다.
</span>
</div>
@ -61,8 +61,46 @@
이미 사용 중인 전화번호입니다.
</span>
<!-- 기존 비밀번호 입력 -->
<UserFormInput title="기존 비밀번호" name="currentPw" type="password"
:value="password.current" @update:data="password.current = $event"
@blur="checkCurrentPassword" />
<span v-if="passwordError" class="text-danger invalid-feedback mt-1 d-block">
비밀번호가 일치하지 않습니다.
</span>
<!-- 비밀번호 재설정 -->
<div v-if="showResetPw">
<UserFormInput title="새 비밀번호" name="newPw" type="password"
:value="password.new" @update:data="password.new = $event" />
<span v-if="password.new && password.new.length < 4"
class="text-danger invalid-feedback mt-1 d-block">
비밀번호는 최소 4자리 이상이어야 합니다.
</span>
<span v-if="password.new === password.current"
class="text-danger invalid-feedback mt-1 d-block">
기존 비밀번호와 다르게 설정해주세요.
</span>
<UserFormInput title="비밀번호 확인" name="confirmPw" type="password"
:value="password.confirm" @update:data="password.confirm = $event" />
<span v-if="password.confirm && password.confirm !== password.new"
class="text-danger invalid-feedback mt-1 d-block">
비밀번호와 일치하지 않습니다.
</span>
<div class="d-flex justify-content-end mt-2">
<button type="button" class="btn btn-sm btn-outline-primary"
:disabled="!canResetPassword"
@click="handlePasswordReset">
비밀번호 변경
</button>
</div>
</div>
<div class="d-flex mt-5">
<button type="submit" class="btn btn-primary w-100" :disabled="!isChanged || phoneDuplicated || colorDuplicated">
<button type="submit" class="btn btn-primary w-100"
:disabled="!isChanged || phoneDuplicated || colorDuplicated">
정보 수정
</button>
</div>
@ -73,7 +111,7 @@
</template>
<script setup>
import { ref, onMounted, computed } from 'vue';
import { ref, computed, onMounted } from 'vue';
import $api from '@api';
import UserFormInput from '@c/input/UserFormInput.vue';
import FormSelect from '@c/input/FormSelect.vue';
@ -83,12 +121,9 @@ import { useToastStore } from '@s/toastStore';
const toastStore = useToastStore();
const form = ref({
entryDate: '',
birth: '',
address: { address: '', detailAddress: '', postcode: '' },
phone: '',
color: '',
mbti: ''
entryDate: '', birth: '', phone: '', color: '', mbti: '',
address: { address: '', detailAddress: '', postcode: '' },
id: ''
});
const originalData = ref({});
const profile = ref('');
@ -101,164 +136,161 @@ const phoneDuplicated = ref(false);
const mbtiList = ref([]);
const colorList = ref([]);
const password = ref({ current: '', new: '', confirm: '' });
const passwordError = ref(false);
const showResetPw = ref(false);
const canResetPassword = computed(() => {
return (
password.value.new.length >= 4 &&
password.value.new !== password.value.current &&
password.value.new === password.value.confirm
);
});
const isChanged = computed(() => {
const f = form.value;
const o = originalData.value;
return (
f.entryDate !== o.entryDate ||
f.birth !== o.birth ||
f.phone !== o.phone ||
f.color !== o.color ||
f.mbti !== o.mbti ||
const f = form.value;
const o = originalData.value;
return (
f.entryDate !== o.entryDate || f.birth !== o.birth || f.phone !== o.phone ||
f.color !== o.color || f.mbti !== o.mbti || profileChanged.value ||
f.address.address !== o.address.address ||
f.address.detailAddress !== o.address.detailAddress ||
f.address.postcode !== o.address.postcode ||
profileChanged.value
);
f.address.postcode !== o.address.postcode
);
});
const baseUrl = $api.defaults.baseURL.replace(/api\/$/, '');
const defaultProfile = "img/avatars/default-Profile.jpg";
const getProfileImageUrl = (fileName) => {
return fileName?.trim()
? `${baseUrl}upload/img/profile/${fileName}?t=${Date.now()}`
: defaultProfile;
};
const getProfileImageUrl = (fileName) =>
fileName?.trim() ? `${baseUrl}upload/img/profile/${fileName}?t=${Date.now()}` : defaultProfile;
const profilePreviewStyle = computed(() => ({
width: '100px',
height: '100px',
backgroundImage: `url(${profile.value})`,
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
backgroundPosition: 'center'
width: '100px',
height: '100px',
backgroundImage: `url(${profile.value})`,
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
backgroundPosition: 'center'
}));
const profileUpload = (e) => {
const file = e.target.files[0];
if (!file) return;
if (file.size > 5 * 1024 * 1024 || !['image/jpeg', 'image/png'].includes(file.type)) {
const file = e.target.files[0];
if (!file) return;
if (file.size > 5 * 1024 * 1024 || !['image/jpeg', 'image/png'].includes(file.type)) {
profilerr.value = '5MB 이하의 JPG/PNG 파일만 업로드 가능합니다.';
return;
}
profilerr.value = '';
if (currentBlobUrl.value) URL.revokeObjectURL(currentBlobUrl.value);
uploadedFile.value = file;
const newBlobUrl = URL.createObjectURL(file);
profile.value = newBlobUrl;
currentBlobUrl.value = newBlobUrl;
profileChanged.value = true;
}
profilerr.value = '';
if (currentBlobUrl.value) URL.revokeObjectURL(currentBlobUrl.value);
uploadedFile.value = file;
const newBlobUrl = URL.createObjectURL(file);
profile.value = newBlobUrl;
currentBlobUrl.value = newBlobUrl;
profileChanged.value = true;
};
const onlyNumber = (e) => {
if (!/[0-9]/.test(e.key)) e.preventDefault();
if (!/[0-9]/.test(e.key)) e.preventDefault();
};
const checkPhoneDuplicate = async () => {
const currentPhone = form.value.phone;
if (currentPhone === originalData.value.phone) {
phoneDuplicated.value = false;
return;
}
const res = await $api.get('/user/checkPhone', { params: { memberTel: form.value.phone } });
phoneDuplicated.value = !res.data.data;
const currentPhone = form.value.phone;
phoneDuplicated.value = currentPhone !== originalData.value.phone &&
!(await $api.get('/user/checkPhone', { params: { memberTel: currentPhone } })).data.data;
};
const handleColorUpdate = async (colorVal) => {
form.value.color = colorVal;
if (colorVal !== originalData.value.color) {
const res = await $api.get('/user/checkColor', {
params: { memberCol: colorVal }
});
colorDuplicated.value = res.data.data; // true
} else {
colorDuplicated.value = false;
}
form.value.color = colorVal;
colorDuplicated.value = colorVal !== originalData.value.color &&
(await $api.get('/user/checkColor', { params: { memberCol: colorVal } })).data.data;
};
const formatDate = (isoDate) => (isoDate ? isoDate.split('T')[0] : '');
const checkCurrentPassword = async () => {
if (!password.value.current) return;
const res = await $api.post('/user/checkPassword', {
id: form.value.id,
password: password.value.current
});
passwordError.value = res.data.data;
showResetPw.value = !res.data.data;
};
const handlePasswordReset = async () => {
const res = await $api.patch('/user/pwNew', {
id: form.value.id,
password: password.value.new
});
if (res.data.data) {
toastStore.onToast('비밀번호가 변경되었습니다.', 's');
password.value = { current: '', new: '', confirm: '' };
showResetPw.value = false;
passwordError.value = false;
} else {
toastStore.onToast('비밀번호 변경 실패', 'e');
}
};
const formatDate = (isoDate) => isoDate?.split('T')[0] || '';
const loadInitialData = async () => {
const userRes = await $api.get('/user/userInfo');
const user = userRes.data.data;
const user = (await $api.get('/user/userInfo')).data.data;
const serverColors = (await $api.get('/user/color', { params: { type: 'YON' } })).data.data.map(c => ({
value: c.CMNCODVAL, label: c.CMNCODNAM
}));
const matchedColor = serverColors.find(c => c.label === user.usercolor);
const colorCode = matchedColor ? matchedColor.value : user.color;
colorList.value = serverColors.some(c => c.value === colorCode)
? serverColors
: [{ value: colorCode, label: user.usercolor }, ...serverColors];
const colorRes = await $api.get('/user/color', { params: { type: 'YON' } });
const serverColors = colorRes.data.data.map(c => ({
value: c.CMNCODVAL,
label: c.CMNCODNAM
}));
const matchedColor = serverColors.find(c => c.label === user.usercolor);
const colorCode = matchedColor ? matchedColor.value : user.color;
const exists = serverColors.some(c => c.value === colorCode);
colorList.value = exists ? serverColors : [{ value: colorCode, label: user.usercolor }, ...serverColors];
const initData = {
const initData = {
id: user.loginId,
entryDate: formatDate(user.isCdt),
birth: formatDate(user.birth),
phone: user.phone || '',
color: colorCode,
mbti: user.mbit || '',
address: {
address: user.address || '',
detailAddress: user.addressDetail || '',
postcode: user.zipcode || ''
},
phone: user.phone || '',
color: colorCode,
mbti: user.mbit || ''
};
}
};
form.value = { ...initData };
originalData.value = { ...initData };
profile.value = getProfileImageUrl(user.profile);
profileChanged.value = false;
form.value = { ...initData };
originalData.value = { ...initData };
profile.value = getProfileImageUrl(user.profile);
profileChanged.value = false;
const mbtiRes = await $api.get('/user/mbti');
mbtiList.value = mbtiRes.data.data.map(m => ({
value: m.CMNCODVAL,
label: m.CMNCODNAM
}));
const mbtiRes = await $api.get('/user/mbti');
mbtiList.value = mbtiRes.data.data.map(m => ({ value: m.CMNCODVAL, label: m.CMNCODNAM }));
};
const handleSubmit = async () => {
const formData = new FormData();
formData.append('entryDate', form.value.entryDate);
formData.append('birth', form.value.birth);
formData.append('phone', form.value.phone);
formData.append('color', form.value.color);
formData.append('mbti', form.value.mbti);
formData.append('address', form.value.address.address);
formData.append('detailAddress', form.value.address.detailAddress);
formData.append('postcode', form.value.address.postcode);
if (uploadedFile.value) formData.append('profileFile', uploadedFile.value);
//
if (form.value.color !== originalData.value.color) {
if (form.value.color) {
await $api.patch('/user/updateColorYon', {
color: form.value.color,
type: 'YON'
});
}
if (originalData.value.color) {
await $api.patch('/user/updateColorChange', {
color: originalData.value.color,
type: 'YON'
});
}
const formData = new FormData();
Object.entries(form.value).forEach(([k, v]) => {
if (typeof v === 'object') {
formData.append('address', v.address);
formData.append('detailAddress', v.detailAddress);
formData.append('postcode', v.postcode);
} else {
formData.append(k, v);
}
});
if (uploadedFile.value) formData.append('profileFile', uploadedFile.value);
await $api.patch('/user/updateInfo', formData, { isFormData: true });
if (form.value.color !== originalData.value.color) {
if (form.value.color) await $api.patch('/user/updateColorYon', { color: form.value.color, type: 'YON' });
if (originalData.value.color) await $api.patch('/user/updateColorChange', { color: originalData.value.color, type: 'YON' });
}
originalData.value = { ...form.value };
profileChanged.value = false;
location.reload();
toastStore.onToast('정보가 수정되었습니다.', 's');
await $api.patch('/user/updateInfo', formData, { isFormData: true });
originalData.value = { ...form.value };
profileChanged.value = false;
location.reload();
toastStore.onToast('정보가 수정되었습니다.', 's');
};
onMounted(() => {
loadInitialData();
});
onMounted(() => loadInitialData());
</script>
<style scoped>