index.js 1.4 KB

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