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

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

相关推荐
计算机毕设指导610 小时前
基于微信小程序的校园物品租赁与二手交易系统【源码文末联系】
spring boot·mysql·微信小程序·小程序·tomcat·maven·intellij-idea
计算机毕设指导610 小时前
基于微信小程序的水上警务通系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
李慕婉学姐14 小时前
【开题答辩过程】以《基于uni-app框架的助学管理系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
微信小程序·uni-app
爱吃的强哥15 小时前
uni-app 开发微信小程序注意点
微信小程序·小程序·uni-app
计算机毕设指导61 天前
基于微信小程序的鸟博士系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
周杰伦fans1 天前
微信小程序信息提示组件大全
微信小程序·小程序
QQ_21696290961 天前
Spring Boot大学生社团管理平台 【部署教程+可完整运行源码+数据库】
java·数据库·spring boot·微信小程序
风月歌2 天前
基于微信小程序的学习资料销售平台源代码(源码+文档+数据库)
java·数据库·mysql·微信小程序·小程序·毕业设计·源码
多仔ヾ2 天前
微信小程序开发实战之 05-微信小程序常用 API(下)
微信小程序
半兽先生2 天前
微信小程序与web-view页面双向通信
前端·微信小程序·小程序