IntelligentRecognition/ah-jjsp-web/.svn/pristine/1f/1f99bd39e8fcd4590439c5c29b9...

198 lines
6.8 KiB
Plaintext
Raw Normal View History

2024-05-24 16:09:40 +08:00
/**
* 老首页
*/
layui.use(['layer', 'form', 'element'], function(){
form=layui.form;
})
document.onkeydown = function (e) { // 回车提交表单
// 兼容FF和IE和Opera
var theEvent = window.event || e;
var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
if (code == 13) {
login();
}
}
function login(){
var userName=$("#userName").val();
var password=$("#password").val();
if(userName==null || userName ==''){
layer.msg('请输入用户名', {
icon: 2,
time: 2000 //2秒关闭如果不配置默认是3秒
});
return ;
}
if(password==null || password ==''){
layer.msg('请输入密码', {
icon: 2,
time: 2000 //2秒关闭如果不配置默认是3秒
});
return ;
}
Ajax().post({
url: dataUrl + 'auth/login',
data: {
username: encrypt(userName),//加密处理
password: encrypt(password)//加密处理
},
async : true,
success : function(data) {
var code=data.code;
if(code=='200'){
localStorage.setItem("passWay","to-2");
var token=data.data.access_token;
localStorage.setItem("tokens",token);
localStorage.setItem("us",encrypt(data.data.us));
localStorage.setItem("html_type",1);
window.location.href="bns/html/home/home.html";
}else{
layer.msg(data.msg, {
icon: 2,
time: 2000 //2秒关闭如果不配置默认是3秒
});
}
}
});
}
/*
* ajax封装及拦截
*/
var Ajax = function() {
var that = this;
// 创建异步请求对象方法
that.createXHR = function() {
if (window.XMLHttpRequest) { // IE7+、Firefox、Opera、Chrome 和Safari
return new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE6 及以下
var versions = [ 'MSXML2.XMLHttp', 'Microsoft.XMLHTTP' ];
for (var i = 0, len = versions.length; i < len; i++) {
try {
return new ActiveXObject(version[i]);
break;
} catch (e) {
// 跳过
}
}
} else {
throw new Error('浏览器不支持XHR对象');
}
}
// 初始化数据方法
that.init = function(obj) {
// 初始化数据
var objAdapter = {
method : 'get',
data : {},
success : function() {
},
complete : function() {
},
error : function(s) {
console.log('status:' + s + 'error!')
},
async : true
}
// 通过使用JS随机字符串解决IE浏览器第二次默认获取缓存的问题
that.url = obj.url;
that.method = obj.method || objAdapter.method;
that.data = that.params(obj.data) || that.params(objAdapter.data);
that.async = obj.async || objAdapter.async;
that.complete = obj.complete || objAdapter.complete;
that.success = obj.success || objAdapter.success;
that.error = obj.error || objAdapter.error;
}
// ajax异步调用
that.ajax = function(obj) {
that.method = obj.method || 'get';
if (obj.method === 'post') {
that.post(obj);
} else {
that.get(obj);
}
}
// post方法
that.post = function(obj) {
var xhr = that.createXHR(); // 创建XHR对象
that.init(obj);
that.method = 'post';
if (that.async === true) { // true表示异步false表示同步
// 使用异步调用的时候需要触发readystatechange 事件
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) { // 判断对象的状态是否交互完成
that.callback(obj, this); // 回调
}
};
}
// 在使用XHR对象时必须先调用open()方法,
// 它接受三个参数:请求类型(get、post)、请求的URL和表示是否异步。
xhr.open(that.method, that.url, that.async);
// post方式需要自己设置http的请求头来模仿表单提交。
// 放在open方法之后send方法之前。
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
//xhr.setRequestHeader("key","222"); //将token放在header 里面
xhr.send(that.data); // post方式将数据放在send()方法里
if (that.async === false) { // 同步
that.callback(obj, this); // 回调
}
};
// get方法
that.get = function(obj) {
var xhr = that.createXHR(); // 创建XHR对象
that.init(obj);
if (that.async === true) { // true表示异步false表示同步
// 使用异步调用的时候需要触发readystatechange 事件
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) { // 判断对象的状态是否交互完成
that.callback(obj, this); // 回调
}
};
}
// 若是GET请求则将数据加到url后面
that.url += that.url.indexOf('?') == -1 ?'?' + that.data : '&'
+ that.data;
// 在使用XHR对象时必须先调用open()方法,
// 它接受三个参数:请求类型(get、post)、请求的URL和表示是否异步。
xhr.open(that.method, that.url, that.async);
xhr.send(null); // get方式则填null
if (that.async === false) { // 同步
that.callback(obj, this); // 回调
}
}
// 请求成功后,回调方法
that.callback = function(obj, xhr) {
console.log(xhr)
if (xhr.status == 200) { // 判断http的交互是否成功200表示成功
var succobj=JSON.parse(xhr.responseText);
obj.success(succobj); // 回调传递参数
} else if (xhr.status == 401) {
// alert('未授权');
console.log('status:' + s + 'error!')
}else{
console.log('获取数据错误!错误代号:' + xhr.status + ',错误信息:' + xhr.statusText)
// alert('获取数据错误!错误代号:' + xhr.status + ',错误信息:' + xhr.statusText);
}
}
// 数据转换
that.params = function(data) {
var arr = [];
for ( var i in data) {
// 特殊字符传参产生的问题可以使用encodeURIComponent()进行编码处理
arr.push(encodeURIComponent(i) + '=' + encodeURIComponent(data[i]));
}
return arr.join('&');
}
return {
post : that.post,
get : that.get,
ajax : that.ajax
}
}