index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { hideLoading, showLoading } from './loading';
  2. import { MyRequestOptions, HttpResponse } from './type/index';
  3. import * as Pinia from 'pinia';
  4. import { useUserStore } from '@/store';
  5. const mapBaseUrl = {
  6. dev: "192.168.2.6",
  7. prod: "221.182.8.141"
  8. };
  9. const port = "21088"
  10. export const BASE_URL = `http://${mapBaseUrl.prod}:${port}`;
  11. /**
  12. * 封装uni.request
  13. * @param {String} option.method 请求方式
  14. * @param {String} option.url 请求url
  15. * @param {Object} option.data 请求参数
  16. * @param {Object} option.loading loading配置信息
  17. * @param {Boolean} option.loading.show 显示loading
  18. * 余下配置参考 RequestOptions 类型定义
  19. * 如果要扩展 可在 MyRequestOptions 下进行类型扩展
  20. *
  21. * ====
  22. * MyRequestOptions HttpResponse 放在了./type/index.ts 建议放在 global.d.ts 全局 ts 类型定义中
  23. * ====
  24. * @returns
  25. */
  26. const request = <T = any>(
  27. option : MyRequestOptions
  28. ) : Promise<HttpResponse<T>> => {
  29. const userStore = useUserStore(Pinia.createPinia())
  30. const token = userStore.token || uni.getStorageSync('token')
  31. if (token !== '') {
  32. //Token不为空,请求参数默认增加Token参数
  33. option['header']['X-Authorization'] = `Bearer ${token}`
  34. } else if (option.url !== '/base/api/auth/login') {
  35. uni.showToast({
  36. title: '登录信息失效,请重新登录',
  37. duration: 1500,
  38. icon: 'none',
  39. success: function () {
  40. setTimeout(function () {
  41. uni.reLaunch({
  42. url: '/pages/login/index'
  43. })
  44. }, 1500)
  45. }
  46. })
  47. }
  48. return new Promise((resolve, reject) => {
  49. // 处理请求loading
  50. if (option.loading?.show) {
  51. showLoading(option.loading.option);
  52. }
  53. uni.request({
  54. method: option.method || 'GET',
  55. url: `${BASE_URL}${option.url}`, // 完整URL
  56. data: option.data || {},
  57. header: option.header || {},
  58. success: async (res) => {
  59. // 处理响应数据
  60. if (option.loading?.show) {
  61. hideLoading();
  62. }
  63. const data = res.data as HttpResponse<T>;
  64. if (option.url === '/base/api/auth/login') {
  65. if (data?.token) {
  66. resolve(data);
  67. } else {
  68. uni.$msg('登录失败')
  69. reject()
  70. }
  71. }
  72. else if(option.url==='/base/api/yt/user/me/info'){
  73. if (data?.username) {
  74. resolve(data);
  75. } else {
  76. uni.$msg('用户信息请求失败')
  77. reject()
  78. }
  79. }
  80. else {
  81. if (data.code === 200) {
  82. resolve(data);
  83. } else {
  84. uni.$msg(data.errMsg);
  85. reject()
  86. }
  87. if (data.code === 401) {
  88. uni.removeStorageSync('token')
  89. uni.removeStorageSync('userInfo')
  90. // 删除缓存
  91. uni.reLaunch({
  92. url: '/pages/login/index'
  93. });
  94. }
  95. }
  96. },
  97. fail: (err) => {
  98. if (option.loading?.show) {
  99. hideLoading();
  100. }
  101. uni.$msg(err.errMsg);
  102. reject(err);
  103. }
  104. });
  105. });
  106. };
  107. // 这里仅封装了部分常用的请求方法,可以根据需求继续封装其他方法
  108. request.post = <T = any>(option : MyRequestOptions) => {
  109. return request({
  110. method: 'POST',
  111. ...option
  112. }) as unknown as Promise<HttpResponse<T>>;
  113. };
  114. request.get = <T = any>(option : MyRequestOptions) => {
  115. return request({
  116. method: 'GET',
  117. ...option
  118. }) as unknown as Promise<HttpResponse<T>>;
  119. };
  120. request.put = <T = any>(option : MyRequestOptions) => {
  121. return request({
  122. method: 'PUT',
  123. ...option
  124. }) as unknown as Promise<HttpResponse<T>>;
  125. };
  126. request.delete = <T = any>(option : MyRequestOptions) => {
  127. return request({
  128. method: 'DELETE',
  129. ...option
  130. }) as unknown as Promise<HttpResponse<T>>;
  131. };
  132. export default request;