index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import {
  2. editAddress,
  3. getAddressDetail
  4. } from '../../api/user.js';
  5. import {
  6. getCity
  7. } from '../../api/api.js';
  8. const app = getApp();
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. parameter: {
  15. 'navbar': '1',
  16. 'return': '1',
  17. 'title': '添加地址'
  18. },
  19. region: ['省', '市', '区'],
  20. valueRegion: [0, 0, 0],
  21. cartId: '', //购物车id
  22. pinkId: 0, //拼团id
  23. couponId: 0, //优惠券id
  24. id: 0, //地址id
  25. userAddress: {
  26. is_default: false
  27. }, //地址详情
  28. cityId: 0,
  29. district: [],
  30. multiArray: [],
  31. multiIndex: [0, 0, 0],
  32. },
  33. /**
  34. * 授权回调
  35. */
  36. onLoadFun: function () {
  37. this.getUserAddress();
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: function (options) {
  43. this.setData({
  44. cartId: options.cartId || '',
  45. pinkId: options.pinkId || 0,
  46. couponId: options.couponId || 0,
  47. id: options.id || 0,
  48. 'parameter.title': options.id ? '修改地址' : '添加地址'
  49. });
  50. this.getCityList();
  51. },
  52. getCityList: function () {
  53. let that = this;
  54. getCity().then(res => {
  55. that.setData({
  56. district: res.data
  57. });
  58. that.initialize();
  59. })
  60. },
  61. initialize: function () {
  62. let that = this,
  63. province = [],
  64. city = [],
  65. area = [];
  66. if (that.data.district.length) {
  67. let cityChildren = that.data.district[0].c || [];
  68. let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
  69. that.data.district.forEach(function (item) {
  70. province.push(item.n);
  71. });
  72. cityChildren.forEach(function (item) {
  73. city.push(item.n);
  74. });
  75. areaChildren.forEach(function (item) {
  76. area.push(item.n);
  77. });
  78. that.setData({
  79. multiArray: [province, city, area],
  80. });
  81. }
  82. },
  83. bindRegionChange: function (e) {
  84. let multiIndex = this.data.multiIndex,
  85. province = this.data.district[multiIndex[0]] || {
  86. c: []
  87. },
  88. city = province.c[multiIndex[1]] || {
  89. v: 0
  90. },
  91. multiArray = this.data.multiArray,
  92. value = e.detail.value;
  93. console.log(value);
  94. console.log(province);
  95. this.setData({
  96. region: [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]],
  97. cityId: city.v,
  98. valueRegion: [0, 0, 0]
  99. });
  100. this.initialize();
  101. },
  102. bindMultiPickerColumnChange: function (e) {
  103. let that = this,
  104. column = e.detail.column,
  105. value = e.detail.value,
  106. currentCity = this.data.district[value] || {
  107. c: []
  108. },
  109. multiArray = that.data.multiArray,
  110. multiIndex = that.data.multiIndex;
  111. multiIndex[column] = value;
  112. switch (column) {
  113. case 0:
  114. let areaList = currentCity.c[0] || {
  115. c: []
  116. };
  117. multiArray[1] = currentCity.c.map((item) => {
  118. return item.n;
  119. });
  120. multiArray[2] = areaList.c.map((item) => {
  121. return item.n;
  122. });
  123. break;
  124. case 1:
  125. let cityList = that.data.district[multiIndex[0]].c[multiIndex[1]].c || [];
  126. multiArray[2] = cityList.map((item) => {
  127. return item.n;
  128. });
  129. break;
  130. case 2:
  131. break;
  132. }
  133. this.setData({
  134. multiArray: multiArray,
  135. multiIndex: multiIndex
  136. });
  137. },
  138. getUserAddress: function () {
  139. if (!this.data.id) return false;
  140. let that = this;
  141. getAddressDetail(this.data.id).then(res => {
  142. var region = [res.data.province, res.data.city, res.data.district];
  143. that.setData({
  144. userAddress: res.data,
  145. region: region,
  146. cityId: res.data.city_id,
  147. });
  148. });
  149. },
  150. getWxAddress: function () {
  151. wx.showToast({
  152. title: '敬请期待',
  153. })
  154. // var that = this;
  155. // wx.authorize({
  156. // scope: 'scope.address',
  157. // success: function (res) {
  158. // wx.chooseAddress({
  159. // success: function (res) {
  160. // var addressP = {};
  161. // addressP.province = res.provinceName;
  162. // addressP.city = res.cityName;
  163. // addressP.district = res.countyName;
  164. // editAddress({
  165. // address: addressP,
  166. // is_default: 1,
  167. // real_name: res.userName,
  168. // post_code: res.postalCode,
  169. // phone: res.telNumber,
  170. // detail: res.detailInfo,
  171. // id: 0,
  172. // type: 1,
  173. // }).then(res => {
  174. // setTimeout(function () {
  175. // if (that.data.cartId) {
  176. // var cartId = that.data.cartId;
  177. // var pinkId = that.data.pinkId;
  178. // var couponId = that.data.couponId;
  179. // that.setData({
  180. // cartId: '',
  181. // pinkId: '',
  182. // couponId: ''
  183. // })
  184. // wx.navigateTo({
  185. // url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  186. // });
  187. // } else {
  188. // wx.navigateBack({
  189. // delta: 1
  190. // });
  191. // }
  192. // }, 1000);
  193. // return app.Tips({
  194. // title: "添加成功",
  195. // icon: 'success'
  196. // });
  197. // }).catch(err => {
  198. // return app.Tips({
  199. // title: err
  200. // });
  201. // });
  202. // },
  203. // fail: function (res) {
  204. // if (res.errMsg == 'chooseAddress:cancel') return app.Tips({
  205. // title: '取消选择'
  206. // });
  207. // },
  208. // })
  209. // },
  210. // fail: function (res) {
  211. // wx.showModal({
  212. // title: '您已拒绝导入微信地址权限',
  213. // content: '是否进入权限管理,调整授权?',
  214. // success(res) {
  215. // if (res.confirm) {
  216. // wx.openSetting({
  217. // success: function (res) {
  218. // console.log(res.authSetting)
  219. // }
  220. // });
  221. // } else if (res.cancel) {
  222. // return app.Tips({
  223. // title: '已取消!'
  224. // });
  225. // }
  226. // }
  227. // })
  228. // },
  229. // })
  230. },
  231. /**
  232. * 提交用户添加地址
  233. */
  234. formSubmit: function (e) {
  235. var that = this,
  236. value = e.detail.value;
  237. if (!value.real_name) return app.Tips({
  238. title: '请填写收货人姓名'
  239. });
  240. if (!value.phone) return app.Tips({
  241. title: '请填写联系电话'
  242. });
  243. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return app.Tips({
  244. title: '请输入正确的手机号码'
  245. });
  246. if (that.data.region[0] == '省') return app.Tips({
  247. title: '请选择所在地区'
  248. });
  249. if (!value.detail) return app.Tips({
  250. title: '请填写详细地址'
  251. });
  252. value.id = that.data.id;
  253. value.address = {
  254. province: that.data.region[0],
  255. city: that.data.region[1],
  256. district: that.data.region[2],
  257. city_id: that.data.cityId,
  258. };
  259. value.is_default = that.data.userAddress.is_default ? 1 : 0;
  260. editAddress(value).then(res => {
  261. if (that.data.id)
  262. app.Tips({
  263. title: '修改成功',
  264. icon: 'success'
  265. });
  266. else
  267. app.Tips({
  268. title: '添加成功',
  269. icon: 'success'
  270. });
  271. setTimeout(function () {
  272. if (that.data.cartId) {
  273. var cartId = that.data.cartId;
  274. var pinkId = that.data.pinkId;
  275. var couponId = that.data.couponId;
  276. that.setData({
  277. cartId: '',
  278. pinkId: '',
  279. couponId: ''
  280. })
  281. wx.navigateTo({
  282. url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  283. });
  284. } else {
  285. wx.navigateBack({
  286. delta: 1
  287. });
  288. }
  289. }, 1000);
  290. }).catch(err => {
  291. return app.Tips({
  292. title: err
  293. });
  294. })
  295. },
  296. ChangeIsDefault: function (e) {
  297. this.setData({
  298. 'userAddress.is_default': !this.data.userAddress.is_default
  299. });
  300. },
  301. })