request.js 3.5 KB

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