const App = getApp(); //获取全局数据
var qts = () => {
//网络数据请求
this.request = (url, OBJECT, isload) => {
//是否开发加载效果
this.showLoading(isload)
return new Promise((resolve, reject) => {
//获取数据
var defaults = {};
//合并对象参数
var datas = Object.assign(defaults, OBJECT);
my.request({
url: url,
method: 'POST',
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify(datas),
timeout: 50000,
dataType: 'json',
success: (res) => {
this.hideLoading(isload);
resolve(res)
},
fail: function(res) {
my.hideLoading();
if (res.error == 12) {
this.toast('网络出错');
} else if (res.error = 13) {
resolve(res.error)
} else if (res.error = 14) {
this.toast('解析失败')
}
}
});
})
};
//提示框
this.toast = (content, type) => {
my.showToast({
type: type == undefined ? "none" : type, //默认空类型
content: content,
duration: 2000
})
};
//确认框
this.confirm = (data) => {
var obj = data;
return new Promise((resolve, reject) => {
my.confirm({
title: obj.title || '温馨提示',
content: obj.content,
confirmButtonText: obj.confirmButtonText || '确定',
cancelButtonText: obj.cancelButtonText || '取消',
success: (result) => {
if (result.confirm) {
resolve(true);
} else {
resolve(false);
}
}
});
})
}
//加载状态
this.showLoading = (isload, content) => {
if (isload) {
my.showLoading({
content: content === undefined ? '加载中...' : content
})
}
};
//关闭加载状态
this.hideLoading = (isload) => {
if (isload) {
my.hideLoading()
}
};
//获取手机信息
this.getSystemInfoPage = () => {
return new Promise((resolve, reject) => {
my.getSystemInfo({
success: (res) => {
resolve(res)
}
})
})
};
//缓存数据
this.setStorage = (name, datas) => {
my.setStorage({
key: name,
data: datas,
fail: () => {
this.toast('缓存失败');
}
});
};
//获取缓存的数据
this.getStorage = (name) => {
return new Promise((resolve, reject) => {
my.getStorage({
key: name,
success: (res) => {
resolve(res)
},
fail: (res) => {
this.toast('获取缓存失败');
}
});
})
};
//清除缓存
this.removeStorage = (name) => {
return new Promise((resolve, reject) => {
my.removeStorage({
key: name,
success: () => {
resolve('success');
},
fail: () => {
this.toast('清除缓存失败')
}
});
})
};
//预览图片
this.previewImage = (currentIndex, arr) => {
my.previewImage({
current: currentIndex,
urls: arr
})
};
//全局参数调用,不建议多用
this.globalData = () => {
return App.globalData
}
//创建对象的处理
if (!(this instanceof qts)) {
return new qts();
};
};
//定义输出接口
module.exports = {
qts: qts
}
//使用方法
var qts = require('/utils/qts.js').qts();
qts.request(url, data).then((res) => {});