You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.4 KiB
JavaScript

/**
* 常用方法封装 请求,文件上传等
* @author echo.
**/
const tui = {
//接口地址
interfaceUrl: function() {
// return 'http://127.0.0.1:9993/api'
return 'http://60.209.125.238:9993/api'
// return 'http://118.190.210.123:9993/api'
},
//App升级地址
appUrl: function() {
// return 'http://127.0.0.1:9993/Update/'
return 'http://60.209.125.238:9993/Update/'
// return 'http://118.190.210.123:9993/Update'
},
//上传图片地址
uploadUrl: function() {
// return 'http://192.168.0.212:9991/api/VW_INFO_FILES/UploadDJZ'
return 'http://60.209.125.238:9991/api/VW_INFO_FILES/UploadDJZ'
// return 'http://118.190.210.123:9992/api/VW_INFO_FILES/UploadDJZ'
},
toast: function(text, duration, success) {
uni.showToast({
title: text || "出错啦~",
icon: success ? 'success' : 'none',
duration: duration || 2000
})
},
modal: function(title, content, showCancel, callback, confirmColor, confirmText) {
uni.showModal({
title: '提示',
content: content,
showCancel: showCancel,
cancelColor: "#555",
confirmColor: confirmColor || "#5677fc",
confirmText: confirmText || "确定",
success(res) {
if (res.confirm) {
callback && callback(true)
} else {
callback && callback(false)
}
}
})
},
isAndroid: function() {
const res = uni.getSystemInfoSync();
return res.platform.toLocaleLowerCase() == "android"
},
constNum: function() {
let time = 0;
// #ifdef APP-PLUS
time = this.isAndroid() ? 300 : 0;
// #endif
return time
},
delayed: null,
/**
* 请求数据处理
* @param string url 请求地址
* @param string method 请求方式
* GET or POST
* @param {*} postData 请求参数
* @param bool isDelay 是否延迟显示loading
* @param bool isForm 数据格式
* true: 'application/x-www-form-urlencoded'
* false:'application/json'
* @param bool hideLoading 是否隐藏loading
* true: 隐藏
* false:显示
*/
request: function(url, method, postData, isDelay, isForm, hideLoading) {
//接口请求
// let loadding = false;
// tui.delayed && uni.hideLoading();
// clearTimeout(tui.delayed);
// tui.delayed = null;
// if (!hideLoading) {
// tui.delayed = setTimeout(() => {
// uni.showLoading({
// mask: true,
// title: '请稍候...',
// success(res) {
// loadding = true
// }
// })
// }, isDelay ? 1000 : 0)
// }
return new Promise((resolve, reject) => {
uni.showLoading()
// // 追加统一请求参数
// const tempdata = {
// UserId: uni.getStorageSync("UserId"),
// }
// // console.log('统一数据',tempdata)
// Object.assign(postData, tempdata)
// console.log('请求数据',postData)
// console.log('请求加密数据',Encrypt.Encrypt(JSON.stringify(postData)))
var inurl = tui.interfaceUrl() + url;
console.log('请求地址',inurl)
// console.log("token",tui.getToken())
uni.request({
url: inurl,
// data: {
// ReqSign: Encrypt.Encrypt(JSON.stringify(postData)),
// },
data: postData,
header: {
'content-type': isForm ? 'application/x-www-form-urlencoded' : 'application/json',
'Authorization': 'Bearer ' + tui.getToken()
},
method: method, //'GET','POST'
dataType: 'json',
success: (res) => {
uni.hideLoading()
// console.log('返回数据',res)
// console.log('返回数据0',res.data)
// console.log('返回数据1',Encrypt.Decrypt(res.data))
// var result = JSON.parse(Encrypt.Decrypt(res.data)).ResSign
// var result = JSON.parse(res.data)
// resolve(JSON.parse(result))
// console.log('解密数据',result)
resolve(res.data)
},
fail: (res) => {
console.log('fail返回数据',res)
uni.hideLoading()
reject(res)
},
})
})
},
/**
* 上传文件
* @param string url 请求地址
* @param string src 文件路径
*/
uploadFile: function(url, src) {
uni.showLoading({
title: '请稍候...'
})
return new Promise((resolve, reject) => {
const uploadTask = uni.uploadFile({
url: tui.interfaceUrl() + url,
filePath: src,
name: 'imageFile',
header: {
'Authorization': tui.getToken()
},
formData: {
// sizeArrayText:""
},
success: function(res) {
uni.hideLoading()
let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
if (d.code % 100 == 0) {
//返回图片地址
let fileObj = d.data;
resolve(fileObj)
} else {
that.toast(res.msg);
}
},
fail: function(res) {
reject(res)
that.toast(res.msg);
}
})
})
},
tuiJsonp: function(url, callback, callbackname) {
// #ifdef H5
window[callbackname] = callback;
let tuiScript = document.createElement("script");
tuiScript.src = url;
tuiScript.type = "text/javascript";
document.head.appendChild(tuiScript);
document.head.removeChild(tuiScript);
// #endif
},
//设置用户信息
setUserInfo: function(mobile, token) {
//uni.setStorageSync("thorui_token", token)
uni.setStorageSync("thorui_mobile", mobile)
},
//获取token
getToken() {
return uni.getStorageSync("Token")
},
//判断是否登录
isLogin: function() {
return uni.getStorageSync("thorui_mobile") ? true : false
},
//跳转页面,校验登录状态
href(url, isVerify) {
if (isVerify && !tui.isLogin()) {
uni.navigateTo({
url: '/pages/common/login/login'
})
} else {
uni.navigateTo({
url: url
});
}
}
}
export default tui