store-home.js 5.86 KB
'use strict';

const _ = require('lodash');
const rightsJson = require('../../../public/js/activity/store-home/rights-detail.json'); // 写死的会员特权

class storeHome extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    _userInfo(params) {
        let options = {
            data: {
                method: 'extend.trade.userinfo',
                uid: params.uid,
                shop_type: params.shopType
            },
            param: {
                code: 200
            },
            api: global.yoho.ExtStoreAPI
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    baseInfo(params) {
        return Promise.all([
            this._userInfo(params)
        ]).then(result => {
            if (_.get(result, '[0].data')) {
                let coffee = result[0].data.vip_type === 1;
                let green = result[0].data.vip_type === 2;
                let photography = result[0].data.vip_type === 3;

                Object.assign(result[0].data, {
                    photography: photography,
                    coffee: coffee,
                    green: green,
                    process: this.getProcess(_.get(result[0], 'data.credit_points'), result[0].data.vip_type)
                });

                result[0].data.parentsGender = result[0].data.gender === '1' ? '男' : '女';

                if (result[0].data.babyInfo) {
                    let thisGender = result[0].data.babyInfo.gender;

                    if (!thisGender) {
                        result[0].data.unknow = true;
                    }

                    result[0].data.gender = (thisGender === '1' ? '男' : '女');
                    result[0].data.otherGender = (thisGender === '1' ? '女' : '男');
                    if (result[0].data.gender === '男') {
                        result[0].data.genderId = 1;
                        result[0].data.otherGenderId = 2;
                    } else {
                        result[0].data.genderId = 2;
                        result[0].data.otherGenderId = 1;
                    }
                }

                let rightsObj = {};
                let build = [];
                let rightsUrl = '';
                let vipLevel = `vip${result[0].data.vip_level}`;

                if (photography) {
                    rightsObj = rightsJson.photography;
                    rightsUrl = '//mp.weixin.qq.com/s/iSd__pG6tEodktIlPMJgoQ';
                }
                if (green) {
                    rightsObj = rightsJson.green;
                    rightsUrl = '//mp.weixin.qq.com/s/JJgJW77sT9Ur87BR0iWevw';
                }
                if (coffee) {
                    rightsObj = rightsJson.coffee;
                    rightsUrl = '//mp.weixin.qq.com/s/o04mbaNXZ_kmvT2Bvlqg9w';
                }

                _.forEach(rightsObj, (val, key) => {
                    if (key === vipLevel) {
                        build = val;
                    }
                });
                result[0].data.rights = build;
                result[0].data.rightsUrl = rightsUrl;
                result = result[0].data;
            }
            return result;
        });
    }

    history(params) {
        let options = {
            data: {
                method: 'extend.trade.consumelist',
                uid: params.uid,
                shop_type: params.shopType,
                page: params.page || 1,
                limit: 20
            },
            param: {
                code: 200
            },
            api: global.yoho.ExtStoreAPI
        };

        return this.get(options).then(result => {
            let resu = {
                list: []
            };

            if (_.get(result, 'data.consume_list')) {
                let build = [];

                _.forEach(result.data.consume_list, val => {
                    build.push({
                        amount: val.trade_amount_desc,
                        date: val.trade_date,
                        title: val.trade_title
                    });
                });

                resu.list = build;
            }
            return resu;
        });
    }

    modify(params) {
        let options = {
            data: {
                method: 'extend.trade.editBabyInfo',
                uid: params.uid,
                nick_name: params.nickName,
                gender: params.gender,
                birthday: params.birthday
            },
            param: {
                code: 200
            },
            api: global.yoho.ExtStoreAPI
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    getProcess(amount, type) {
        let process = 0;

        switch (type) {
            case 1:
                if (amount <= 0) {
                    process = 0;
                }
                if (amount > 0 && amount <= 800) {
                    process = (amount / 800 * 0.5) * 100;
                }
                if (amount > 800 && amount < 2000) {
                    process = ((amount - 800) / 1200 * 0.5 + 0.5) * 100;
                }
                if (amount >= 2000) {
                    process = 100;
                }
                return process;
            case 2:
                amount -= 1000;
                if (amount < 0) {
                    amount = 0;
                }
                if (amount > 4000) {
                    amount = 4000;
                }
                process = (amount / 4000) * 100;
                return process;
            case 3:
                if (amount < 0) {
                    amount = 0;
                }
                if (amount > 10000) {
                    amount = 10000;
                }
                process = (amount / 10000) * 100;
                return process;
            default:
                return process;
        }
    }
}

module.exports = storeHome;