|
|
|
|
import { hideLoading, showLoading } from '@/utils/loading.js'
|
|
|
|
|
|
|
|
|
|
const NO_TOKEN_URL_PREFIXES = [
|
|
|
|
|
'/mall/order/getOrdersByUserId/',
|
|
|
|
|
'/mall/delivery/page',
|
|
|
|
|
'/mall/delivery/countOrderByStatus',
|
|
|
|
|
'/mall/order/countByShop/',
|
|
|
|
|
'/app/shopArea/getByParentId/'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
function matchesNoTokenUrl(url, prefix) {
|
|
|
|
|
const path = String(url || '').split('?')[0]
|
|
|
|
|
if (String(prefix || '').endsWith('/')) {
|
|
|
|
|
return path.indexOf(prefix) === 0
|
|
|
|
|
}
|
|
|
|
|
return path === prefix
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shouldAttachToken(url) {
|
|
|
|
|
return !NO_TOKEN_URL_PREFIXES.some(prefix => matchesNoTokenUrl(url, prefix))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSafeDeviceInfo() {
|
|
|
|
|
if (uni.getDeviceInfo) {
|
|
|
|
|
return uni.getDeviceInfo() || {}
|
|
|
|
|
}
|
|
|
|
|
return uni.getSystemInfoSync() || {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSafeWindowInfo() {
|
|
|
|
|
if (uni.getWindowInfo) {
|
|
|
|
|
return uni.getWindowInfo() || {}
|
|
|
|
|
}
|
|
|
|
|
return uni.getSystemInfoSync() || {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tui = {
|
|
|
|
|
//接口地址
|
|
|
|
|
interfaceUrl: function() {
|
|
|
|
|
return 'https://hbkuaishi.com/hiver' //正式
|
|
|
|
|
// return 'http://127.0.0.1:8888/hiver' //测试
|
|
|
|
|
},
|
|
|
|
|
toast: function(text, duration, success) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
duration: duration || 1000,
|
|
|
|
|
title: text || "出错啦~",
|
|
|
|
|
icon: success ? 'success' : 'none',
|
|
|
|
|
mask:true
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
modal: function(title, content, showCancel, callback, confirmColor, confirmText) {
|
|
|
|
|
uni.showModal({
|
|
|
|
|
title: 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 = getSafeDeviceInfo()
|
|
|
|
|
const osName = String(res.osName || res.system || '').toLowerCase()
|
|
|
|
|
return osName.indexOf('android') !== -1
|
|
|
|
|
},
|
|
|
|
|
isPhoneX: function() {
|
|
|
|
|
const res = getSafeWindowInfo()
|
|
|
|
|
const safeArea = res.safeArea || {}
|
|
|
|
|
const screenHeight = Number(res.screenHeight || res.windowHeight || 0)
|
|
|
|
|
return !!(safeArea.bottom && screenHeight && safeArea.bottom < screenHeight)
|
|
|
|
|
},
|
|
|
|
|
constNum: function() {
|
|
|
|
|
let time = 0;
|
|
|
|
|
time = this.isAndroid() ? 300 : 0;
|
|
|
|
|
return time
|
|
|
|
|
},
|
|
|
|
|
delayed: null,
|
|
|
|
|
loadding: false,
|
|
|
|
|
showLoading: function(title, mask = true) {
|
|
|
|
|
showLoading({
|
|
|
|
|
mask: mask,
|
|
|
|
|
title: title || '请稍候...'
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
hideLoading: function() {
|
|
|
|
|
hideLoading()
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* 请求数据处理
|
|
|
|
|
* @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: async function(url, method, postData, isDelay, isForm, hideLoading) {
|
|
|
|
|
//接口请求
|
|
|
|
|
tui.loadding && tui.hideLoading();
|
|
|
|
|
tui.loadding = false;
|
|
|
|
|
if (!hideLoading) {
|
|
|
|
|
if (isDelay) {
|
|
|
|
|
tui.delayed = setTimeout(() => {
|
|
|
|
|
tui.loadding = true
|
|
|
|
|
tui.showLoading()
|
|
|
|
|
}, 1000)
|
|
|
|
|
} else {
|
|
|
|
|
tui.loadding = true
|
|
|
|
|
tui.showLoading()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let executeRequest = () => {
|
|
|
|
|
const header = {
|
|
|
|
|
'content-type': isForm ? 'application/x-www-form-urlencoded' : 'application/json'
|
|
|
|
|
}
|
|
|
|
|
const token = tui.getToken()
|
|
|
|
|
if (shouldAttachToken(url) && token && typeof token === 'string' && token.trim() && token !== 'null' && token !== 'undefined') {
|
|
|
|
|
header.accessToken = token.trim()
|
|
|
|
|
}
|
|
|
|
|
wx.request({
|
|
|
|
|
url: tui.interfaceUrl() + url,
|
|
|
|
|
data: postData,
|
|
|
|
|
timeout:60000,
|
|
|
|
|
header,
|
|
|
|
|
method: method, //'GET','POST'
|
|
|
|
|
dataType: 'json',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
if(res.statusCode == 401 || (res.data && res.data.code == 401)){
|
|
|
|
|
if(uni.getStorageSync('codePage')){
|
|
|
|
|
uni.clearStorage()
|
|
|
|
|
uni.setStorageSync('codePage',true)
|
|
|
|
|
}else{
|
|
|
|
|
uni.clearStorage()
|
|
|
|
|
}
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: '/package2/login/login'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if(uni.getStorageSync(url)){
|
|
|
|
|
uni.removeStorageSync(url)
|
|
|
|
|
}
|
|
|
|
|
clearTimeout(tui.delayed)
|
|
|
|
|
tui.delayed = null;
|
|
|
|
|
if (tui.loadding && !hideLoading) {
|
|
|
|
|
tui.hideLoading()
|
|
|
|
|
}
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
},
|
|
|
|
|
fail: (res) => {
|
|
|
|
|
if(uni.getStorageSync(url)){
|
|
|
|
|
if(uni.getStorageSync(url)< 4){
|
|
|
|
|
let count = uni.getStorageSync(url)
|
|
|
|
|
uni.setStorageSync(url,count + 1)
|
|
|
|
|
executeRequest();
|
|
|
|
|
}else{
|
|
|
|
|
uni.removeStorageSync(url)
|
|
|
|
|
reject(res)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
uni.setStorageSync(url,1)
|
|
|
|
|
executeRequest();
|
|
|
|
|
}
|
|
|
|
|
clearTimeout(tui.delayed)
|
|
|
|
|
tui.delayed = null;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
executeRequest();
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* 上传文件
|
|
|
|
|
* @param string url 请求地址
|
|
|
|
|
* @param string src 文件路径
|
|
|
|
|
*/
|
|
|
|
|
uploadFile: function(url, src) {
|
|
|
|
|
tui.showLoading()
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const uploadTask = uni.uploadFile({
|
|
|
|
|
url: tui.interfaceUrl() + url,
|
|
|
|
|
filePath: src,
|
|
|
|
|
name: 'file',
|
|
|
|
|
header: {
|
|
|
|
|
// 'Authorization': tui.getToken()
|
|
|
|
|
'accessToken': uni.getStorageSync("hiver_token")
|
|
|
|
|
},
|
|
|
|
|
formData: {
|
|
|
|
|
// sizeArrayText:""
|
|
|
|
|
},
|
|
|
|
|
success: function(res) {
|
|
|
|
|
tui.hideLoading()
|
|
|
|
|
let responseText = res.result || res.data || "{}"
|
|
|
|
|
let d = JSON.parse(String(responseText).replace(/\ufeff/g, "") || "{}")
|
|
|
|
|
if (d.code % 100 == 0) {
|
|
|
|
|
// 返回OSS文件地址
|
|
|
|
|
let fileObj = d.result !== undefined ? d.result : d.data;
|
|
|
|
|
resolve(fileObj)
|
|
|
|
|
} else {
|
|
|
|
|
tui.toast(d.message || res.message || '上传失败');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: function(res) {
|
|
|
|
|
tui.hideLoading()
|
|
|
|
|
reject(res)
|
|
|
|
|
tui.toast(res.message || '上传失败');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
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("hiver_token", token)
|
|
|
|
|
uni.setStorageSync("hiver_mobile", mobile)
|
|
|
|
|
},
|
|
|
|
|
//获取token
|
|
|
|
|
getToken() {
|
|
|
|
|
return uni.getStorageSync("hiver_token")
|
|
|
|
|
},
|
|
|
|
|
//判断是否登录
|
|
|
|
|
isLogin: function() {
|
|
|
|
|
return uni.getStorageSync("hiver_mobile") ? true : false
|
|
|
|
|
},
|
|
|
|
|
//跳转页面,校验登录状态
|
|
|
|
|
href(url, isVerify) {
|
|
|
|
|
if (isVerify && !tui.isLogin()) {
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: '/package2/login/login'
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: url
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default tui
|