orders.js 20.8 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 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
import {API_HOST, SERVICE_HOST} from '../../libs/config';
import {GET, POST} from '../../libs/request';
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.list";
const APP_SPACEORDERS_CLOSE = "app.SpaceOrders.close";
const APP_SPACEORDERS_DELORDERBYCODE = "app.SpaceOrders.delOrderByCode";
const APP_SPACEORDERS_CONFIRM = "app.SpaceOrders.confirm";
const APP_SPACEORDERS_DEPOSITTAIL_ASSETS_ROLLBACK =
  "app.Shopping.depositTail.assets.rollback"
var app = getApp()
let PV_ID = new Date().getTime() + '';
let timer;

Page({
  data: {
    index: 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:'',

    closeOrder_tips: '订单取消后不能恢复,确定取消吗?'
  },
  
  onLoad:function(options){
    // 生命周期函数--监听页面加载
    if (options.index == "0") {
      this.data.index = 0;
    }
    else if (options.index == "1") {
      this.data.index = 1;
    }
    else if (options.index == "2") {
      this.data.index = 2;
    }
    else if (options.index == "3") {
      this.data.index = 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({
      index: this.data.index
    });

    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.index + 1);
        this.getRefundSeason();
      } else {
        let app = getApp()
        if (!app.getUid() || app.getUid()===0){
          wx.switchTab({
            url: '../../pages/userCenter/userCenter',
          })
        }
      }
    }.bind(this))
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
  },
  onShow:function(){
    // 生命周期函数--监听页面显示
    this.getOrders(this.data.index + 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() {
    // 页面上拉触底事件的处理函数
  },

  onCloseTailOrderTapped(e) {
    var code = e.currentTarget.dataset.order_code;
    let closeOrder_tips = e.currentTarget.dataset.tips;
    var app = getApp();
    var _self = this;

    let content = this.data.closeOrder_tips;

    if (closeOrder_tips) {
      content = closeOrder_tips;
    }

    _self.setData({
      order_code: code,
    });
       
    wx.showModal({
      title: '提示',
      content: content,
      cancelText: '确定',
      cancelColor: '#e0e0e0',
      confirmText: '算了',
      confirmColor: "#d0021b",
      success: function (res) {
        if (res.cancel) {
          let param = {
            method: APP_SPACEORDERS_DEPOSITTAIL_ASSETS_ROLLBACK,
            order_code: code,
            uid: app.getUid(),
            miniapp_type: app.globalData.miniapp_type,
          }
          GET(API_HOST, param)
            .then((json) => {
              if (json && json.code && json.code == 200) {
                _self.getOrders(_self.data.index + 1);
              }
              else if (json && json.code && json.code != 200) {
                wx.showModal({ title: json.message, showCancel: false })
              }
            })
            .catch(error => {
            })
        }
      }
    });
  },
  formSubmit: function (e) {
    // console.log('####formID:', e.detail.formId)
    let app = getApp();
    let _self = this;
    let formId = e.detail.formId;
    _self.setData({
      formId,
    })
    postFormId(formId,"7")
  },

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

    GET(API_HOST,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");
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 100000
    });
    if (!app.getUid() || app.getUid() === 0) {
      wx.navigateTo({
        url: '../bindPhoneNumber/bindPhoneNumber',
      })
      return;
    }

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

    GET(API_HOST, param)
    .then(json => {
      wx.hideToast();
      if (json && json.code && json.code == 200) {
        this.formatImgUrl(json);
        let orders = json.data.order_list;
        orders = this.formatOrders(orders);
        let index = mType - 1;
        this.setData({
          orders,
          index,
          showEmptyContainer: json.data.order_list == 0
        });
      }
    })
    .catch(error => {
       wx.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.currentTarget.dataset.type;
    // console.log(mType);
    this.getOrders(mType);
  },
  /**
   * 取消订单
   */
  onCloseOrderTapped: function(e) {
    var code = e.currentTarget.dataset.order_code;
    var tabStatus = e.currentTarget.dataset.tab_index;
    let closeOrder_tips = e.currentTarget.dataset.tips;
    let refundApply_tips = e.currentTarget.dataset.refund_tips;

    let content = tabStatus == '0' ? '确认取消该订单吗?' : '确认申请退货该订单吗?';

    if (closeOrder_tips) {
      content = closeOrder_tips;
    }

    if (refundApply_tips) {
      content = refundApply_tips;
    }

    var _self = this;
    var app = getApp();
    this.setData({
      orderCode: code,
    });

    wx.showModal({
      title: tabStatus == '0' ? '取消订单' :'申请退款',
      // content: '订单取消后不能恢复,确认取消此订单么?',
      content: content,
      cancelText: '确定',
      cancelColor: '#e0e0e0',
      confirmText: '算了',
      confirmColor: "#d0021b",
      success: function(res) {
        if (res.cancel) {
          let param = {
            method: APP_SPACEORDERS_CLOSE,
            order_code: code,
            uid: app.getUid(),
            miniapp_type: app.globalData.miniapp_type,
          }
          GET(API_HOST, param)
            .then((json) => {
            if (json && json.code && json.code == 200) {
              _self.getOrders(_self.data.index + 1);
            }
            else if (json && json.code && json.code != 200){
              wx.showModal({title: json.message, showCancel:false})
            }
          })
          .catch(error => {
          })
        }
      }
    })
  },
  /**
   * 确认收货
   */
  onConfirmTapped: function(e) {
    var code = e.currentTarget.dataset.order_code;
    var _self = this;
    var app = getApp();

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

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

    // _self.postFormId()

    wx.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,
          }
          GET(API_HOST, param)
            .then((json) => {
            if (json && json.code && json.code == 200) {
              _self.getOrders(_self.data.index + 1);
            }
            else {

            }
          })
          .catch(error => {
          })
        }
      }
    })
  },
  //拼团购商品 点击立即购买
  onBuyFromApp:function(e){
    wx.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 payment_stage = e.target.dataset.payment_stage ? e.target.dataset.payment_stage : '';
    let payment_amount;
    if (e.target.dataset.amount) {
      payment_amount = e.target.dataset.amount;
    }
    if (e.target.dataset.order_deposit_amount) {
      payment_amount = e.target.dataset.order_deposit_amount;
    }
    let that = this;
    if (payment_stage === '1') {
      // wx.showModal({
      //   title: "提示",
      //   content: '预售商品定金不支持退款,如需取消订单,只退等同于定金30%的有货币,同意后方可下单。',
      //   showCancel: true,
      //   cancelText: "我再想想",
      //   confirmText: "同意下单",
      //   success: function (res) {
      //     if (res.cancel) {
      //     } else {
            let mOrder = { payment_amount };
            let payParam = {};
            if (group_no && group_no != '') {
              payParam = {
                group_no,
                activity_id,
                fromPage: "groupPurchaseDetail"
              }
            }
            mOrder.order_code = mCode;
            wexinPay(mOrder, payParam);
      //     }
      //   }
      // });
      return;
    } else if (payment_stage === '3') {
      that.navToOrderDetail(e);
      return;
    }
    let mOrder = { payment_amount };
    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.currentTarget.dataset.order_code;
    var _self = this;
    wx.showModal({
      content: '确认删除该订单吗?删除了的订单不可恢复!',
      cancelText: '取消',
      confirmText: '确定',
      confirmColor: '#FF0000',
      success: function(res) {
        if (res.confirm) {
          let param = {
            method: APP_SPACEORDERS_DELORDERBYCODE,
            order_code: code,
            uid: app.getUid()
          }
          GET(API_HOST, param)
            .then((json) => {
            if (json && json.code && json.code == 200) {
              _self.getOrders(_self.data.index + 1);
            }
          })
          .catch(error => {
          })
        }
      }
    })
  },
  //申请退款
  onRefundApplyTapped:function(e){
    let that = this;
    var code = e.currentTarget.dataset.order_code;
    let refundApply_tips = e.currentTarget.dataset.refund_tips;

    let content = '申请退款后,本单享有的优惠可能会一并取消,确定申请吗?';
    this.setData({
      orderCode: code,
    });

    // that.postFormId()

    if (refundApply_tips) {
      content = refundApply_tips;
    }

    wx.showModal({
      content: content,
      cancelText: '确定',
      cancelColor: '#e0e0e0',
      confirmText: '算了',
      confirmColor: "#d0021b",
      success:function(res){
        if (res.cancel) {
          // 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'),

      }
      GET(API_HOST, param)
        .then(json => {
          this.hidderPicker();
          if (json && json.code == 200) {
            this.getOrders(this.data.index + 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"
      }
      GET(API_HOST,param)
      .then(json=>{
        this.hidderPicker();
        if(json && json.code == 200){
          this.getOrders(this.data.index + 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.currentTarget.dataset.order_code;
    // console.log("查看物流");
    wx.navigateTo({
      url: '../logisticsDetails/logisticsDetails?order_code=' + code,
    })
  },
  /**
   * 申请售后
   */
  onAfterServiceTapped: function(e) {
    wx.showModal({
      title: '提示',
      content:'小程序暂不支持退换货,请使用有货APP或官网退换货!',
      // content: '请至微信-有货服务号-YOHOBUY有货,联系在线客服处理',
      confirmText: '确定',
    })
  },
  /**
   * 再次购买
   */
  onReaddTapped:function(e){
    // console.log("再次购买tab");
    var code = e.currentTarget.dataset.order_code;
    var that = this;
    wx.showModal({
      content: '确认将商品再次加入购物车?',
      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"
          }
          GET(API_HOST,param)
          .then(json =>{
            if(json && json.code && json.code == 200){
              wx.showToast({
                title: '加入购物车成功',
                complete:function(){                  
                  wx.switchTab({
                    url: '../shopCart/shopCart',
                  })
                }
              })
            }
          })
        }
      }
    })
  },
  /**
   * 跳转到订单详情页面
   */
  navToOrderDetail: function(e) {
    let order_code = e.currentTarget.dataset.order_code;
    if (e.currentTarget.dataset.attribute === "3"){
      wx.navigateTo({
        url: "visualOrderDetail/visualOrderDetail?order_code=" + order_code + '&page_name=' + this.data.current_page_name + '&page_param=' + this.data.current_page_param
      });
    } else {
    wx.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() {
    wx.navigateTo({
      url: '../goodsList/newArrival' + '?page_name=' + this.data.current_page_name + '&page_param=' + this.data.current_page_param
    })
  },
  goTODefraudPage: function () {
    wx.navigateTo({
      url: '/pagesSecond/pages/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.index + 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;
  },
  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);
    wx.navigateTo({
      url: "../groupPurchase/groupPurchaseResult?activity_id=" + activity_id + '&group_no=' + group_no + '&page_name=' + 'orders' + '&page_param=' + this.data.current_page_param

    });
  }
})