http.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // #ifdef H5
  2. let path = `${location.origin}/service`
  3. // #endif
  4. // get方法
  5. export function get(url, data = {}) {
  6. url = path + url;
  7. return new Promise((resolve, reject) => {
  8. uni.request({
  9. url,
  10. data,
  11. header: {
  12. 'Cache-Control': 'no-cache'
  13. },
  14. success(res) {
  15. if(res.statusCode === 200){
  16. resolve(res.data);
  17. }else{
  18. reject();
  19. uni.showToast({
  20. icon: 'none',
  21. title: '请求数据失败!'
  22. });
  23. }
  24. },
  25. fail(err) {
  26. reject();
  27. uni.showToast({
  28. icon: 'none',
  29. title: '请求数据失败!'
  30. });
  31. }
  32. })
  33. });
  34. }
  35. // post方法
  36. export function post(url, data = {}) {
  37. url = path + url;
  38. return new Promise((resolve, reject) => {
  39. uni.request({
  40. method: 'POST',
  41. url,
  42. data,
  43. header: {
  44. 'Cache-Control': 'no-cache'
  45. },
  46. success(res) {
  47. if(res.statusCode === 200){
  48. resolve(res.data);
  49. }else{
  50. reject();
  51. uni.showToast({
  52. icon: 'none',
  53. title: '请求数据失败!'
  54. });
  55. }
  56. },
  57. fail(err) {
  58. reject();
  59. uni.showToast({
  60. icon: 'none',
  61. title: '请求数据失败!'
  62. });
  63. }
  64. })
  65. });
  66. }