git init
This commit is contained in:
27
tonecn/src/lib/copyText.ts
Normal file
27
tonecn/src/lib/copyText.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
*
|
||||
* @param textToCopy 需要被复制的文本
|
||||
* @returns 成功返回 true, 不成功返回 false
|
||||
*/
|
||||
const copyText = (textToCopy: string): boolean => {
|
||||
// 创建一个<textarea>元素,将文本放入其中
|
||||
const textArea = document.createElement("textarea");
|
||||
textArea.value = textToCopy;
|
||||
// 将<textarea>元素添加到DOM中
|
||||
document.body.appendChild(textArea);
|
||||
// 选择<textarea>元素中的文本
|
||||
textArea.select();
|
||||
let copyStatus = false; // true成功, false失败
|
||||
try {
|
||||
// 尝试将选定的文本复制到剪贴板
|
||||
document.execCommand("copy");
|
||||
copyStatus = true;
|
||||
} catch (err) {
|
||||
console.error("复制文本失败:", err);
|
||||
}
|
||||
// 从DOM中移除<textarea>元素
|
||||
document.body.removeChild(textArea);
|
||||
return copyStatus;
|
||||
}
|
||||
|
||||
export default copyText;
|
||||
23
tonecn/src/lib/request.ts
Normal file
23
tonecn/src/lib/request.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import axios from "axios";
|
||||
|
||||
axios.defaults.baseURL = "http://localhost:8080";
|
||||
|
||||
interface ResponseData<T> {
|
||||
code: number;
|
||||
msg: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
axios.interceptors.response.use((response) => {
|
||||
// 确保响应数据符合ResponseData接口的结构
|
||||
const responseData: ResponseData<any> = response.data;
|
||||
// 根据code值做不同的处理
|
||||
if (responseData.code === 200) { // 假设200为成功的code
|
||||
return responseData.data; // 当成功时,只返回data字段
|
||||
} else {
|
||||
// 当不成功时,抛出整个responseData或创建一个Error对象
|
||||
throw new Error(`请求错误: ${responseData.msg}`);
|
||||
}
|
||||
});
|
||||
|
||||
export { axios as request };
|
||||
Reference in New Issue
Block a user