request.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. uni.request({
  39. url: BASE_URL + options.url,
  40. method: options.method || 'GET',
  41. data: options.data || {},
  42. header: {
  43. 'Content-Type': 'application/json',
  44. 'Content-Language': language,
  45. 'clientid': CLIENT_ID,
  46. 'Authorization': 'Bearer ' + token,
  47. ...options.header
  48. },
  49. success: (res) => {
  50. const { code, msg, data, total, rows } = res.data
  51. // 根据状态码处理
  52. switch (code) {
  53. case 200:
  54. // 操作成功,返回数据
  55. if (total !== undefined && rows !== undefined) {
  56. // 分页查询返回体
  57. resolve({
  58. total,
  59. rows,
  60. code,
  61. msg
  62. })
  63. } else {
  64. // 一般返回体
  65. resolve({
  66. code,
  67. msg,
  68. data
  69. })
  70. }
  71. break
  72. case 401:
  73. // 登录失效
  74. uni.showToast({
  75. title: t('common.error.unauthorized'),
  76. icon: 'none',
  77. duration: 2000
  78. })
  79. // 清除 token
  80. uni.removeStorageSync('token')
  81. // 跳转到登录页
  82. setTimeout(() => {
  83. uni.reLaunch({
  84. url: '/pages/login/login'
  85. })
  86. }, 2000)
  87. reject({ code, msg: t('common.error.unauthorized') })
  88. break
  89. case 500:
  90. // 系统错误
  91. uni.showToast({
  92. title: t('common.error.serverError'),
  93. icon: 'none',
  94. duration: 2000
  95. })
  96. reject({ code, msg: t('common.error.serverError') })
  97. break
  98. case 501:
  99. // 提示返回的 msg
  100. uni.showToast({
  101. title: msg || t('common.error.requestFailed'),
  102. icon: 'none',
  103. duration: 2000
  104. })
  105. reject({ code, msg })
  106. break
  107. default:
  108. // 其他状态码
  109. uni.showToast({
  110. title: msg || t('common.error.requestFailed'),
  111. icon: 'none',
  112. duration: 2000
  113. })
  114. reject({ code, msg })
  115. break
  116. }
  117. },
  118. fail: (err) => {
  119. uni.showToast({
  120. title: t('common.error.networkFailed'),
  121. icon: 'none',
  122. duration: 2000
  123. })
  124. reject(err)
  125. }
  126. })
  127. })
  128. }
  129. export default request