index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. getPath: (api, data, keys, loading) => {
  44. let newRequestPath = api;
  45. Object.keys(keys).forEach(function (key) {
  46. newRequestPath = newRequestPath.replace(
  47. "{" + key + "}",
  48. keys[key]
  49. );
  50. });
  51. return baseRequest(newRequestPath, 'GET', data, loading)
  52. }
  53. }
  54. // ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  55. // request[method] = (api, data, loading) => baseRequest(api, method, data, loading)
  56. // })
  57. export default request