request.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //页面中想用.then()就必须是Prnmise实例
  2. export default {
  3. config: {
  4. baseUrl: "http://localhost:8201", //本地开发
  5. // baseUrl: "https://talent.younggee.com:8201", //默认的公共域名
  6. // baseUrl: "https://139.9.53.143:8201", //默认的公共域名
  7. },
  8. request(options) {
  9. return new Promise((resolve, reject) => { //把调取的的接口给了一个Promise实例
  10. uni.request({ //uni-app调接口
  11. ...options, //所有的数据通过options传过来
  12. success: res => {
  13. if (options.native) {
  14. //假如用户得到res的原型数据,就在页面第三个参数把native:true就好了
  15. resolve(res)
  16. }
  17. if (res.statusCode === 200) {
  18. //等于200证明请求接口成功,就把里面的data数据给用户返回,
  19. //这里只返回data数据,用户想要原型数据,就在页面的第三个参数里写:native:true
  20. resolve(res)
  21. } else {
  22. //调取接口错误时返回给程序员
  23. reject(res)
  24. }
  25. }
  26. })
  27. })
  28. },
  29. //get方法
  30. get(url, data = {}, header = {}, options = {}) {
  31. options.url = this.config.baseUrl + url
  32. options.data = data
  33. options.method = "get"
  34. return this.request(options)
  35. },
  36. //post方法
  37. post(url, data = {}, options = {}) {
  38. options.url = this.config.baseUrl + url
  39. options.data = data
  40. options.method = "post"
  41. return this.request(options)
  42. }
  43. }