orders.js 18.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 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 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
import api from '../../common/api.js';
// import {wexinPay} from '../../utils/wxpay';
// import {
//   logEvent,
//   YB_PAGE_OPEN_L,
//   YB_MY_GRP_BUY_INV_C,
// } from '../../libs/analytics.js'
// import { listen } from '../../utils/login';
import { getYHStorageSync } from '../../utils/util';
// import { postFormId } from '../../libs/formIdCollectRequest.js'

const APP_SPACEORDERS_GET = "app.SpaceOrders.get";
const APP_SPACEORDERS_CLOSE = "app.SpaceOrders.close";
const APP_SPACEORDERS_DELORDERBYCODE = "app.SpaceOrders.delOrderByCode";
const APP_SPACEORDERS_CONFIRM = "app.SpaceOrders.confirm";

var app = getApp()
let PV_ID = new Date().getTime() + '';
let timer;

Page({
  data: {
    navData: [ '全部', '待付款', '待发货', '待收货' ],
    selectIndex: 0,
    orders:[],
    showEmptyContainer: false,
    from_page_name: '',
    from_page_param: '',
    current_page_name: 'order',
    current_page_param: '',
    tipimgsrc:'images/tan-ic@2x.png',
    arrowImg:'images/arrow_right@2x.png',
    refundReason:[],//退款理由
    showReason:false,
    reFaudCode:'',//申请退款的code
    reasonIndex:0,
    formId:'',
    orderCode:'',
  },
  
  onLoad:function(options){
    // 生命周期函数--监听页面加载
    if (options.index == "0") {
      this.data.selectIndex = 0;
    }
    else if (options.index == "1") {
      this.data.selectIndex = 1;
    }
    else if (options.index == "2") {
      this.data.selectIndex = 2;
    }
    else if (options.index == "3") {
      this.data.selectIndex = 3;
    }

    let from_page_name = options.page_name ? options.page_name : '';
    let from_page_param = options.page_param ? options.page_param : '';
    this.setData({
      from_page_name, from_page_param
    });
    this.setData({
      selectIndex: this.data.selectIndex
    });

    var pages = getCurrentPages()
    var currentPage = pages[pages.length - 1]
    var url = currentPage.route
    // let params = {
    //   PAGE_NAME: this.data.current_page_name,
    //   PAGE_PARAM: this.data.current_page_param,
    //   FROM_PAGE_NAME: this.data.from_page_name,
    //   FROM_PAGE_PARAM: this.data.from_page_param,
    //   PV_ID: PV_ID,
    //   PAGE_PATH: url        
    // };
    // logEvent(YB_PAGE_OPEN_L, params);

    // //订阅登录完成通知
    // listen(function (succeed) {
    //   if (succeed) {
    //     this.getOrders(this.data.selectIndex + 1);
    //     this.getRefundSeason();
    //   } else {
    //     let app = getApp()
    //     if (!app.getUid() || app.getUid()===0){
    //       tt.switchTab({
    //         url: '../../pages/userCenter/userCenter',
    //       })
    //     }
    //   }
    // }.bind(this))
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
  },
  onShow:function(){
    // 生命周期函数--监听页面显示
    this.getOrders(this.data.selectIndex + 1);
    //获取退款理由
    this.getRefundSeason();
  },
  onHide:function(){
    // 生命周期函数--监听页面隐藏
    this.stopTimer();
  },
  onUnload:function(){
    // 生命周期函数--监听页面卸载
    this.stopTimer();
  },
  onPullDownRefresh: function() {
    // let params = {
    //   PAGE_NAME: this.data.current_page_name,
    //   PAGE_PARAM: this.data.current_page_param,
    //   FROM_PAGE_NAME: this.data.from_page_name,
    //   FROM_PAGE_PARAM: this.data.from_page_param,
    //   PV_ID: PV_ID,
    // };
    // logEvent(YB_PAGE_OPEN_L, params);
    // 页面相关事件处理函数--监听用户下拉动作
  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
  },

  formSubmit: function (e) {
    // // console.log('####formID:', e.detail.formId)
    // let formId = e.detail.formId;
    // this.setData({
    //   formId,
    // })
    // postFormId(formId,"7")
  },

  getRefundSeason:function(){
    let param={
      method:'app.SpaceOrders.refundApplyReasons',
      fromPage:'aFP_MineOrderContent'
    }

    api.get({data: param})
    .then(json=>{
      if(json && json.code == 200){
        // console.log('退货理由获取成功')
        this.setData({
          refundReason:json.data
        })
      }
    })
  },
  fraudRemindAction: function() {
    // console.log("tap 防诈骗提醒");
  },

  formatImgUrl: function(json) {
    json.data.order_list && json.data.order_list.map((item, index) => {
        item.order_goods && item.order_goods.map((good, good_index) => {
          let replaceStr = "{width}";
          let url = good.goods_image;

          url = url.replace(new RegExp('{width}', 'gm'), '65').replace(new RegExp('{height}', 'gm'), '100');
          good.goods_image = url;
        });
    });

    // console.log(json);
  },

  getOrders: function(mType) {
    // console.log("getOrders");
    tt.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 100000
    });

    let param = {
      method: APP_SPACEORDERS_GET,
      type: mType,
      uid: app.getUid(),
      page: 1,
      limit: 50
    }

    api.get({data: param})
    .then(json => {
      tt.hideToast();
      if (json && json.code && json.code == 200) {
        this.formatImgUrl(json);
        let orders = json.data.order_list;
        orders = this.formatOrders(orders);
        let selectIndex = mType - 1;
        this.setData({
          orders,
          selectIndex,
          showEmptyContainer: json.data.order_list == 0
        });
      }
    })
    .catch(error => {
       tt.hideToast()
    });
  },
  formatOrders:function(orders){
    let _orders=[];
    let needTimer = false;

    orders && orders.map((item,index)=>{
      item.pay_lefttime = parseInt(item.pay_lefttime);
      item.time_left = this.formatDate(parseInt(item.pay_lefttime));
      if (item.counter_flag == 'Y' && item.is_cancel != 'Y' && parseInt(item.pay_lefttime) > 0) {
        needTimer = true;
      }
      let order_goods = item.order_goods;
      for (var i = 0; i < item.order_goods.length;i++){
        if (new Number(item.order_goods[i].real_pay_price).toFixed(2) == new Number(item.order_goods[i].sales_price).toFixed(2)  || !item.order_goods[i].real_pay_price){
          item.order_goods[i].real_pay_price = item.order_goods[i].goods_price ? item.order_goods[i].goods_price : item.order_goods[i].sales_price;
          item.order_goods[i].sales_price = '';
          }
        if (item.order_goods[i].sales_price){
          item.order_goods[i].sales_price = new Number(item.order_goods[i].sales_price).toFixed(2);
        }
      }
      _orders.push(item);
    })

    if (needTimer){
      this.stopTimer();
      this.startTimer();
    }else {
      this.stopTimer();
    }

    return _orders;
  },
  onSelectChanged: function(e) {
    let mType = e.detail.currentTarget.dataset.type;
    // console.log(mType);
    this.getOrders(mType);
  },
  /**
   * 取消订单
   */
  onCloseOrderTapped: function(e) {
    var code = e.detail.currentTarget.dataset.order_code;
    var tabStatus = e.detail.currentTarget.dataset.tab_index;
    var _self = this;
    var app = getApp();
    this.setData({
      orderCode: code,
    });

    tt.showModal({
      title: tabStatus == '0' ? '取消订单' :'申请退款',
      // content: '订单取消后不能恢复,确认取消此订单么?',
      content: tabStatus == '0' ? '确认取消该订单吗?' :'确认申请退货该订单吗?',
      cancelText: '返回',
      confirmText: tabStatus == '0' ?'取消订单':'申请退款',
      confirmColor: '#000000',
      success: function(res) {
        if (res.confirm) {
          let param = {
            method: APP_SPACEORDERS_CLOSE,
            order_code: code,
            uid: app.getUid(),
            miniapp_type: app.globalData.miniapp_type,
          }
          api.get({data: param})
          .then((json) => {
            if (json && json.code && json.code == 200) {
              _self.getOrders(_self.data.selectIndex + 1);
            }
            else if (json && json.code && json.code != 200){
              tt.showModal({title: json.message, showCancel:false})
            }
          })
          .catch(error => {
          })
        }
      }
    })
  },
  /**
   * 确认收货
   */
  onConfirmTapped: function(e) {
    var code = e.detail.currentTarget.dataset.order_code;
    var _self = this;
    var app = getApp();

    this.setData({
      orderCode:code,
    });

    // console.log('orderCode',code)

    // _self.postFormId()

    tt.showModal({
      title: '确认收货',
      content: '请您收到货后再点击“确认”!',
      cancelText: '取消',
      confirmText: '确定',
      confirmColor: '#FF0000',
      success: function(res) {
        if (res.confirm) {
          let param = {
            method: APP_SPACEORDERS_CONFIRM,
            order_code: code,
            uid: app.getUid(),
            miniapp_type: app.globalData.miniapp_type,
          }
          api.get({data: param})
          .then((json) => {
            if (json && json.code && json.code == 200) {
              _self.getOrders(_self.data.selectIndex + 1);
            }
            else {

            }
          })
          .catch(error => {
          })
        }
      }
    })
  },
  //拼团购商品 点击立即购买
  onBuyFromApp:function(e){
    tt.showModal({
      title: '提示',
      content: "拼团订单只能在APP支付,请在应用市场搜索下载“Yoho!buy有货”,使用微信登录完成支付",
      confirmText:'确定',
      showCancel:false,
      success:function(res){
        if(res.confirm){

        }
      }
    })
  },
  /**
   * 立即付款
   */
  onBuyNowTapped: function(e) {
    let mCode = e.target.dataset.order_code;
    let group_no = e.target.dataset.group_no ? e.target.dataset.group_no:'';
    let activity_id = e.target.dataset.activity_id ? e.target.dataset.activity_id:'';
    let mOrder = {};
    let payParam = {};
    if (group_no && group_no != '') {
      payParam = {
        group_no,
        activity_id,
        fromPage: "groupPurchaseDetail"
      }
    }
    mOrder.order_code = mCode;
    // wexinPay(mOrder, payParam)
  },
  /**
   * 删除订单
   */
  onDelOrderTapped: function(e) {
    var code = e.detail.currentTarget.dataset.order_code;
    var _self = this;
    tt.showModal({
      content: '确认删除该订单吗?删除了的订单不可恢复!',
      cancelText: '取消',
      confirmText: '确定',
      confirmColor: '#FF0000',
      success: function(res) {
        if (res.confirm) {
          let param = {
            method: APP_SPACEORDERS_DELORDERBYCODE,
            order_code: code,
            uid: app.getUid()
          }
          api.get({data: param})
          .then((json) => {
            if (json && json.code && json.code == 200) {
              _self.getOrders(_self.data.selectIndex + 1);
            }
          })
          .catch(error => {
          })
        }
      }
    })
  },
  //申请退款
  onRefundApplyTapped:function(e){
    let that = this;
    var code = e.detail.currentTarget.dataset.order_code;
    this.setData({
      orderCode: code,
    });

    // that.postFormId()

    tt.showModal({
      content: '申请退款后,本单享有的优惠可能会一并取消,确定申请吗?',
      cancelText: '取消',
      confirmText: '确定',
      confirmColor: '#FF0000',
      success:function(res){
        if (res.confirm) {
          // console.log('用户点击确定')
          that.setData({
            showReason: true,
            reFaudCode: code,
          })
        } 
      }
    })
  },
  //选定退货理由
  reasonSure: function (e) {
    // console.log('选定退货理由');
    let app = getApp();
    let that = this;
    if (!this.data.refundReason || this.data.refundReason.length < 0) {
      return;
    }
    let reasonIndex = this.data.reasonIndex;

    // console.log("下标" + reasonIndex)
    let reason_id = this.data.refundReason[reasonIndex].id;
    let reason = this.data.refundReason[reasonIndex].reason;
    let order_code = this.data.reFaudCode;
    this.setData({
      reasonIndex: 0
      
    })
    if (order_code) {
      this.setData({
        orderCode: order_code,
      });

      // that.postFormId()

      let param = {
        method: 'app.SpaceOrders.refundApply',
        reason_id,
        reason,
        order_code,
        fromPage: "aFP_MineOrderContent",
        miniapp_type: app.globalData.miniapp_type,
        formId: that.data.formId,
        openId: app.globalData.openID ? app.globalData.openID : getYHStorageSync('openID','order'),

      }
      api.get({data: param})
      .then(json => {
          this.hidderPicker();
          if (json && json.code == 200) {
            this.getOrders(this.data.selectIndex + 1);
          }
        })
        .catch(error => {
          this.hidderPicker();
        })
    }

  },
  chooseReason:function(e){
  
    let reason_id = e.currentTarget.dataset.id;
    let reason = e.currentTarget.dataset.reason;
    let order_code = this.data.reFaudCode;
    if (order_code){
      let param={
        method:'app.SpaceOrders.refundApply',
        reason_id,
        reason,
        order_code,
        fromPage:"aFP_MineOrderContent"
      }
      api.get({data: param})
      .then(json=>{
        this.hidderPicker();
        if(json && json.code == 200){
          this.getOrders(this.data.selectIndex + 1);
        }
      })
      .catch(error=>{
        this.hidderPicker();
      })
    }
    
  },
  bindChange:function(e){
    // console.log(e);
    let value = e.detail.value;
    this.setData({
      reasonIndex: value[0]
    })
  },
  hidderPicker:function(){
    this.setData({
      showReason: false,
      reFaudCode:''
    })
  },
  onExpressTapped:function(e){
    var code = e.detail.currentTarget.dataset.order_code;
    // console.log("查看物流");
    tt.navigateTo({
      url: '../logisticsDetails/logisticsDetails?order_code=' + code,
    })
  },
  /**
   * 申请售后
   */
  onAfterServiceTapped: function(e) {
    tt.showModal({
      title: '提示',
      content:'小程序暂不支持退换货,请使用有货APP或官网退换货!',
      // content: '请至微信-有货服务号-YOHOBUY有货,联系在线客服处理',
      confirmText: '确定',
    })
  },
  /**
   * 再次购买
   */
  onReaddTapped:function(e){
    // console.log("再次购买tab");
    var code = e.detail.currentTarget.dataset.order_code;
    var that = this;
    tt.showModal({
      title: '确认将商品再次加入购物车?',
      cancelText:'取消',
      confirmText:'确定',
      confirmColor: '#FF0000',
      success: function (res) {
        if (res.confirm) {
          // console.log("Click confirm button");
          let param={
            method:"app.Shopping.readd",
            order_code: code,
            fromPage:"aFP_MineOrderContent"
          }
          api.get({data: param})
          .then(json =>{
            if(json && json.code && json.code == 200){
              tt.showToast({
                title: '加入购物车成功',
                complete:function(){                  
                  tt.switchTab({
                    url: '../shopCart/shopCart',
                  })
                }
              })
            }
          })
        }
      }
    })
  },
  /**
   * 跳转到订单详情页面
   */
  navToOrderDetail: function(e) {
    let order_code = e.detail.currentTarget.dataset.order_code;
    if (e.detail.currentTarget.dataset.attribute === "3"){
      tt.navigateTo({
        url: "visualOrderDetail/visualOrderDetail?order_code=" + order_code + '&page_name=' + this.data.current_page_name + '&page_param=' + this.data.current_page_param
      });
    } else {
    tt.navigateTo({
      url: "./orderDetail/orderDetail?order_code=" + order_code + '&page_name=' + this.data.current_page_name + '&page_param=' + this.data.current_page_param
    });
    }
  },
  output:function (log) {
    // console.log(log);
  },
  /**
   * 去逛逛
   */
  goBackToTab:function() {
    tt.navigateTo({
      url: '../goodsList/newArrival' + '?page_name=' + this.data.current_page_name + '&page_param=' + this.data.current_page_param
    })
  },
  goTODefraudPage: function () {
    tt.navigateTo({
      url: './antifraud/antifraud',
    })
  },


  startTimer: function (e) {
    let that = this;
    timer = setInterval(function () {
      let orders = that.data.orders;
      for (var i = 0; i < orders.length; i++) {
        let item = orders[i];
        let pay_lefttime = parseInt(item.pay_lefttime);
        if (item.counter_flag == 'Y' && item.is_cancel != 'Y' && pay_lefttime  > 0){
          item.pay_lefttime = pay_lefttime - 1;
          item.time_left = that.formatDate(pay_lefttime - 1);
          if (item.pay_lefttime == 0){
            that.getOrders(that.data.selectIndex + 1);
          }
        }else{
          item.pay_lefttime = 0;
        }
      }
      that.setData({
        orders,
      })
    }, 1000);
  },

  stopTimer: function(e) {
    // console.log('stopTimer');
    clearInterval(timer);
  },


  add0: function (m) {
    return m < 10 ? '0' + m : m
  },

  formatDate: function (second) {
    var dateStr = "";
    var hr = Math.floor(second / 3600);
    var min = Math.floor((second - hr * 3600) / 60);
    var sec = (second - hr * 3600 - min * 60);// equal to => var sec = second % 60;
    dateStr = this.add0(hr) + ":" + this.add0(min) + ":" + this.add0(sec);
    return dateStr;
  },
  onShareAppMessage: function (res) {
    return {
      title: '我在有货给你发红包啦,专属好礼送不停!',
      path: '/pages/envelopesActivity/envelopesActivity?token=' + this.data.token,
      imageUrl: 'https://cdn.yoho.cn/20171218/fenxianghongbao.png',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  gotoGroupBuyDetail:function(res){
    let order_code = res.target.dataset.order_code ? res.target.dataset.order_code:'';
    let group_no = res.target.dataset.group_no ? res.target.dataset.group_no : '';
    let activity_id = res.target.dataset.activity_id ? res.target.dataset.activity_id : '';
    // let param = {
    //   ORDER_NUM: order_code,
    //   GRP_ID: group_no + '',
    // }
    // logEvent(YB_MY_GRP_BUY_INV_C, param);
    tt.navigateTo({
      url: "../groupPurchase/groupPurchaseResult?activity_id=" + activity_id + '&group_no=' + group_no + '&page_name=' + 'orders' + '&page_param=' + this.data.current_page_param

    });
  }
})