|
@@ -73,6 +73,8 @@
|
|
|
import Toast from '@/components/Toast.vue';
|
|
|
import NavBar from '@/components/NavBar.vue';
|
|
|
import { bindAddress } from '@/api/profile';
|
|
|
+import { getUserInfo } from '@/api/home';
|
|
|
+
|
|
|
export default {
|
|
|
name: 'ShdizhiPage',
|
|
|
components: {
|
|
@@ -88,76 +90,51 @@ export default {
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
- computed: {
|
|
|
- // 表单验证
|
|
|
- isFormValid() {
|
|
|
- return this.addressForm.name.trim() &&
|
|
|
- this.addressForm.phone.trim() &&
|
|
|
- this.addressForm.address.trim() &&
|
|
|
- this.isValidPhone(this.addressForm.phone);
|
|
|
- }
|
|
|
- },
|
|
|
mounted() {
|
|
|
this.loadSavedAddress();
|
|
|
},
|
|
|
methods: {
|
|
|
// 自定义返回处理
|
|
|
handleBack() {
|
|
|
- // 可以在这里添加自定义逻辑,比如询问是否保存草稿
|
|
|
- if (this.hasUnsavedChanges()) {
|
|
|
- this.$refs.toast.show('检测到未保存的更改', 'warning');
|
|
|
- // 可以显示确认对话框
|
|
|
- return;
|
|
|
- }
|
|
|
this.$router.back();
|
|
|
},
|
|
|
|
|
|
- // 检查是否有未保存的更改
|
|
|
- hasUnsavedChanges() {
|
|
|
- const current = this.addressForm;
|
|
|
- const saved = localStorage.getItem('userAddress');
|
|
|
-
|
|
|
- if (!saved && (current.name || current.phone || current.address)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- if (saved) {
|
|
|
- const savedData = JSON.parse(saved);
|
|
|
- return current.name !== savedData.name ||
|
|
|
- current.phone !== savedData.phone ||
|
|
|
- current.address !== savedData.address;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- },
|
|
|
-
|
|
|
// 加载已保存的地址
|
|
|
async loadSavedAddress() {
|
|
|
try {
|
|
|
- const savedAddress = localStorage.getItem('userAddress');
|
|
|
- if (savedAddress) {
|
|
|
- this.addressForm = JSON.parse(savedAddress);
|
|
|
+ const res = await getUserInfo();
|
|
|
+ if (res.code === 1 && res.data) {
|
|
|
+ this.addressForm.name = res.data.name || '';
|
|
|
+ this.addressForm.phone = res.data.phone || '';
|
|
|
+ this.addressForm.address = res.data.address || '';
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error('加载地址失败:', error);
|
|
|
+ this.$refs.toast.show('加载地址信息失败', 'error');
|
|
|
}
|
|
|
},
|
|
|
|
|
|
- // 验证手机号
|
|
|
- isValidPhone(phone) {
|
|
|
- const phoneRegex = /^1[3-9]\d{9}$/;
|
|
|
- return phoneRegex.test(phone);
|
|
|
- },
|
|
|
-
|
|
|
// 保存地址
|
|
|
async saveAddress() {
|
|
|
- const res = await bindAddress({
|
|
|
- name: this.addressForm.name,
|
|
|
- phone: this.addressForm.phone,
|
|
|
- address: this.addressForm.address
|
|
|
- });
|
|
|
- console.log(res);
|
|
|
-
|
|
|
+ try {
|
|
|
+ const res = await bindAddress({
|
|
|
+ name: this.addressForm.name,
|
|
|
+ phone: this.addressForm.phone,
|
|
|
+ address: this.addressForm.address
|
|
|
+ });
|
|
|
+
|
|
|
+ if (res.code === 1) {
|
|
|
+ this.$refs.toast.show(res.msg || '地址保存成功', 'success');
|
|
|
+ setTimeout(() => {
|
|
|
+ this.$router.back();
|
|
|
+ }, 1500);
|
|
|
+ } else {
|
|
|
+ this.$refs.toast.show(res.msg || '保存失败', 'error');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('保存地址失败:', error);
|
|
|
+ this.$refs.toast.show('保存失败,请重试', 'error');
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -201,10 +178,10 @@ export default {
|
|
|
position: relative;
|
|
|
width: 100px;
|
|
|
height: 100px;
|
|
|
- img{
|
|
|
- width: 100%;
|
|
|
- height: 100%;
|
|
|
- }
|
|
|
+}
|
|
|
+.illustration img {
|
|
|
+ width: 100%;
|
|
|
+ height: 100%;
|
|
|
}
|
|
|
|
|
|
.person {
|