addressAdd.js 3.4 KB

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