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

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

相关推荐
鸭鸭梨吖7 小时前
微信小程序---下拉框
微信小程序·小程序
CRMEB定制开发8 小时前
CRMEB Pro版前端环境配置指南
前端·微信小程序·uni-app·商城源码·微信商城·crmeb
mon_star°10 小时前
搭建一款结合传统黄历功能的日历小程序
微信·微信小程序·小程序·微信公众平台
The_era_achievs_hero11 小时前
微信小程序91~100
微信小程序·小程序
假装我不帅11 小时前
微信小程序下拉加载更多实现
微信小程序·小程序
高压锅_122011 小时前
Cursor+Coze+微信小程序实战: AI春联生成器
人工智能·微信小程序·notepad++
2301_8059629311 小时前
微信小程序控制空调之微信小程序篇
微信小程序·小程序
Ama_tor17 小时前
mini-program01の系统认识微信小程序开发
微信小程序·小程序
毛毛三由1 天前
基于svga+uniapp的微信小程序动画组件开发指南
微信小程序·uni-app·notepad++
Python智慧行囊1 天前
微信小程序交互精髓:点击操作与状态管理实战
微信小程序·小程序·交互