//utils/api.js
const App = getApp();
const baseUrl = 'https://www.***.com';//域名
// 网络请求
const https = ({
url = '',
data = {},
method
} = {}) => {
wx.showLoading({
title: '加载中...'
});
return new Promise((resolve, reject) => {
wx.request({
url: getUrl(url),
method: method,
data: data,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
complete: (res) => {
wx.hideLoading();
console.log('返回数据');
console.log(res)
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(res.data)
} else {
reject(res)
}
}
});
});
}
// 上传文件
const uploadFile = ({
url = '',
filePath,
formData = {},
name
} = {}) => {
wx.showLoading({
title: '加载中...'
});
return new Promise((resolve, reject) => {
wx.uploadFile({
url: getUrl(url),
filePath: filePath,
name: name,
formData: formData,
success: (res) => {
console.log('上传接口数据', res);
wx.hideLoading();
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(res.data)
} else {
reject(res)
}
}
})
});
}
// 请求网址
const getUrl = (url) => {
if (url.indexOf('://') == -1) {
url = baseUrl + url;
}
return url
}
// get方法
const _get = (url, data = {}) => {
return https({
url,
method: 'GET',
data
})
}
// post方法
const _post = (url, data = {}) => {
return https({
url,
method: 'POST',
data
})
}
// 上传录音
const uploadAudio = (filePath, formData = { 'token': App.globalData.token }) => {
return uploadFile({
url: '/index.php/Asset/Upload/plupload',
filePath,
formData,
name: 'file'
})
}
// 上传图片
const uploadImg = (filePath, formData = { 'token': App.globalData.token }) => {
return uploadFile({
url: '/index.php/Asset/Upload/advpPlupload',
filePath,
formData,
name: 'file'
})
}
// 上传视频
const uploadVideo = (filePath, formData = { 'token': App.globalData.token }) => {
return uploadFile({
url: '/index.php/Asset/Upload/advpPlupload',
filePath,
formData,
name: 'file'
})
}
module.exports = {
baseUrl,
_get,
_post,
uploadAudio,
uploadImg,
uploadVideo
}
//index.js 获取应用实例
const Request = require('../../utils/api.js');
const App = getApp();
Page({
data: {
token: null,
userInfo: {},
},
onLoad: function() {
var _this = this;
var g_userInfo = App.getStorageSync("g_userInfo");
_this.setData({
userInfo: g_userInfo
});
},
onShow: function() {
var _this = this;
var g_userInfo = App.getStorageSync("g_userInfo");
_this.setData({
userInfo: g_userInfo
});
//使用实例
// POST方式获取
var data = {
token: App.globalData.token
}
Request._post(
'/index.php?g=User&m=Consumer&a=moneyAndAdv',
data
).then(res => {
console.log(res);
}).catch(e => {
console.log(e);
});
// GET方式获取
var data = {
token: App.globalData.token
}
Request._post(
'/index.php?g=User&m=Consumer&a=moneyAndAdv',
data
).then(res => {
console.log(res);
}).catch(e => {
console.log(e);
});
//上传音频
Request.uploadAudio(
_this.data.touchFile //音频文件
).then(res => {
//成功之后返回值
console.log(res);
}).catch(e => {
//失败之后返回值
console.log(e)
});
// 上传图片
Request.uploadImg(
_this.data.img_path //图片文件
).then(res => {
console.log(res);
}).catch(e => {
console.log(e)
});
//上传视频
Request.uploadVideo(
_this.data.video_path //视频文件
).then(res => {
console.log(res);
}).catch(e => {
console.log(e)
});
//接口使用方法,按需使用
},
});