index.js 7.9 KB

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