request.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { showLoading, hideLoading } from './plugIn/globalLoading'
  2. import { J_STORAGE_KEY } from '../config/constant'
  3. const request = (url, data, method = 'GET') => new Promise((resolve, reject) => {
  4. const header = {
  5. 'Content-Type': 'application/json'
  6. }
  7. const res = uni.getStorageSync(J_STORAGE_KEY)
  8. const token = res.token
  9. if (token) {
  10. header.Authorization = token
  11. }
  12. showLoading()
  13. uni.request({
  14. url,
  15. data,
  16. method,
  17. header,
  18. success: (res) => {
  19. hideLoading()
  20. if (res.statusCode == 200) {
  21. if (res.data.code === '200' || res.data.code === '') {
  22. resolve(res.data)
  23. } else if (res.data.code === '20004' || res.data.code === '20005') {
  24. uni.removeStorageSync(J_STORAGE_KEY)
  25. uni.navigateTo({
  26. url: '/pages_category_page2/userModule/login'
  27. })
  28. } else {
  29. uni.showToast({
  30. title: res.data.message,
  31. icon: 'none'
  32. })
  33. }
  34. } else {
  35. reject(res)
  36. }
  37. },
  38. fail: (res) => {
  39. hideLoading()
  40. reject(res)
  41. }
  42. })
  43. })
  44. // 不带token接口请求,首页
  45. const request1 = (url, data, method = 'GET') => new Promise((resolve, reject) => {
  46. const header = {
  47. 'Content-Type': 'application/json',
  48. 'tenant': 'MDAwMA=='
  49. }
  50. showLoading()
  51. uni.request({
  52. url,
  53. data,
  54. method,
  55. header,
  56. success: (res) => {
  57. hideLoading()
  58. if (res.data.code === '200' || data.code === '') {
  59. resolve(res.data)
  60. } else {
  61. reject(res)
  62. }
  63. },
  64. fail: (res) => {
  65. hideLoading()
  66. reject(res)
  67. }
  68. })
  69. })
  70. module.exports = {
  71. request,
  72. request1
  73. }