1,封装request.js
let baseURL = '';
let token = ""
let white =['']
export const myRequest = (options) => {
uni.showLoading({title:"加载中......"})
return new Promise((resolve, reject) => {
uni.request({
url: baseURL + options.url,
method: options.method || 'GET',
data: options.data || {},
header: {
'Authorization': uni.getStorageSync('Token'),
'content-type': 'application/x-www-form-urlencoded',
},
success: (res) => {
if(res.statusCode === 401){
uni.reLaunch({
url:'/pages/login/toLogin'
});
return
}
if (res.data.state.code !== "1" && !white.includes(options.url)) {
return uni.showToast({
title: res.data.state.msg,
icon: 'none'
})
}
resolve(res)
},
fail: (err) => {
uni.hideLoading();
reject(err)
},
complete: () => {
uni.hideLoading();
typeof options.complete == "function" && options.complete(e.data);
return;
}
})
})
}
###
调用
import {Request} from './request.js'
export function getCode (data) {
return Request({
url:'api/login',
method:'post',
data
})
}
###
token 在调用登录接口时存储
2 获取高度
uni.getSystemInfoSync().windowHeight