request.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * 网络请求封装
  3. */
  4. import { t } from './i18n.js'
  5. // 固定的 clientid
  6. const CLIENT_ID = '2f847927afb2b3ebeefc870c13d623f2'
  7. // 基础 URL(根据实际情况修改)
  8. const BASE_URL = 'http://127.0.0.1:8080'
  9. /**
  10. * 获取当前语言环境
  11. * @returns {String} 语言代码(转换为下划线格式,如 zh_CN)
  12. */
  13. const getLanguage = () => {
  14. try {
  15. // 从本地存储获取语言设置,默认为 zh-CN
  16. const locale = uni.getStorageSync('locale') || 'zh-CN'
  17. // 将连字符转换为下划线格式(zh-CN -> zh_CN)
  18. return locale.replace('-', '_')
  19. } catch (e) {
  20. return 'zh_CN'
  21. }
  22. }
  23. /**
  24. * 封装的请求方法
  25. * @param {Object} options - 请求配置
  26. * @param {String} options.url - 请求地址
  27. * @param {String} options.method - 请求方法,默认 GET
  28. * @param {Object} options.data - 请求参数
  29. * @param {Object} options.header - 额外的请求头
  30. * @returns {Promise}
  31. */
  32. const request = (options) => {
  33. return new Promise((resolve, reject) => {
  34. // 获取存储的 token
  35. const token = uni.getStorageSync('token') || ''
  36. // 获取当前语言环境
  37. const language = getLanguage()
  38. // 清理请求参数,移除 undefined、null 和空字符串
  39. const cleanData = {}
  40. if (options.data) {
  41. Object.keys(options.data).forEach(key => {
  42. const value = options.data[key]
  43. // 只添加有效值(非 undefined、非 null、非空字符串)
  44. if (value !== undefined && value !== null && value !== '') {
  45. cleanData[key] = value
  46. }
  47. })
  48. console.log('清理后的请求参数:', cleanData)
  49. }
  50. uni.request({
  51. url: BASE_URL + options.url,
  52. method: options.method || 'GET',
  53. data: cleanData,
  54. header: {
  55. 'Content-Type': 'application/json',
  56. 'Content-Language': language,
  57. 'clientid': CLIENT_ID,
  58. 'Authorization': 'Bearer ' + token,
  59. ...options.header
  60. },
  61. success: (res) => {
  62. const { code, msg, data, total, rows } = res.data
  63. // 根据状态码处理
  64. switch (code) {
  65. case 200:
  66. // 操作成功,返回数据
  67. if (total !== undefined && rows !== undefined) {
  68. // 分页查询返回体
  69. resolve({
  70. total,
  71. rows,
  72. code,
  73. msg
  74. })
  75. } else {
  76. // 一般返回体
  77. resolve({
  78. code,
  79. msg,
  80. data
  81. })
  82. }
  83. break
  84. case 401:
  85. // 登录失效
  86. uni.showToast({
  87. title: t('common.error.unauthorized'),
  88. icon: 'none',
  89. duration: 2000
  90. })
  91. // 清除 token
  92. uni.removeStorageSync('token')
  93. // 跳转到登录页
  94. setTimeout(() => {
  95. uni.reLaunch({
  96. url: '/pages/login/login'
  97. })
  98. }, 2000)
  99. reject({ code, msg: t('common.error.unauthorized') })
  100. break
  101. case 500:
  102. // 系统错误
  103. uni.showToast({
  104. title: t('common.error.serverError'),
  105. icon: 'none',
  106. duration: 2000
  107. })
  108. reject({ code, msg: t('common.error.serverError') })
  109. break
  110. case 501:
  111. // 提示返回的 msg
  112. uni.showToast({
  113. title: msg || t('common.error.requestFailed'),
  114. icon: 'none',
  115. duration: 2000
  116. })
  117. reject({ code, msg })
  118. break
  119. default:
  120. // 其他状态码
  121. uni.showToast({
  122. title: msg || t('common.error.requestFailed'),
  123. icon: 'none',
  124. duration: 2000
  125. })
  126. reject({ code, msg })
  127. break
  128. }
  129. },
  130. fail: (err) => {
  131. uni.showToast({
  132. title: t('common.error.networkFailed'),
  133. icon: 'none',
  134. duration: 2000
  135. })
  136. reject(err)
  137. }
  138. })
  139. })
  140. }
  141. export default request