Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hjs557523 opened this issue Mar 12, 2020 · 0 comments

Comments

@hjs557523
Copy link

可封装到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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant