index.js 7.36 KB


import NewZeroSellService from './service/zero-service-new'
import {login as loginModel} from '../../models'
import {getGlobalData, getUid, getStorageUserInfo, isLogin, setUserInfo} from '../../utils/index'

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

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

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,

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

  onLoad() {
    this._init();

    new app.WeToast();

    // this._getResouceCode()
    // this._getBottomBanner()
  },

  onShow() {
    // let uid = app.globalData.userInfo.uid > 0 ? app.globalData.userInfo.uid : 0;
    //
    this.setData({
      hasUnionID:  false,
      isLogin: isLogin(),
      uid: getUid()
    })

      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;
    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: '暂无更多内容'
      });
    })
  },

  _getPage(page) {
    return  new NewZeroSellService().getList({page: page, type: this.data.tabIndex}).then((result) => {
      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: '内容加载中...'
    })

    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 = getStorageUserInfo;
    
    if (isLogin() && (!userInfo || !userInfo.nickName)) {
      return loginModel.profile({uid: getUid()}).then(json => {
        if (json && json.code && json.code === 200) {
          userInfo.nickName = json.data.nickname || "";
          userInfo.avatarUrl = json.data.head_ico || "";
          setUserInfo(userInfo);
        }
      })
    } else {
      return Promise.resolve();
    }
  },


  _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));