108 lines
3.9 KiB
JavaScript
108 lines
3.9 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../../common/vendor.js");
|
|
const api_appUser = require("../../api/appUser.js");
|
|
const _sfc_main = {
|
|
__name: "index",
|
|
setup(__props) {
|
|
const list = common_vendor.ref([]);
|
|
const loading = common_vendor.ref(false);
|
|
const error = common_vendor.ref("");
|
|
async function fetchList() {
|
|
const user_id = common_vendor.index.getStorageSync("user_id");
|
|
const token = common_vendor.index.getStorageSync("token");
|
|
const phoneBound = !!common_vendor.index.getStorageSync("phone_bound");
|
|
if (!user_id || !token || !phoneBound) {
|
|
common_vendor.index.showModal({
|
|
title: "提示",
|
|
content: "请先登录并绑定手机号",
|
|
confirmText: "去登录",
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
common_vendor.index.navigateTo({ url: "/pages/login/index" });
|
|
}
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
loading.value = true;
|
|
error.value = "";
|
|
try {
|
|
const data = await api_appUser.listAddresses(user_id);
|
|
list.value = Array.isArray(data) ? data : data && (data.list || data.items) || [];
|
|
} catch (e) {
|
|
error.value = e && (e.message || e.errMsg) || "获取地址失败";
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
function toAdd() {
|
|
common_vendor.index.removeStorageSync("edit_address");
|
|
common_vendor.index.navigateTo({ url: "/pages/address/edit" });
|
|
}
|
|
function toEdit(item) {
|
|
common_vendor.index.setStorageSync("edit_address", item);
|
|
common_vendor.index.navigateTo({ url: `/pages/address/edit?id=${item.id}` });
|
|
}
|
|
function onDelete(item) {
|
|
const user_id = common_vendor.index.getStorageSync("user_id");
|
|
common_vendor.index.showModal({
|
|
title: "确认删除",
|
|
content: "确定删除该地址吗?",
|
|
success: async (res) => {
|
|
if (res.confirm) {
|
|
try {
|
|
await api_appUser.deleteAddress(user_id, item.id);
|
|
fetchList();
|
|
} catch (e) {
|
|
common_vendor.index.showToast({ title: "删除失败", icon: "none" });
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
async function onSetDefault(item) {
|
|
try {
|
|
const user_id = common_vendor.index.getStorageSync("user_id");
|
|
await api_appUser.setDefaultAddress(user_id, item.id);
|
|
fetchList();
|
|
} catch (e) {
|
|
common_vendor.index.showToast({ title: "设置失败", icon: "none" });
|
|
}
|
|
}
|
|
common_vendor.onLoad(() => {
|
|
fetchList();
|
|
});
|
|
return (_ctx, _cache) => {
|
|
return common_vendor.e({
|
|
a: common_vendor.o(toAdd),
|
|
b: error.value
|
|
}, error.value ? {
|
|
c: common_vendor.t(error.value)
|
|
} : {}, {
|
|
d: list.value.length === 0 && !loading.value
|
|
}, list.value.length === 0 && !loading.value ? {} : {}, {
|
|
e: common_vendor.f(list.value, (item, k0, i0) => {
|
|
return common_vendor.e({
|
|
a: common_vendor.t(item.name || item.realname),
|
|
b: common_vendor.t(item.phone || item.mobile),
|
|
c: item.is_default
|
|
}, item.is_default ? {} : {}, {
|
|
d: common_vendor.t(item.province),
|
|
e: common_vendor.t(item.city),
|
|
f: common_vendor.t(item.district),
|
|
g: common_vendor.t(item.address || item.detail),
|
|
h: common_vendor.o(($event) => toEdit(item), item.id),
|
|
i: common_vendor.o(($event) => onDelete(item), item.id),
|
|
j: item.is_default,
|
|
k: common_vendor.o(($event) => onSetDefault(item), item.id),
|
|
l: item.id
|
|
});
|
|
})
|
|
});
|
|
};
|
|
}
|
|
};
|
|
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
|