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

const utils = '../../../utils';
const api = global.yoho.API;
const _ = require('lodash');
const camelCase = global.yoho.camelCase;
const decodeURIComponentExt = require(`${utils}/string-code`).decodeURIComponentExt;

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
            },
            param: {code: 200}
        });
    }

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

    /**
     * 调用接口保存地址数据
     * @param params
     * @private
     */
    _saveAddressData(params) {
        return this.get({
            data: {
                method: params.id ? 'app.address.update' : 'app.address.add',
                id: decodeURIComponentExt(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
            }
        });
    }

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

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

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

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

        return api.all(apiList).then(result => {
            let selfAddress = _.get(result, '[0].data', []);
            let editAddress = {};

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

                if (params.id && value.address_id === params.id) {
                    editAddress = value;
                }

                value.address_id = encodeURIComponent(value.address_id);
            });

            return {
                address: camelCase(params.id ? editAddress : selfAddress),
                addressList: camelCase(_.get(result, '[1].data', [])),
                navTitle: params.id ? '编辑地址' : '添加地址'
            };
        });
    }

    /**
     * 保存地址数据
     * @param params
     */
    saveAddress(params) {
        return this._saveAddressData(params).then(result => {
            return result;
        });
    }

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

    /**
     * 删除地址
     * @param params
     */
    delAddress(params) {
        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: _.parseInt(params.id) || 0
            },
            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: decodeURIComponentExt(addressId)
            }
        }).then((result) => {
            return result;
        });
    }
};