api.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // const BASE_URL = 'https://chtech.ncjti.edu.cn/carstop/carBuy'
  2. const BASE_URL = 'https://car.meiyishuoo.com/ride-sharing/carBuy'
  3. export function myRequest(options : any) {
  4. uni.showLoading({
  5. title: '加载中',
  6. mask: true,
  7. })
  8. // 当前时间
  9. const timestamp = new Date().getTime()
  10. // 将时间戳添加到URL中
  11. options.url += `${options.url.includes('?') ? '&' : '?'}timestamp=${timestamp}`
  12. return new Promise((resolve, reject) => {
  13. uni.request({
  14. url: BASE_URL + options.url,
  15. method: options.method || 'GET',
  16. data: options.data || {},
  17. header: options.header || {
  18. // token: uni.getStorageSync('auth').token || '',
  19. },
  20. success: (res : any) => {
  21. console.log(res);
  22. uni.hideLoading()
  23. if (res.data.code === 200) {
  24. resolve(res.data)
  25. }
  26. else {
  27. // 定义需要静默处理的code(如205代表“未查询到认证信息”,无需页面提示)
  28. const silentMessages = ["未查询到认证信息"];
  29. if (!silentMessages.includes(res.data.message) && res.data.code) {
  30. // 非静默code且有message时,通过弹框提示(如uni.showToast)
  31. uni.showToast({
  32. title: res.data.message || "接口报错",
  33. icon: 'none',
  34. })
  35. if (res.data.message=="未查到用户信息") {
  36. uni.removeStorageSync('carUserInfo')
  37. }
  38. }
  39. }
  40. },
  41. fail: (err) => {
  42. console.log(err)
  43. uni.hideLoading()
  44. uni.showToast({
  45. title: '请求数据失败',
  46. icon: 'none',
  47. })
  48. reject(err)
  49. },
  50. })
  51. })
  52. }
  53. // 新增 uni.uploadFile 封装
  54. export function myUploadFile(options: {
  55. url: string;
  56. filePath: string;
  57. name: string;
  58. formData?: Record<string, any>;
  59. header?: Record<string, string>;
  60. }) {
  61. uni.showLoading({ title: '上传中', mask: true });
  62. return new Promise((resolve, reject) => {
  63. uni.uploadFile({
  64. url: BASE_URL + options.url,
  65. filePath: options.filePath,
  66. name: options.name,
  67. formData: options.formData || {},
  68. header: options.header || {
  69. // 可添加全局请求头,如 token
  70. // 'Authorization': uni.getStorageSync('auth').token || ''
  71. },
  72. success: (res) => {
  73. uni.hideLoading();
  74. const data = JSON.parse(res.data);
  75. if (data.code === 200) {
  76. resolve(data);
  77. } else {
  78. uni.showToast({ title: data.message || '上传失败', icon: 'none' });
  79. reject(data);
  80. }
  81. },
  82. fail: (err) => {
  83. console.log(err)
  84. uni.hideLoading();
  85. uni.showToast({ title: '网络异常', icon: 'none' });
  86. reject(err);
  87. }
  88. });
  89. });
  90. }
  91. export const uploadFile = (filePath: string) => {
  92. return new Promise((resolve, reject) => {
  93. uni.uploadFile({
  94. url: BASE_URL +'/filesload.action', // 后端上传接口地址
  95. filePath,
  96. name: 'myFile',
  97. formData: { name: '认证图片' },
  98. success: (res) => {
  99. console.log(res,'api中')
  100. const data = JSON.parse(res.data);
  101. if (data.code === 200) {
  102. resolve(data.data.url); // 返回上传后的图片链接
  103. } else {
  104. reject(data.message || '上传失败');
  105. }
  106. },
  107. fail: (err) => {
  108. reject(err);
  109. }
  110. });
  111. });
  112. };