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

//获取应用实例
let app = getApp();

Page({
    data: {
        productList: [],
        userInfo: {},
        isLogin: false
    },
    onLoad: function () {
        event.on('user-login-success', this.showUserInfo);
        event.on('wx-union-id-update', () => {
            this.setData({
                hasUnionID: !!app.globalData.unionID
            });
        });
        
        if (app.getUid()) {
            this.showUserInfo();
        }
        
        this.chooseForYouList();
    },
    backToTop: function () {
        wx.pageScrollTo({
            scrollTop: 0
        })
    },
    chooseForYouList: function () {
        homeModel.productList({
            page: 1,
            limit: 20
        }).then(res => {
            if (res.code === 200) {
                const keyAdapter = {
                    skn: 'product_skn',
                    salePriceStr: 'sales_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);
        wx.navigateTo({
            url: '/pages/home/order/order?type=' + type + '&page_name=' + 'userCenter' + '&page_param=' + ''
        })
    },
    toAddress:function(){
        if (!this.data.isLogin) {
            return wx.showToast({
                title: '请先完成登录/注册,再查看!',
                icon: 'none',
                duration: 2000
            });
        }
        wx.navigateTo({
            url: '/pages/home/address/list?currentMode=modeEdit' + '&page_name=' + 'userCenter' + '&page_param=' + ''
        });
    },
    toService:function (){
        wx.navigateTo({
            url: '/pages/home/service/service',
        });
    },
    showUserInfo: function() {
        const defaultAvatar = '../../static/icons/default-avatar.png';

        accountModel.getProfile().then(res => {
            this.data.userInfo = app.globalData.userInfo;
            this.data.userInfo.nickName = res.data.nickname;
            this.data.userInfo.avatarUrl = res.data.head_ico || defaultAvatar;

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

        this.chooseForYouList();
    }
});