detail.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. let App = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. disabled: false,
  8. nav_select: false, // 快捷导航
  9. region: '',
  10. detail: {},
  11. error: '',
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function(options) {
  17. // 获取当前地址信息
  18. this.getAddressDetail(options.address_id);
  19. },
  20. /**
  21. * 获取当前地址信息
  22. */
  23. getAddressDetail: function(address_id) {
  24. let _this = this;
  25. App._get('address/detail', {
  26. address_id
  27. }, function(result) {
  28. _this.setData(result.data);
  29. });
  30. },
  31. /**
  32. * 表单提交
  33. */
  34. saveData: function(e) {
  35. let _this = this,
  36. values = e.detail.value
  37. values.region = this.data.region;
  38. // 表单验证
  39. if (!_this.validation(values)) {
  40. App.showError(_this.data.error);
  41. return false;
  42. }
  43. // 按钮禁用
  44. _this.setData({
  45. disabled: true
  46. });
  47. // 提交到后端
  48. values.address_id = _this.data.detail.address_id;
  49. App._post_form('address/edit', values, function(result) {
  50. App.showSuccess(result.msg, function() {
  51. wx.navigateBack();
  52. });
  53. }, false, function() {
  54. // 解除禁用
  55. _this.setData({
  56. disabled: false
  57. });
  58. });
  59. },
  60. /**
  61. * 表单验证
  62. */
  63. validation: function(values) {
  64. if (values.name === '') {
  65. this.data.error = '收件人不能为空';
  66. return false;
  67. }
  68. if (values.phone.length < 1) {
  69. this.data.error = '手机号不能为空';
  70. return false;
  71. }
  72. // if (values.phone.length !== 11) {
  73. // this.data.error = '手机号长度有误';
  74. // return false;
  75. // }
  76. let reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/;
  77. if (!reg.test(values.phone)) {
  78. this.data.error = '手机号不符合要求';
  79. return false;
  80. }
  81. if (!this.data.region) {
  82. this.data.error = '省市区不能空';
  83. return false;
  84. }
  85. if (values.detail === '') {
  86. this.data.error = '详细地址不能为空';
  87. return false;
  88. }
  89. return true;
  90. },
  91. /**
  92. * 修改地区
  93. */
  94. bindRegionChange: function(e) {
  95. this.setData({
  96. region: e.detail.value
  97. })
  98. },
  99. /**
  100. * 获取微信地址
  101. */
  102. chooseAddress: function() {
  103. let _this = this;
  104. wx.chooseAddress({
  105. success: function(res) {
  106. _this.setData({
  107. 'detail.name': res.userName,
  108. 'detail.phone': res.telNumber,
  109. 'detail.detail': res.detailInfo,
  110. region: [res.provinceName, res.cityName, res.countyName],
  111. });
  112. }
  113. })
  114. },
  115. })