addressAdd.js 3.4 KB

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