index.js 19.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 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected: 0,
    autoplay: true,
    interval: 5000,
    duration: 500,
    circular: true,
    current: 0,
    zcurrent: 0, //用来记录当前swiper对应的索引index
    length_num: 9, //指定展示的小分类数量
    nav_index: 0,
    c_index: 0, //筛选分类下标
    page: 1, //页数
    price: 'price desc', //价格排序方式
    paynum: 'paynum desc', //销量排序方式
    stick: 0,
    activity: [],
    xianshilist: [{}],
    day: '',
    hou: '',
    min: '',
    sec: '',
    endTime: '',
    activitystatus: '',
    showtime: false,
    status1: '',
    status2: '',
    title2: '',
    scrollTop: '',
    pid: '',
    logok: '',
    typename: 1,
    list: [{
      pagePath: "/packageA/pages/index/index",
      text: "商品",
      iconPath: "/images/tabbar/icon_75.png",
      selectedIconPath: "/images/tabbar/icon_68.png"
    },
    {
      pagePath: "/packageA/pages/message/message",
      text: "消息",
      iconPath: "/images/tabbar/message.png",
      selectedIconPath: "/images/tabbar/messageactive.png"
    },
    {
      pagePath: "/packageA/pages/shop_cart/shop_cart",
      text: "购物车",
      iconPath: "/images/tabbar/icon_70.png",
      selectedIconPath: "/images/tabbar/icon_73.png"
    },
    {
      pagePath: "/packageA/pages/my/my",
      text: "我的",
      iconPath: "/images/tabbar/icon_71.png",
      selectedIconPath: "/images/tabbar/icon_72.png"
    }
    ]
  },




  scroll(e) {
    // console.log(e.detail.scrollTop)
    // if (e.detail.scrollTop >300){
    //   console.log(8888)
    //   this.setData({
    //     stick:1
    //   })
    // }else{
    //   this.setData({
    //     stick:0
    //   })
    // }

    // console.log(this.data.stick)
  },


  onLoad: function (options) {
    console.log(app.globalData.index)
    if (wx.getStorageSync('entrance_type') == '2') {
      wx.setNavigationBarTitle({
        title: '工会商城'
      })
    } else if (wx.getStorageSync('entrance_type') == '1') {
      wx.setNavigationBarTitle({
        title: '劳保商城'
      })
    } else if (wx.getStorageSync('entrance_type') == '3') {
      wx.setNavigationBarTitle({
        title: '办公采办'
      })
    }
    if (app.globalData.index == undefined) {
      let that = this;
      let token = wx.getStorageSync('token');

      console.log('woshitoe', token);
      if (token != "") {
        console.log(9999)
        this.getInfoFun()
      }

      this.get_notice()
      this.get_shopnum()
      this.getmessagelist();
      console.log('998867', this.data.pid)
      if (this.data.pid) {
        this.get_info(this.data.pid, 2);
      } else {
        this.get_info(0, 1);
      }

      // 获取活动专区
      this.getzhuanqulist();
      this.getxianshi();
      setTimeout(function () {
        that.setData({
          showtime: true
        })
      }, 990)
    }
    //控制显示隐藏
    this.ishow()

  },

  // 获取个人信息
  getInfoFun() {
    let that = this;
    let u = app.interface.readteacher;
    app.post(u, {}).then((r) => {
      console.log('3489523498', r)
      if (r.code == 200) {

        app.globalData.login_new = r.msg.login_new;
        app.globalData.status = r.msg.status;
        console.log(app.globalData.status)
        // t.setData({
        //   obj: r.msg
        // })
      }
    })
  },
  moremiao() {
    let token = wx.getStorageSync("token")
    if (token == '') {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageA/pages/register/register',
            })
          } else if (res.cancel) { }
        }
      })
    } else {
      let login_new = wx.getStorageSync("login");
      if (login_new == 0) {
        // wx.showToast({
        //   title: '您还不是会员',
        //   icon:"none"
        // })
        // setTimeout(function(){
        //   wx.navigateTo({
        //     url: '/pages/login/login',
        //   })
        // },1500)
        // return false

        wx.showModal({
          title: '提示',
          content: '您还不是会员',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/packageA/pages/login/login',
              })
            } else if (res.cancel) { }
          }
        })

        return false


      }
      wx.navigateTo({
        url: '/packageA/pages/xianshi/xianshi?title2=' + this.data.title2,
      })
    }

  },

  // 控制活动专区和限时抢购显示隐藏
  ishow() {
    let that = this
    let url = '//wb/wb';
    let data = {

    }
    app.post(url, data, "POST").then((r) => {
      console.log('4456789', r)
      that.setData({
        status1: r.msg.status1,
        status2: r.msg.status2,
        title2: r.msg.title2
      })



    }).catch((err) => { })
  },

  // 限时抢购
  getxianshi() {
    let that = this
    let url = '/special/get_all';
    let data = {
      page: 1,
      pageNum: 6

    }
    app.post(url, data, "POST").then((r) => {
      console.log(r)
      console.log(r.msg.residue_time)
      that.setData({
        xianshilist: r.msg.list,
        endTime: r.msg.residue_time,
        activitystatus: r.msg.status
      })
      var time = that.date_format(r.msg.residue_time);
      console.log('9988776', that.data.activitystatus)

      that.countDown()

      console.log(that.data.xianshilist)
      // that.setData({
      //   messagelist: that.data.messagelist.concat(r.msg)
      // })

      // console.log(that.data.messagelist)

    }).catch((err) => { })
  },

  date_format(micro_second) {
    // 总秒数
    var second = micro_second;
    // var second = Math.floor(micro_second / 1000);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  },

  // 倒计时
  countDown() {
    var that = this;
    console.log('999', that.data.endTime)
    var time = that.data.endTime;
    var interval = setInterval(function () {
      // 秒数
      let day = parseInt(time / (60 * 60 * 24));
      let hou = parseInt(time % (60 * 60 * 24) / 3600);
      let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
      let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);

      that.setData({
        day: that.timeFormat(day),
        hou: that.timeFormat(hou),
        min: that.timeFormat(min),
        sec: that.timeFormat(sec)
      })

      time--;
      if (time <= 0) {
        clearInterval(interval);

      }
    }.bind(this), 1000);
  },

  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },

  //获取活动专区列表
  getzhuanqulist() {
    let that = this
    let url = '/activity/get_all';
    let data = {}
    app.post(url, data, "POST").then((r) => {
      console.log(r)
      that.setData({
        activity: r.msg
      })

      console.log(that.data.activity)
      // that.setData({
      //   messagelist: that.data.messagelist.concat(r.msg)
      // })

      // console.log(that.data.messagelist)

    }).catch((err) => { })
  },

  // 活动专区查看更多
  seeactive(e) {
    let id = e.currentTarget.dataset.id
    let token = wx.getStorageSync("token")
    if (token == '') {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageA/pages/register/register',
            })
          } else if (res.cancel) { }
        }
      })
    } else {
      let login_new = wx.getStorageSync('login');
      if (login_new == 0) {
        wx.showModal({
          title: '提示',
          content: '您还不是会员',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/packageA/pages/login/login',
              })
            } else if (res.cancel) { }
          }
        })

        return false
      }
      wx.navigateTo({
        url: '/packageA/pages/zhuanqu/zhuanqu?id=' + id,
      })
    }

  },

  more() {
    let token = wx.getStorageSync("token")
    if (token == '') {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageA/pages/register/register',
            })
          } else if (res.cancel) { }
        }
      })

      return false
    }
    let login_new = wx.getStorageSync('login');
    if (login_new == 0) {
      wx.showModal({
        title: '提示',
        content: '您还不是会员',
        success(res) {
          if (res.confirm) {


            wx.navigateTo({
              url: '/packageA/pages/login/login',
            })
          } else if (res.cancel) { }
        }
      })

      return false
    }
    wx.navigateTo({
      url: '/packageA/pages/classify/classify',
    })
  },
  //展示所有的小分类
  show_all() {
    this.setData({
      length_num: this.data.length_num == 9 ? this.data.class_arr.length : 9
    })
  },
  // 查看小分类
  look_item(e) {
    let token = wx.getStorageSync("token")
    if (token == '') {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageA/pages/register/register',
            })
          } else if (res.cancel) { }
        }
      })
    } else {
      let login_new = wx.getStorageSync('login');
      let gh_login = wx.getStorageSync('gh_login');
      let cb_login = wx.getStorageSync('cb_login');
      let loginType = wx.getStorageSync('entrance_type');
      if (loginType == 1) {
        if (login_new == 0) {
          wx.showModal({
            title: '提示',
            content: '请绑定账户后操作',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/packageA/pages/login/login',
                })
              } else if (res.cancel) {

              }
            }
          })

          return false
        }
      } else if(loginType == 2){
        if (gh_login == 0) {
          wx.showModal({
            title: '提示',
            content: '请绑定账户后操作',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/packageA/pages/login/login',
                })
              } else if (res.cancel) {

              }
            }
          })

          return false
        }
      }else if(loginType == 3){
        if (cb_login == 0) {
          wx.showModal({
            title: '提示',
            content: '请绑定账户后操作',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/packageA/pages/login/login',
                })
              } else if (res.cancel) {

              }
            }
          })

          return false
        }
      }

      let title = this.data.nav_arr[this.data.nav_index].name
      let index = e.currentTarget.dataset.index ? e.currentTarget.dataset.index : 0
      let pid = this.data.nav_arr[this.data.nav_index].id
      wx.navigateTo({
        url: '/packageA/pages/search/search_result/search_result?title=' + title + "&index=" + index + '&pid=' + pid
      })
    }

  },
  //监测轮播图变化
  swiperChange(e) {
    this.setData({
      zcurrent: e.detail.current
    })
  },
  //轮播图
  look_more(e) {
    let token = wx.getStorageSync("token")
    let id = e.currentTarget.dataset.id
    let style = e.currentTarget.dataset.style
    if (token == '') {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageA/pages/register/register',
            })
          } else if (res.cancel) { }
        }
      })
    } else {
      let login_new = wx.getStorageSync('login');
      if (login_new == 0) {
        wx.showModal({
          title: '提示',
          content: '您还不是会员',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/packageA/pages/login/login',
              })
            } else if (res.cancel) { }
          }
        })
        return false
      }
      if (style == 2) {
        wx.navigateTo({
          url: '/packageA/pages/outerChain/outerChain?src=' + e.currentTarget.dataset.src
        })
      } else {
        wx.navigateTo({
          url: '/packageA/pages/shop_detail/shop_detail?id=' + id
        })
      }
    }

  },




  // 排序
  reorder(e) {
    let that = this
    let index = Number(e.currentTarget.dataset.index)
    if (index == 1) {
      if (that.data.price == "price") {
        that.setData({
          price: 'price desc'
        })
      } else {
        that.setData({
          price: 'price'
        })
      }
    } else if (index == 2) {
      if (that.data.paynum == "paynum") {
        that.setData({
          paynum: 'paynum desc'
        })
      } else {
        that.setData({
          paynum: 'paynum'
        })
      }
    }
    this.setData({
      c_index: e.currentTarget.dataset.index,
      page: 1,
      page_type: false
    })
    //调用排序接口
    this.good_list(this.data.pid, e.currentTarget.dataset.index)
  },

  select_nav(e) {
    let index = Number(e.currentTarget.dataset.index)
    let id = e.currentTarget.dataset.id;
    this.setData({
      pid: id
    })

    this.setData({
      scrollTop: 0
    })
    if (index == this.data.nav_index) {
      return
    } else {
      this.setData({
        nav_index: index,
        page: 1,
        page_type: false,
        c_index: 0, //筛选分类下标
        price: 'price desc', //价格排序方式
        paynum: 'paynum desc' //销量排序方式
      })
      this.get_info(id, 2)
    }
  },
  // 轮播图
  get_banner() {
    let url = app.interface.banner
    app.post(url, {}).then((res) => {
      this.setData({
        imgurl: res.msg
      })
    })
  },
  //获取公告
  get_notice() {
    let that = this
    let url = app.interface.notice
    app.post(url, {}).then((res) => {
      for (let obj of res.msg) {
        obj.createtime = obj.createtime.substring(0, 10)
      }
      that.setData({
        notice: res.msg
      })
    })
  },
  //获取页面信息
  get_info(pid, type) { //type 1为小标题 2为小标题下的小分类 
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let that = this
    let url = app.interface.index
    let params = {
      pid: pid
    }
    app.post(url, params).then((res) => {
      console.log('88776654', res)
      wx.hideLoading()
      if (type == 1) {
        let obj = {
          name: '首页',
          id: ''
        }
        res.msg.unshift(obj)
        that.setData({
          nav_arr: res.msg
        })
        that.get_info(res.msg[0].id, 2)
      } else if (type == 2) {
        console.log('666', res)
        // let obj = {
        //   name: '全部',
        //   id: ''
        // }
        // res.msg.unshift(obj)
        that.setData({
          class_arr: res.msg,
          cid: ''
        })
        that.good_list(pid, that.data.c_index)
      }
    }).catch((er) => {
      wx.hideLoading()
    })
  },
  //获取商品列表
  good_list(pid, c_index) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let that = this
    let url = app.interface.good_list
    let params = {
      pid: pid,
      page: that.data.page
    }
    if (c_index == 1) {
      params.price = that.data.price
    } else if (c_index == 2) {
      params.paynum = that.data.paynum
    }
    app.post(url, params).then((res) => {
      let list = res.msg;
      for (let obj of list) {
        obj.sales = obj.paynum
      }
      if (that.data.page == 1) {
        wx.hideLoading()

        that.setData({
          shop_arr: list
        })
      } else {
        if (res.msg.length == 0) {
          wx.hideLoading()
          that.setData({
            page_type: true
          })
        } else {
          setTimeout(() => {
            wx.hideLoading()
            that.setData({
              shop_arr: that.data.shop_arr.concat(list)
            })
          }, 1000)
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */

  //获取购物车数量
  get_shopnum() {
    let that = this
    let url = app.interface.shopcarNum
    app.post(url, {}).then((res) => {
      if (res.msg > 0) {
        wx.showTabBarRedDot({
          index: 2,
          success: function (red) {
            wx.setTabBarBadge({
              index: 2,
              text: res.msg.toString(),
            })
          }
        })
      } else {
        wx.hideTabBarRedDot({
          index: 2,
        })
      }
    })
  },

  getmessagelist() {
    let that = this
    let url = '/information/get_all';
    let data = {
      page: 1,
      pageNum: 10,

    }
    app.post(url, data, "POST").then((r) => {
      // console.log(r)
      // that.setData({
      //   messagelist: that.data.messagelist.concat(r.msg)
      // })

      let newlist = [];
      r.msg.forEach(function (value, index, array) {
        console.log(newlist)
        if (value.status == 1) {
          newlist.push(value)
        }
      })

      console.log('77889944556', newlist)
      if (newlist.length != 0) {
        wx.showTabBarRedDot({
          index: 1,
          success: function (red) {

            wx.setTabBarBadge({
              index: 1,
              text: newlist.length.toString(),
            })
          }
        })
      }


    }).catch((err) => { })
  },
  // 获取个人信息

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.setData({
      logok: app.globalData.baseUrlimg + "assets/img/cb2ad36a391388f19f5f912fa021f8f.png"
    })
    console.log(this.data.logok)
    //  this.logok=app.globalData.baseUrlimg+"assets/img/cb2ad36a391388f19f5f912fa021f8f.png''
    this.get_banner()
    console.log(app.globalData.index)
    if (app.globalData.index == 0) {
      app.globalData.index = undefined
      let token = wx.getStorageSync('token');
      console.log('woshitoe', token)
      if (token != "") {
        this.getInfoFun()
      }
      this.get_notice()
      this.get_shopnum()
      this.getmessagelist();
      console.log('998867', this.data.pid)
      if (this.data.pid) {
        this.get_info(this.data.pid, 2);
      } else {
        this.get_info(0, 1);
      }

      //控制显示隐藏
      this.ishow()
    }



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  loadMore(e) {
    console.log(e)
    let that = this
    if (that.data.page_type) {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 1000
      })
      return
    }
    that.setData({
      page: that.data.page + 1
    })
    that.good_list(that.data.pid, that.data.c_index)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(43898438493)
    let that = this
    if (that.data.page_type) {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 1000
      })
      return
    }
    that.setData({
      page: that.data.page + 1
    })
    that.good_list(that.data.pid, that.data.c_index)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})