jiangchengfeiyi-xiaochengxu/unpackage/dist/dev/mp-alipay/pages/mine/Contact/Contact.js

275 lines
8.6 KiB
JavaScript
Raw Normal View History

2024-12-12 02:17:33 +00:00
"use strict";
const common_vendor = require("../../../common/vendor.js");
const api_request = require("../../../api/request.js");
if (!Array) {
const _easycom_uni_popup2 = common_vendor.resolveComponent("uni-popup");
_easycom_uni_popup2();
}
const _easycom_uni_popup = () => "../../../uni_modules/uni-popup/components/uni-popup/uni-popup.js";
if (!Math) {
_easycom_uni_popup();
}
const _sfc_main = {
__name: "Contact",
setup(__props) {
common_vendor.onMounted(() => {
getContacts();
});
const newContact = common_vendor.ref({
name: "",
phone: "",
isDefault: 0,
id: 0
});
const newContacts = common_vendor.ref({
name: "",
phone: "",
isDefault: 0,
id: 0
});
const editContact = async (index) => {
popup1.value.open("center");
const contact = filteredContacts.value[index];
newContact.value = {
name: contact.name,
id: contact.id,
phone: contact.phone,
isDefault: contact.isDefault
};
};
const handleIsDefaultChanges = () => {
newContacts.value.isDefault = 1;
console.log("赋值后newContacts.value.isDefault", newContacts.value.isDefault);
if (newContacts.value.isDefault) {
for (let i = 0; i < filteredContacts.value.length; i++) {
filteredContacts.value[i].isDefault = 0;
}
}
console.log("函数执行完毕newContacts.value.isDefault最终值", newContacts.value.isDefault);
};
const handleIsDefaultChange = (e) => {
console.log("进入handleIsDefaultChanges函数当前e.detail.value", e.detail.value);
newContact.value.isDefault = 1;
console.log("赋值后newContacts.value.isDefault", newContact.value.isDefault);
if (newContact.value.isDefault) {
for (let i = 0; i < filteredContacts.value.length; i++) {
filteredContacts.value[i].isDefault = 0;
}
}
console.log("函数执行完毕newContacts.value.isDefault最终值", newContacts.value.isDefault);
};
const getContacts = async () => {
const res = await common_vendor.index.request({
url: api_request.baseUrl + "/contacts/list",
method: "POST",
header: {
cookie: wx.getStorageSync("cookie")
}
});
console.log(res);
if (res.data.code === 1) {
const correctContacts = [];
let defaultContact = null;
res.data.data.forEach((contact) => {
if (contact.isDefault === 1) {
if (!defaultContact) {
defaultContact = contact;
} else {
contact.isDefault = 0;
}
}
correctContacts.push(contact);
});
filteredContacts.value = correctContacts;
if (defaultContact) {
const index = filteredContacts.value.findIndex(
(c) => c.id === defaultContact.id
);
if (index !== -1) {
filteredContacts.value[index].isDefault = 1;
}
}
} else {
common_vendor.index.showToast({
icon: "error",
title: "获取失败"
});
}
};
const searchText = common_vendor.ref("");
const filteredContacts = common_vendor.ref([]);
common_vendor.watch(searchText, () => {
if (searchText.value === "") {
filteredContacts.value = contacts.value;
} else {
filteredContacts.value = contacts.value.filter((contact) => {
return contact.name.includes(searchText.value) || contact.phone.includes(
searchText.value
);
});
}
});
const addContact = async () => {
popup.value.close();
if (newContacts.value.isDefault) {
for (let i = 0; i < filteredContacts.value.length; i++) {
if (filteredContacts.value[i].isDefault === 1) {
filteredContacts.value[i].isDefault = 0;
}
}
}
const res = await common_vendor.index.request({
url: api_request.baseUrl + "/contacts/add",
method: "POST",
header: {
cookie: wx.getStorageSync("cookie")
},
data: {
name: newContacts.value.name,
phone: newContacts.value.phone,
isDefault: newContacts.value.isDefault
}
});
if (res.data.code === 1) {
console.log("添加成功");
newContacts.value = {
name: "",
phone: "",
isDefault: 0,
id: 0
};
} else {
common_vendor.index.showToast({
icon: "error",
title: "添加失败"
});
}
getContacts();
};
const deleteContact = async (index) => {
const res = await common_vendor.index.request({
url: api_request.baseUrl + "/contacts/delete",
method: "POST",
header: {
cookie: wx.getStorageSync("cookie")
},
data: {
id: filteredContacts.value[index].id
}
});
console.log(res, "1111111111111");
if (res.data.code === 1) {
console.log("删除成功");
} else {
common_vendor.index.showToast({
icon: "error",
title: "删除失败"
});
}
getContacts();
};
common_vendor.ref({});
common_vendor.ref(null);
const saveModifiedContact = async () => {
popup1.value.close();
if (!newContact.value.name || !newContact.value.phone) {
common_vendor.index.showToast({
icon: "error",
title: "不能为空"
});
return;
}
if (newContact.value.isDefault) {
for (let i = 0; i < filteredContacts.value.length; i++) {
if (filteredContacts.value[i].isDefault === 1) {
filteredContacts.value[i].isDefault = 0;
}
}
}
const res = await common_vendor.index.request({
url: api_request.baseUrl + "/contacts/update",
method: "POST",
header: {
cookie: wx.getStorageSync("cookie")
},
data: {
name: newContact.value.name,
phone: newContact.value.phone,
id: newContact.value.id,
isDefault: newContact.value.isDefault
}
});
if (res.data.code === 1) {
console.log("更新成功");
console.log(newContact.value.isDefault, "000000000000000000000000 ");
} else {
common_vendor.index.showToast({
icon: "error",
title: "更新失败"
});
}
getContacts();
};
const popup = common_vendor.ref(null);
const popup1 = common_vendor.ref(null);
const open = () => {
popup.value.open("center");
};
const close = () => {
popup.value.close();
};
const close1 = () => {
popup1.value.close();
};
return (_ctx, _cache) => {
return {
a: newContacts.value.name,
b: common_vendor.o(($event) => newContacts.value.name = $event.detail.value),
c: newContacts.value.phone,
d: common_vendor.o(($event) => newContacts.value.phone = $event.detail.value),
e: common_vendor.o(handleIsDefaultChanges),
f: common_vendor.o(addContact),
g: common_vendor.o(close),
h: () => ({
r: popup,
k: "popup"
}),
i: common_vendor.p({
["mask-click"]: false,
["background-color"]: "white"
}),
j: newContact.value.name,
k: common_vendor.o(($event) => newContact.value.name = $event.detail.value),
l: newContact.value.phone,
m: common_vendor.o(($event) => newContact.value.phone = $event.detail.value),
n: common_vendor.o(handleIsDefaultChange),
o: common_vendor.o(saveModifiedContact),
p: common_vendor.o(close1),
q: () => ({
r: popup1,
k: "popup1"
}),
r: common_vendor.p({
["mask-click"]: false,
["background-color"]: "white"
}),
s: common_vendor.f(filteredContacts.value, (item, index, i0) => {
return common_vendor.e({
a: common_vendor.t(item.name),
b: common_vendor.t(item.phone),
c: common_vendor.t(item.id),
d: item.isDefault === 1
}, item.isDefault === 1 ? {} : {}, {
e: common_vendor.o(($event) => editContact(index)),
f: common_vendor.o(($event) => deleteContact(index)),
g: index
});
}),
t: common_vendor.o(open)
};
};
}
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-0aa6b83b"], ["__file", "D:/jiangchengfeiyi-xiaochengxu/pages/mine/Contact/Contact.vue"]]);
my.createPage(MiniProgramPage);