封装请求地址 https.js
let baseUrl='https://XX.XXX.com/index.php/';
export {
baseUrl
}
结构目录

封装 request.js
import { baseUrl } from './https.js'
module.exports = {
request : function(url, methodType, data){
let fullUrl = `${baseUrl}${url}`
wx.showLoading({ title: "数据请求中" });
return new Promise((resolve,reject)=>{
wx.request({
url: fullUrl,
method:methodType,
data:data,
header: {
'content-type': 'application/json',
},
success(res){
resolve(res)
wx.hideLoading()
},
fail(){
wx.showToast({
title: '接口请求错误',
icon:'none'
})
reject('接口请求错误')
}
})
})
},
uploadFile : function(url, filePath, fname, formData){
let fullUrl = `${baseUrl}${url}`
wx.showLoading({ title: "上传图片中" });
return new Promise((resolve,reject)=>{
wx.uploadFile({
filePath: filePath,
name: fname,
url: fullUrl,
formData:formData,
success(res){
resolve(res)
},
fail(){
wx.showToast({
title: '接口请求错误',
icon:'none'
})
reject('接口请求错误')
}
})
})
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
自己的js文件构造
import { request } from './request'
module.exports = {
getPurchaseList: (data) => request('api/Api/list', 'POST', data),
}
如何调用
const api = require('../../../api/purchase');
getPurchaseList(){
var that = this;
let data ={
"openId": that.data.userInfo.app_openid,
"token": that.data.userInfo.login_token
};
api.getPurchaseList(data).then((res) => {
if (res.data.status == 1) {
that.setData({
goodsTypes: res.data
})
} else {
app.myShowToast(res.data.msg);
}
})
},
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22