|
|
- const fsm = wx.getFileSystemManager();
- const FILE_BASE_NAME = 'tmp_base64src';
-
- const base64src = function(base64data) {
- return new Promise((resolve, reject) => {
- // 添加时间戳生成唯一文件名
- const timestamp = Date.now();
- const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}_${timestamp}.png`;
- const buffer = wx.base64ToArrayBuffer(base64data);
-
- fsm.writeFile({
- filePath,
- data: buffer,
- encoding: 'binary',
- success() {
- resolve(filePath);
- },
- fail() {
- reject(new Error('ERROR_BASE64SRC_WRITE'));
- },
- });
- });
- };
-
- const unlink = function() {
- return new Promise((resolve, reject) => {
- // 获取临时文件列表
- fsm.readdir({
- dirPath: wx.env.USER_DATA_PATH,
- success: (res) => {
- // 删除所有tmp_base64src开头的临时文件
- const promises = res.files
- .filter(file => file.startsWith(FILE_BASE_NAME))
- .map(file => {
- return new Promise((res, rej) => {
- fsm.unlink({
- filePath: `${wx.env.USER_DATA_PATH}/${file}`,
- success: res,
- fail: rej
- });
- });
- });
-
- Promise.all(promises)
- .then(resolve)
- .catch(reject);
- },
- fail: reject
- });
- });
- };
-
- export default {
- base64src,
- unlink,
- };
|