index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. })
  23. }
  24. } else {
  25. uni.showToast({
  26. title: '网络连接失败,请稍后重试'
  27. })
  28. reject(res)
  29. }
  30. },
  31. fail: (msg) => {
  32. uni.showToast({
  33. title: '网络连接失败,请稍后重试'
  34. })
  35. reject(msg)
  36. },
  37. complete: () => {
  38. uni.hideLoading()
  39. },
  40. })
  41. })
  42. }
  43. const request = {
  44. get: (api, data, loading) => baseRequest(api, 'GET', data, loading),
  45. getPath: (api, data, keys, loading) => {
  46. let newRequestPath = api;
  47. Object.keys(keys).forEach(function (key) {
  48. newRequestPath = newRequestPath.replace(
  49. "{" + key + "}",
  50. keys[key]
  51. );
  52. });
  53. return baseRequest(newRequestPath, 'GET', data, loading)
  54. },
  55. post: (api, data, loading) => baseRequest(api, 'POST', data, loading),
  56. }
  57. export default request