index.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Import the core alova instance
  2. import { BASE_URL } from "@/config";
  3. import alovaInstance from "./core/instance";
  4. // Export the global Apis object from the generated code
  5. import { createApis, withConfigType } from "./createApis";
  6. // Export the alova instance for direct use if needed
  7. export { alovaInstance };
  8. // Configure method options for specific APIs
  9. export const $$userConfigMap = withConfigType({});
  10. // Create the global Apis object
  11. const Apis = createApis(alovaInstance, $$userConfigMap);
  12. // Export both default and named export for AutoImport
  13. export default Apis;
  14. export { Apis };
  15. type Data<T> = {
  16. code: number;
  17. message: string;
  18. result: T;
  19. };
  20. export function uploadFile<T>(options: UniApp.UploadFileOption) {
  21. uni.showLoading({ title: "上传中...", mask: true });
  22. return new Promise<Data<T>>((resolve, reject) => {
  23. uni.uploadFile({
  24. url: BASE_URL + options.url,
  25. filePath: options.filePath,
  26. name: "file",
  27. method: "POST",
  28. header: {
  29. "Content-Type": "multipart/form-data",
  30. "X-Access-Token": useUserStore().token,
  31. },
  32. success(res) {
  33. uni.hideLoading();
  34. resolve(JSON.parse(res.data));
  35. },
  36. fail(err) {
  37. uni.hideLoading();
  38. reject(err);
  39. uni.showToast({ title: err.errMsg, icon: "none" });
  40. },
  41. });
  42. });
  43. }