home.js 4.34 KB
import wx from '../../utils/wx';
import event from '../../common/event';
import Yas from '../../common/yas';
import homeModel from '../../models/home/index';
import accountModel from '../../models/account/index';
import {tapToLogin, getPhoneNumber} from '../../common/login';

//获取应用实例
let app = getApp();
let yas = new Yas();
let router = global.router;

Page({
    data: {
        productList: [],
        userInfo: {},
        isLogin: false,
        hasUnionID: false,
        infoNum: {}
    },
    getPhoneNumber,
    onLoad: function () {
        event.on('user-login-success', this.loginSucess);
        event.on('bind-auto-register-type-report', params => {
            yas.report('YB_REGISTER_SUCCESS', params);
        });
        event.on('wx-union-id-update', () => {
            this.setData({
                hasUnionID: !!app.globalData.unionID
            });
        });

        this.setData({
            hasUnionID: !!app.getUnionID()
        });

        if (app.getUid()) {
            this.showUserInfo();
        }

        this.getInfoNum();
        this.chooseForYouList();
        yas.pageOpenReport();
    },
    onShow: function () {
        yas.report('YB_MAIN_TAB_C', {TAB_ID: 4})
    },
    loginSucess: function () {
        this.showUserInfo();
        this.getInfoNum();
        this.chooseForYouList();
    },
    getInfoNum:function(){
        if (app.getUid()) {
            homeModel.infoNum()
                .then(res => {
                    if (res.code === 200) {
                        this.data.infoNum = res.data;
                        this.setData({
                            infoNum: this.data.infoNum,
                        });
                    }
                });
        }
    },
    chooseForYouList: function () {
        homeModel.productList({
            page: 1,
            limit: 20
        }).then(res => {
            if (res.code === 200) {
                const keyAdapter = {
                    skn: 'product_skn',
                    salesPrice: 'sales_price',
                    marketPrice: 'market_price',
                    productName: 'product_name',
                    defaultImages: 'default_images'
                };
                let list = [];

                (res.data.product_list || []).forEach(product => {
                    let item = {};

                    Object.keys(keyAdapter).forEach(key => {
                        item[key] = product[keyAdapter[key]]
                    });
                    list.push(item);
                });

                this.setData({
                    productList: list
                });
            }
        });
    },
    loginRegTap: function() {
        tapToLogin()
            .then(res => {
                if (res.code === 10003) {
                    event.emit('user-login-success');
                }
            });
    },
    toOrdersTap: function (e) {
        if (!this.data.isLogin) {
            return wx.showToast({
                title: '请先完成登录/注册,再查看!',
                icon: 'none',
                duration: 2000
            });
        }
        let type = parseInt(e.currentTarget.dataset.type);

        const EVENTS = {
            1: 'YB_MY_ORD',    // 我的订单
            2: 'YB_MY_TOPAY',  // 待付款
            3: 'YB_MY_TOSEND', // 待发货
            4: 'YB_MY_TOREC',  // 待收货
        };
        yas.report(EVENTS[type]);

        router.go('orderList', {type});
    },
    toAddress:function(){
        if (!this.data.isLogin) {
            return wx.showToast({
                title: '请先完成登录/注册,再查看!',
                icon: 'none',
                duration: 2000
            });
        }
        router.go('address');
    },
    toService:function (){
        router.go('service');
    },
    showUserInfo: function() {
        const defaultAvatar = 'https://img10.static.yhbimg.com/headimg/2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif';

        accountModel.getProfile().then(res => {
            wx.hideLoading();
            this.setData({
                isLogin: true,
                userInfo: {
                    phoneNum: res.data.mobile,
                    nickName: res.data.nickname,
                    avatarUrl: res.data.head_ico || defaultAvatar
                }
            });
        }).catch(() => {
            wx.hideLoading();
        });
    }
});