address.js 4.8 KB

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