创建promose.js文件
import Promise from './es6-promise.min';
export const request = (method = 'GET') => (url, data) => {
return new Promise((resolve, reject) => {
wx.request({
url,
data,
method,
header: {
'Content-Type': 'application/json'
},
success: function (res) {
resolve(res.data)
},
fail: function (err) {
reject(err)
}
});
})
}
const get = request('GET');
const post = request('POST');
const put = request('PUT');
const del = request('DELETE');
module.exports = {
get,
post,
put,
del
}
创建api.js来存放api
import api from './promise.js'
export const getApp = ids => {
return api.get('https://example.com/sns/jscode2session',ids).then();
};
在index.js中使用定义好的api
import {
getApp
} from '../../utils/api';
const app = getApp()
Page({
data: {
},
bindViewTap: function() {
let ids='abc'
getApp(ids).then(res => {
console.log(res.data)
})
},
})