index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const baseRequest = async (url, method, data = {}, loading = true) => {
  2. let header = {}
  3. return new Promise((reslove, reject) => {
  4. loading && uni.showLoading({
  5. title: '加载中'
  6. })
  7. uni.request({
  8. url: 'https://xnh.xazhima.com/prod-api/wap/business/weixin' + url,
  9. method: method || 'GET',
  10. header: header,
  11. timeout: 10000,
  12. data: data || {},
  13. success: (successData) => {
  14. const res = successData.data
  15. if (successData.statusCode == 200) {
  16. if (res.code === 200) {
  17. reslove(res)
  18. } else {
  19. uni.showToast({
  20. title: '未知错误'
  21. })
  22. }
  23. } else {
  24. uni.showToast({
  25. title: '网络连接失败,请稍后重试'
  26. })
  27. reject(res)
  28. }
  29. },
  30. fail: (msg) => {
  31. uni.showToast({
  32. title: '网络连接失败,请稍后重试'
  33. })
  34. reject(msg)
  35. },
  36. complete: () => {
  37. uni.hideLoading()
  38. },
  39. })
  40. })
  41. }
  42. const request = {
  43. get: (api, data, loading) => baseRequest(api, 'GET', data, loading),
  44. getPath: (api, data, keys, loading) => {
  45. let newRequestPath = api;
  46. Object.keys(keys).forEach(function (key) {
  47. newRequestPath = newRequestPath.replace(
  48. "{" + key + "}",
  49. keys[key]
  50. );
  51. });
  52. return baseRequest(newRequestPath, 'GET', data, loading)
  53. },
  54. post: (api, data, loading) => baseRequest(api, 'POST', data, loading),
  55. }
  56. export default request