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

.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,很少看博客,对你有帮助别忘记点赞收藏。

相关推荐
源码_V_saaskw1 天前
JAVA国际版同城跑腿源码快递代取帮买帮送同城服务源码支持Android+IOS+H5
android·java·ios·微信小程序
tbit1 天前
fluwx 拉起小程序WXLog:Error:fail to load Keychain status:-25300, keyData null:1
flutter·ios·微信小程序
book多得1 天前
刷题专用微信小程序推荐
微信小程序·小程序
技术与健康1 天前
微信小程序云开发实践:共享环境与LLM整合经验
微信小程序·小程序
项目題供诗2 天前
微信小程序黑马优购(项目)(一)
微信小程序·小程序
项目題供诗2 天前
微信小程序黑马优购(项目)(三)
微信小程序·小程序
H_ZMY2 天前
微信小程序 mp-html:专为小程序设计的富文本渲染组件
微信小程序·小程序·html
千寻技术帮3 天前
50043_基于微信小程序的小区物业管理系统
mysql·微信小程序·源码·文档·ppt
项目題供诗4 天前
微信小程序黑马优购(项目)(二)
微信小程序·小程序
计算机徐师兄4 天前
Java基于SpringBoot的足球队管理平台小程序【附源码、文档说明】
微信小程序·小程序·足球队管理平台小程序·java足球队管理平台小程序·足球队管理微信小程序·足球队管理平台·jav足球队管理小程序