addressAdd.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // pages/addressAdd/addressAdd.js
  2. import { addAddress, editAddress, getRegion } 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. regionIndex: [0,0,0]
  16. },
  17. onShow () {
  18. this.setData({
  19. region: []
  20. })
  21. },
  22. onLoad (options) {
  23. this.getRegionFn()
  24. if (options.type && options.type == 0) {
  25. wx.setNavigationBarTitle({
  26. title: '编辑地址',
  27. })
  28. this.setData({
  29. isEdit: true,
  30. addressId: options.addressId,
  31. region: [options.province, options.city, options.county],
  32. contactPhone: options.contactPhone,
  33. contactName: options.contactName,
  34. detailAddress: options.detailAddress
  35. })
  36. }
  37. },
  38. // 获取地址的有效氛围
  39. getRegionFn () {
  40. getRegion({}).then(res => {
  41. console.log(res);
  42. })
  43. },
  44. bindMultiPickerChange: function (e) {
  45. console.log('picker发送选择改变,携带值为', e.detail.value)
  46. this.setData({
  47. regionIndex: e.detail.value
  48. })
  49. },
  50. bindMultiPickerColumnChange (e) {
  51. console.log(e);
  52. },
  53. /**
  54. * 保存地址
  55. * **/
  56. handleAddAddress () {
  57. if (this.valid()) {
  58. var that = this
  59. var data = {
  60. province: this.data.region[0],
  61. city: this.data.region[1],
  62. county: this.data.region[2],
  63. detailAddress: this.data.detailAddress,
  64. contactName: this.data.contactName,
  65. contactPhone: this.data.contactPhone,
  66. isDefault: false
  67. }
  68. wx.showLoading({
  69. title: '加载中...',
  70. mask: true
  71. })
  72. if (that.data.isEdit) {
  73. data.addressId = this.data.addressId
  74. editAddress(data).then(res => {
  75. wx.hideLoading()
  76. that.resetForm()
  77. wx.navigateBack()
  78. }).catch(e => {
  79. wx.hideLoading()
  80. wx.showModal({
  81. content: e,
  82. confirmColor: '#333',
  83. showCancel: false
  84. })
  85. })
  86. } else {
  87. addAddress(data).then(res => {
  88. wx.hideLoading()
  89. that.resetForm()
  90. wx.navigateBack()
  91. }).catch(e => {
  92. wx.hideLoading()
  93. wx.showModal({
  94. content: e,
  95. confirmColor: '#333',
  96. showCancel: false
  97. })
  98. })
  99. }
  100. }
  101. },
  102. bindinput () {},
  103. resetForm () {
  104. this.setData({
  105. region: [],
  106. detailAddress: '',
  107. contactName: '',
  108. contactPhone: ''
  109. })
  110. },
  111. // 提交表单验证
  112. valid () {
  113. var valid = false
  114. if (this.data.contactName == '') {
  115. wx.showToast({
  116. title: '请输入联系人',
  117. icon: 'error'
  118. })
  119. return
  120. }
  121. if (this.data.contactPhone == '') {
  122. wx.showToast({
  123. title: '请输入手机号',
  124. icon: 'error'
  125. })
  126. return
  127. }
  128. if (this.data.region.length == 0) {
  129. wx.showToast({
  130. title: '请选择地址',
  131. icon: 'error'
  132. })
  133. return
  134. }
  135. if (this.data.detailAddress == '') {
  136. wx.showToast({
  137. title: '请输入地址',
  138. icon: 'error'
  139. })
  140. return
  141. }
  142. valid = true
  143. return valid
  144. },
  145. /**
  146. * 用户点击区域选择
  147. */
  148. bindRegonPickerChange(e) {
  149. var value = e.detail.value
  150. console.log(value);
  151. this.setData({
  152. region: value
  153. })
  154. }
  155. })