addressAdd.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. if (that.data.isEdit) {
  47. data.addressId = this.data.addressId
  48. editAddress(data).then(res => {
  49. that.resetForm()
  50. wx.navigateBack()
  51. })
  52. } else {
  53. addAddress(data).then(res => {
  54. that.resetForm()
  55. wx.navigateBack()
  56. })
  57. }
  58. }
  59. },
  60. bindinput () {},
  61. resetForm () {
  62. this.setData({
  63. region: [],
  64. detailAddress: '',
  65. contactName: '',
  66. contactPhone: ''
  67. })
  68. },
  69. // 提交表单验证
  70. valid () {
  71. var valid = false
  72. if (this.data.region.length == 0) {
  73. wx.showToast({
  74. title: '请选择地址',
  75. icon: 'error'
  76. })
  77. return
  78. }
  79. if (this.data.detailAddress == '') {
  80. wx.showToast({
  81. title: '请输入地址',
  82. icon: 'error'
  83. })
  84. return
  85. }
  86. if (this.data.contactName == '') {
  87. wx.showToast({
  88. title: '请输入联系人',
  89. icon: 'error'
  90. })
  91. return
  92. }
  93. if (this.data.contactPhone == '') {
  94. wx.showToast({
  95. title: '请输入手机号',
  96. icon: 'error'
  97. })
  98. return
  99. }
  100. valid = true
  101. return valid
  102. },
  103. /**
  104. * 用户点击区域选择
  105. */
  106. bindRegonPickerChange(e) {
  107. var value = e.detail.value
  108. console.log(value);
  109. this.setData({
  110. region: value
  111. })
  112. }
  113. })