buy.js 4.9 KB
Newer Older
G
ShopXO  
gongfuxiang 已提交
1 2 3 4 5 6 7
const app = getApp();
Page({
  data: {
    data_list_loding_status: 1,
    buy_submit_disabled_status: false,
    data_list_loding_msg: '',
    params: null,
G
buy  
gongfuxiang 已提交
8
    goods_list: [],
G
ShopXO  
gongfuxiang 已提交
9 10 11 12 13 14 15 16 17 18 19
    address: null,
    is_first: 1,
    address_id: 0,
    total_price: 0,
    user_note_value: '',
  },
  onLoad(params) {
    if((params.data || null) == null || app.get_length(JSON.parse(params.data)) == 0)
    {
      my.alert({
        title: '温馨提示',
G
buy  
gongfuxiang 已提交
20
        content: '订单信息有误',
G
ShopXO  
gongfuxiang 已提交
21 22 23 24 25 26
        buttonText: '确认',
        success: () => {
          my.navigateBack();
        },
      });
    } else {
G
buy  
gongfuxiang 已提交
27
      this.setData({ params: JSON.parse(params.data)});
G
ShopXO  
gongfuxiang 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

      // 删除地址缓存
      my.removeStorageSync({key: app.data.cache_buy_user_address_select_key});
    }
  },

  onShow() {
    my.setNavigationBar({title: app.data.common_pages_title.buy});
    this.init();
    this.setData({is_first: 0});
  },

  // 获取数据列表
  init() {
    // 本地缓存地址
    if(this.data.is_first == 0)
    {
      var cache_address = my.getStorageSync({
        key: app.data.cache_buy_user_address_select_key
      });
      if((cache_address.data || null) != null)
      {
        this.setData({
          address: cache_address.data,
          address_id: cache_address.data.id
        });
      }
    }

    // 加载loding
    my.showLoading({content: '加载中...'});
    this.setData({
      data_list_loding_status: 1
    });

G
buy  
gongfuxiang 已提交
63 64
    var data = this.data.params;
    data['address_id'] = this.data.address_id;
G
ShopXO  
gongfuxiang 已提交
65 66 67
    my.httpRequest({
      url: app.get_request_url("Index", "Buy"),
      method: "POST",
G
buy  
gongfuxiang 已提交
68
      data: data,
G
ShopXO  
gongfuxiang 已提交
69 70 71 72 73
      dataType: "json",
      success: res => {
        my.hideLoading();
        if (res.data.code == 0) {
          var data = res.data.data;
G
buy  
gongfuxiang 已提交
74
          if (data.goods_list.length == 0)
G
ShopXO  
gongfuxiang 已提交
75
          {
G
buy  
gongfuxiang 已提交
76
            this.setData({data_list_loding_status: 0});
G
ShopXO  
gongfuxiang 已提交
77
          } else {
G
buy  
gongfuxiang 已提交
78 79 80 81 82
            this.setData({
              goods_list: data.goods_list,
              total_price: data.base.total_price,
              address: data.base.address,
              address_id: ((data.base.address || null) == null) ? 0 : data.base.address.id,
G
ShopXO  
gongfuxiang 已提交
83 84 85 86
              data_list_loding_status: 3,
            });
          }
        } else {
G
buy  
gongfuxiang 已提交
87
          this.setData({
G
ShopXO  
gongfuxiang 已提交
88 89 90 91 92 93 94 95 96 97 98
            data_list_loding_status: 2,
            data_list_loding_msg: res.data.msg,
          });
          my.showToast({
            type: "fail",
            content: res.data.msg
          });
        }
      },
      fail: () => {
        my.hideLoading();
G
buy  
gongfuxiang 已提交
99
        this.setData({
G
ShopXO  
gongfuxiang 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
          data_list_loding_status: 2,
          data_list_loding_msg: '服务器请求出错',
        });
        
        my.showToast({
          type: "fail",
          content: "服务器请求出错"
        });
      }
    });
  },

  // 用户留言事件
  bind_user_note_event(e) {
    this.setData({user_note_value: e.detail.value});
  },

  // 提交订单
  buy_submit_event(e) {
    if((this.data.address_id || 0) == 0)
    {
      my.showToast({
        type: "fail",
        content: "请选择地址"
      });
      return false;
    }
    var self = this;
    // 加载loding
    my.showLoading({content: '提交中...'});
    this.setData({
      buy_submit_disabled_status: true,
    });

    my.httpRequest({
      url: app.get_request_url("Submit", "Buy"),
      method: "POST",
      data: {
        goods: this.data.params,
        address_id: this.data.address_id,
        user_note: this.data.user_note_value,
      },
      dataType: "json",
      success: res => {
        my.hideLoading();

        if (res.data.code == 0) {
          var data = res.data.data;
          if(data.status == 1)
          {
            my.confirm({
              title: '',
              content: res.data.msg,
              confirmButtonText: '立即支付',
              cancelButtonText: '进入订单',
              success: (result) => {
                self.setData({buy_submit_disabled_status: false});
                var is_pay = (result.confirm) ? 1 : 0;
                my.redirectTo({
                  url: '/pages/user-order/user-order?is_pay='+is_pay+'&order_id='+res.data.data.id
                });
              },
            });
          } else {
            my.showToast({
              type: "success",
              content: res.data.msg
            });
            setTimeout(function()
            {
              self.setData({buy_submit_disabled_status: false});
              my.redirectTo({
                url: '/pages/user-order/user-order'
              });
            }, 1000);
          }
        } else {
          self.setData({buy_submit_disabled_status: false});
          my.showToast({
            type: "fail",
            content: res.data.msg
          });
        }
      },
      fail: () => {
        my.hideLoading();
        self.setData({buy_submit_disabled_status: false});
        
        my.showToast({
          type: "fail",
          content: "服务器请求出错"
        });
      }
    });
  },

});