address-act.page.js 6.15 KB
/**
 * 修改地址
 * @author: bikai<kai.bi@yoho.cn>
 * @date: 2015/11/30
 */
const $ = require('yoho-jquery'),
    tip = require('js/plugin/tip'),
    security = require('js/plugin/security'),
    loading = require('js/plugin/loading');

let orderInfo;

if (window.queryString.refer === 'shopping') {
    orderInfo = require('../cart/order-info').orderInfo;
}

if (window.queryString.refer === 'buynow') {
    orderInfo = require('../cart/buynow/order-info').orderInfo;
}

const stringHandle = require('js/common/helpers/stringHandle');

let $addressForm = $('.edit-address'),
    $submit = $('.submit'),
    $editAddressPage = $('.my-edit-address-page'),
    $addressListPage = $('.my-address-list-page'),
    $backBtn = $('.nav-back'),
    $navTitle = $('.nav-title'),
    $input = $('input, textarea'),
    navTitle = $navTitle.html(),
    $area = $('.area'),
    isSubmiting,
    currentPage = 'edit';

let $usernameInput = $addressForm.find('[name="consignee"]');

import Vue from 'vue';
import vueAddressAct from 'vues/home/address/address-act.vue';

let addressVact = new Vue({
    el: '#vAddressAct',
    components: {
        vueAddressAct: vueAddressAct
    }
});

require('js/common');

$($editAddressPage, $addressListPage).css('min-height', function() {
    return $(window).height() - $('#yoho-header').height();
});

// 清除返回按钮原有链接
$backBtn.attr('href', 'javascript:void(0);'); // eslint-disable-line

// 自定义返回按钮事件
$backBtn.on('touchend', function(e) {
    if (currentPage === 'list') {
        $addressListPage.hide();
        $editAddressPage.show();
        e.preventDefault();
        currentPage = 'edit';
        $navTitle.html(navTitle);

        // 恢复默认的三级选择
        $addressListPage.hide();
        $addressListPage.find('ul').hide().find('li').removeClass('highlight');
        $addressListPage.children('ul').show().children('li').show();
    } else {
        window.history.go(-1);
    }
});

$usernameInput.bind('input propertychange', function() {
    if (stringHandle.chinese2($usernameInput.val()) > 20) {
        tip.show('收货人姓名最多支持20个字');
        $usernameInput.val($usernameInput.val() && stringHandle.sub_chinese2($usernameInput.val(), 20));
    }
});

/**
 * 添加/编辑保存并设为默认地址
 */
const setDefault = (addressData) => {
    let address = {
        address_id: addressData.address_id,
        consignee: addressData.consignee,
        mobile: addressData.mobile,
        address_info: `${addressData.area} ${addressData.address}`,
        is_support: addressData.is_support
    };

    orderInfo('address_id', addressData.address_id);
    orderInfo('address', address);
};

// 提交表单请求
$addressForm.on('submit', function() {
    let username = $usernameInput.val().replace(/(^\s+)|(\s+$)/g, '');

    if (isSubmiting) {
        return false;
    }

    if (security.hasDangerInput(false)) {
        return false;
    }

    // 简单的表单校验
    if (!$(this).find('[name="consignee"]').val()) {
        tip.show('收件人不能为空');
        return false;
    }

    if (username && !/^[\u4E00-\u9FA5A-Za-z0-9*]+$/gi.test(username)) {
        tip.show('收货人姓名不支持特殊符号');
        return false;
    }

    if (!$(this).find('[name="mobile"]').val()) {
        tip.show('手机号不能为空');
        return false;
    }

    if (!$(this).find('[name="area_code"]').val() || !$(this).find('[name="area"]').val() ||
        $(this).find('[name="area_code"]').val().length < 6) {
        tip.show('省市区不能为空');
        return false;
    }
    if (!$(this).find('[name="address"]').val()) {
        tip.show('地址不能为空');
        return false;
    }

    isSubmiting = true;
    loading.showLoadingMask();
    $submit.css('background', '#777');

    $.ajax({
        method: 'POST',
        url: '/home/saveAddress',
        data: $(this).serialize()
    }).then(function(res) {
        if ($.type(res) !== 'object') {
            res = {};
        }
        if (res.code !== 200) {
            tip.show(res.message || '网络出了点问题~');
            isSubmiting = false;
            loading.hideLoadingMask();
        } else if (window.queryString) {
            if (window.queryString.refer === 'buynow') {
                setDefault(res.data);
                if (window.queryString.is_group === '1') {
                    window.location.href = '/cart/index/buynow/orderensure?product_sku=' +
                    window.queryString.product_sku + '&buy_number=' +
                    window.queryString.buy_number + '&is_group=' +
                    window.queryString.is_group + '&activity_id=' +
                    window.queryString.activity_id + '&group_no=' +
                    window.queryString.group_no;
                } else {
                    window.location.href = '/cart/index/buynow/orderensure?product_sku=' +
                    window.queryString.product_sku + '&buy_number=' +
                    window.queryString.buy_number;
                }
            } else if (window.queryString.refer === 'shopping') {
                setDefault(res.data);

                window.location.href = '/cart/index/new/orderEnsure?cartType=ordinary';
            } else if (window.queryString.refer === 'modify') {
                window.location.href = '/home/addressModify';
            } else {
                window.location.href = '/home/address';
            }
        } else {
            window.location.href = '/home/address';
        }
    }).fail(function() {
        tip.show('网络出了点问题~');
        isSubmiting = false;
        loading.hideLoadingMask();
        $submit.css('background', '#444');
    });
    return false;
});

$submit.on('touchend', function() {
    if (security.hasDangerInput(false)) {
        return false;
    }
    $input.blur();
    $addressForm.submit();
    return false;
}).on('touchstart', function() {
    $(this).addClass('highlight');
}).on('touchend touchcancel', function() {
    $(this).removeClass('highlight');
});

// 省市区
$area.on('touchstart', function() {
    addressVact.$children[0].show = true;
});

// IOS 光标隐藏
$area.find('input[name=area]').on('touchend', function() {
    return false;
});