addressAdd.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // pages/addressAdd/addressAdd.js
  2. import { addAddress, editAddress } from '../../api/address'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. region:[],
  9. detailAddress: '',
  10. contactName: '',
  11. contactPhone: '',
  12. rangText: '',
  13. isEdit: false,
  14. addressId: ''
  15. },
  16. onLoad (options) {
  17. if (options.type && options.type == 0) {
  18. wx.setNavigationBarTitle({
  19. title: '编辑地址',
  20. })
  21. this.setData({
  22. isEdit: true,
  23. addressId: options.addressId,
  24. region: [options.province, options.city, options.county],
  25. contactPhone: options.contactPhone,
  26. contactName: options.contactName,
  27. detailAddress: options.detailAddress
  28. })
  29. }
  30. },
  31. /**
  32. * 保存地址
  33. * **/
  34. handleAddAddress () {
  35. if (this.valid()) {
  36. var that = this
  37. var data = {
  38. province: this.data.region[0],
  39. city: this.data.region[1],
  40. county: this.data.region[2],
  41. detailAddress: this.data.detailAddress,
  42. contactName: this.data.contactName,
  43. contactPhone: this.data.contactPhone,
  44. isDefault: false
  45. }
  46. wx.showLoading({
  47. title: '加载中...',
  48. mask: true
  49. })
  50. if (that.data.isEdit) {
  51. data.addressId = this.data.addressId
  52. editAddress(data).then(res => {
  53. wx.hideLoading()
  54. that.resetForm()
  55. wx.navigateBack()
  56. }).catch(e => {
  57. wx.hideLoading()
  58. wx.showModal({
  59. content: e,
  60. confirmColor: '#333',
  61. showCancel: false
  62. })
  63. })
  64. } else {
  65. addAddress(data).then(res => {
  66. wx.hideLoading()
  67. that.resetForm()
  68. wx.navigateBack()
  69. }).catch(e => {
  70. wx.hideLoading()
  71. wx.showModal({
  72. content: e,
  73. confirmColor: '#333',
  74. showCancel: false
  75. })
  76. })
  77. }
  78. }
  79. },
  80. bindinput () {},
  81. resetForm () {
  82. this.setData({
  83. region: [],
  84. detailAddress: '',
  85. contactName: '',
  86. contactPhone: ''
  87. })
  88. },
  89. // 提交表单验证
  90. valid () {
  91. var valid = false
  92. if (this.data.region.length == 0) {
  93. wx.showToast({
  94. title: '请选择地址',
  95. icon: 'error'
  96. })
  97. return
  98. }
  99. if (this.data.detailAddress == '') {
  100. wx.showToast({
  101. title: '请输入地址',
  102. icon: 'error'
  103. })
  104. return
  105. }
  106. if (this.data.contactName == '') {
  107. wx.showToast({
  108. title: '请输入联系人',
  109. icon: 'error'
  110. })
  111. return
  112. }
  113. if (this.data.contactPhone == '') {
  114. wx.showToast({
  115. title: '请输入手机号',
  116. icon: 'error'
  117. })
  118. return
  119. }
  120. valid = true
  121. return valid
  122. },
  123. /**
  124. * 用户点击区域选择
  125. */
  126. bindRegonPickerChange(e) {
  127. var value = e.detail.value
  128. console.log(value);
  129. this.setData({
  130. region: value
  131. })
  132. }
  133. })