address.js 6.3 KB
/**
 * Created by PhpStorm.
 * User: Targaryen
 * Date: 2016/8/17
 * Time: 13:41
 */
'use strict';

const api = global.yoho.API;
const _ = require('lodash');
const camelCase = global.yoho.camelCase;
const crypto = global.yoho.crypto;

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * 从接口获取地址列表
     * @param params
     */
    _getAddressData(params) {
        return this.get({
            data: {
                method: 'app.address.gethidden',
                uid: params.uid
            },
            api: global.yoho.API,
            param: {code: 200}
        });
    }

    /**
     * 获取三级地址列表数据
     * @param params
     * @returns {*}
     * @private
     */
    _getThrAddressData(params) {
        return this.get({
            data: {
                method: 'app.address.getlist',
                uid: params.uid
            },
            api: global.yoho.API,
            param: {code: 200, cache: true}
        });
    }

    /**
     * 调用接口保存地址数据
     * @param params
     * @private
     */
    _saveAddressData(params) {
        return this.get({
            data: {
                method: params.id ? 'app.address.update' : 'app.address.add',
                id: params.id,
                address: params.address,
                area_code: params.area_code,
                consignee: params.consignee,
                email: params.email,
                mobile: params.mobile,
                zip_code: params.zip_code,
                uid: params.uid
            },
            api: global.yoho.API
        });
    }

    /**
     * 调用接口设置默认地址
     * @param params
     * @private
     */
    _setDefaultAddress(params) {
        return this.get({
            data: {
                method: 'app.address.setdefault',
                uid: params.id,
                id: params.id
            },
            api: global.yoho.API
        });
    }

    /**
     * 调用接口删除地址
     * @param params
     * @private
     */
    _delAddressData(params) {
        return this.get({
            data: {
                method: 'app.address.del',
                id: params.id,
                uid: params.uid
            },
            api: global.yoho.API
        });
    }

    /**
     * 地址管理页面
     * @param params
     */
    address(params) {
        let apiList = [
            this._getAddressData(params)
        ];

        if (params.id) { // 如果有 ID 是编辑地址页面
            apiList.push(this._getThrAddressData(params));
        }

        return api.all(apiList).then(result => {
            let selfAddress = result[0];

            if (selfAddress.data) {

                /* 加密地址 ID */
                _.forEach(selfAddress.data, (value, key) => {
                    selfAddress.data[key].address_id =
                        encodeURIComponent(crypto.encryption(null, selfAddress.data[key].address_id));

                    if (value.area.length > 11) {
                        value.showArea = value.area.substr(0, 5) + '...' + value.area.substr(-5);
                    } else {
                        value.showArea = value.area;
                    }
                });

                /* 如果有 ID 是地址编辑页面 */
                if (params.id) {
                    let returnResult = {
                        address: camelCase(_.find(selfAddress.data, o => {
                            return decodeURIComponent(o.address_id) === params.id;
                        })),
                        addressList: camelCase(result[1] ? result[1].data : []),
                        navTitle: '编辑地址'
                    };

                    let area = _.get(returnResult, 'address.area', '');

                    if (area.length > 11) {
                        returnResult.address.area = area.substr(0, 5) + '...' + area.substr(-5);
                    }

                    return returnResult;
                } else {
                    return {
                        address: camelCase(selfAddress.data),
                        addressList: camelCase(result[1] ? result[1].data : []),
                        navTitle: '添加地址'
                    };
                }
            }
        });
    }

    /**
     * 保存地址数据
     * @param params
     */
    saveAddress(params) {
        if (params.id) {
            params.id = crypto.decrypt(null, decodeURIComponent(params.id));
        }

        return this._saveAddressData(params).then(result => {
            return result;
        });
    }

    /**
     * 设置默认地址
     * @param params
     */
    setDefaultAddress(params) {
        if (params.id) {
            params.id = crypto.encryption(null, params.id);
        }
        return this._setDefaultAddress(params).then(result => {
            return result;
        });
    }

    /**
     * 删除地址
     * @param params
     */
    delAddress(params) {
        if (params.id) {
            params.id = crypto.decrypt(null, params.id);
        }
        return this._delAddressData(params).then(result => {
            return result;
        });
    }

    /**
     * 三级地址数据
     * @param params
     */
    locationList(params) {
        return this._getThrAddressData(params).then(result => {
            if (result.data) {
                return {
                    addressList: result.data
                };
            }
        });
    }

    /**
     * 新版地址选择获取地址
     */
    newGetAddress(params) {
        return this.get({
            data: {
                method: 'app.address.provinces',
                id: params.id || '0'
            },
            api: global.yoho.API,
            param: {code: 200, cache: true}
        }).then(result => {
            return result && result.data;
        });
    }

    /**
     * 订单详情选择地址
     * @param params
     */
    chooseAddress(orderCode, addressId, uid) {
        return this.get({
            data: {
                method: 'app.SpaceOrders.updateDeliveryAddress',
                uid: uid,
                order_code: orderCode,
                address_id: crypto.decrypt(null, addressId)
            },
            api: global.yoho.API
        }).then((result) => {
            return result;
        });
    }
};