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

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

相关推荐
计算机毕设指导615 小时前
基于微信小程序的智能停车场管理系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
2501_9339072115 小时前
如何选择西安优质小程序开发服务与本凡码农合作?
科技·微信小程序·小程序
咖啡の猫2 天前
微信小程序案例 - 自定义 tabBar
微信小程序·小程序·notepad++
咖啡の猫2 天前
微信小程序全局数据共享
微信小程序·小程序
桐溪漂流2 天前
微信小程序cli脚本预览上传
微信小程序·小程序
咖啡の猫2 天前
微信小程序使用 npm 包
微信小程序·小程序·npm
东东5162 天前
xxx食堂移动预约点餐系统 (springboot+微信小程序)
spring boot·微信小程序·小程序·毕业设计·个人开发·毕设
韩立学长3 天前
【开题答辩实录分享】以《智慧校园平台微信小程序》为例进行选题答辩实录分享
spring boot·微信小程序·小程序
h_65432103 天前
微信小程序:按顺序一张图片加载完后,再加载另一张
微信小程序·小程序
qq_316837754 天前
uniapp打包微信小程序使用插件
微信小程序·小程序·uni-app