微信小程序生成小程序码缓存删除

.TS

复制代码
const fsm = uni.getFileSystemManager()
/**
* 将 base64 转换为本地图片路径
* @param base64data base64 数据
*/
export const writeFile = (base64data: string) => new Promise((resolve) => {
  const FILE_BASE_NAME = Date.parse(new Date().toString())
  const result = /data:image\/(\w+);base64,(.*)/.exec(base64data)
  if (!result || result.length < 3) {
    resolve('')
    return
  }
  const [, format, bodyData] = result
  const filePath = `${wx.env.USER_DATA_PATH}/poster/${FILE_BASE_NAME}.${format}`
  const buffer = uni.base64ToArrayBuffer(bodyData)
  fsm.writeFile({
    filePath,
    data: buffer,
    encoding: 'binary',
    success() {
      resolve(filePath)
    },
    fail() {
      resolve('')
    },
  })
})
export const readFile = (base64data: string) => new Promise((resolve) => {
  fsm.access({
    path: `${wx.env.USER_DATA_PATH}/poster`,
    success() {
      writeFile(base64data).then((filePath) => {
        resolve(filePath)
      })
    },
    fail() {
      fsm.mkdir({
        dirPath: `${wx.env.USER_DATA_PATH}/poster`,
        success() {
          writeFile(base64data).then((filePath) => {
            resolve(filePath)
          })
        },
      })
    },
  })
})
/**
 * 删除目录
 */
export const delFile = () => new Promise((resolve) => {
  fsm.rmdir({
    dirPath: `${wx.env.USER_DATA_PATH}/poster`,
    recursive: true,
    success() {
      resolve(true)
    },
  })
})

.JS

复制代码
//引入TS方法
import { readFile, delFile } from '@/static/utils/poster'

//这是删除防止内存超出
delFile().then(() => {
	console.log('删除成功')
})

// 获取小程序码接口
export const getQrCode = async (path:string) => {
  const res = await http.get('接口地址', {
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    responseType: 'arraybuffer',
    params: { path },
  })
  return res.data
}

//获取小程序码
const rect = await getQrCode(path)
//转成本地图片路径
readFile(`data:image/jpeg;base64,${uni.arrayBufferToBase64(rect)}`).then((res) => {
	console.log('路径',res)
})

遇到问题可以看我主页加我Q,很少看博客,对你有帮助别忘记点赞收藏。

相关推荐
炫彩@之星15 小时前
微信小程序渗透测试指北(附案例)
微信小程序·小程序
像素之间2 天前
微信小程序中安装vant
微信小程序
java1234_小锋2 天前
[免费]微信小程序音乐播放器(爬取网易云音乐数据)(node.js后端)【论文+源码】
微信小程序·小程序·node.js·音乐播放器·网易云音乐
野盒子2 天前
前端面试题 微信小程序兼容性问题与组件适配策略
前端·javascript·面试·微信小程序·小程序·cocoa
Maitians2 天前
微信小程序 - 保存手机号等信息到通讯录
微信小程序·小程序
Maitians2 天前
微信小程序 - 手机震动
微信小程序·小程序
像素之间2 天前
在微信小程序中使用骨架屏
微信小程序·小程序
幽络源小助理3 天前
美食推荐系统微信小程序
微信小程序·小程序·美食
浩宇软件开发3 天前
基于微信小程序的天气预报app
微信小程序·小程序·天气app