uniapp之快手小程序踩坑纪要

题主当前项目的配置:

  • uniapp:2.0.1-alpha-36920221121001
  • vue: 2.6.11
  • hbuidler: 3.8.4
  • mp开发工具: 1.32.1

1. 设置小程序appid

hbuilder可以在配置文件中输入appid,但是实际小程序开发工具中需要反复输入appid才能进行扫码预览和真机预览。

解决: uniapp的配置文件manifest.json中手动加入 (hello uniapp的demo中没有对应的字段)

json 复制代码
    "mp-kuaishou" : {
        "appid": "ksxxxxxxxxxxxxxxx",
        "usingComponents" : true
    }

2. unzip解压图片包不完整的问题(安卓)

项目中将一个zip文件使用downloadFile + unzip获取一系列图片资源,但是发现安卓设备解压出来的图片总是不完整,具体表现为:图片的底部会出现不定高度的缺失。但是ios设备表现正常。

解决: 手动下载文件并逐个存储。(存的时候需要使用await保证依次执行,否则会大大增加耗时)

javascript 复制代码
//package.json ->     "jszip": "^3.10.1",
uni.request({
    url: 'xxxxxxxxx',
    method: 'GET',
    responseType: 'arraybuffer', //貌似并没有用
    success: (img:any)=> {
        JSZip.loadAsync(Buffer.from(img.data, 'base64')).then(async (res)=> {
            for (const fileName of Object.keys(res.files)) {
                let file = res.files[fileName]
                await file.async('arraybuffer').then(async buffer=> {
                    let path = `${dir}/${fileName}`
                    await this.writeFileToPath(buffer, path)
                })
            }
        });
    }, fail: err=> {
        console.log(`fetch zip fail`, this.remote_url, err)
    }
})

3. mkdir失败

不管是安卓设备还是ios都会mkdir失败。

首先快手的默认临时目录为: kwfile://tmp 。这个可以在开发工具的项目详情-文件系统中看到。用户目录为 kwfile://usr 。但是实际项目中我们想要创建一个tmp/xxx文件夹的时候,真机预览和体验版会返回错误。

解决 通过unzip解压路径来绕过这个问题,下载一个很小的zip文件,将解压路径指定为需要创建的目录即可。

ps: 三级目录可能无法直接创建(kwfile://tmp/abc可以解压, 但kwfile://tmp/abc/dfe可能无法解压)

javascript 复制代码
    _mkdir2 (dir:string) {
        return new Promise(async (resolve)=> {
            uni.downloadFile({
                url: 'xxxxx.zip',
                success: (res)=> {
                    fs.unzip({
                        zipFilePath: res.tempFilePath,
                        targetPath: dir,
                        success:(res)=> {
                            console.log(`unzip zip success for mkdir`, dir, res)
                            resolve(true)
                        },
                        fail:(err)=> {
                            console.log(`unzip zip fail for mkdir`, dir, err)
                            resolve(!true)
                        },
                    })
                },
                fail: (err)=> {
                    console.log(`download zip file for mkdir fail`, err)
                    resolve(!true)
                }
            })
        })
    }

4. innnerAudioContext播放buffer/base64数据

在H5上我们可以给audio.src赋值base64字符串来播放音频,快手开发工具上也是可以播放的,但是当真机预览的时候,会提示播放不了。base64字符串中的斜杠会被认为是文件路径...

还有一个需要注意的问题,ios设备上会偶尔无法解析base64的音频文件,audiocontext播放这些音频的时候也不会报错,duration时长显示为0。 可以设置base64对应的临时文件后缀为mp3来解决。

解决 将base64写入文件,让audio.src指向临时文件地址

javascript 复制代码
  let tmpfilePath = "kwfile://tmp/" + Date.now() + '.mp3';
  this.fs.writeFile({
    data: data, //这个base64字符串不用带data:audio/wav;base64,标头
    encoding: "base64",
    filePath: tmpfilePath,
    success: (res) => {
      console.log(`write audio file success => ${Date.now() - st}`, res);
      this.queueMapForAudio.set(keyInQueueMap, tmpfilePath);
    },
  });

5. requestAnimationFrame

  • 小程序上没办法获得我们在H5上用来定时渲染的requestAnimationFrame方法。但是setInterval相对来说还比较靠谱。
  • canvas倒是提供了一个平替的api来处理渲染前的逻辑。但是这个requestAnimationFrame不是每次都执行,而是执行一次。这里的createSelectorQuery方法可以参考快手开发平台的KSML说明
javascript 复制代码
    this?.createSelectorQuery().select('#canvas_shot').node((res)=> {
        console.log(`Get canvas Node`, res.node)
        if(res.node) {
            this.canvasNode = res.node
            this.canvasAnimationFrameId = this.canvasNode.requestAnimationFrame(()=> {
                console.log(`FIRST requestAnimationFrame1 at => ${Date.now()}`)
            })
        }
    }).exec()

6. canvas.drawImage 和 canvasPutImageData

  • H5的putimage可以支持多种类型的参数,但是小程序只能支持文件地址,所以需要把自己获取到的base64或者buffer写成本地文件才能显示。
  • canvasPutImageData这个API的执行速度非常非常慢,除非是简单的渲染,否则不太建议

参考文章

相关推荐
2301_32241428041 天前
springboot学生课堂出勤统计系统15835-计算机毕设 原创(免费领源码+部署教程+开发环境)
java·mysql·随机森林·微信小程序·uni-app·微信开发者工具·推荐算法
2501_915921433 天前
iOS应用从开发到上架App Store的完整步骤详解
android·ios·小程序·https·uni-app·iphone·webview
小蒜学长5 天前
基于SpringBoot+uni-app的闪洁家政系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·uni-app·家政服务
anyup_前端梦工厂5 天前
uni-app 模板 Starter 一键清理 Skill 发布
前端·人工智能·uni-app
anyup_前端梦工厂5 天前
将uni-app工程迁移uni-app x,AI 能帮我们做什么?
前端·人工智能·uni-app
蒿豪郝灬号5 天前
UniApp接入鸿蒙版阿里百川
华为·uni-app·harmonyos
2501_915921436 天前
详细教程:使用Perfdog工具测量iOS应用的功耗性能与优化方法
android·macos·ios·小程序·uni-app·cocoa·iphone
梦曦i6 天前
uni-app、uni-app x 与 Flutter 深度对比:2026年跨平台开发框架选型指南
flutter·uni-app·uni-app x
普马萨特7 天前
uni-app x 定位踩坑实录:H5/小程序/App 统一经纬度与逆地理编码
uni-app
Geek_Vison7 天前
保险APP已经用了Flutter,还能接入小程序吗?
flutter·小程序·uni-app·mpaas·小程序容器