index.js 8.0 KB

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