import CryptoJS from 'crypto-js' //加密js var data_flag = true //标准时间转换成时间戳 var formatDateTime = function (te) { if (te == '') { return '' } else { var time = new Date(te); var dateTime = time.getTime(); return dateTime; } } // 取得cookie function getCookie(name) { var arrstr = document.cookie.split("; "); for (var i = 0; i < arrstr.length; i++) { var temp = arrstr[i].split("="); if (temp[0] == name) return temp[1]; } // if (document.cookie.length > 0) { // var arr = document.cookie.split('; '); //这里显示的格式需要切割一下自己可输出看下 // console.log(arr) // for (var i = 0; i < arr.length; i++) { // var arr2 = arr[i].split('='); //再次切割 // //这里会切割出以username为第0项的数组、以password为第0项的数组,判断查找相对应的值 // if (arr2[0] == 'username') { // name = arr2[1]; //拿到账号 // } else if (arr2[0] == 'password') { // //拿到拿到加密后的密码arr2[1]并解密 // var bytes = CryptoJS.AES.decrypt(arr2[1].toString(), 'swuE9cmCZQwrkYRV'); // var plaintext = bytes.toString(CryptoJS.enc.Utf8); //拿到解密后的密码(登录时输入的密码) // name = plaintext; // } // } // } // var nameEQ = name + '=' // var ca = document.cookie.split(';') // 把cookie分割成组 // for (var i = 0; i < ca.length; i++) { // var c = ca[i] // 取得字符串 // while (c.charAt(0) == ' ') { // 判断一下字符串有没有前导空格 // c = c.substring(1, c.length) // 有的话,从第二位开始取 // } // if (c.indexOf(nameEQ) == 0) { // 如果含有我们要的name // return unescape(c.substring(nameEQ.length, c.length)) // 解码并截取我们要值 // } // } // return false } // 清除cookie function clearCookie(name) { setCookie(name, "", -1); } // 设置cookie function setCookie(username, password, days) { var text = CryptoJS.AES.encrypt(password, 'swuE9cmCZQwrkYRV');//使用CryptoJS方法加密 var saveDays = new Date(); //获取时间 saveDays.setTime(saveDays.getTime() + 24 * 60 * 60 * 1000 * days); //保存的天数 console.log(saveDays) console.log(saveDays.toGMTString()) //字符串拼接存入cookie window.document.cookie = "username" + "=" + username + ";path=/;expires=" + saveDays.toGMTString(); window.document.cookie = "password" + "=" + text + ";path=/;expires=" + saveDays.toGMTString(); // seconds = seconds || 0; //seconds有值就直接赋值,没有为0,这个根php不一样。 // var expires = ""; // if (seconds != 0 ) { //设置cookie生存时间 // var date = new Date(); // date.setTime(date.getTime()+(seconds*1000)); // expires = "; expires="+date.toGMTString(); // } // document.cookie = name+"="+escape(value)+expires+"; path=/"; //转码并赋值 } //时间戳转时间格式 var formatDateTime_date = function (date) { var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; return y + '-' + m + '-' + d; }; export { data_flag, formatDateTime, getCookie, clearCookie, setCookie, formatDateTime_date }