manage-address.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import tickleBehavior, {
  3. getTickleContext,
  4. } from "../../../../core/behavior/tickle.behavior";
  5. import { getAddressListMethod, orderPayAddressMethod } from "../../request";
  6. // module/diet/pages/delivery-address/delivery-address.ts
  7. Page({
  8. behaviors: [PageContainerBehavior, tickleBehavior],
  9. properties: {},
  10. data: {
  11. addressList: [], // 当前展示的列表
  12. allAddressList: [], // 原始完整列表
  13. selectedIndex: -1,
  14. searchValue: "",
  15. noResult: false, // 新增
  16. type: "",
  17. orderId: "",
  18. },
  19. onLoad(options: any) {
  20. if (options.type) {
  21. this.setData({ type: options.type });
  22. wx.setStorageSync("type", options.type);
  23. }
  24. if (options.orderId) {
  25. this.setData({ orderId: options.orderId });
  26. wx.setStorageSync("orderId", options.orderId);
  27. }
  28. },
  29. // 更新订单地址
  30. async updataOrderAddress(id: string, address: any) {
  31. const data = {
  32. provinceName: address.provinceName,
  33. provinceCode: address.provinceCode,
  34. cityName: address.cityName,
  35. cityCode: address.cityCode,
  36. areaName: address.areaName,
  37. areaCode: address.areaCode,
  38. detailAddress: address.detailAddress,
  39. liaison: address.liaison,
  40. phone: address.phone,
  41. };
  42. console.log(id, data, "更新订单地址");
  43. const res = await orderPayAddressMethod(id, data);
  44. console.log(res, "更新订单地址");
  45. },
  46. // 选择地址
  47. async selectAddress(e: any) {
  48. const item = e.currentTarget.dataset.item;
  49. const type = wx.getStorageSync("type");
  50. const orderId = wx.getStorageSync("orderId");
  51. console.log(type, "页面切换type", item);
  52. const pages = getCurrentPages();
  53. console.log(pages, "pages");
  54. if (type === "orderDetail") {
  55. // 订单详情 切换地址
  56. const orderPage = pages.find(
  57. (page) =>
  58. page.route === "module/article/pages/order-detail/order-detail"
  59. );
  60. console.log(orderPage, "orderPage");
  61. try {
  62. await this.updataOrderAddress(orderId, item);
  63. if (orderPage) {
  64. // orderPage.setData({ selectedAddress: item });
  65. wx.navigateBack();
  66. }
  67. } catch (error: any) {
  68. getTickleContext.call(this).showWarnMessage(error.errMsg);
  69. }
  70. } else if (type === "orderList") {
  71. // 订单列表 切换地址
  72. const findPage = pages.find(
  73. (page) => page.route === "module/article/pages/order-list/order-list"
  74. );
  75. console.log(findPage, "orderPage", orderId,item);
  76. try {
  77. await this.updataOrderAddress(orderId, item);
  78. if (findPage) {
  79. // orderPage.setData({ selectedAddress: item });
  80. console.log("orderPage8888");
  81. // wx.navigateBack({
  82. // delta: 1,
  83. // fail: () => {
  84. // console.log("orderPage9999");
  85. wx.redirectTo({
  86. url: "/"+findPage.route+"?tab=pending",
  87. });
  88. // },
  89. // });
  90. }
  91. } catch (error: any) {
  92. getTickleContext.call(this).showWarnMessage(error.errMsg);
  93. }
  94. } else {
  95. // 其他页面 切换地址
  96. console.log("其他页面");
  97. }
  98. // 获取上一页的数据
  99. // const pages = getCurrentPages();
  100. // if (pages.length > 1) {
  101. // const prevPage = pages[pages.length - 2];
  102. // console.log(prevPage.route, "上一页");
  103. // // 订单详情 切换地址
  104. // if (prevPage.route === "module/article/pages/order-detail/order-detail") {
  105. // // 设置上一页的数据
  106. // prevPage.setData({ selectedAddress: item });
  107. // wx.navigateBack();
  108. // // 订单列表切换地址
  109. // } else if (
  110. // prevPage.route === "module/article/pages/order-list/order-list"
  111. // ) {
  112. // prevPage.setData({
  113. // selectedAddress: e.currentTarget.dataset.address,
  114. // });
  115. // wx.navigateBack();
  116. // } else {
  117. // console.log("其他页面不返回");
  118. // }
  119. // }
  120. },
  121. // 搜索
  122. onSearchChange(e: any) {
  123. const keyword = (e.detail.value || "").trim().toLowerCase();
  124. const allList = this.data.allAddressList || [];
  125. if (!keyword) {
  126. this.setData({ addressList: allList, noResult: false });
  127. return;
  128. }
  129. const filtered = allList.filter(
  130. (item: any) =>
  131. (item.liaison || "").toLowerCase().includes(keyword) ||
  132. (item.phone || "").toLowerCase().includes(keyword) ||
  133. (item.fullAddress || "").toLowerCase().includes(keyword)
  134. );
  135. this.setData({
  136. addressList: filtered,
  137. noResult: filtered.length === 0,
  138. });
  139. },
  140. // 编辑地址
  141. onEdit(e: any) {
  142. const id = e.currentTarget.dataset.id;
  143. // 跳转编辑
  144. wx.navigateTo({
  145. url: `/module/article/pages/add-address/add-address?id=${id}&type=${this.data.type} `,
  146. });
  147. },
  148. // 新增地址
  149. onAddress() {
  150. // 新增地址逻辑
  151. wx.navigateTo({
  152. url:
  153. "/module/article/pages/add-address/add-address?type=" + this.data.type,
  154. });
  155. },
  156. async load() {
  157. wx.showLoading({ title: "加载中" });
  158. try {
  159. // 获取所有的地址列表
  160. const res = await getAddressListMethod("", "");
  161. if (res && res.length > 0) {
  162. res.forEach((item: any) => {
  163. item.fullAddress = `${item.provinceName} ${item.cityName} ${item.areaName} ${item.detailAddress}`;
  164. item.tag = item.tagList[0] || "";
  165. });
  166. this.setData({
  167. noResult: false,
  168. addressList: res, // 当前展示
  169. allAddressList: res, // 原始
  170. });
  171. } else {
  172. this.setData({
  173. noResult: true,
  174. });
  175. }
  176. } catch (error: any) {
  177. getTickleContext.call(this).showWarnMessage(error.errMsg);
  178. }
  179. wx.hideLoading();
  180. },
  181. // 导入微信地址
  182. onImportWechatAddress() {
  183. wx.chooseAddress({
  184. success: (res) => {
  185. console.log(res, "导入微信地址");
  186. // 拼接参数
  187. const params = encodeURIComponent(
  188. JSON.stringify({
  189. liaison: res.userName,
  190. phone: res.telNumber,
  191. region:
  192. res.provinceName + " " + res.cityName + " " + res.countyName,
  193. provinceName: res.provinceName,
  194. cityName: res.cityName,
  195. areaName: res.countyName,
  196. detailAddress: res.detailInfoNew,
  197. })
  198. );
  199. console.log(params, "微信地址的拼接");
  200. wx.navigateTo({
  201. url: `/module/article/pages/add-address/add-address?imported=${params}`,
  202. });
  203. },
  204. fail: () => {
  205. wx.showToast({ title: "导入失败或用户取消", icon: "none" });
  206. },
  207. });
  208. },
  209. onShow() {
  210. // 加载地址列表
  211. this.load();
  212. },
  213. });