http.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* eslint-disable no-console */
  2. // 全局请求封装
  3. const http = {
  4. request: function (params) {
  5. // 请求参数处理
  6. if (Object.prototype.toString.call(params.data) === '[object Array]') {
  7. params.data = JSON.stringify(params.data)
  8. } else if (Object.prototype.toString.call(params.data) === '[object Number]') {
  9. params.data = params.data + ''
  10. }
  11. // 发起请求
  12. return new Promise((resolve, reject) => {
  13. uni.request({
  14. dataType: 'json',
  15. responseType: params.responseType === undefined ? 'text' : params.responseType,
  16. header: {
  17. Authorization: uni.getStorageSync('mall4jStationToken'),
  18. locale: uni.getStorageSync('bbcLang') || 'zh_CN',
  19. timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
  20. },
  21. url: (params.domain ? params.domain : import.meta.env.VITE_APP_BASE_API) + params.url,
  22. data: params.data,
  23. method: params.method === undefined ? 'POST' : params.method,
  24. success: (res) => {
  25. const responseData = res.data
  26. if (responseData.code === 'A00001') {
  27. if (!params.hasCatch) {
  28. uni.showToast({
  29. title: responseData.msg || responseData.data || 'Error',
  30. icon: 'none'
  31. })
  32. }
  33. }
  34. // 00000 请求成功
  35. // A00002 用于直接显示提示系统的成功,内容由输入决定
  36. if (responseData.code === '00000' || responseData.code === 'A00002') {
  37. resolve(responseData)
  38. }
  39. // A00004 未授权
  40. if (responseData.code === 'A00004') {
  41. uni.removeStorageSync('mall4jStationLoginResult')
  42. uni.removeStorageSync('mall4jStationToken')
  43. uni.hideLoading()
  44. if (!params.dontTrunLogin) {
  45. if (uni.getStorageSync('mall4jHadLogin')) {
  46. uni.showModal({
  47. title: '提示',
  48. content: '登录已过期,请重新登录!',
  49. success: res => {
  50. if (res.confirm) {
  51. // 跳转登录页面
  52. uni.redirectTo({
  53. url: '/pages/account-login/account-login'
  54. })
  55. } else {
  56. uni.navigateTo({
  57. url: '/pages/index/index'
  58. })
  59. }
  60. }
  61. })
  62. } else {
  63. // 跳转登录页面
  64. uni.navigateTo({
  65. url: '/pages/account-login/account-login'
  66. })
  67. }
  68. }
  69. }
  70. // A00005 服务器出了点小差
  71. if (responseData.code === 'A00005') {
  72. this.onRequestFail(params, responseData)
  73. uni.showToast({
  74. title: '服务器出了点小差~',
  75. icon: 'none'
  76. })
  77. }
  78. if (responseData.code !== '00000') {
  79. reject(responseData)
  80. }
  81. },
  82. fail: (err) => {
  83. uni.showToast({
  84. title: '请求失败'
  85. })
  86. reject(err)
  87. }
  88. })
  89. })
  90. },
  91. onRequestFail: (params, responseData) => {
  92. console.error('============== 请求异常 ==============')
  93. console.log('接口地址: ', params.url)
  94. console.log('异常信息: ', responseData)
  95. console.error('============== 请求异常 end ==========')
  96. },
  97. /**
  98. * 上传文件统一接口
  99. */
  100. upload: (params) => {
  101. uni.uploadFile({
  102. url: import.meta.env.VITE_APP_BASE_API + params.url,
  103. filePath: params.filePath,
  104. name: params.name,
  105. header: {
  106. Authorization: params.login ? undefined : uni.getStorageSync('mall4jStationToken')
  107. },
  108. dataType: 'json',
  109. responseType: params.responseType === undefined ? 'json' : params.responseType,
  110. success: (res) => {
  111. const responseData = JSON.parse(res.data)
  112. if (responseData.code === '00000') {
  113. if (params.callBack) {
  114. params.callBack(responseData.data)
  115. }
  116. } else {
  117. uni.showToast({
  118. title: '服务器出了点小差~',
  119. icon: 'none'
  120. })
  121. }
  122. },
  123. fail: function () {
  124. uni.hideLoading()
  125. }
  126. })
  127. }
  128. }
  129. export default http