위 챗 애플 릿 API 사용 Promise 패키지 wx. request 통합 요청 입구

http 패키지 js: httpService. js
import {
  httpMethod
} from "./commonConst";

function wxPromisify() {
  return function (obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function (res) {
        resolve(res.data);
      };
      obj.fail = function (error) {
        reject(error);
      };
      wx.request(obj);
    });
  };
}

//  promise            
Promise.prototype.finally = function (callback) {
  let P = this.constructor;
  return this.then(
    value => P.resolve(callback()).then(() => value),
    reason => P.resolve(callback()).then(() => { throw reason; })
  );
};

function postMethod(url, headerObj, bodyObj) {
  var getRequest = wxPromisify(wx.request);
  return getRequest({
    url: url,
    header: headerObj,
    data: bodyObj,
    method: httpMethod.POST
  });
}

function getMethod(url, headerObj) {
  var getRequest = wxPromisify();
  return getRequest({
    url: url,
    header: headerObj,
    method: httpMethod.GET
  });
}
function deleteMethod(url, headerObj) {
  var getRequest = wxPromisify();
  return getRequest({
    url: url,
    header: headerObj, 
    method: httpMethod.DELETE
  });
}

function putMethod(url, headerObj, bodyObj) {
  var getRequest = wxPromisify();
  return getRequest({
    url: url,
    header: headerObj, 
    data: bodyObj,
    method: httpMethod.PUT
  });
}

//      
module.exports = {
  get: getMethod,
  post: postMethod,
  delete: deleteMethod,
  put: putMethod
};

사용 예시: org. js
import {
  post
} from '../../utils/httpServices'
import {
  URL
} from '../../utils/constants'

/**
 *   
 */
function add(headerObj, bodyObj) {
  return post(URL.add, headerObj, bodyObj);
}

/**
 *     
 */
function list(headerObj, bodyObj) {
  return post(URL.list, headerObj, bodyObj);
}

/**
 *   
 */
function delete(headerObj, bodyObj) {
  return post(URL.delete, bodyObj, headerObj);
}

/**
 *   
 */
function update(headerObj, bodyObj) {
  return post(URL.update, headerObj, bodyObj);
}

/**
 *   
 */
function search(headerObj, bodyObj) {
  return post(URL.search, bodyObj, headerObj);
}

module.exports.orgService = {
  add: add,
  delete: delete,
  update: update,
  list: list,
  search: search
};

호출 방법: update. js
import {
  orgService
} from '../../../services/org';

Page({
  ...
  ...
  data: {
  
  },
  /**
   *       --      
   */
  onLoad: function () {
    this.getUpdateData();
  },

  /**
   *   
   */
  getUpdateData: async function() {
    let headerObj = { ... };
    let bodyObj = {...};
    return await orgService.update(headerObj, bodyObj).then(res => {
      console.log('res',res);
    }).catch(error => {
      console.log('error',error);
    })
  },

})



constants.js

// host 
const testHost = "https://test.com";

//         path
const addPath = '/api/add';
const listPath = '/api/list';
const searchPath = '/api/search';
const updatePath = '/api/infoupdate';
const deletePath = '/api/delete';



// pathURL
module.exports.URL = {
  add: testHost + addPath ,
  list: testHost + listPath ,
  search: testHost + searchPath ,
  update: testHost + updatePath ,
  delete: testHost + deletePath 
}

좋은 웹페이지 즐겨찾기