index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const baseRequest = async (url, method, data = {}, loading = true, header = {}) => {
  2. url = url.indexOf('https://') > -1 ? url : 'https://xnh.xazhima.com/prod-api/wap/business/weixin' + url
  3. return new Promise((reslove, reject) => {
  4. loading && uni.showLoading({
  5. title: '加载中'
  6. })
  7. uni.request({
  8. url,
  9. method: method || 'GET',
  10. 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. icon: 'none'
  22. })
  23. }
  24. } else {
  25. uni.showToast({
  26. title: '网络连接失败,请稍后重试',
  27. icon: 'none'
  28. })
  29. reject(res)
  30. }
  31. },
  32. fail: (msg) => {
  33. uni.showToast({
  34. title: '网络连接失败,请稍后重试',
  35. icon: 'none'
  36. })
  37. reject(msg)
  38. },
  39. complete: () => {
  40. uni.hideLoading()
  41. },
  42. })
  43. })
  44. }
  45. const request = {
  46. get: (api, data, loading) => baseRequest(api, 'GET', data, loading),
  47. getPath: (api, data, keys, loading) => {
  48. let newRequestPath = api;
  49. Object.keys(keys).forEach(function(key) {
  50. newRequestPath = newRequestPath.replace(
  51. "{" + key + "}",
  52. keys[key]
  53. );
  54. });
  55. return baseRequest(newRequestPath, 'GET', data, loading)
  56. },
  57. post: (api, data, loading) => baseRequest(api, 'POST', data, loading),
  58. postForm: (api, data, loading) => baseRequest(api, 'POST', data, loading, {
  59. 'content-type': 'application/x-www-form-urlencoded'
  60. }),
  61. }
  62. export default request