address.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var map = require('../../../utils/map.js');
  4. var app = getApp();
  5. Page({
  6. data: {
  7. addressList: [],
  8. total: 0,
  9. addressName: "",
  10. isShowServiceAddress: false
  11. },
  12. onLoad: function (options) {
  13. // 页面初始化 options为页面跳转所带来的参数
  14. var pages = getCurrentPages();
  15. var prevPage = pages[pages.length - 2];
  16. if (prevPage.route == "pages/index/index") {
  17. this.setData({
  18. isShowServiceAddress: true
  19. })
  20. }
  21. this.getUserLocation();
  22. },
  23. shaxin() {
  24. this.getUserLocation();
  25. },
  26. getUserLocation: function () {
  27. map.getUserLocation();
  28. let that = this;
  29. setTimeout(function () {
  30. that.setData({
  31. addressName: app.globalData.currentLocationName
  32. })
  33. }, 2000)
  34. },
  35. onReady: function () {
  36. // 页面渲染完成
  37. },
  38. onShow: function () {
  39. // 页面显示
  40. this.getAddressList();
  41. },
  42. getAddressList() {
  43. let that = this;
  44. util.request(api.AddressList).then(function (res) {
  45. if (res.errno === 0) {
  46. that.setData({
  47. addressList: res.data.list,
  48. total: res.data.total
  49. });
  50. }
  51. });
  52. },
  53. updateAddress(event) {
  54. let addressId = event.currentTarget.dataset.addressId;
  55. wx.navigateTo({
  56. url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
  57. })
  58. },
  59. async updateAddressData(address) {
  60. await util.request(api.AddressSave, address, 'POST').then(function (res) {
  61. if (res.errno === 0) {
  62. var city = res.data.city.indexOf("市辖") != -1 ? res.data.province : res.data.city;
  63. var addresInfo = {
  64. city: city,
  65. lng: res.data.lng,
  66. lat: res.data.lat,
  67. shopId: res.data.shopId,
  68. address: res.data.addressDetail,
  69. addressId:res.data.id,
  70. name: res.data.name,
  71. tel: res.data.tel
  72. }
  73. wx.setStorageSync('addressInfo', addresInfo);
  74. }
  75. });
  76. },
  77. addressAddOrUpdate(event) {
  78. // console.log(event)
  79. let addressId = event.currentTarget.dataset.addressId;
  80. let name = event.currentTarget.dataset.addressName;
  81. let tel = event.currentTarget.dataset.addressTel;
  82. let addressDetail = event.currentTarget.dataset.addressDetail;
  83. let address = null;
  84. this.data.addressList.forEach(item => {
  85. if (addressId == item.id)
  86. address = item;
  87. })
  88. console.info(address)
  89. //返回之前,先取出上一页对象,并设置addressId
  90. var pages = getCurrentPages();
  91. console.log(pages);
  92. var prevPage = pages[pages.length - 2];
  93. if (null != address && (prevPage.route == "pages/index/index" || prevPage.route == "pages/ucenter/serviceDetail/serviceDetail")) {
  94. // console.log("市辖区".indexOf("市"));
  95. var city = address.city.indexOf("市辖") != -1 ? address.province : address.city;
  96. //通过jz_shop的shop_no关联服务力分配的门店id,拿实际的jz_shop的门店id
  97. //这个是拿的服务力分配的门店,如果不转要改后台查询商品,要用shopNo,这里的shopId是shopNo
  98. // let distribution_unit=address.shopId;
  99. if (address.shopId == null) {
  100. this.updateAddressData(address);
  101. } else {
  102. var addresInfo = {
  103. city: city,
  104. lng: address.lng,
  105. lat: address.lat,
  106. shopId: address.shopId,
  107. address: address.addressDetail,
  108. shopName: address.shopName,
  109. name: name,
  110. tel: tel,
  111. addressId:address.id
  112. }
  113. wx.setStorageSync('addressInfo', addresInfo);
  114. }
  115. setTimeout(() => {
  116. wx.navigateBack();
  117. }, 300);
  118. } else if (prevPage.route == "pages/checkout/checkout") {
  119. try {
  120. wx.setStorageSync('addressId', event.currentTarget.dataset.addressId);
  121. } catch (e) {
  122. }
  123. if (addressId && addressId != 0) {
  124. wx.navigateBack();
  125. } else {
  126. wx.navigateTo({
  127. url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
  128. })
  129. }
  130. } else if (prevPage.route == "pages/goods/infoCollect/infoCollect") {
  131. if (addressId && addressId != 0) {
  132. app.globalData.infoAddress = {
  133. addressId: addressId,
  134. addressDetail: addressDetail
  135. }
  136. wx.navigateBack();
  137. } else {
  138. wx.navigateTo({
  139. url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
  140. })
  141. }
  142. } else {
  143. wx.navigateTo({
  144. url: '/pages/ucenter/addressAdd/addressAdd?id=' + event.currentTarget.dataset.addressId
  145. })
  146. }
  147. },
  148. deleteAddress(event) {
  149. console.log(event.target)
  150. let that = this;
  151. wx.showModal({
  152. title: '',
  153. content: '确定要删除地址?',
  154. success: function (res) {
  155. if (res.confirm) {
  156. let addressId = event.target.dataset.addressId;
  157. util.request(api.AddressDelete, {
  158. id: addressId
  159. }, 'POST').then(function (res) {
  160. if (res.errno === 0) {
  161. that.getAddressList();
  162. wx.removeStorage({
  163. key: 'addressId',
  164. success: function (res) {},
  165. })
  166. }
  167. });
  168. console.log('用户点击确定')
  169. }
  170. }
  171. })
  172. return false;
  173. },
  174. onHide: function () {
  175. // 页面隐藏
  176. },
  177. onUnload: function () {
  178. // 页面关闭
  179. }
  180. })