uni-app 小程序二维码的生成及保存

2019-07-17  本文已影响0人  葶子123

前言:最近写了小程序二维码,分享一下

前面只写了小程序端的实现(有人在评论指出),现在更新下兼容版本,主要实现还是在-图像base64保存为文件,下面上一下兼容版本的

export function base64ToPath(base64) {
    return new Promise(function(resolve, reject) {
        if (typeof window === 'object' && 'document' in window) {
            base64 = base64.split(',')
            var type = base64[0].match(/:(.*?);/)[1]
            var str = atob(base64[1])
            var n = str.length
            var array = new Uint8Array(n)
            while (n--) {
                array[n] = str.charCodeAt(n)
            }
            return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))
        }
        var extName = base64.match(/data\:\S+\/(\S+);/)
        if (extName) {
            extName = extName[1]
        } else {
            reject(new Error('base64 error'))
        }
        var fileName = Date.now() + '.' + extName
        if (typeof plus === 'object') {
            var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
            bitmap.loadBase64Data(base64, function() {
                var filePath = '_doc/uniapp_temp/' + fileName
                bitmap.save(filePath, {}, function() {
                    bitmap.clear()
                    resolve(filePath)
                }, function(error) {
                    bitmap.clear()
                    reject(error)
                })
            }, function(error) {
                bitmap.clear()
                reject(error)
            })
            return
        }
        if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
            var filePath = wx.env.USER_DATA_PATH + '/' + fileName
            wx.getFileSystemManager().writeFile({
                filePath: filePath,
                data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
                encoding: 'base64',
                success: function() {
                    resolve(filePath)
                },
                fail: function(error) {
                    reject(error)
                }
            })
            return
        }
        reject(new Error('not support'))
    })
}

应用:

import { base64ToPath } from '@/common/plus.js'
base64ToPath(url).then(path=>{
    console.log(path)
})

下面是从前的内容

import {getQrCode} from "@/api/spu.js"
//获取小程序码
getQrCode(this.option.path).then(res=>{
    uni.showLoading({title: '正在生成图片'});
    this.base64ToPath(res.result,path=>{
        console.log(path)
        this.qrCodeRes = path
        this.toDrawCanvas() //画图
    })
})

base64ToPath(path,success) {
    let that = this
    var fileName = Date.now() + '.' + 'png'
    if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
        var filePath = wx.env.USER_DATA_PATH + '/' + fileName
        wx.getFileSystemManager().writeFile({
            filePath: filePath,
            data: path,
            encoding: 'base64',
            success() {
                success(filePath)
            },
            fail(error) {
                that.$toast(error)
            }
        })
        return
    }
}
//获取微信小程序二维码(接口)
export function getQrCode(path,success) {
    let param = {
        "page":"pages/product/detail/index",
        "scene":path.split('?')[1],
        "width":"280"
    }
    return apiBase('wx/getXcxQrCode',param,{showLoading:false,resAll:true})
}

好啦,完成。。。撒花花~~~

上一篇 下一篇

猜你喜欢

热点阅读