index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { getAddressList, setAddressDefault, delAddress, editAddress} from '../../api/user.js';
  2. var app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. parameter: {
  9. 'navbar': '1',
  10. 'return': '1',
  11. 'title': '地址管理'
  12. },
  13. addressList:[],
  14. cartId:'',
  15. pinkId:0,
  16. couponId:0,
  17. loading:false,
  18. loadend:false,
  19. loadTitle:'加载更多',
  20. page:1,
  21. limit:8,
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. this.setData({
  28. cartId: options.cartId || '',
  29. pinkId: options.pinkId || 0,
  30. couponId: options.couponId || 0,
  31. })
  32. },
  33. onShow:function(){
  34. var that = this;
  35. if (app.globalData.isLog) that.getAddressList(true);
  36. },
  37. onLoadFun:function(){
  38. this.getAddressList();
  39. },
  40. /*
  41. * 导入微信地址
  42. */
  43. getWxAddress: function () {
  44. var that = this;
  45. wx.authorize({
  46. scope: 'scope.address',
  47. success: function (res) {
  48. wx.chooseAddress({
  49. success: function (res) {
  50. var addressP = {};
  51. addressP.province = res.provinceName;
  52. addressP.city = res.cityName;
  53. addressP.district = res.countyName;
  54. editAddress({
  55. address: addressP,
  56. is_default: 1,
  57. real_name: res.userName,
  58. post_code: res.postalCode,
  59. phone: res.telNumber,
  60. detail: res.detailInfo,
  61. type:1,
  62. id: 0
  63. }).then(res=>{
  64. app.Tips({ title: "添加成功", icon: 'success' }, function () {
  65. that.getAddressList(true);
  66. });
  67. }).catch(err=>{
  68. return app.Tips({title:err});
  69. });
  70. },
  71. fail: function (res) {
  72. if (res.errMsg == 'chooseAddress:cancel') return app.Tips({ title:'取消选择'});
  73. },
  74. })
  75. },
  76. fail: function (res) {
  77. wx.showModal({
  78. title: '您已拒绝导入微信地址权限',
  79. content:'是否进入权限管理,调整授权?',
  80. success(res) {
  81. if (res.confirm) {
  82. wx.openSetting({
  83. success: function (res) {
  84. console.log(res.authSetting)
  85. }
  86. });
  87. } else if (res.cancel){
  88. return app.Tips({ title: '已取消!' });
  89. }
  90. }
  91. })
  92. },
  93. })
  94. },
  95. /**
  96. * 获取地址列表
  97. *
  98. */
  99. getAddressList: function (isPage){
  100. var that=this;
  101. if (isPage) that.setData({ loadend: false, page: 1, addressList:[]});
  102. if (that.data.loading) return;
  103. if (that.data.loadend) return;
  104. that.setData({ loading:true,loadTitle:''});
  105. getAddressList({ page: that.data.page, limit: that.data.limit }).then(res=>{
  106. var list = res.data;
  107. var loadend = list.length < that.data.limit;
  108. that.data.addressList = app.SplitArray(list, that.data.addressList);
  109. that.setData({
  110. addressList: that.data.addressList,
  111. loadend: loadend,
  112. loadTitle: loadend ? '我也是有底线的' : '加载更多',
  113. page: that.data.page + 1,
  114. loading: false,
  115. });
  116. }).catch(err=>{
  117. that.setData({ loading: false, loadTitle: '加载更多' });
  118. });
  119. },
  120. /**
  121. * 设置默认地址
  122. */
  123. radioChange:function(e){
  124. var index = parseInt(e.detail.value),that=this;;
  125. var address = this.data.addressList[index];
  126. if (address==undefined) return app.Tips({title:'您设置的默认地址不存在!'});
  127. setAddressDefault(address.id).then(res=>{
  128. for (var i = 0, len = that.data.addressList.length; i < len; i++) {
  129. if (i == index) that.data.addressList[i].is_default = true;
  130. else that.data.addressList[i].is_default = false;
  131. }
  132. app.Tips({ title: '设置成功', icon: 'success' }, function () {
  133. that.setData({ addressList: that.data.addressList });
  134. });
  135. }).catch(err=>{
  136. return app.Tips({title:err});
  137. });
  138. },
  139. /**
  140. * 编辑地址
  141. */
  142. editAddress:function(e){
  143. var cartId = this.data.cartId,pinkId = this.data.pinkId,couponId = this.data.couponId;
  144. this.setData({cartId: '',pinkId: '',couponId: ''})
  145. wx.navigateTo({
  146. url: '/pages/user_address/index?id=' + e.currentTarget.dataset.id + '&cartId=' + cartId + '&pinkId=' + pinkId + '&couponId=' + couponId
  147. })
  148. },
  149. /**
  150. * 删除地址
  151. */
  152. delAddress:function(e){
  153. var index = e.currentTarget.dataset.index, that = this, address=this.data.addressList[index];
  154. if (address == undefined) return app.Tips({ title: '您删除的地址不存在!' });
  155. delAddress(address.id).then(res=>{
  156. app.Tips({ title: '删除成功', icon: 'success' }, function () {
  157. that.data.addressList.splice(index, 1);
  158. that.setData({ addressList: that.data.addressList });
  159. });
  160. }).catch(err=>{
  161. return app.Tips({title:err});
  162. });
  163. },
  164. /**
  165. * 新增地址
  166. */
  167. addAddress: function () {
  168. var cartId = this.data.cartId,pinkId = this.data.pinkId,couponId = this.data.couponId;
  169. this.setData({ cartId: '', pinkId: '', couponId: ''})
  170. wx.navigateTo({
  171. url: '/pages/user_address/index?cartId=' + cartId + '&pinkId=' + pinkId + '&couponId=' + couponId
  172. })
  173. },
  174. goOrder:function(e){
  175. var id = e.currentTarget.dataset.id;
  176. var cartId = '';
  177. var pinkId = '';
  178. var couponId = '';
  179. if (this.data.cartId && id) {
  180. cartId = this.data.cartId;
  181. pinkId = this.data.pinkId;
  182. couponId = this.data.couponId;
  183. this.setData({
  184. cartId: '',
  185. pinkId: '',
  186. couponId: '',
  187. })
  188. wx.redirectTo({
  189. url: '/pages/order_confirm/index?is_address=1&cartId=' + cartId + '&addressId=' + id + '&pinkId=' + pinkId + '&couponId=' + couponId
  190. })
  191. }
  192. },
  193. /**
  194. * 页面上拉触底事件的处理函数
  195. */
  196. onReachBottom: function () {
  197. this.getAddressList();
  198. }
  199. })