index.js 9.6 KB


import ZeroSellService from './service/zero-sell'
import CommonService from './service/common'

import {
  decodePhoneNumber,
  getUnionID,
  decodeUnionId,
  openAuthorizeSettings,
} from '../../utils/login';

import {formatImageUrl} from '../../utils/util'

import {
  Toast
} from '../../vendors/zanui/index';
import router from './router/router';
import { jumpByUrl } from '../../libs/urlRoute';

let app = getApp()

Page(Object.assign({
  data: {
    list: [{
      page: 1,
      products: [],
    }, {
      page: 1,
      products: []
    }, {
      page: 1,
      products: []
    }],
    tabIndex: 0,

    my_zero_list: [{
      page: 1,
      data: []
    }, {
      page: 1,
      data: []
    }],

    footText: '',
    hasUnionID: false,
    uid: 0,
    isLogin: false,
    dialogSrc: null,
    dialogUrl: null,
    bannerSrc: null,
    bannerUrl: null
  },

  onLoad(options) {
    this.service = new ZeroSellService();
    this.commonService = new CommonService();

    this._init();

    new app.WeToast();
  if(!(options && +options.reload === 0)) {
    this._getResouceCode()
  }
    this._getBottomBanner()
  },

  onShow() {
    let uid = app.globalData.userInfo.uid > 0 ? app.globalData.userInfo.uid : 0;

    this.setData({
      hasUnionID: app.globalData.WXUnion_ID !== null && app.globalData.WXUnion_ID !== '' && app.globalData.WXUnion_ID !== undefined ? true : false,
      isLogin: app.isLogin(),
      uid
    })

    this._getUser();

    this.dialog = this.selectComponent("#dialog");
  },

  onPullDownRefresh() {
    // Do something when pull down.
    this._refresh().then(() => {
      wx.stopPullDownRefresh();
    });
  },

  onReachBottom() {
    // Do something when page reach bottom.
    this.setData({
      footText: '内容加载中...'
    });
    let tabIndex = this.data.tabIndex;
    if (tabIndex !== 3) {
      let page = this.data.list[tabIndex].page;
      let pageKey = `list[${tabIndex}].page`;
      let productsKey = `list[${tabIndex}].products`
  
      this._getPage(page).then((list) => {
        let oldList = this.data.list[tabIndex].products;
  
        if (list.length > 0) {
          this.setData({
            [productsKey]: oldList.concat(list),
            [pageKey]: page + 1,
          })
        }
  
        this.setData({
          footText: '暂无更多内容'
        });
      })
    } else {
      console.log('++++++++++++++++++++');
      this._getReleaseData();
    }
  },

  _getPage(page) {
    return this.service.getList({page: page, type: this.data.tabIndex}).then((result) => {
      if (result.code !== 200) {
        return [];
      }

      return result.data;
    });
  },

  _init() {
    let tabIndex = this.data.tabIndex;
    let page = this.data.list[tabIndex].page;
    let pageKey = `list[${tabIndex}].page`;
    let productsKey = `list[${tabIndex}].products`

    this.setData({
      footText: '内容加载中...'
    })

    this._getProceedingData();
    this._getReleaseData();

    return this._getPage(page).then(list => {
      if (list.length === 0) {
        this.setData({
          footText: '暂无更多内容'
        });
        return;
      }

      this.setData({
        [pageKey]: page + 1,
        [productsKey]: list,
        footText: '暂无更多内容'
      })
    })
  },

  _refresh() {
    let tabIndex = this.data.tabIndex;
    let pageKey = `list[${tabIndex}].page`;

    this.setData({
      [pageKey]: 1
    });

    return this._init();
  },

  _getUser() {
    const userInfo = app.getUserInfo();
    
    if (app.isLogin() && (!userInfo || !userInfo.nickName)) {
      return this.service.getUserProfile({uid: app.getUid()}).then(json => {
        if (json && json.code && json.code === 200) {
          userInfo.nickName = json.data.nickname || "";
          userInfo.avatarUrl = json.data.head_ico || "";
          app.setUserInfo(userInfo);
        }
      })
    } else {
      return Promise.resolve();
    }
  },

  _getResouceCode() {
    this.commonService.getResourceCode('194ffd46254d6ad20e6c538bea4d89d7')
    .then(data => {
      // console.log(data)
      this.setData({
        dialogSrc: formatImageUrl(data.src, 340 * app.globalData.systemInfo.pixelRatio, 340 * app.globalData.systemInfo.pixelRatio, 2),
        dialogUrl: data.url
      })
      this.dialog.showDialog();
    })
    .catch(error => {
      console.log(error)
    })
  
  },
  _getBottomBanner() {
    this.commonService.getResourceCode('5a2203f5656fbc9788bd8af70f2823d3')
    .then(data => {
      // console.log(data)
      this.setData({
        bannerSrc: formatImageUrl(data.src, app.globalData.systemInfo.screenWidth, 70, 2),
        bannerUrl: data.url
      })
    })
    .catch(error => {
        console.log(error)
    })
  },

  _getProceedingData() {
    let type = 0;
    let page = 1;
    let limit = 1000;
    let key = `my_zero_list[${type}].data`
    this.service.getMyList({type, page, limit}).then(res => {
      let data = []
      if (res && res.code === 200) {
        data = res.data;
      }
      this.setData({
        [key]: data
      })
    });
  },

  _getReleaseData() {
    let type = 1;
    let my_zero_list = this.data.my_zero_list;
    let page = my_zero_list[type].page;
    let oldData = my_zero_list[type].data;
    let key = `my_zero_list[${type}].data`;
    let keyPage = `my_zero_list[${type}].page`;
    this.service.getMyList({type, page}).then(res => {
      let data = []
      if (res && res.code === 200) {
        data = res.data;
      }
      this.setData({
        [key]: oldData.concat(data),
        [keyPage]: ++page
      })
      this.setData({
        footText: ''
      })
      if (data.length === 0) {
        this.setData({
          footText: '暂无更多内容'
        })
      }
    });
  },

  _jumpBanner() {
    if (this.data.bannerUrl == null) {
      return;
    }
    jumpByUrl(that.data.bannerUrl);
  },

  onTabChange({detail}) {
    this.setData({
      tabIndex: detail,
      [`list[${detail}].page`]: 1
    });

    this._init();
  },

  getUserInfo: function(e) {
    var that = this;
    if (e.detail.errMsg === 'getUserInfo:ok') {
      decodeUnionId(app.getWechatThirdSession(), e, function(response) {
        if (response.isHaveUnionID) {
          that.setData({
            hasUnionID: true,
          });
        }
      });
    } else {
      openAuthorizeSettings(function(response) {
        if (response.isHaveUnionID) {
          that.setData({
            hasUnionID: true,
          })
        };
      });
    }
  },

  getPhoneNumber: function(e) {
    var that = this;
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      decodePhoneNumber(e.detail.iv, e.detail.encryptedData, '', function(result) {
        // console.log('!!!!result',result)
        if (result.code != 200) {
          that.wetoast.toast({
            title: result.message,
            titleClassName: 'wetoast-title',
            duration: 1000,
            success: function() {
              that.loginAndRegisterTapped()
            }.bind(that)
          });
        } else {
          if (result.is_register) {
            that.showZanToast({
              title: '欢迎加入Yoho!Family!新人礼包已发放到个人中心-优惠券,请注意查收',
              success: function() {
                let uid = app.globalData.userInfo.uid > 0 ? app.globalData.userInfo.uid : 0;
                that.setData({
                  uid,
                  isLogin: app.isLogin(),
                });
              }
            }, 1500);
          } else {
            let uid = app.globalData.userInfo.uid > 0 ? app.globalData.userInfo.uid : 0;
            that.setData({
              uid,
              isLogin: app.isLogin(),
            });
          }
        }
      })
    } else {
      let needBind = true;
      that.setData({
        needBind
      });
      // that.loginAndRegisterTapped(true);
      wx.navigateTo({
        url: '/pages/bindPhoneNumber/bindPhoneNumber'
      })
    }
  },
  //跳转到登录页面
  loginAndRegisterTapped(needBind) {
    let that = this;
    let app = getApp()
    if (wx.getSetting) {
      wx.getSetting({
        success: (res) => {
          if (res && res.authSetting && res.authSetting["scope.userInfo"] !== null && res.authSetting["scope.userInfo"] !== undefined && res.authSetting["scope.userInfo"] === false) {
            wx.showModal({
              title: '',
              content: '检测到您未打开微信用户信息授权,开启后即可进行登录',
              confirmText: "去开启",
              confirmColor: "#000000",
              success: function(res) {
                if (res.confirm) {
                  wx.openSetting({
                    success: (res) => {
                      // console.log(res.authSetting["scope.userInfo"])
                      if (res.authSetting["scope.userInfo"] !== null && res.authSetting["scope.userInfo"] !== undefined && res.authSetting["scope.userInfo"] === true) {
                        getUnionID(app.getWechatThirdSession(), function(response) {
                          // console.log(response)
                        })
                      }
                    }
                  })
                } else {
                  wx.navigateTo({
                    url: '/pages/bindPhoneNumber/bindPhoneNumber'
                  });
                }
              }
            })
          } else {
            wx.navigateTo({
              url: '/pages/bindPhoneNumber/bindPhoneNumber'
            });
          } 
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      });
    }
  },

  goMyList() {
    router.go('myList');
  }

}, Toast));