axios-ts

Posted 转角90

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了axios-ts相关的知识,希望对你有一定的参考价值。

request类的封装

// request/index.ts 
import axios from \'axios\'
import type  AxiosInstance  from \'axios\'
import type  XYRequestInterceptors, XYRequestConfig  from \'./type\'
class HYRequest 
  instance: AxiosInstance
  interceptors?: XYRequestInterceptors
  constructor(config: XYRequestConfig) 
    this.instance = axios.create(config)
    this.interceptors = config.interceptors
    //单独实例的拦截
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    )
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )
    //所有实例的拦截
    this.instance.interceptors.request.use(
      (config) => 
        console.log(\'所有实例请求成功的拦截\')
        return config
      ,
      (err) => 
        console.log(\'所有实例请求失败的拦截\')
        return err
      
    )
    this.instance.interceptors.response.use(
      (res) => 
        console.log(\'所有实例响应成功的拦截\')
        return res.data
      ,
      (err) => 
        console.log(\'所有实例响应失败的拦截\')
        return err
      
    )
  
  request<T>(config: XYRequestConfig<T>): void 
      
    //对单个请求的拦截
    if (config.interceptors?.requestInterceptor) 
      config = config.interceptors.requestInterceptor(config)
    
      
    return new Promise<T>((resolve,reject)=>
        this.instance.request<any,T>(config).then((res) =>   // res 是AxiosResponse类型,需要转为T类型
          if (config.interceptors?.responseInterceptor) 
            res = config.interceptors.responseInterceptor(res)
          
          resolve(res)
        )
    )
  

export default HYRequest
import axios from "axios";
import type  AxiosInstance  from "axios";
import type  HYRequestConfig  from "./type";

class HYRequest 
	instance: AxiosInstance;

	constructor(config: HYRequestConfig) 
		this.instance = axios.create(config);
		// 全局接口实例
		this.instance.interceptors.request.use(
			(config) => 
				console.log("全局请求成功拦截");
				config.headers["token"] = "xxxx";
				return config;
			,
			(error) => 
				console.log("全局请求失败拦截");
				return error;
			
		);

		this.instance.interceptors.request.use(
			(res) => 
				console.log("响应成功拦截");
				return res.data;
			,
			(error) => 
				console.log("响应失败拦截");
				return error;
			
		);

		// 单个接口实例
		this.instance.interceptors.request.use(
			config.interceptors?.requestInterceptor,
			config.interceptors?.requestInterceptorCatch
		);
		this.instance.interceptors.response.use(
			config.interceptors?.responseInterceptor,
			config.interceptors?.responseInterceptorCatch
		);
	

	request<T = any>(config: HYRequestConfig<T>) 
		// 单次请求成功拦截
		if (config.interceptors?.requestInterceptor) 
			config = config.interceptors.requestInterceptor(config as any);
		

		return new Promise((resolve, reject) => 
			this.instance
				.request<any, T>(config)
				.then((res) => 
					resolve(res);
				)
				.catch((err) => 
					reject(err);
				);
		);
	
	get<T = any>(config: HYRequestConfig<T>) 
		return this.request<T>(
			...config,
			method: "get"
		);
	

	post<T = any>(config: HYRequestConfig<T>) 
		return this.request<T>(
			...config,
			method: "post"
		);
	

	delete<T = any>(config: HYRequestConfig<T>) 
		return this.request<T>(
			...config,
			method: "delete"
		);
	

	put<T = any>(config: HYRequestConfig<T>) 
		return this.request<T>(
			...config,
			method: "put"
		);
	


export default HYRequest;

类型配置

import type 
	AxiosRequestConfig,
	AxiosResponse,
	InternalAxiosRequestConfig
 from "axios";
export interface HYRequestInterceptors<T = AxiosResponse> 
	requestInterceptor?: (
		config: InternalAxiosRequestConfig
	) => InternalAxiosRequestConfig;
	requestInterceptorCatch?: (error: any) => any;
	responseInterceptor?: (config: T) => T;
	responseInterceptorCatch?: (error: any) => any;

export interface HYRequestConfig<T = AxiosResponse> extends AxiosRequestConfig 
	interceptors?: HYRequestInterceptors<T>;


环境配置

// request/config.ts
let BASE_URL:string = \'\'
let TIME_OUT:number = 5000
if (process.env.NODE_ENV === \'development\') 
  BASE_URL = \'https://api.development\'
 else if (process.env.NODE_ENV === \'production\') 
  BASE_URL = \'https://api.production\'
 else if (process.env.NODE_ENV === \'releave\') 
  BASE_URL = \'https://api.releave\'

export  BASE_URL,TIME_OUT 

实例化

import HYRequest from \'./request\'
import  BASE_URL,TIME_OUT  from \'./request/config\'
const HYRequest = new HYRequest(
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: 
    requestInterceptor: (config) => 
      console.log(\'请求拦截成功\')
      return config
    ,
    responseInterceptorCatch: (err) => 
      console.log(\'请求拦截失败\')
      return err
    ,
    responseInterceptor: (res) => 
      console.log(\'响应拦截成功\')
      return res
    ,
    requestInterceptorCatch: (err) => 
      console.log(\'响应拦截失败\')
      return err
    
  
)
export default HYRequest

单独请求

import HYRequest from \'./service\'
interface CityType 
    data:any
    list:array
    ...

HYRequest.request<CityType>(
  url: \'/city/all\',
  interceptors: 
    requestInterceptor: (config) => 
      console.log(\'/city/all请求的成功拦截\')
      return config
    ,
    responseInterceptor: (res) => 
      console.log(\'/city/all响应的失败拦截\')
      return res
    
  
).then(res=>
    console.log(res)
)

Blender制作多个动画片段时踩的坑

最近在用Blender制作工业模型的简单动画,但涉及到多个物体不同的动画,导入到U3中莫名出现了很多不需要的动画,摸索了好几天总结出了如下一套制作简单动画的流程。

  1. 窗口调整到Animation

  2. 左下调整到动作编辑器

  3. 选中某一需要做动画的物体

  4. 点击新建

  5. 开始制作动画

  6. 点击盾牌

  7. 点击新的物体(此时不要管之前的动画,点击新物体时,自动会变成上面新建的样子
    ps:引用对象数量

    • 右边大纲视图中,该动画被布置到多少个物体中。
    • 新建了动画后,点击新的物体,再点击动画,会自动把动画布置到该物体后
    • 如果发现多个引用,可以在右边大纲视图里找找,有没有把片段用在多个物体上,否则删除不了
  8. 导出FBX,再导入到UNITY
    如果没有下推到NLA轨道,如果导出选项中,烘焙动画中,选择了单独全部动作,会自动把每个动画生成到每个物体
    ps:比如五个物体,分别创建了五个动画,没有下推到NLA轨道,会创建5*5=25个动画

    • 创建好动画并布置到对象上后,需要下推打NLA轨道
    • 导出选项中不要选全部动作,要选NLA片段

以上是关于axios-ts的主要内容,如果未能解决你的问题,请参考以下文章