newAddress.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var _request = require('../../../../utils/request.js')
  2. var validator = require('../../../../utils/validator.js')
  3. var _handle = require('../../../../utils/handle.js')
  4. Page({
  5. data: {
  6. address: '请选择',
  7. region: ['北京市', '北京市', '东城区'],
  8. name: '',
  9. tel: 0,
  10. more: ''
  11. },
  12. onLoad: function (options) {
  13. },
  14. bindName: function (e) {
  15. this.setData({
  16. name: e.detail.value
  17. })
  18. },
  19. bindTel: function (e) {
  20. this.setData({
  21. tel: e.detail.value
  22. })
  23. },
  24. bindMore: function (e) {
  25. this.setData({
  26. more: e.detail.value
  27. })
  28. },
  29. validate () {
  30. var msg
  31. if (!validator.required(this.data.name)) {
  32. msg = '收货人不能为空'
  33. } else if (!validator.required1(this.data.name)) {
  34. msg = '国家安全局规定:运单禁止出现“先生”,“小姐”,“女士”等,请填写完整名字'
  35. } else if (!validator.required(this.data.tel)) {
  36. msg = '联系电话不能为空'
  37. } else if (!validator.telphone(this.data.tel)) {
  38. msg = '手机号码格式不正确'
  39. } else if (this.data.address === '请选择') {
  40. msg = '请选择所在地区'
  41. } else if (!validator.required(this.data.more)) {
  42. msg = '详细地址不能为空'
  43. }
  44. return { isOk: !msg, msg }
  45. },
  46. save: function (e) {
  47. _handle.setFormId(e)
  48. var { isOk, msg } = this.validate()
  49. if (isOk) {
  50. this.requestAddress()
  51. } else {
  52. wx.showToast({
  53. title: msg,
  54. icon: 'none',
  55. duration: 2000
  56. })
  57. }
  58. },
  59. requestAddress () {
  60. var that = this
  61. var url = 'v1/address'
  62. var params = {
  63. contact: this.data.name,
  64. tel: this.data.tel,
  65. province: this.data.region[0],
  66. city: this.data.region[1],
  67. district: this.data.region[2],
  68. address: this.data.more,
  69. set_default: 1
  70. }
  71. var success = function (res) {
  72. wx.navigateBack({
  73. delta: 1
  74. })
  75. }
  76. _request.$post(url, params, success)
  77. },
  78. bindRegionChange: function (e) {
  79. console.log(e)
  80. this.setData({
  81. region: e.detail.value,
  82. address: e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
  83. })
  84. },
  85. onShareAppMessage: function (val) {
  86. return _request.share({
  87. sc: 'xcx_user_newaddress'
  88. })
  89. }
  90. })