审查视图

pages/order/order.js 14.8 KB
朱振飞 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    success: false,
    fail: false,
    name: '',
    addressId: 0,
    tel: '',
    area: '',
    address: '',
    cover_type: false,
王楠 authored
16 17 18 19 20 21 22 23 24 25 26
    total: '',
    info: {
      address: {
        address: '',
        area: '',
        id: '',
        tel: '',
        name: '',
        goods: []
      },
      goods: {}
朱振飞 authored
27
    },
王楠 authored
28 29 30 31 32 33 34 35 36 37 38 39 40
    totalnum: '',
    datak: 1,
    car_ids: [],
    user_ticket_id2: '',
    user_ticket_id1: '',
    coupon: '',
    couponone: '',
    nouse: '',
    obj: '',
    type: 0,
    combo_id: '',
    entrance_type: '',
    copyaddress: '',
朱振飞 authored
41 42 43
  },

  // 选择优惠券
王楠 authored
44 45
  selectcoupon(e) {
    let goods = this.data.info.goods;
朱振飞 authored
46
    console.log(goods)
王楠 authored
47 48
    let arr = []
    goods.forEach(function (value, index, array) {
朱振飞 authored
49 50
      arr.push(value.id)
    })
王楠 authored
51
    let couponid = e.currentTarget.dataset.id;
朱振飞 authored
52 53
    console.log(couponid)
    wx.navigateTo({
王楠 authored
54
      url: '/pages/lingqucoupon/lingqucoupon?type=' + couponid + '&total=' + this.data.info.real_total + '&goods_ids=' + JSON.stringify(arr)
朱振飞 authored
55 56 57
    })
  },
  // 领取优惠券
王楠 authored
58
  lingqucoupon() {
朱振飞 authored
59
    wx.navigateTo({
王楠 authored
60
      url: '/pages/coupon/coupon?type=' + 2
朱振飞 authored
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    })
  },
  //加减商品数量
  num_change(e) {
    let that = this
    let num = Number(e.currentTarget.dataset.num)
    let type = e.currentTarget.dataset.type
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let check = e.currentTarget.dataset.check
    let coudan = this.data.coudan
    let coudan_arr = this.data.coudan_arr
    if (type == 1) {
      num++
    } else {
      num--
      if (num < 1) {
        return
      }
    }
    that.setData({
      [`info.coudanProduct[${index}].num`]: num
    })
    if (check) {
      coudan.forEach((item, index) => {
        if (item.id == id) {
          item.num = num
          coudan_arr[index].num = num
        }
      })
      this.get_choudan()
    }
  },
  // 计算凑单费用
  get_choudan() {
    let coudan_arr = this.data.coudan_arr
    let choudan_money = 0;
    for (let obj of coudan_arr) {
      console.log(obj)
      console.log(obj.price2)
王楠 authored
101
      if (obj.price2 != null) {
朱振飞 authored
102 103 104 105 106 107 108 109 110 111
        console.log(111)
        choudan_money = (obj.price2 * 100 * obj.num) / 100 + choudan_money
      } else {
        console.log(222)
        choudan_money = (obj.price * 100 * obj.num) / 100 + choudan_money
      }
    }
    console.log(typeof (choudan_money))
    console.log(choudan_money)
    this.setData({
王楠 authored
112 113
      choudan_money: choudan_money * 100
      //  choudan_money: Math.floor(choudan_money* 100) / 100
朱振飞 authored
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
    })
  },
  show_cover() {
    let that = this
    if (!that.data.cover_type) {
      if (that.data.info.status == 0) {
        that.setData({
          fail: true
        })
        return
      }
      if (!that.data.info.address) {
        wx.showToast({
          title: '请添加地址',
          icon: 'none',
          duration: 1000
        })
        return
      }
    }
    that.setData({
      cover_type: !this.data.cover_type
    })
  },
  // 去添加地址
  add_address() {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  //选择凑单商品
  chooseGoods(e) {
    // coudan 凑单商品组成的数组
    // coudan_type 凑单的状态  可能废弃
    let that = this
    let coudan = this.data.coudan ? this.data.coudan : []
    let coudan_arr = this.data.coudan_arr ? this.data.coudan_arr : []
    let check = e.currentTarget.dataset.check
    let num = e.currentTarget.dataset.num
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let price = e.currentTarget.dataset.price;
    let price2 = e.currentTarget.dataset.price2;
    if (check) {
      check = 0
      coudan.forEach((item, index) => {
        if (item.id == id) {
          coudan.splice(index, 1)
          coudan_arr.splice(index, 1)
        }
      })
    } else {
      check = 1
      //选中
      coudan.push({
        id: id,
        num: num
      })
      coudan_arr.push({
        id: id,
        num: num,
        price: Number(price),
王楠 authored
176
        price2: Number(price2)
朱振飞 authored
177 178 179 180 181 182 183 184 185 186
      })
    }
    that.setData({
      coudan: coudan,
      coudan_arr: coudan_arr,
      [`info.coudanProduct[${index}].check`]: check
    })
    that.get_choudan()
  },
  //获取页面信息
王楠 authored
187
  get_info(combo_id, data, address_id, user_ticket_id1, user_ticket_id2) {
朱振飞 authored
188
    let that = this
王楠 authored
189 190 191 192 193 194 195
    let url = that.data.type == 4 ? app.interface.prev_pay : that.data.type == 3 ? app.interface.now_prev_pay : '/order/affirm';
    if (that.data.type == 3 || that.data.type == 4) {
      let params = {
        info: that.data.params,
        address_id: address_id
      }
      app.post(url, params).then((res) => {
196
          console.log('返回订单信息',res)
王楠 authored
197 198 199 200 201
        console.log('99999', res, Number(res.msg.total))
        res.msg.total = Number(res.msg.total * 100)
        res.msg.yunfei = Number(res.msg.yunfei * 100)
        let total = (res.msg.total + res.msg.yunfei).toFixed("1");
        console.log(total)
朱振飞 authored
202
王楠 authored
203 204 205
        that.setData({
          info: res.msg,
          total: total
朱振飞 authored
206 207

        })
王楠 authored
208 209 210 211 212

        console.log('9988756', that.data.info)

      })
    } else {
朱振飞 authored
213
      let params = {
王楠 authored
214 215 216 217 218
        combo_id: combo_id,
        data: data,
        address_id: address_id,
        user_ticket_id1: user_ticket_id1,
        user_ticket_id2: user_ticket_id2
朱振飞 authored
219 220
      }
      app.postk(url, params).then((res) => {
221
          console.log('订单',res)
朱振飞 authored
222
        console.log('99999', res, Number(res.data.total))
王楠 authored
223 224
        let coupon = wx.getStorageSync('coupon');
        if (coupon != '') {
朱振飞 authored
225
          this.setData({
王楠 authored
226
            coupon: wx.getStorageSync('coupon')
朱振飞 authored
227 228
          })
        }
王楠 authored
229 230
        let couponone = wx.getStorageSync('couponone');
        if (couponone != '') {
朱振飞 authored
231
          this.setData({
王楠 authored
232
            couponone: wx.getStorageSync('couponone')
朱振飞 authored
233 234 235
          })
        }
        console.log(this.data.couponone)
王楠 authored
236 237 238
        res.data.total = Number(res.data.total * 100) / 100
        res.data.freight = Number(res.data.freight * 100) / 100
        res.data.sum_goods_total = Number(res.data.sum_goods_total * 100) / 100
朱振飞 authored
239
        // let total = (res.msg.total + res.data.yunfei).toFixed("1");
王楠 authored
240
        console.log(res.data)
朱振飞 authored
241 242
        that.setData({
          info: res.data,
王楠 authored
243
          totalnum: res.data.goods.length,
朱振飞 authored
244
        })
王楠 authored
245 246 247 248 249 250 251 252 253 254 255 256
        let info = that.data.info
        console.log(info)
        info.address = {}
        info.address.address = that.data.copyaddress.address
        info.address.area = that.data.copyaddress.area
        info.address.id = that.data.copyaddress.id
        info.address.tel = that.data.copyaddress.tel
        info.address.name = that.data.copyaddress.name
        that.setData({
            info
        })
        console.log(info)
朱振飞 authored
257 258 259 260 261 262 263
        console.log('9988756', that.data.info.goods)
      }).catch(function (err) {
        console.log(err);
      });


    }
王楠 authored
264
朱振飞 authored
265 266 267 268 269 270 271 272 273 274 275 276 277 278
  },

  // get_info(type, info, id) {
  //   let that = this
  //   let url = type == 4 ? app.interface.prev_pay : app.interface.now_prev_pay
  //   let params = {
  //     info: info
  //   }
  //   app.post(url, params).then((res) => {
  //     console.log('99999', res, Number(res.msg.total))
  //     res.msg.total = Number(res.msg.total * 100)
  //     res.msg.yunfei = Number(res.msg.yunfei * 100)
  //     let total = (res.msg.total + res.msg.yunfei).toFixed("1");
  //     console.log(total)
王楠 authored
279
朱振飞 authored
280 281 282 283 284 285 286 287 288 289
  //     that.setData({
  //       info: res.msg,
  //       total:total

  //     })

  //     console.log('9988756', that.data.info)

  //   })
  // },
王楠 authored
290
  get_info1(combo_id, data, address_id, user_ticket_id1, user_ticket_id2) {
朱振飞 authored
291 292 293
    let that = this
    // let url='/order/affirm'
    // 判断进入入口
王楠 authored
294 295

    let url = type == 3 ? app.interface.now_prev_pay : app.interface.prev_pay
朱振飞 authored
296
    let params = {
王楠 authored
297 298 299 300 301
      combo_id: combo_id,
      data: data,
      address_id: address_id,
      user_ticket_id1: user_ticket_id1,
      user_ticket_id2: user_ticket_id2,
朱振飞 authored
302 303
    }
    app.postk(url, params).then((res) => {
王楠 authored
304 305
      let couponone = wx.getStorageSync('couponone');
      if (couponone != '') {
朱振飞 authored
306
        this.setData({
王楠 authored
307
          couponone: wx.getStorageSync('couponone')
朱振飞 authored
308 309 310
        })
      }
      console.log(this.data.couponone)
王楠 authored
311 312 313
      res.data.total = Number(res.data.total * 100) / 100
      res.data.freight = Number(res.data.freight * 100) / 100
      res.data.sum_goods_total = Number(res.data.sum_goods_total * 100) / 100
朱振飞 authored
314 315 316
      // let total = (res.msg.total + res.data.yunfei).toFixed("1");
      that.setData({
        info: res.data,
王楠 authored
317
        totalnum: res.data.goods.length
朱振飞 authored
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
      })
      console.log('9988756', that.data.info.goods)
    }).catch(function (err) {
      console.log(err);
    });
  },
  //获取页面信息
  get_youfei(id) {
    let that = this
    let url = that.data.type == 4 ? app.interface.youfei1 : app.interface.youfei
    let params = {
      info: that.data.params,
      address_id: id
    }
    app.post(url, params).then((res) => {
      that.setData({
王楠 authored
334
        "info.yunfei": Number(res.msg.yunfei) * 100
朱振飞 authored
335 336 337 338 339 340 341
      })
    })
  },
  //点击支付
  submit() {
    // let type = wx.getStorageSync('entrance_type');
    // if(type==0){
王楠 authored
342
    let that = this
朱振飞 authored
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    let list = that.data.info.goods
    let coudan = that.data.coudan
    let info = []
    if (that.data.type == 4) {
      for (let obj of list) {
        info.push({
          goods_id: obj.id,
          number: obj.number
        })
      }
    } else {
      for (let obj of list) {
        info.push({
          goods_id: obj.id,
          number: obj.number
        })
      }
    }
    if (coudan && coudan.length > 0) {
      info = info.concat(coudan)
    }
王楠 authored
364
    let arr = [];
朱振飞 authored
365 366
    arr.push(that.data.obj)
    let url = '/order/create_order';
王楠 authored
367 368
    console.log('9998887755', that.data.datak)
    let data = that.data.datak
朱振飞 authored
369 370
    let params = {
      combo_id: that.data.combo_id,
王楠 authored
371 372
      data: that.data.datak,
      car_ids: that.data.car_ids.join(","),
朱振飞 authored
373
      // info: JSON.stringify(info),
王楠 authored
374 375 376
      address_id: that.data.info.address.id,
      user_ticket_id1: that.data.user_ticket_id1,
      user_ticket_id2: that.data.user_ticket_id2,
朱振飞 authored
377 378 379 380 381 382 383 384 385 386 387 388
      // data:JSON.stringify(arr),
    }
    console.log(params)
    app.postk(url, params).then((res) => {
      that.setData({
        cover_type: false
      })
      that.order_pay(res.data.order_id)
    })
    // }else{
    //   this.submit_score()
    // }
王楠 authored
389
朱振飞 authored
390
  },
王楠 authored
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  //点击支付
  submit_score() {
    let that = this;
    let list = that.data.info.products
    let coudan = that.data.coudan
    let info = []
    if (that.data.type == 4) {
      for (let obj of list) {
        info.push({
          id: obj.product.id,
          num: obj.product.num
        })
      }
    } else {
      for (let obj of list) {
        info.push({
          id: obj.id,
          num: obj.num
        })
      }
      console.log(info)
朱振飞 authored
412
    }
王楠 authored
413 414
    if (coudan && coudan.length > 0) {
      info = info.concat(coudan)
朱振飞 authored
415
    }
王楠 authored
416 417 418 419 420 421 422 423
    let url = app.interface.now_add
    let params = {
      address: that.data.info.address.id,
      info: JSON.stringify(info)
    }
    app.post(url, params).then((res) => {
      that.setData({
        cover_type: false
朱振飞 authored
424
      })
王楠 authored
425 426 427 428 429 430 431 432 433 434 435
      that.order_pay_score(res.msg)
    })
  },

  //积分商城订单支付
  order_pay_score(order_odd) {
    let url = app.interface.order_pay
    let params = {
      order_odd: order_odd
    }
    app.post(url, params).then((res) => {
朱振飞 authored
436
      this.setData({
王楠 authored
437
        order: res.msg
朱振飞 authored
438
      })
王楠 authored
439 440 441 442 443 444 445 446 447 448 449
      if (res.msg.message == "支付成功") {
        this.setData({
          success: true
        })
      } else if (res.msg.message == "余额不足") {
        this.setData({
          fail: true
        })
      }
    })
  },
朱振飞 authored
450 451 452

  //开放订单支付
  order_pay(order_odd) {
王楠 authored
453
    let that = this;
朱振飞 authored
454 455 456 457 458
    let url = '/order/pay'
    let params = {
      order_id: order_odd
    }
    app.postk(url, params).then((res) => {
王楠 authored
459
      that.payment(res.data)
朱振飞 authored
460 461 462 463 464 465 466 467 468 469 470 471 472
    })
  },
  payment(res) {
    let that = this;
    console.log(347734748)
    var timeStamp = res.timeStamp.toString();
    // console.log(timeStamp);
    wx.requestPayment({
      timeStamp: timeStamp,
      nonceStr: res.nonceStr,
      package: res.package,
      signType: res.signType,
      paySign: res.paySign,
王楠 authored
473
      success: function (res) {
朱振飞 authored
474 475
        console.log(res)
        wx.redirectTo({
王楠 authored
476
          url: '/pages/order/order_list/order_list?type=' + 1 + '&statusk=' + 2,
朱振飞 authored
477 478
        })
      },
王楠 authored
479
      fail: function (res) {
朱振飞 authored
480 481 482 483 484 485 486 487
        console.log(123);
        console.log(res);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
王楠 authored
488 489 490
  onLoad: function (options) {

    if (options.obj) {
朱振飞 authored
491
      this.setData({
王楠 authored
492
        obj: JSON.parse(options.obj)
朱振飞 authored
493 494 495 496
      })
    }
    this.setData({
      type: options.type,
王楠 authored
497
      entrance_type: wx.getStorageSync('entrance_type'),
朱振飞 authored
498
      params: options.info,
王楠 authored
499
      datak: options.info,
朱振飞 authored
500
    })
王楠 authored
501
    if (options.car_id) {
朱振飞 authored
502
      this.setData({
王楠 authored
503
        car_ids: JSON.parse(options.car_id)
朱振飞 authored
504 505
      })
    }
王楠 authored
506 507
    console.log('343购物车id', this.data.car_ids)
    this.get_info('', options.info, '', '', '')
朱振飞 authored
508 509 510 511 512 513
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
王楠 authored
514
  onReady: function () {
朱振飞 authored
515 516 517 518 519 520

  },

  /**
   * 生命周期函数--监听页面显示
   */
王楠 authored
521
  onShow: function () {
朱振飞 authored
522 523
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]
王楠 authored
524 525
    console.log('887766554', this.data.params)
    console.log(currPage.data)
朱振飞 authored
526 527 528 529 530 531 532
    if (currPage.data.id) {
      this.setData({
        "info.address.address": currPage.data.address,
        "info.address.area": currPage.data.area,
        "info.address.id": currPage.data.id,
        "info.address.tel": currPage.data.tel,
        "info.address.name": currPage.data.name,
王楠 authored
533 534 535 536 537 538 539
        copyaddress: {
          address: currPage.data.address,
          area: currPage.data.area,
          id: currPage.data.id,
          tel: currPage.data.tel,
          name: currPage.data.name,
        }
朱振飞 authored
540 541 542
      })
      // this.get_youfei(currPage.data.user_ticket_id1)
    }
王楠 authored
543 544 545 546 547 548 549 550

    console.log(this.data.info.address.address)
    this.setData({
      user_ticket_id1: currPage.data.user_ticket_id1
    })

    this.get_info('', this.data.params, currPage.data.id, currPage.data.user_ticket_id1, currPage.data.user_ticket_id2)
朱振飞 authored
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    // if (currPage.data.user_ticket_id2) {
    //   console.log(66666,this.data.params)
    //   this.setData({
    //     user_ticket_id2:currPage.data.user_ticket_id2
    //   })
    //   this.get_info1('', this.data.params, '','',currPage.data.user_ticket_id2)
    // }
    // if (wx.getStorageSync('no_address')) {
    //   wx.setStorageSync('no_address', false)
    //   this.setData({
    //     "info.address": null
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
王楠 authored
569
  onHide: function () {
朱振飞 authored
570 571 572 573 574 575

  },

  /**
   * 生命周期函数--监听页面卸载
   */
王楠 authored
576
  onUnload: function () {
朱振飞 authored
577 578 579 580 581 582

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
王楠 authored
583
  onPullDownRefresh: function () {
朱振飞 authored
584 585 586 587 588 589

  },

  /**
   * 页面上拉触底事件的处理函数
   */
王楠 authored
590
  onReachBottom: function () {
朱振飞 authored
591 592 593 594 595 596

  },

  /**
   * 用户点击右上角分享
   */
王楠 authored
597
  onShareAppMessage: function () {
朱振飞 authored
598 599 600

  }
})