| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
- import tickleBehavior, {
- getTickleContext,
- } from "../../../../core/behavior/tickle.behavior";
- import { getAddressListMethod, orderPayAddressMethod } from "../../request";
- // module/diet/pages/delivery-address/delivery-address.ts
- Page({
- behaviors: [PageContainerBehavior, tickleBehavior],
- properties: {},
- data: {
- addressList: [], // 当前展示的列表
- allAddressList: [], // 原始完整列表
- selectedIndex: -1,
- searchValue: "",
- noResult: false, // 新增
- type: "",
- orderId: "",
- },
- onLoad(options: any) {
- if (options.type) {
- this.setData({ type: options.type });
- wx.setStorageSync("type", options.type);
- }
- if (options.orderId) {
- this.setData({ orderId: options.orderId });
- wx.setStorageSync("orderId", options.orderId);
- }
- },
- // 更新订单地址
- async updataOrderAddress(id: string, address: any) {
- const data = {
- provinceName: address.provinceName,
- provinceCode: address.provinceCode,
- cityName: address.cityName,
- cityCode: address.cityCode,
- areaName: address.areaName,
- areaCode: address.areaCode,
- detailAddress: address.detailAddress,
- liaison: address.liaison,
- phone: address.phone,
- };
- console.log(id, data, "更新订单地址");
- const res = await orderPayAddressMethod(id, data);
- console.log(res, "更新订单地址");
- },
- // 选择地址
- async selectAddress(e: any) {
- const item = e.currentTarget.dataset.item;
- const type = wx.getStorageSync("type");
- const orderId = wx.getStorageSync("orderId");
- console.log(type, "页面切换type", item);
- const pages = getCurrentPages();
- console.log(pages, "pages");
- if (type === "orderDetail") {
- // 订单详情 切换地址
- const orderPage = pages.find(
- (page) =>
- page.route === "module/article/pages/order-detail/order-detail"
- );
- console.log(orderPage, "orderPage");
- try {
- await this.updataOrderAddress(orderId, item);
- if (orderPage) {
- // orderPage.setData({ selectedAddress: item });
- wx.navigateBack();
- }
- } catch (error: any) {
- getTickleContext.call(this).showWarnMessage(error.errMsg);
- }
- } else if (type === "orderList") {
- // 订单列表 切换地址
- const findPage = pages.find(
- (page) => page.route === "module/article/pages/order-list/order-list"
- );
- console.log(findPage, "orderPage", orderId,item);
- try {
- await this.updataOrderAddress(orderId, item);
- if (findPage) {
- // orderPage.setData({ selectedAddress: item });
- console.log("orderPage8888");
- // wx.navigateBack({
- // delta: 1,
- // fail: () => {
- // console.log("orderPage9999");
- wx.redirectTo({
- url: "/"+findPage.route+"?tab=pending",
- });
- // },
- // });
-
- }
- } catch (error: any) {
- getTickleContext.call(this).showWarnMessage(error.errMsg);
- }
- } else {
- // 其他页面 切换地址
- console.log("其他页面");
- }
- // 获取上一页的数据
- // const pages = getCurrentPages();
- // if (pages.length > 1) {
- // const prevPage = pages[pages.length - 2];
- // console.log(prevPage.route, "上一页");
- // // 订单详情 切换地址
- // if (prevPage.route === "module/article/pages/order-detail/order-detail") {
- // // 设置上一页的数据
- // prevPage.setData({ selectedAddress: item });
- // wx.navigateBack();
- // // 订单列表切换地址
- // } else if (
- // prevPage.route === "module/article/pages/order-list/order-list"
- // ) {
- // prevPage.setData({
- // selectedAddress: e.currentTarget.dataset.address,
- // });
- // wx.navigateBack();
- // } else {
- // console.log("其他页面不返回");
- // }
- // }
- },
- // 搜索
- onSearchChange(e: any) {
- const keyword = (e.detail.value || "").trim().toLowerCase();
- const allList = this.data.allAddressList || [];
- if (!keyword) {
- this.setData({ addressList: allList, noResult: false });
- return;
- }
- const filtered = allList.filter(
- (item: any) =>
- (item.liaison || "").toLowerCase().includes(keyword) ||
- (item.phone || "").toLowerCase().includes(keyword) ||
- (item.fullAddress || "").toLowerCase().includes(keyword)
- );
- this.setData({
- addressList: filtered,
- noResult: filtered.length === 0,
- });
- },
- // 编辑地址
- onEdit(e: any) {
- const id = e.currentTarget.dataset.id;
- // 跳转编辑
- wx.navigateTo({
- url: `/module/article/pages/add-address/add-address?id=${id}&type=${this.data.type} `,
- });
- },
- // 新增地址
- onAddress() {
- // 新增地址逻辑
- wx.navigateTo({
- url:
- "/module/article/pages/add-address/add-address?type=" + this.data.type,
- });
- },
- async load() {
- wx.showLoading({ title: "加载中" });
- try {
- // 获取所有的地址列表
- const res = await getAddressListMethod("", "");
- if (res && res.length > 0) {
- res.forEach((item: any) => {
- item.fullAddress = `${item.provinceName} ${item.cityName} ${item.areaName} ${item.detailAddress}`;
- item.tag = item.tagList[0] || "";
- });
- this.setData({
- noResult: false,
- addressList: res, // 当前展示
- allAddressList: res, // 原始
- });
- } else {
- this.setData({
- noResult: true,
- });
- }
- } catch (error: any) {
- getTickleContext.call(this).showWarnMessage(error.errMsg);
- }
- wx.hideLoading();
- },
- // 导入微信地址
- onImportWechatAddress() {
- wx.chooseAddress({
- success: (res) => {
- console.log(res, "导入微信地址");
- // 拼接参数
- const params = encodeURIComponent(
- JSON.stringify({
- liaison: res.userName,
- phone: res.telNumber,
- region:
- res.provinceName + " " + res.cityName + " " + res.countyName,
- provinceName: res.provinceName,
- cityName: res.cityName,
- areaName: res.countyName,
- detailAddress: res.detailInfoNew,
- })
- );
- console.log(params, "微信地址的拼接");
- wx.navigateTo({
- url: `/module/article/pages/add-address/add-address?imported=${params}`,
- });
- },
- fail: () => {
- wx.showToast({ title: "导入失败或用户取消", icon: "none" });
- },
- });
- },
- onShow() {
- // 加载地址列表
- this.load();
- },
- });
|