index.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. function fetch (method, url, data, loading){
  2. return new Promise((resolve, reject) => {
  3. //显示加载动画
  4. if (loading) uni.showLoading({title: '数据加载中...',mask: true});
  5. // let URL = 'http://localhost:9529' + url
  6. // let URL = 'https://xcx.sgjyun.com' + url
  7. let URL = 'https://xcx.kuyicloud.com' + url
  8. //获取token
  9. // let token = wx.getStorageSync("token")
  10. // let obj = { token, }
  11. //添加token
  12. // Object.assign(data,obj)
  13. let header = {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"}
  14. //发起请求
  15. uni.request({
  16. url: URL,
  17. method: method,
  18. header: header,
  19. data: data || {},
  20. success: res => {
  21. let code = res.data.code
  22. let message = res.data.message
  23. resolve(res);
  24. },
  25. fail: err => {
  26. uni.hideLoading();
  27. // console.log(err)
  28. reject(err)
  29. },
  30. complete: () => {
  31. uni.hideLoading();
  32. }
  33. });
  34. })
  35. }
  36. export default {
  37. get: function (url, params, loading){
  38. return fetch('GET', url, params, loading)
  39. },
  40. post: function (url, params, loading){
  41. return fetch('POST', url, params, loading)
  42. },
  43. put: function (url, params, loading){
  44. return fetch('PUT', url, params, loading)
  45. },
  46. delete: function (url, params, loading){
  47. return fetch('DELETE', url, params, loading)
  48. }
  49. }