Skip to content
ts

// 示例:通过本地存储获取聊天记录
export function getChatHistory() {
    const chatHistory = wx.getStorageSync('chatHistory'); // 获取存储在本地的聊天记录
    if (chatHistory) {
        console.log('Chat History:', chatHistory);
    } else {
        console.log('No chat history found.');
    }
}

// 保存聊天记录到本地存储
export function saveChatHistory(chatData: string[]) {
    wx.setStorageSync('chatHistory', chatData);
}


// chooseImage.ts

export function chooseImage() {
    wx.chooseImage({
        count: 3, // 最多选择 3 张图片
        sizeType: ['original', 'compressed'], // 选择原图或压缩图
        sourceType: ['album', 'camera'], // 可以选择从相册或使用摄像头拍照
        success: function (res) {
            const tempFilePaths = res.tempFilePaths; // 选择的图片路径
            const tempFiles = res.tempFiles; // 选择的图片信息,包括路径、大小等

            console.log('Selected image paths:', tempFilePaths);
            console.log('Selected image files:', tempFiles);

            // 可以在这里进行图片处理,比如上传到服务器
        },
        fail: function (err) {
            console.error('Failed to choose image:', err);
        },
        complete: function () {
            console.log('Image selection completed');
        }
    });
}

// chooseMessageFile.ts

interface FileInfo {
    path: string;
    size: number;
    name: string;
    type: string;
}

export function chooseMessageFile() {
    wx.chooseMessageFile({
        count: 3, // 最多选择 3 个文件
        type: 'all', // 可以选择所有类型的文件
        extension: ['jpg', 'png', 'pdf', '.xlsx', '.xls', '.XLSX', '.XLS', 'xlsx', 'xls', 'XLSX', 'XLS', '.doc', '.word', '.docx'], // 限制选择的文件类型(图片或 PDF)
        success: function (res: { tempFiles: FileInfo[] }) {
            // 成功回调
            const files = res.tempFiles;
            if (files.length > 0) {
                files.forEach((file: FileInfo) => {
                    console.log('Selected file:', file);
                    // 进一步处理文件,如上传到服务器
                    // 这里可以根据 file.path 上传文件
                });
            }
        },
        fail: function (err: any) {
            // 失败回调
            console.error('Failed to choose message file:', err);
        },
        complete: function () {
            // 完成回调,无论成功或失败都会调用
            console.log('File selection process completed');
        }
    });
}

// chooseVideo.ts

export function chooseVideo() {
    wx.chooseVideo({
        sourceType: ['album', 'camera'], // 可以选择从相册或录制视频
        maxDuration: 60, // 设置视频最大时长为 60 秒
        camera: 'back', // 使用后置摄像头录制
        success: function (res) {
            const tempFilePath = res.tempFilePath; // 视频文件的临时路径
            const duration = res.duration; // 视频时长(秒)
            const size = res.size; // 视频文件大小(字节)
            const width = res.width; // 视频宽度(像素)
            const height = res.height; // 视频高度(像素)

            console.log('Selected video path:', tempFilePath);
            console.log('Video duration:', duration);
            console.log('Video size:', size);
            console.log('Video resolution:', width, 'x', height);

            // 可以在这里进一步处理视频文件,例如上传到服务器
        },
        fail: function (err) {
            console.error('Failed to choose video:', err);
        },
        complete: function () {
            console.log('Video selection process completed');
        }
    });
}

export function readFile() {
    const fs = wx.getFileSystemManager();

    // 读取文件内容
    fs.readFile({
        filePath: `${wx.env.USER_DATA_PATH}/example.txt`,  // 文件路径
        encoding: 'utf8',  // 编码格式,支持 'utf8', 'base64', 'ascii' 等
        success(res) {
            console.log('File content:', res.data);  // 输出文件内容
        },
        fail(err) {
            console.error('Failed to read file:', err);  // 错误处理
        }
    });
}

export function writeFile() {
    const fs = wx.getFileSystemManager();

    // 写入文件内容
    fs.writeFile({
        filePath: `${wx.env.USER_DATA_PATH}/example.txt`,  // 文件路径
        data: 'Hello, wx.getFileSystemManager!',  // 写入的内容
        encoding: 'utf8',  // 编码格式
        success() {
            console.log('File written successfully!');
        },
        fail(err) {
            console.error('Failed to write file:', err);  // 错误处理
        }
    });
}

export function getFileStats() {
    const fs = wx.getFileSystemManager();

    // 获取文件信息
    fs.stat({
        path: `${wx.env.USER_DATA_PATH}/example.txt`,  // 文件路径
        success(res) {
            console.log('File stats:', res);  // 输出文件信息(例如文件大小、创建时间等)
        },
        fail(err) {
            console.error('Failed to get file stats:', err);  // 错误处理
        }
    });
}

export function createDirectory() {
    const fs = wx.getFileSystemManager();

    // 创建目录
    fs.mkdir({
        dirPath: `${wx.env.USER_DATA_PATH}/new_folder`,  // 目录路径
        recursive: true,  // 如果父目录不存在,则递归创建
        success() {
            console.log('Directory created successfully!');
        },
        fail(err) {
            console.error('Failed to create directory:', err);  // 错误处理
        }
    });
}

export function deleteFile() {
    const fs = wx.getFileSystemManager();

    // 删除文件
    fs.unlink({
        filePath: `${wx.env.USER_DATA_PATH}/example.txt`,  // 文件路径
        success() {
            console.log('File deleted successfully!');
        },
        fail(err) {
            console.error('Failed to delete file:', err);  // 错误处理
        }
    });
}


export function readDirectory() {
    const fs = wx.getFileSystemManager();

    // 读取目录内容
    fs.readdir({
        dirPath: `${wx.env.USER_DATA_PATH}`,  // 目录路径
        success(res) {
            console.log('Directory content:', res.files);  // 输出目录中的文件列表
        },
        fail(err) {
            console.error('Failed to read directory:', err);  // 错误处理
        }
    });
}


export function moveFile() {
    const fs = wx.getFileSystemManager();

    // 移动文件
    fs.rename({
        oldPath: `${wx.env.USER_DATA_PATH}/example.txt`,  // 原文件路径
        newPath: `${wx.env.USER_DATA_PATH}/new_folder/example.txt`,  // 新文件路径
        success() {
            console.log('File moved successfully!');
        },
        fail(err) {
            console.error('Failed to move file:', err);  // 错误处理
        }
    });
}


export function openDocument() {
    const filePath = `${wx.env.USER_DATA_PATH}/example.pdf`;  // 假设文件已存在本地存储路径

    wx.openDocument({
        filePath: filePath,  // 文件的本地路径
        success(res) {
            console.log('File opened successfully:', res);
        },
        fail(err) {
            console.error('Failed to open file:', err);  // 错误处理
        },
        complete() {
            console.log('openDocument process completed');
        }
    });
}

✨ 网站运行时间: 3年11月15天 ❤️ 道阻且长,行则将至 - 微信号: heikedreamer