小程序二维码生成

发布于:2025-03-20 ⋅ 阅读:(21) ⋅ 点赞:(0)
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            console.log(ruleForm)
            getToken({ act: 'miniprogram', op: 'getAccessToken', type: ruleForm.name }).then((res: any) => {
                const ACCESS_TOKEN = res.data.token
                // 根据选择循环生成多张
                selectData[0].children.forEach((element: { children: any; label: any; value: any }) => {
                    console.log(element.children[0].value)
                    const params = {
                        scene: element.children[0].value,
                        page: ruleForm.page,
                        width: 430,
                        env_version: 'develop',
                    }
                    const label = element.label
                    generateMiniProgramQR(params, ACCESS_TOKEN, label)
                })
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
const generateMiniProgramQR = async (
    params: { scene: any; page?: any; width?: number; label?: any },
    ACCESS_TOKEN: any,
    label: any
) => {
    try {
        const url = `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${ACCESS_TOKEN}`
        const response = await axios.post(url, params, { responseType: 'arraybuffer' })
        const blob = new Blob([response.data], { type: 'image/jpeg' })
        const imageUrl = URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.href = imageUrl
        link.download = `${label}.jpg`
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        URL.revokeObjectURL(imageUrl)
    } catch (error) {
        // 错误处理
    }
}

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到