js封裝,常用ajax刪除和手機驗證,上傳圖片的方法,基於layer

weixin_33751566發表於2017-11-10
sessionStorage.setItem("vali_data", false);
/**
 * 上傳圖片
 * @param trigger 觸發事件
 * @param preview 預覽元素
 * @param up_path 插入隱藏input元素
 */
function laryer_upload_one(trigger,preview,up_path){
    layui.use('upload', function(){
        var upload = layui.upload;
        var upload = layui.upload;
        //執行例項
        var uploadInst = upload.render({
            elem: trigger, //點選按鈕
            url: "/user/asset/webuploader.html?_ajax=1", //url
            before: function(obj){
                //預讀本地檔案示例,不支援ie8
                obj.preview(function(index, file, result){
                    $(preview).attr('src', result); //圖片連結(base64)
                });
            },
            done: function(res){
                //如果上傳失敗
                if(res.code != 1){
                    return layer.msg('上傳失敗', {icon: 5});
                }
                var url = res.data.preview_url;

                $(up_path).val(url);
                //上傳成功

                return layer.msg('上傳成功',{icon: 6});
            }
            ,error: function(){
                return layer.msg('上傳失敗',{icon: 5});
            }
        });

    });
}
/**
 * 上傳多張圖片
 * @param trigger 觸發事件
 * @param preview 預覽元素
 * @param up_path 插入隱藏input元素
 * @param name input 存放路徑要
 */
function laryer_upload_multi(trigger,preview,up_path,name){
    layui.use('upload', function(){
        var upload = layui.upload;
        var upload = layui.upload;
        //執行例項
        var uploadInst = upload.render({
            elem: trigger, //點選按鈕
            url: "/user/asset/webuploader.html?_ajax=1", //url
            before: function(obj){
                //預讀本地檔案示例,不支援ie8
                // obj.preview(function(index, file, result){
                //     $(preview).attr('src', result); //圖片連結(base64)
                // });
            },
            done: function(res){
                //如果上傳失敗
                if(res.code != 1){
                    return layer.msg('上傳失敗', {icon: 5});
                }
                var url = res.data.preview_url;

                $(up_path).after('<input type="hidden" class="form-control" value="'+url+'" id="" name="'+name+'[]" >');
                //上傳成功
                add("#fake-img",".fake-div-img",url);
                return layer.msg('上傳成功',{icon: 6});
            }
            ,error: function(){
                return layer.msg('上傳失敗',{icon: 5});
            }
        });

    });
}
function add(ID,Class,img){
    $(Class).append("<div class='col-md-3'><img class='fake-img1' width='200' height='200' src='"+img+"' alt=''/></div>")
}
/**
 * 驗證手機
 * @returns {boolean}
 */
function checkPhone(phone){
    if(!(/^1[34578]\d{9}$/.test(phone))){
        return false;
    }
    return true;
}
$('button').click(function () {
    $('.layui-upload-file').remove();
});

/**
 * 傳送簡訊
 * @param mobile_input 手機號存放的元素
 * @param send_button 倒數計時提示元素
 * @param url 傳送的URL
 */
function send_sms(mobile_input,send_button,ver,url){
    var mobile = $(mobile_input).val();
    if(!checkPhone(mobile))
    {
        return layer.msg('手機號不正確!', {icon: 5});
    }

    $.ajax({
        type: 'POST',
        url:url,
        data: {mobile:mobile,ver:ver},
        dataType: 'json',
        success: function(data)
        {
            if (data['code'] == 1)
            {
                invokeSettime(send_button);
                return layer.msg('傳送成功!', {icon: 6});
            }
            else
            {
                return layer.msg(data.msg, {icon: 5});
            }
        },

        error: function()
        {
            return layer.msg('網路錯誤!', {icon: 5});
        }
    });

}
/**
 * 簡訊倒數計時
 * @param obj
 */
function invokeSettime(obj){
    if($(obj).attr('disabled') == 'disabled'){
        return false;
    }
    var countdown=60;
    settime(obj);
    function settime(obj) {
        if (countdown == 0) {
            $(obj).attr("disabled",false);
            $(obj).text("獲取驗證碼");
            countdown = 60;
            return;
        } else {
            $(obj).attr("disabled",true);
            $(obj).text("(" + countdown + ") s 重新傳送");
            countdown--;
        }
        setTimeout(function() {
                settime(obj) }
            ,1000)
    }
}

/**
 * 驗證手機簡訊
 * @param mobile_input 獲取手機號元素
 * @param valicode_button 獲取驗證碼元素
 */
function vali_code(mobile_input,valicode_button,url){
    var mobile = $(mobile_input).val();
    var valicode = $(valicode_button).val();
    $.ajax({
        type: 'POST',
        url:url,
        data: {mobile:mobile,ver:valicode},
        dataType: 'json',
        success: function(data)
        {
            if (data['code'] == 1)
            {
                sessionStorage.setItem("vali_data", true);
                return layer.msg('驗證成功!', {icon: 6});
            }
            else
            {
                sessionStorage.setItem("vali_data", false);
                layer.msg("驗證失敗!", {icon: 5});
                return false;
            }
        },

        error: function()
        {
            return layer.msg('網路錯誤!', {icon: 5});
        }
    });
}
function reloadPage(win) {
    var location = win.location;
    location.href = location.pathname + location.search;
}
//所有的刪除操作,刪除資料後重新整理頁面
if ($('a.js-ajax-delete').length) {
    layui.use('layer', function () {
        $('.js-ajax-delete').on('click', function (e) {
            e.preventDefault();
            var _this = $(this);
            href = _this.attr('href');
            msg = _this.attr('msg');
            href = href ? href : _this.attr('href');
            msg = msg ? msg : "確定要刪除?";
            layer.confirm(msg, {
                btn: ['確定','取消'] //按鈕
            }, function(){
                $.getJSON(href).done(function (data) {
                    if (data.code == '1') {
                        _this.parents('tr').remove();
                        layer.msg('刪除成功!',{icon: 6});
                    } else if (data.code == '0') {
                        layer.msg('刪除失敗!',{icon: 5});
                    }
                });
            }, function(){
                return false;
            });

        });

    });
}

相關文章