JewelryMall/miniprogram/unpackage/dist/dev/mp-weixin/pages/address/index.js

166 lines
5.8 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
const common_assets = require("../../common/assets.js");
const store_user = require("../../store/user.js");
const api_user = require("../../api/user.js");
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
__name: "index",
setup(__props) {
const statusBarHeight = common_vendor.ref(20);
const navBarHeight = common_vendor.ref(44);
try {
const sysInfo = common_vendor.index.getSystemInfoSync();
statusBarHeight.value = sysInfo.statusBarHeight || 20;
const menuBtn = common_vendor.index.getMenuButtonBoundingClientRect();
navBarHeight.value = (menuBtn.top - (sysInfo.statusBarHeight || 20)) * 2 + menuBtn.height;
} catch {
}
function goBack() {
common_vendor.index.navigateBack({ delta: 1 });
}
const userStore = store_user.useUserStore();
const showForm = common_vendor.ref(false);
const editingId = common_vendor.ref(null);
const form = common_vendor.reactive({
name: "",
phone: "",
province: "",
city: "",
district: "",
detail: "",
isDefault: false
});
function resetForm() {
form.name = "";
form.phone = "";
form.province = "";
form.city = "";
form.district = "";
form.detail = "";
form.isDefault = false;
editingId.value = null;
}
function addNewAddress() {
resetForm();
showForm.value = true;
}
function editAddress(addr) {
editingId.value = addr.id;
form.name = addr.name;
form.phone = addr.phone;
form.province = addr.province;
form.city = addr.city;
form.district = addr.district;
form.detail = addr.detail;
form.isDefault = addr.isDefault;
showForm.value = true;
}
async function handleSave() {
if (!form.name || !form.phone || !form.detail) {
common_vendor.index.showToast({ title: "请填写完整信息", icon: "none" });
return;
}
try {
const data = {
name: form.name,
phone: form.phone,
province: form.province,
city: form.city,
district: form.district,
detail: form.detail,
isDefault: form.isDefault
};
if (editingId.value) {
await api_user.updateAddress(editingId.value, data);
} else {
await api_user.addAddress(data);
}
showForm.value = false;
await loadAddresses();
common_vendor.index.showToast({ title: "保存成功", icon: "success" });
} catch {
common_vendor.index.showToast({ title: "保存失败", icon: "none" });
}
}
async function handleDelete(id) {
common_vendor.index.showModal({
title: "提示",
content: "确定要删除该地址吗?",
success: async (res) => {
if (res.confirm) {
try {
await api_user.deleteAddress(id);
await loadAddresses();
common_vendor.index.showToast({ title: "已删除", icon: "success" });
} catch {
common_vendor.index.showToast({ title: "删除失败", icon: "none" });
}
}
}
});
}
async function loadAddresses() {
try {
const list = await api_user.getAddressList();
userStore.setAddresses(list);
} catch {
}
}
common_vendor.onMounted(() => {
loadAddresses();
});
return (_ctx, _cache) => {
return common_vendor.e({
a: common_assets._imports_0$2,
b: common_vendor.o(goBack),
c: navBarHeight.value + "px",
d: statusBarHeight.value + "px",
e: statusBarHeight.value + navBarHeight.value + "px",
f: !showForm.value
}, !showForm.value ? common_vendor.e({
g: common_vendor.f(common_vendor.unref(userStore).addresses, (addr, k0, i0) => {
return common_vendor.e({
a: common_vendor.t(addr.name),
b: common_vendor.t(addr.phone),
c: addr.isDefault
}, addr.isDefault ? {} : {}, {
d: common_vendor.t(addr.province),
e: common_vendor.t(addr.city),
f: common_vendor.t(addr.district),
g: common_vendor.t(addr.detail),
h: common_vendor.o(($event) => editAddress(addr), addr.id),
i: common_vendor.o(($event) => editAddress(addr), addr.id),
j: common_vendor.o(($event) => handleDelete(addr.id), addr.id),
k: addr.id
});
}),
h: common_vendor.unref(userStore).addresses.length === 0
}, common_vendor.unref(userStore).addresses.length === 0 ? {} : {}, {
i: common_vendor.o(addNewAddress)
}) : {}, {
j: showForm.value
}, showForm.value ? {
k: form.name,
l: common_vendor.o(($event) => form.name = $event.detail.value),
m: form.phone,
n: common_vendor.o(($event) => form.phone = $event.detail.value),
o: form.province,
p: common_vendor.o(($event) => form.province = $event.detail.value),
q: form.city,
r: common_vendor.o(($event) => form.city = $event.detail.value),
s: form.district,
t: common_vendor.o(($event) => form.district = $event.detail.value),
v: form.detail,
w: common_vendor.o(($event) => form.detail = $event.detail.value),
x: form.isDefault,
y: common_vendor.o(($event) => form.isDefault = $event.detail.value),
z: common_vendor.o(($event) => showForm.value = false),
A: common_vendor.o(handleSave)
} : {});
};
}
});
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-c47feaaa"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/address/index.js.map