http.js
const app = getApp();// 接口地址(測試需改本頁接口地址和app.js的圖片地址)
const api_url = 'https://api.sea-1st.com/';//正式
// 錯(cuò)誤提示
const tips = {
????1: '抱歉,出現(xiàn)了一個(gè)未知錯(cuò)誤',
????401: 'Token過期或無效',
????404: '服務(wù)器被偷了',
????405: '請求錯(cuò)誤',
????500: '服務(wù)器錯(cuò)誤',
????501: '請求未被支持'
}
class HTTP {
// 網(wǎng)絡(luò)請求函數(shù)
????request(params) {
????????if (!params.method) {
????????????params.method = 'GET'
????????}
????????wx.showLoading({
????????????title: '正在請求',
????????????mask:true,
????????})
????????wx.request({
????????????url: api_url + params.url,
????????????method: params.method,
????????????data: params.data,
????????????header: {
????????????????'content-type': 'application/x-www-form-urlencoded',
????????????????'Authorization': app.globalData.token
? ? ? ? ? ? },
????????????success: (res) => {? ?
?????????????????let statusCode = res.statusCode.toString();
????????????????if (statusCode=='200') {
????????????????????// 回調(diào)函數(shù)
????????????????????params.success(res.data);
????????????????????// console.log(res.data);
????????????????} else {? ?
????????????????????if (statusCode=='401'){
????????????????????????app.login_out();
????????????????????????app.goLogin('index');
????????????????????}
????????????????????this._show_error(statusCode);
? ? ? ? ? ? ? ? }
????????????},
????????????fail: () => {
????????????????this._show_error(1);
????????????},
????????????complete: () => {
????????????????wx.hideLoading()
????????????}
????????})
????}
????// 錯(cuò)誤處理函數(shù)
????_show_error(error_code) {
????????if (!tips[error_code]) {
????????????error_code = 1
????????}
????????wx.showToast({
????????????title: tips[error_code],
????????????icon: 'none',
????????????duration: 2000
????????})
????}
}
export {
????HTTP
}
// 調(diào)用模板-僅適用該程序接口數(shù)據(jù)返回形式
// http.request({
????// url: '',
????// data: { // },
????// method: 'POST',
????// success: (res) => {
????????// var info = res;
????????// app.tip(info.msg);
????????// if (info.code == 0) {
????????// }
????// }
// })
調(diào)用
import { HTTP } from '../../utils/http.js';
let http = new HTTP();