您的位置:首页 > Web前端 > JavaScript

JavaScript处理cookie和session问题已经日期问题

2018-02-27 17:01 381 查看
/**
 * 2018/01/29
 * 公用方法
 */

var root = "http://211.95.60.40:16868";
//var root = "http://hupes.cn";
var api = {

    // 登录接口
    loginApi: root + "/health/carduser",

    //套餐选择页面 --快速添加受检人接口
    acceptApi: root + "/health/hsubject",

    // 套餐选择页面 -- 快速添加收货地址接口
    acceptAddress: root + "/health/hcollect",

    agreePay: root + "/health/asubs",

    showExpress: root + "/health/showExpress",

    onum: root + "/health/onum",

    //订单信息表格
    orderTable: root + "/health/showExpress",

    //订单页物流信息
    orderLogistics: root + "/health/onum",

    // 检测报告
    testReport: root + "/health/report"

}

//两个参数,一个是cookie的名子,一个是值
function SetCookie(name, value) {
    var Days = 30; //此 cookie 将被保存 30 天
    var exp = new Date(); //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//取cookies函数  
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]);
    return null;
}

//删除cookie
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

// 日期去横杠
function toStr(str) {
    return str.split('-').join('');
}

function changeDate2(val) {
    var date = new Date().getTime() - (val * 24 * 60 * 60 * 1000);
    var ndate = new Date(date);
    var Y = ndate.getFullYear() + '-';
    var M = (ndate.getMonth() + 1 < 10 ? '0' + (ndate.getMonth() + 1) : (ndate.getMonth() + 1)) + "-";
    var D = (ndate.getDate() < 10 ? '0' + (ndate.getDate()) : ndate.getDate() + "");
    ndate = Y + M + D;
    return ndate;
}
//获取当前时间
function changeDate() {
    var ndate = new Date();
    var Y = ndate.getFullYear() + '-';
    var M = (ndate.getMonth() + 1 < 10 ? '0' + (ndate.getMonth() + 1) : (ndate.getMonth() + 1)) + '-';
    var D = (ndate.getDate() < 10 ? '0' + (ndate.getDate()) : ndate.getDate());
    ndate = Y + M + D;
    return ndate;
}

// 本地存储  localS
function setLocVal(key, value) {
    window.localStorage[key] = value;
}

function getLocVal(key) {
    if (window.localStorage[key])
        return window.localStorage[key];
    else
        return "";
}

function delLocVal(key) {
    if (window.localStorage[key])
        return ""
}

// setLocVal("A",arr)
// getLocVal("A")

// 验证cookie是否过期
function loginAgain() {
    var cookie = getCookie("cardid");
    if (cookie == null) {
        window.location.href = "nocookie.html";
    }
}

// js数字转化成金额格式
function toMoney(num) {
    num = num.toFixed(2);
    num = parseFloat(num)
    num = num.toLocaleString();
    return num; //返回的是字符串23,245.12保留2位小数
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: