returns-apply.js 14.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
/**
 * 退换货申请
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/2/29
 */
var $ = require('yoho-jquery'),
    Addr = require('./common-address');

var $goodsTable = $('#goods-table'),
    $checkBox = $goodsTable.find('input[type="checkbox"]'),
    $reasons = $goodsTable.find('.return-reason'),
    $file = $goodsTable.find('input[type=file]'),
    $refundType = $('input[name="refund-type"]'),
    $province = $('#province'),
    $city = $('#city'),
    $areaCode = $('#area');

var $refundInfo = $('#refund-pay-info'),
    $payYoho = $refundInfo.find('.type-yoho'),
    $payBank = $refundInfo.find('.type-bank'),
    $payAli = $refundInfo.find('.type-alipay');

var $exchange = $('.exchange-detail'),
    $hidePhone = $exchange.find('input[name="hide-phone"]'),
    $refundMode = $exchange.find('.refund-mode');

var $submitBox = $('.submit-box'),
    $waitPoint = $submitBox.find('.wait-gif span');

var pageType = 0, // 0-换货  1-退货
    orderCode = $('#order-code').val(),
    defaultArea = $exchange.find('input[name="hide-area"]').val(),
    defaultPhone = $hidePhone.val(),
    disPhone = '',
    verifyTip = '',
    colorSize = {},
    timer;

require('../plugins/jquery.qupload');
require('../common/ajaxfileupload');

if (defaultPhone) {
    $hidePhone.remove();
    disPhone = defaultPhone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    $exchange.find('input[name="phone"]').val(disPhone);
}

function syncRefundMode(code) {
    $.ajax({
        type: 'POST',
        url: '/home/returns/getDelivery',
        data: {
            areaCode: code
        }
    }).then(function(jsonData) {
        var _html = '';

        if (jsonData.code === 200) {
            $.each(jsonData.data, function(k, v) {
                var checked = '';

                if (v.is_default === 'Y') {
                    checked = 'checked="checked"';
                }
                _html += '<input type="radio" name="delivery-tpye" value="' +
                    v.id + '" ' + checked + '"">' + v.name;
            });
            $refundMode.html(_html);
        }
    });
}

function setBackInput(type) {
    $payYoho.hide();
    if (type === 2) {
        $payBank.show();
        $payAli.hide();
        $payYoho.hide();
    } else if (type === 3) {
        $payAli.show();
        $payBank.hide();
        $payYoho.hide();
    } else if (type === 4) {
        $payYoho.show();
        $payBank.hide();
        $payAli.hide();
    }
}

var resetImg = function($p) {
    $p.removeClass('selected');
    $p.find('input[name=imgs]').val('');
    $p.find('img').attr('src', '');
};


if ($refundInfo.length) {
    pageType = 1;

    $refundType.each(function() {
        if ($(this).prop('checked')) {
            setBackInput($(this).val() * 1);
        }
    });
} else if (defaultArea) {
    syncRefundMode(defaultArea);
}

defaultArea = defaultArea ? defaultArea : '';
Addr.loadAllData(defaultArea, {
    areaDomId: 'area'
});

// 页面获取size列表数据
$goodsTable.find('.sizeinfo').each(function() {
    var sizes = [];

    $(this).children().each(function() {
        sizes.push($(this).data());
    });
    colorSize[$(this).data().id] = sizes;
});

$reasons.change(function() {
    var $par = $(this).parent().parent(),
        type = $(this).val() * 1;

    if (type === 4 || type === 6 || type === 8) {
        $par.next().show();
    } else {
        $par.next().hide();
    }
});

$checkBox.change(function() {
    var $par = $(this).parent().parent();

    if ($(this).prop('checked')) {
        $par.find('select').prop('disabled', false);
    } else {
        $par.find('select').val(0).prop('disabled', true);
        $par.next().hide();
    }
});

$goodsTable.on('change', '.exchange-color', function() {
    var $warp = $(this).closest('.exchange-choose-wrap'),
        _html = '<option value="0">请选择尺码</option>',
        id = $(this).val();

    if (colorSize[id]) {
        $.each(colorSize[id], function(k, v) {
            _html += '<option value="' + v.id + '">' + v.name + '</option>';
        });
    }
    $warp.find('.exchange-size').html(_html).val(0);
});

$goodsTable.on('click', '.btn-del', function(e) {
    var $p = $(this).closest('li');

    e.stopPropagation();
    resetImg($p);
});

// $file.each(function(e) {
//     var $this = $('#upload-img-' + e);

    // if (!window.location.origin) {
    //     window.location.origin = window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
    // }

    fileChangeEvent();//

    // $this.qupload({
    //     button_image_url: '',
    //     upload_url: window.location.origin + '/home/returns/imgUpload',
    //     file_post_name: 'fileData',
    //     button_text: '<span class="btn_upload_text">上传图片</span>',
    //     button_text_style: '.btn_upload_text{color: #ffffff;}',
    //     button_width: 116,
    //     button_height: 33,
    //     button_text_left_padding: 32,
    //     button_text_top_padding: 8,
    //     button_action: window.SWFUpload.BUTTON_ACTION.SELECT_FILE,
    //     file_size_limit: '10240',
    //     file_types: '*.jpg;*.jpeg;*.png;*.bmp',
    //     uploadSuccessed: function(data) {
    //         var $imgList = $par.find('li'),
    //             isShow = false,
    //             img, _html;
    //
    //         img = JSON.parse(data).imgList[0];
    //         _html = '<span class="btn-del" title="删除"></span>' +
    //             '<img src="' + img[0] + '" width="126" height="126">' +
    //             '<input type="hidden" name="imgs" value="' + img.imgRelUrl + '">';
    //         $imgList.each(function() {
    //             if (!isShow && !$(this).find('img').length) {
    //                 isShow = true;
    //                 $(this).html(_html);
    //             }
    //         });
    //     }
    // });
// });

function fileChangeEvent() {

    // 图片上传
    $goodsTable.on('change', 'input[type=file]', function() {

        var $f = $(this);
        var $p = $f.parent().siblings('.up-image-list');

        var $unSelectLi = $p.find('li:not(.selected)');

        if ($f.val()) {
            $.ajaxFileUpload({
                url: '/common/upload/image',
                secureuri: false,
                fileElementId: $f,
                data: {
                    bucket: 'evidenceImages'
                },
                dataType: 'json',
                success: function(data /* , status */) {

                    var resData = data.data,
                        len = resData.images.length > $unSelectLi.length ?
                            $unSelectLi.length : resData.images.length;

                    if (data && data.code === 200 && data.data && data.data.images) {

                        for (var i = 0; i < len; i++) {
                            var url, relaUrl,
                                $li = $($unSelectLi[i]);

                            url = resData.images[i] +
                                '?imageMogr2/thumbnail/130x130/extent/130x130/background/d2hpdGU=/position/center/quality/90';
                            relaUrl = resData.imagesList[i];
                            $li.find('img').attr('src', url).attr('source-url', url);
                            $li.find('input[name=imgs]').val(relaUrl);
                            $li.addClass('selected');
                        }


                    } else if (data && data.message) {
                        alert(data.message);
                    }
                },
                error: function(error) {    /** data, status, e **/
                    alert('上传失败,请稍后再试!');
                },
                complete: function() {
                    $f.clone().replaceAll($f);
                }
            });
        }
    });
}

$exchange.on('change', '#area', function() {
    var code = $(this).val() * 1;

    if (code) {
        syncRefundMode(code);
    }
});

$refundType.change(function() {
    var type = $(this).val() * 1;

    setBackInput(type);
});

// 打包退换货商品信息
function packGoogsList() {
    var goods = [],
        type = '换货';

    if (pageType) {
        type = '退货';
    }
    if (!$checkBox.length) {
        verifyTip = '没有可以' + type + '的商品';
    }

    $checkBox.each(function() {
        var $par = $(this).parent().parent(),
            $next = $par.next(),
            goodsInfo;

        if (verifyTip.length) {
            return;
        }

        if ($(this).prop('checked')) {
            goodsInfo = {
                product_skn: $par.find('input[name="skn"]').val(),
                product_skc: $par.find('input[name="skc"]').val(),
                product_sku: $par.find('input[name="sku"]').val(),
                goods_type: $par.find('input[name="typeid"]').val(),
                last_price: $par.find('input[name="price"]').val(),
                reason: $par.find('.return-reason').val() * 1
            };

            if (pageType) {
                goodsInfo.returned_reason = goodsInfo.reason;
            } else {
                goodsInfo.exchange_reason = goodsInfo.reason;
                goodsInfo.new_goods_id = $par.find('.exchange-color').val() * 1;
                goodsInfo.new_product_sku = $par.find('.exchange-size').val() * 1;

                if (!verifyTip && !goodsInfo.new_goods_id) {
                    verifyTip = '请选择要换的颜色';
                }

                if (!verifyTip && !goodsInfo.new_product_sku) {
                    verifyTip = '请选择要换的尺码';
                }
            }
            if (!verifyTip && !goodsInfo.reason) {
                verifyTip = '请选择' + type + '原因';
            }

            if (goodsInfo.reason === 4 ||
                goodsInfo.reason === 6 ||
                goodsInfo.reason === 8) {
                goodsInfo.remark = $.trim($next.find('textarea').val());
                goodsInfo.evidence_images = [];
                $next.find('input[name="imgs"]').each(function() {
                    goodsInfo.evidence_images.push($(this).val());
                });

                if (!verifyTip && !goodsInfo.remark) {
                    verifyTip = '请填写' + type + '描述';
                }

                if (!verifyTip && !goodsInfo.evidence_images.length) {
                    verifyTip = '请上传图片';
                }
            }
            delete goodsInfo.reason;

            goods.push(goodsInfo);
        }
    });

    if (!verifyTip && !goods.length) {
        verifyTip = '请选择要' + type + '的商品';
    }

    return goods;
}

// 打包退货信息
function packRefundInfo() {
    var res = {};

    res.goods = packGoogsList();
    res.payment = {};

    $refundType.each(function() {
        if ($(this).prop('checked')) {
            res.payment.return_amount_mode = $(this).val() * 1;
        }
    });

    if (!verifyTip && !res.payment.return_amount_mode) {
        verifyTip = '请选择退款方式';
    }

    if (res.payment.return_amount_mode === 2) {
        res.payment.province = $province.val();
        res.payment.city = $city.val();
        res.payment.area_code = $areaCode.val();
        if (!verifyTip && !res.payment.area_code * 1) {
            verifyTip = '请选择银行所在地';
        }

        res.payment.bank_name = $.trim($refundInfo.find('input[name="bank-name"]').val());
        if (!verifyTip && !res.payment.bank_name) {
            verifyTip = '请填写银行名称';
        }

        res.payment.payee_name = $.trim($refundInfo.find('input[name="payee-name"]').val());
        if (!verifyTip && !res.payment.payee_name) {
            verifyTip = '请填写收款人姓名';
        }

        res.payment.bank_card = $.trim($refundInfo.find('input[name="bank-card"]').val());
        if (!verifyTip && !res.payment.bank_card) {
            verifyTip = '请填写收款账户';
        }

    } else if (res.payment.return_amount_mode === 3) {
        res.payment.alipay_name = $.trim($refundInfo.find('input[name="alipay-name"]').val());
        if (!verifyTip && !res.payment.alipay_name) {
            verifyTip = '请填写收款人姓名';
        }

        res.payment.alipay_account = $.trim($refundInfo.find('input[name="alipay-account"]').val());
        if (!verifyTip && !res.payment.alipay_account) {
            verifyTip = '请填写收款账户';
        }

    }

    res.goods = JSON.stringify(res.goods);
    res.payment = JSON.stringify(res.payment);
    return res;
}

// 打包换货信息
function packExchangeInfo() {
    var res = {};

    res.goods = packGoogsList();

    res.consigneeName = $.trim($exchange.find('input[name="name"]').val());
    if (!verifyTip && !res.consigneeName) {
        verifyTip = '请填写收货人姓名';
    }

    res.areaCode = $areaCode.val();
    if (!verifyTip && !res.areaCode * 1) {
        verifyTip = '请选择地区';
    }

    res.address = $.trim($exchange.find('input[name="address"]').val());
    if (!verifyTip && !res.address) {
        verifyTip = '请填写详细地址';
    }

    res.mobile = $.trim($exchange.find('input[name="phone"]').val());
    if (res.mobile === disPhone) {
        res.mobile = defaultPhone;
    } else {
        if (!verifyTip && !res.mobile) {
            verifyTip = '请填写手机号';
        }
    }

    res.zipCode = $.trim($exchange.find('input[name="postcode"]').val());
    res.deliveryType = $exchange.find('input[name="delivery-tpye"]:checked').val();
    if (!verifyTip && !res.deliveryType) {
        verifyTip = '请选择商品返回方式';
    }

    return res;
}

function loadWaiting(status) {
    var text = '○';

    if (status) {
        $submitBox.addClass('applying');
        timer = setInterval(function() {
            $waitPoint.text(text);
            if (text.length > 3) {
                text = '';
            }
            text += '○';
        }, 600);
    } else {
        $submitBox.removeClass('applying');
        clearInterval(timer);
    }
}

// 提交申请
$('.save-btn').click(function() {
    var url, data;

    verifyTip = ''; // 初始化提示语
    if (pageType) {
        url = '/home/returns/saveRefund';
        data = packRefundInfo();
    } else {
        url = '/home/returns/saveExchange';
        data = packExchangeInfo();
    }
    data.orderCode = orderCode;

    if (verifyTip) {
        alert(verifyTip);
        return;
    }

    loadWaiting(true);
    $.ajax({
        type: 'POST',
        url: url,
        data: data
    }).then(function(jsonData) {
        if (jsonData.code === 200) {
            if (jsonData.data && jsonData.data.refer) {
                window.location.href = jsonData.data.refer;
            }
        } else {
            alert(jsonData.message);
            loadWaiting(false);
        }
    });
});