user-service.js 6.74 KB
/**
 * 个人中心 编辑资料models
 * @author: gaohongwei<hongwei.gao@yoho.cn>
 * @date: 2016/8/16
 */

'use strict';
const api = global.yoho.API;
const userApi = require('./user-api');
const headerModel = require('../../../doraemon/models/header');

const configData = {
    gender: [{
        key: 'gender',
        value: 1,
        text: '男'
    }, {
        key: 'gender',
        value: 2,
        text: '女'
    }],
    income: [{
        key: 'income',
        value: 0,
        text: '请选择'
    }, {
        key: 'income',
        value: 1,
        text: '1000以下'
    }, {
        key: 'income',
        value: 2,
        text: '1000-2999'
    }, {
        key: 'income',
        value: 3,
        text: '3000-5999'
    }, {
        key: 'income',
        value: 4,
        text: '6000-7999'
    }, {
        key: 'income',
        value: 5,
        text: '8000-10000'
    }, {
        key: 'income',
        value: 6,
        text: '10000以上'
    }],
    profession: [{
        key: 'profession',
        value: 1,
        text: '已工作'
    }, {
        key: 'profession',
        value: 2,
        text: '大学生'
    }, {
        key: 'profession',
        value: 3,
        text: '中学生'
    }, {
        key: 'profession',
        value: 4,
        text: '其他'
    }]
};

const getDays = (year, month) => {
    let m = (month - 1) % 7 % 2 ? 30 : 31,
        y400 = year % 400 ? 28 : 29,
        y100 = year % 100 ? 29 : y400,
        y4 = year % 4 ? 28 : y100;

    return month === 2 ? y4 : m;
};

const getBirthday = (birthday) => {
    let year = parseInt(birthday.split('-')[0], 0),
        month = parseInt(birthday.split('-')[1], 0),
        day = parseInt(birthday.split('-')[2], 0),
        nowYear = new Date().getFullYear(),
        yearArr = [{value: 0, text: '年'}],
        monthArr = [{value: 0, text: '月'}],
        dayArr = [{value: 0, text: '日'}],
        days = getDays(year, month);

    for (let i = 1950; i <= nowYear; i++) {
        let yearJson = {value: i, text: i};

        if (i === year) {
            yearJson.isChecked = true;
            console.info(yearJson);
        }
        yearArr.push(yearJson);
    }

    for (let j = 1; j <= 12; j++) {
        let monthJson = {value: j, text: j};

        if (j === month) {
            monthJson.isChecked = true;
        }
        monthArr.push(monthJson);
    }

    for (let k = 1; k <= days; k++) {
        let dayJson = {value: k, text: k};

        if (k === day) {
            dayJson.isChecked = true;
        }
        dayArr.push(dayJson);
    }

    return {
        yearArr: yearArr,
        monthArr: monthArr,
        dayArr: dayArr
    };

};

const getUserInfo = (channel, uid) => {

    return api.all([
        headerModel.requestHeaderData(channel),
        userApi.getUserInfo(uid)
    ]).then(result => {
        let finalResult = {},
            headerData = {},
            userInfo = {};

        if (result[0].code === 200) {
            headerData = result[0];
        }

        if (result[1].code === 200) {
            let gender,
                genderArr,
                birthday,
                birthdayJson,
                yearArr,
                monthArr,
                dayArr,
                profession,
                professionArr,
                income,
                incomeArr;

            userInfo = result[1].data || {};

            if (userInfo) {
                gender = userInfo.gender || 3;
                birthday = userInfo.birthday || '';
                profession = userInfo.profession || 0;
                income = userInfoincome !== undefined ? userInfo.income : 0;
            }

            genderArr = configData.gender;
            for (let value of genderArr) {
                if (value.value === parseInt(gender, 0)) {
                    value.isChecked = true;
                }
            }

            professionArr = configData.profession;
            for (let value of professionArr) {
                if (value.value === parseInt(profession, 0)) {
                    value.isChecked = true;
                }
            }

            incomeArr = configData.income;
            for (let value of incomeArr) {
                if (value.value === parseInt(income, 0)) {
                    value.isChecked = true;
                }
            }

            birthdayJson = getBirthday(birthday);
            yearArr = birthdayJson.yearArr;
            monthArr = birthdayJson.monthArr;
            dayArr = birthdayJson.dayArr;

            finalResult.userPersonalInfo = {
                subTitle: '会员信息',
                firstBox: true,
                submitId: 'base-info',
                profileSrc: userInfo.head_ico,
                email: {
                    labelText: '登录邮箱:',
                    value: userInfo.email || ''
                },
                name: [
                    {
                        labelText: '昵称:',
                        value: userInfo.nickname,
                        key: 'nickname',
                        tips: '与Yoho!业务或商家品牌冲突的昵称,Yoho!将有可能收回'
                    },
                    {
                        labelText: '真实姓名:',
                        value: userInfo.username,
                        key: 'username',
                        tips: '' // 4.8去掉该提示
                    }
                ],
                gender: {
                    labelText: '性别:',
                    key: 'gender',
                    data: genderArr
                },
                birthday: {
                    labelText: '生日:',
                    key: 'birthday',
                    tips: '',
                    selects: [
                        {
                            key: 'year',
                            options: yearArr,
                            unit: '年'
                        },
                        {
                            key: 'month',
                            options: monthArr,
                            unit: '月'
                        },
                        {
                            key: 'day',
                            options: dayArr,
                            unit: '日'
                        }
                    ]
                },
                profession: {
                    labelText: '职业:',
                    key: 'profession',
                    data: professionArr
                },
                income: {
                    labelText: '收入状况:',
                    key: 'income',
                    data: incomeArr
                }
            };
        }



        finalResult.headerData = headerData;
        finalResult.meEditPage = true;
        return finalResult;
    });
};

module.exports = {
    getUserInfo
};