2025-11-24 22:37:11 +08:00

145 lines
5.7 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
const api_appUser = require("../../api/appUser.js");
const _sfc_main = {
__name: "edit",
setup(__props) {
const id = common_vendor.ref("");
const name = common_vendor.ref("");
const mobile = common_vendor.ref("");
const province = common_vendor.ref("");
const city = common_vendor.ref("");
const district = common_vendor.ref("");
const detail = common_vendor.ref("");
let isDefault = false;
const loading = common_vendor.ref(false);
const error = common_vendor.ref("");
function fill(data) {
name.value = data.name || data.realname || "";
mobile.value = data.mobile || data.phone || "";
province.value = data.province || "";
city.value = data.city || "";
district.value = data.district || "";
detail.value = data.address || data.detail || "";
isDefault = !!data.is_default;
}
async function init(idParam) {
if (!idParam) {
const data = common_vendor.index.getStorageSync("edit_address") || {};
if (data && data.id)
fill(data);
return;
}
const user_id = common_vendor.index.getStorageSync("user_id");
try {
const list = await api_appUser.listAddresses(user_id);
const arr = Array.isArray(list) ? list : list && (list.list || list.items) || [];
const found = arr.find((a) => String(a.id) === String(idParam));
if (found)
fill(found);
} catch (e) {
}
}
async function onSubmit() {
const user_id = common_vendor.index.getStorageSync("user_id");
if (!name.value || !mobile.value || !province.value || !city.value || !district.value || !detail.value) {
common_vendor.index.showToast({ title: "请完善必填信息", icon: "none" });
return;
}
loading.value = true;
error.value = "";
const payload = {
name: name.value,
mobile: mobile.value,
province: province.value,
city: city.value,
district: district.value,
address: detail.value,
is_default: isDefault
};
try {
let savedId = id.value;
if (id.value) {
await api_appUser.updateAddress(user_id, id.value, payload);
savedId = id.value;
} else {
try {
const res = await api_appUser.addAddress(user_id, payload);
savedId = res && (res.id || res.address_id) || "";
} catch (eAdd) {
const sc = eAdd && eAdd.statusCode;
const bc = eAdd && eAdd.data && (eAdd.data.code || eAdd.code) || void 0;
const msg = eAdd && (eAdd.message || eAdd.errMsg || "");
const isUniqueErr = sc === 400 && (bc === 10011 || msg && msg.toLowerCase().includes("unique"));
if (isUniqueErr) {
try {
const list = await api_appUser.listAddresses(user_id);
const arr = Array.isArray(list) ? list : list && (list.list || list.items) || [];
const found = arr.find((a) => (a.mobile === mobile.value || a.phone === mobile.value) && (a.address === detail.value || a.detail === detail.value) && a.city === city.value && a.district === district.value && a.province === province.value);
if (found) {
savedId = found.id;
await api_appUser.updateAddress(user_id, savedId, payload);
}
} catch (_) {
}
} else {
throw eAdd;
}
}
}
if (isDefault) {
if (!savedId) {
try {
const list = await api_appUser.listAddresses(user_id);
const arr = Array.isArray(list) ? list : list && (list.list || list.items) || [];
const found = arr.find((a) => (a.mobile === mobile.value || a.phone === mobile.value) && (a.address === detail.value || a.detail === detail.value));
if (found)
savedId = found.id;
} catch (_) {
}
}
if (savedId) {
await api_appUser.setDefaultAddress(user_id, savedId);
}
}
common_vendor.index.showToast({ title: "保存成功", icon: "success" });
common_vendor.index.navigateBack();
} catch (e) {
error.value = e && (e.message || e.errMsg) || "保存失败";
} finally {
loading.value = false;
}
}
common_vendor.onLoad((opts) => {
id.value = opts && opts.id || "";
init(id.value);
});
return (_ctx, _cache) => {
return common_vendor.e({
a: name.value,
b: common_vendor.o(($event) => name.value = $event.detail.value),
c: mobile.value,
d: common_vendor.o(($event) => mobile.value = $event.detail.value),
e: province.value,
f: common_vendor.o(($event) => province.value = $event.detail.value),
g: city.value,
h: common_vendor.o(($event) => city.value = $event.detail.value),
i: district.value,
j: common_vendor.o(($event) => district.value = $event.detail.value),
k: detail.value,
l: common_vendor.o(($event) => detail.value = $event.detail.value),
m: common_vendor.unref(isDefault),
n: common_vendor.o((e) => common_vendor.isRef(isDefault) ? isDefault.value = e.detail.value : isDefault = e.detail.value),
o: loading.value,
p: common_vendor.o(onSubmit),
q: error.value
}, error.value ? {
r: common_vendor.t(error.value)
} : {});
};
}
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-dcb1f0d8"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/address/edit.js.map