index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.basePost(app.U({ c: 'user_api', a: 'edit_user_address' }), value,function(res){
  74. if (that.data.id) app.Tips({ title: '修改成功', icon: 'success'});
  75. else app.Tips({ title: '添加成功', icon: 'success'});
  76. setTimeout(function(){
  77. if (that.data.cartId) {
  78. var cartId = that.data.cartId;
  79. var pinkId = that.data.pinkId;
  80. var couponId = that.data.couponId;
  81. that.setData({cartId: '', pinkId: '',couponId: ''})
  82. wx.navigateTo({
  83. url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  84. });
  85. }else{
  86. wx.navigateBack({ delta: 1 });
  87. }
  88. },1000);
  89. });
  90. },
  91. ChangeIsDefault:function(e){
  92. this.setData({ 'userAddress.is_default': !this.data.userAddress.is_default});
  93. },
  94. })