/*封装部分公共方法
 * @auth zhjy
 */
var CAIMEI = window.CAIMEI = {};
var isWuHeng = isLocalStorageSupported();//无痕
var AmtRegExp =/^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/;
CAIMEI.Storage = {
    setItem:function(key,val){
        if(isWuHeng){
            window.localStorage.setItem(key,val);
        }else{
            setCookie(key,val,{maxAge:300000});
        }
    },
    getItem:function(key){
        if(isWuHeng){
            return window.localStorage.getItem(key);
        }else{
            var name = getCookie(key);
            return name;
        }
    },
    removeItem:function(key){
        if(isWuHeng){
            return window.localStorage.removeItem(key);
        }else{
            var name = getCookie(key);
            return name;
        }
    },
    clear:function(){
        if(isWuHeng){
            window.localStorage.clear();
        }else{
            clearCookie();
        }
    }
};
function isLocalStorageSupported(){
    var testKey = 'testWu',
        storage = window.sessionStorage;
    try {
        storage.setItem(testKey, 'testValue');
        storage.removeItem(testKey);
        return true;
    } catch (error) {
        return false;
    }
};
function getCookiesObj(){
    var cookies = {};
    if(document.cookie){
        var objs = document.cookie.split('; ');
        for(var i in objs){
            var index = objs[i].indexOf('='),
                name = objs[i].substr(0, index),
                value = objs[i].substr(index + 1, objs[i].length);
            cookies[name] = value;
        }
    }
    return cookies;
};
function setCookie(name, value,opts){
    if(name && value){
        var cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);
        if(opts){
            if(opts.maxAge){
                cookie += '; max-age=' + opts.maxAge;
            }
        }
        document.cookie = cookie;
    }else{
        return '';
    }
};
//获取cookie
function getCookie(name){
    return decodeURIComponent(getCookiesObj()[name]) || null;
};
//清除所有cookie
function clearCookie(){
    var cookies = getCookiesObj();
    for(var key in cookies){
        document.cookie = key + '=; max-age=0';
    }
};
/*获取URL后的传递参数
 * @param key 参数的传递字段
 * @auth zhjy
 */
CAIMEI.getUrlParam=function(key){
    var href = window.location.href;
    var param = href.substr(href.indexOf('?')+1).split('&'),obj={};
    for(var i=0;i<param.length;i++){
        var arr = param[i].split('=');
        obj[arr[0]] = arr[1];
    }
    return obj[key];
};
/*取消,确定提示弹窗
 * @param content 提示文字信息
 * @param cancelText 自定义取消按钮文字
 * @param confitmText 自定义确认按钮文字
 * @param callback 回调函数
 * @auth zhjy
 */
CAIMEI.Modal = function(content,cancelText,confitmText,callback){
    $.confirm({
        boxWidth: (isPC?'300px':'70%'),
        title:'提示',
        content: content,
        closeIcon: true,
        animation: 'opacity',
        closeAnimation: 'opacity',
        useBootstrap: false,
        animateFromElement: false,
        buttons: {
            cancel: {
                text: cancelText,
                btnClass: 'btn-cancel',
                action:function () {}
            },
            confirm:{
                text: confitmText,
                btnClass: 'btn-confirm',
                action:function () {
                    callback();
                }
            }
        }
    });
};
/*单个确定提示弹窗
 * @param content 提示文字信息
 * @param confitmText 自定义按钮文字内容
 * @param flg 判断是否需要回调函数
 * @param callback 回调函数
 * @auth zhjy
 */
CAIMEI.Alert = function(content,confitmText,flg,callback){
    $.confirm({
        boxWidth: (isPC?'300px':'70%'),
        title:'',
        content: content,
        closeIcon: true,
        animation: 'opacity',
        closeAnimation: 'opacity',
        useBootstrap: false,
        animateFromElement: false,
        buttons: {
            confirm:{
                text: confitmText,
                btnClass: 'btn-confirm',
                action:function () {
                    if(flg){
                        callback();
                    }
                }
            }
        }
    });
};
/*封装的吐司提示
 * @param content 提示文字信息
 * @param flg 判断是否需要执行回调函数
 * @param callback 回调函数
 * @auth zhjy
 */
CAIMEI.dialog = function(content,flg,callback){
    $.confirm({
        title: false,
        content: content,
        boxWidth: (isPC?'300px':'70%'),
        autoClose: 'close|2000',
        useBootstrap:false,
        buttons: {
            close:{
                isHidden: true,
                action: function () {
                    if(flg){
                        callback()
                    }
                }
            }
        }
    });
};
/*对象合并 IE 兼容方法
 * @param
 * @auth zhjy
 */
CAIMEI.returnedTarget = function(){
    if (typeof Object.assign != 'function') {
        Object.assign = function(target) {
            'use strict';
            if (target == null) {throw new TypeError('Cannot convert undefined or null to object');}
            target = Object(target);
            for (var index = 1; index < arguments.length; index++) {
                var source = arguments[index];
                if (source != null) {
                    for (var key in source) {
                        if (Object.prototype.hasOwnProperty.call(source, key)) {
                            target[key] = source[key];
                        }
                    }
                }
            }
            return target;
        };
    }
};
/*手机校验
 * @param m 输入的手机号
 * @auth zhjy
 */
CAIMEI.isPhone = function(mobile){
    var reg =  /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    return reg.test(mobile);
};
/*固话校验
 * @param m 输入的固定电话
 * @auth zhjy
 */
CAIMEI.isTel = function(mobile){
    var reg =  /^0\d{2,3}-?\d{7,8}$/;//固定电话
    return reg.test(mobile)
};
/*金额格式校验
 * @param m 输入的金额
 * @auth zhjy
 */
CAIMEI.isMoney = function(m){
    if(!AmtRegExp.test(m)){
        return true;
    }else{
        return false;
    }
};
/*邮箱校验
 * @param m 输入的邮箱号
 * @auth zhjy
 */
CAIMEI.isEmail = function(m){
    var reg =  /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;//邮箱正则
    return reg.test(m)
};
CAIMEI.returnedTarget = function(){//对象合并 IE 兼容方法
    if (typeof Object.assign != 'function') {
        Object.assign = function(target) {
            'use strict';
            if (target == null) {
                throw new TypeError('Cannot convert undefined or null to object');
            }
            target = Object(target);
            for (var index = 1; index < arguments.length; index++) {
                var source = arguments[index];
                if (source != null) {
                    for (var key in source) {
                        if (Object.prototype.hasOwnProperty.call(source, key)) {
                            target[key] = source[key];
                        }
                    }
                }
            }
            return target;
        };
    }
}