function baseRequest({url、method、header、data}、resolve、reject){ wx.request({ url、 method、 header、 data、 success:function(res){ //この段落を使用するには、サーバーコードを判断する必要があります //戻り値0は実際の成功を意味し、他のコードは (res.data.code === 0){ resolve(res) } else { reject(res) } resolve(res) }の場合、さまざまなエラーコードを示し、 失敗:function(res ){ 拒否(解像度) } }) }
約束を使う
function requestPromise(options){ let req = new Promise((resolve、reject)=> { baseRequest(options、resolve、reject) }) return req }
getメソッド
function get(options){ options.method = 'GET' return requestPromise(options) }
ポストメソッド
function post(options){ options.method = 'POST' if(!options.header){ options.header = {} } options.header ["Content-Type"] = "application / json" return requestPromise(options) }
putメソッド
function put(options){ options.method = 'PUT' if(!options.header){ options.header = {} } options.header ["Content-Type"] = "application / json" return requestPromise(options) }