wx-github icon indicating copy to clipboard operation
wx-github copied to clipboard

自定义了一个api.js,内部通过定义fetch函数封装了wx.request,以替代微信官方取消的fetch函数,并且对一些常用api进行封装

Open hjs557523 opened this issue 6 years ago • 0 comments

可封装到api.js中,然后在外部使用时导入模块

const githubHost = 'https://api.github.com/';//github接口域名
var githubApi = {

  userUrl: function(githubName) {
    //console.log('调用了这个模块');
    return githubHost + 'users/' + githubName; 
  },

  repoUrl: function(githubName) {
    return this.userUrl(githubName) + '/repos?per_page=100';
  },

  prUrl: function (githubName) {
    return githubHost + 'search/issues?q=type:pr+is:merged+author:' + githubName + '&per_page=100';
  }
}

//对wx.request进行封装为promise对象
function fetch(url) {
  console.log("fetch start");
  return new Promise(function(resolve, reject) {
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        console.log("success");
        res.ok = true;
        resolve(res);
      },

      fail: () => {
        console.log("fail");
        reject();
      }
    });
  })
}

module.exports = {
  githubApi: githubApi,
  fetch: fetch
}

hjs557523 avatar Mar 12 '20 06:03 hjs557523