위챗 애플릿 리퀘스트 봉인 (get,post,put,remove)

10434 단어 #위챗 애플릿
http.js
const app = getApp()
const request = (url, options) => {
   return new Promise((resolve, reject) => {
       wx.request({
           url: `${app.globalData.serviceUrl}${url}`,
           method: options.method,
           data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
           header: {
            Authorization:wx.getStorageSync('token'),
            villageId:wx.getStorageSync('villageId')
           },
           success(request) {
               if (request.data.code === 200) {	// 
                   resolve(request.data)
               } else {
                   reject(request.data)
               }
           },
           fail(error) {
               reject(error.data)
           }
       })
   })
}
const get = (url, options = {}) => {
   return request(url, { method: 'GET', data: options })
}
const post = (url, options) => {
   return request(url, { method: 'POST', data: options })
}
const put = (url, options) => {
   return request(url, { method: 'PUT', data: options })
}
const remove = (url, options) => {
   return request(url, { method: 'DELETE', data: options })
}

module.exports = {
   get,
   post,
   put,
   remove
}

사용
import http from '../../../utils/http'	// http.js
http.get(`api `, {}).then(res => {

}).catch(err => {

})

좋은 웹페이지 즐겨찾기