index.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var app = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. parameter: {
  8. 'navbar': '1',
  9. 'return': '1',
  10. 'title': '添加地址'
  11. },
  12. region: ['省', '市', '区'],
  13. cartId:'',//购物车id
  14. pinkId:0,//拼团id
  15. couponId:0,//优惠券id
  16. id:0,//地址id
  17. userAddress: { is_default:false},//地址详情
  18. },
  19. /**
  20. * 授权回调
  21. *
  22. */
  23. onLoadFun:function(){
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. this.setData({
  30. cartId: options.cartId || '',
  31. pinkId: options.pinkId || 0,
  32. couponId: options.couponId || 0,
  33. id: options.id || 0,
  34. 'parameter.title': options.id ? '修改地址' : '添加地址'
  35. });
  36. this.getUserAddress();
  37. },
  38. bindRegionChange: function (e) {
  39. console.log('picker发送选择改变,携带值为', e.detail.value)
  40. this.setData({
  41. region: e.detail.value
  42. })
  43. },
  44. getUserAddress:function(){
  45. if(!this.data.id) return false;
  46. var that=this;
  47. app.baseGet(app.U({ c: 'user_api', a: 'get_user_address', q: { addressId:this.data.id}}),function(res){
  48. var region = [res.data.province, res.data.city, res.data.district];
  49. that.setData({
  50. userAddress:res.data,
  51. region: region,
  52. });
  53. });
  54. },
  55. /**
  56. * 提交用户添加地址
  57. *
  58. */
  59. formSubmit:function(e){
  60. var that = this, value = e.detail.value, formId=e.detail.formId;
  61. if (!value.real_name) return app.Tips({title:'请填写收货人姓名'});
  62. if (!value.phone) return app.Tips({title:'请填写联系电话'});
  63. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return app.Tips({title:'请输入正确的手机号码'});
  64. if (that.data.region[0] =='省') return app.Tips({title:'请选择所在地区'});
  65. if (!value.detail) return app.Tips({title:'请填写详细地址'});
  66. value.id=that.data.id;
  67. value.address={
  68. province:that.data.region[0],
  69. city: that.data.region[1],
  70. district: that.data.region[2],
  71. };
  72. value.is_default = that.data.userAddress.is_default ? 1 : 0;
  73. app.baseGet(app.U({ c: 'public_api', a: 'get_form_id', q: { formId: formId } }), null, null, true);
  74. app.basePost(app.U({ c: 'user_api', a: 'edit_user_address' }), value,function(res){
  75. if (that.data.id) app.Tips({ title: '修改成功', icon: 'success'});
  76. else app.Tips({ title: '添加成功', icon: 'success'});
  77. setTimeout(function(){
  78. if (that.data.cartId) {
  79. var cartId = that.data.cartId;
  80. var pinkId = that.data.pinkId;
  81. var couponId = that.data.couponId;
  82. that.setData({cartId: '', pinkId: '',couponId: ''})
  83. wx.navigateTo({
  84. url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  85. });
  86. }else{
  87. wx.navigateBack({ delta: 1 });
  88. }
  89. },1000);
  90. });
  91. },
  92. ChangeIsDefault:function(e){
  93. this.setData({ 'userAddress.is_default': !this.data.userAddress.is_default});
  94. },
  95. })