address.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. }
  70. wx.setStorageSync('addressInfo', addresInfo);
  71. }
  72. });
  73. },
  74. addressAddOrUpdate(event) {
  75. // console.log(event)
  76. let addressId = event.currentTarget.dataset.addressId;
  77. let name = event.currentTarget.dataset.addressName;
  78. let tel = event.currentTarget.dataset.addressTel;
  79. let addressDetail = event.currentTarget.dataset.addressDetail;
  80. let address = null;
  81. this.data.addressList.forEach(item => {
  82. if (addressId == item.id)
  83. address = item;
  84. })
  85. console.info(address)
  86. //返回之前,先取出上一页对象,并设置addressId
  87. var pages = getCurrentPages();
  88. var prevPage = pages[pages.length - 2];
  89. if (null != address && (prevPage.route == "pages/index/index" || prevPage.route == "pages/ucenter/serviceDetail/serviceDetail")) {
  90. // console.log("市辖区".indexOf("市"));
  91. var city = address.city.indexOf("市辖") != -1 ? address.province : address.city;
  92. //通过jz_shop的shop_no关联服务力分配的门店id,拿实际的jz_shop的门店id
  93. //这个是拿的服务力分配的门店,如果不转要改后台查询商品,要用shopNo,这里的shopId是shopNo
  94. // let distribution_unit=address.shopId;
  95. if (address.shopId == null) {
  96. this.updateAddressData(address);
  97. } else {
  98. var addresInfo = {
  99. city: city,
  100. lng: address.lng,
  101. lat: address.lat,
  102. shopId: address.shopId,
  103. address: address.addressDetail,
  104. shopName: address.shopName,
  105. name: name,
  106. tel: tel,
  107. addressId:address.id
  108. }
  109. wx.setStorageSync('addressInfo', addresInfo);
  110. }
  111. wx.navigateBack();
  112. } else if (prevPage.route == "pages/checkout/checkout") {
  113. try {
  114. wx.setStorageSync('addressId', event.currentTarget.dataset.addressId);
  115. } catch (e) {
  116. }
  117. if (addressId && addressId != 0) {
  118. wx.navigateBack();
  119. } else {
  120. wx.navigateTo({
  121. url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
  122. })
  123. }
  124. } else if (prevPage.route == "pages/goods/infoCollect/infoCollect") {
  125. if (addressId && addressId != 0) {
  126. app.globalData.infoAddress = {
  127. addressId: addressId,
  128. addressDetail: addressDetail
  129. }
  130. wx.navigateBack();
  131. } else {
  132. wx.navigateTo({
  133. url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
  134. })
  135. }
  136. } else {
  137. wx.navigateTo({
  138. url: '/pages/ucenter/addressAdd/addressAdd?id=' + event.currentTarget.dataset.addressId
  139. })
  140. }
  141. },
  142. deleteAddress(event) {
  143. console.log(event.target)
  144. let that = this;
  145. wx.showModal({
  146. title: '',
  147. content: '确定要删除地址?',
  148. success: function (res) {
  149. if (res.confirm) {
  150. let addressId = event.target.dataset.addressId;
  151. util.request(api.AddressDelete, {
  152. id: addressId
  153. }, 'POST').then(function (res) {
  154. if (res.errno === 0) {
  155. that.getAddressList();
  156. wx.removeStorage({
  157. key: 'addressId',
  158. success: function (res) {},
  159. })
  160. }
  161. });
  162. console.log('用户点击确定')
  163. }
  164. }
  165. })
  166. return false;
  167. },
  168. onHide: function () {
  169. // 页面隐藏
  170. },
  171. onUnload: function () {
  172. // 页面关闭
  173. }
  174. })