newAddress.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. type: 0,//0为新建,1为编辑
  12. },
  13. onLoad: function (options) {
  14. console.log('options',options)
  15. if(options.type == 1){
  16. var address = wx.getStorageSync('editAddr');
  17. console.log('addrhere',address)
  18. this.setData({
  19. type: 1,
  20. address:address.province+address.city+address.district,
  21. region:[address.province,address.city,address.district],
  22. name:address.remark,
  23. tel:address.tel,
  24. name:address.contact,
  25. more:address.address,
  26. addrid:address.id
  27. })
  28. }
  29. },
  30. bindName: function (e) {
  31. this.setData({
  32. name: e.detail.value
  33. })
  34. },
  35. bindTel: function (e) {
  36. this.setData({
  37. tel: e.detail.value
  38. })
  39. },
  40. bindMore: function (e) {
  41. this.setData({
  42. more: e.detail.value
  43. })
  44. },
  45. validate () {
  46. var msg
  47. if (!validator.required(this.data.name)) {
  48. msg = '收货人不能为空'
  49. } else if (!validator.required1(this.data.name)) {
  50. msg = '国家安全局规定:运单禁止出现“先生”,“小姐”,“女士”等,请填写完整名字'
  51. } else if (!validator.required(this.data.tel)) {
  52. msg = '联系电话不能为空'
  53. } else if (!validator.telphone(this.data.tel)) {
  54. msg = '手机号码格式不正确'
  55. } else if (this.data.address === '请选择') {
  56. msg = '请选择所在地区'
  57. } else if (!validator.required(this.data.more)) {
  58. msg = '详细地址不能为空'
  59. }
  60. return { isOk: !msg, msg }
  61. },
  62. save: function (e) {
  63. _handle.setFormId(e)
  64. var { isOk, msg } = this.validate()
  65. if (isOk) {
  66. this.requestAddress()
  67. } else {
  68. wx.showToast({
  69. title: msg,
  70. icon: 'none',
  71. duration: 2000
  72. })
  73. }
  74. },
  75. requestAddress () {
  76. var that = this
  77. if(that.data.type == 1){
  78. var url = 'v1/address/'+that.data.addrid;
  79. }else{
  80. var url = 'v1/address';
  81. }
  82. var params = {
  83. contact: this.data.name,
  84. tel: this.data.tel,
  85. province: this.data.region[0],
  86. city: this.data.region[1],
  87. district: this.data.region[2],
  88. address: this.data.more,
  89. set_default: 1
  90. }
  91. var success = function (res) {
  92. wx.navigateBack({
  93. delta: 1
  94. })
  95. }
  96. if(that.data.type == 1){
  97. _request.$put(url, params, success)
  98. }else{
  99. _request.$post(url, params, success)
  100. }
  101. },
  102. bindRegionChange: function (e) {
  103. console.log(e)
  104. this.setData({
  105. region: e.detail.value,
  106. address: e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
  107. })
  108. },
  109. onShareAppMessage: function (val) {
  110. return _request.share({
  111. sc: 'xcx_user_newaddress'
  112. })
  113. }
  114. })