api.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 silentCodes = [205];
  29. if (!silentCodes.includes(res.data.code) && res.data.message) {
  30. // 非静默code且有message时,通过弹框提示(如uni.showToast)
  31. uni.showToast({
  32. title: res.data.message || "接口报错",
  33. icon: 'none',
  34. })
  35. }
  36. }
  37. },
  38. fail: (err) => {
  39. uni.hideLoading()
  40. uni.showToast({
  41. title: '请求数据失败',
  42. icon: 'none',
  43. })
  44. reject(err)
  45. },
  46. })
  47. })
  48. }
  49. // 新增 uni.uploadFile 封装
  50. export function myUploadFile(options: {
  51. url: string;
  52. filePath: string;
  53. name: string;
  54. formData?: Record<string, any>;
  55. header?: Record<string, string>;
  56. }) {
  57. uni.showLoading({ title: '上传中', mask: true });
  58. return new Promise((resolve, reject) => {
  59. uni.uploadFile({
  60. url: BASE_URL + options.url,
  61. filePath: options.filePath,
  62. name: options.name,
  63. formData: options.formData || {},
  64. header: options.header || {
  65. // 可添加全局请求头,如 token
  66. // 'Authorization': uni.getStorageSync('auth').token || ''
  67. },
  68. success: (res) => {
  69. uni.hideLoading();
  70. const data = JSON.parse(res.data);
  71. if (data.code === 200) {
  72. resolve(data);
  73. } else {
  74. uni.showToast({ title: data.message || '上传失败', icon: 'none' });
  75. reject(data);
  76. }
  77. },
  78. fail: (err) => {
  79. console.log(err)
  80. uni.hideLoading();
  81. uni.showToast({ title: '网络异常', icon: 'none' });
  82. reject(err);
  83. }
  84. });
  85. });
  86. }
  87. export const uploadFile = (filePath: string) => {
  88. return new Promise((resolve, reject) => {
  89. uni.uploadFile({
  90. url: BASE_URL +'/filesload.action', // 后端上传接口地址
  91. filePath,
  92. name: 'myFile',
  93. formData: { name: '认证图片' },
  94. success: (res) => {
  95. console.log(res,'api中')
  96. const data = JSON.parse(res.data);
  97. if (data.code === 200) {
  98. resolve(data.data.url); // 返回上传后的图片链接
  99. } else {
  100. reject(data.message || '上传失败');
  101. }
  102. },
  103. fail: (err) => {
  104. reject(err);
  105. }
  106. });
  107. });
  108. };