addressList.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { api_getUserReceiveAddressPage, api_userReceiveAddressRemove } from "../../api/login";
  2. Page({
  3. data: {
  4. id: "", //当前选中的id
  5. popupShow: false,
  6. selectedId: "",
  7. addressList: [],
  8. addAddressPopupShow: false,
  9. editId: ""
  10. },
  11. onShow() {
  12. this.getPageList()
  13. },
  14. onLoad(options: any) {
  15. const { id } = options;
  16. this.setData({
  17. id
  18. })
  19. },
  20. async getPageList() {
  21. try {
  22. const resData = await api_getUserReceiveAddressPage()
  23. const pageRows = resData?.data?.data?.rows || []
  24. this.setData({
  25. addressList: pageRows
  26. })
  27. } catch (e) {
  28. console.log(e, 'e')
  29. }
  30. },
  31. onDialogClose() {
  32. this.setData({
  33. popupShow: false
  34. })
  35. },
  36. async onDialogOk() {
  37. try {
  38. const { data } = await api_userReceiveAddressRemove(this.data.selectedId)
  39. if (data.code === 200) {
  40. this.setData({
  41. popupShow: false
  42. })
  43. await this.getPageList()
  44. // 当删的是当前选中的
  45. if (this.data.selectedId === this.data.id) {
  46. let item: any
  47. if (this.data.addressList.length) {
  48. item = this.data.addressList[0]
  49. this.setData({
  50. id: item.id
  51. })
  52. }
  53. this.setPagesData(item)
  54. }
  55. }
  56. } catch (e: any) { }
  57. },
  58. onDel(e: any) {
  59. const { dataset } = e.currentTarget
  60. this.setData({
  61. selectedId: dataset.id
  62. })
  63. this.setData({
  64. popupShow: true
  65. })
  66. },
  67. onEdit(e: any) {
  68. const { dataset } = e.currentTarget
  69. this.setData({
  70. editId: dataset.id,
  71. addAddressPopupShow: true
  72. })
  73. },
  74. onAdd() {
  75. this.setData({
  76. editId: "",
  77. addAddressPopupShow: true
  78. })
  79. },
  80. onSelect(e: any) {
  81. const { dataset } = e.currentTarget
  82. const item = dataset.item;
  83. this.setPagesData(item)
  84. wx.navigateBack()
  85. },
  86. async onAddAddress() {
  87. // 编辑
  88. if (this.data.editId) {
  89. await this.getPageList()
  90. if (this.data.editId === this.data.id) {
  91. // 编辑 完了之后刷新上一页的数据
  92. const item = this.data.addressList.find(((item: any) => {
  93. return item.id === this.data.id
  94. }))
  95. this.setPagesData(item)
  96. }
  97. } else {
  98. // 新增
  99. this.getPageList()
  100. }
  101. },
  102. setPagesData(item: any) {
  103. console.log(item,233339999)
  104. const pages = getCurrentPages();
  105. const prevPage = pages[pages.length - 2]; // 获取上一个页面实例
  106. prevPage.setData({ addressInfo: item ? { id: item.id, name: item.name, phoneNumber: item.phoneNumber, addressDes: item.provinceName + item.cityName + (item.regionName || "") + item.detailAddress } : {} });
  107. }
  108. })