Uniapp与golang,使用Protobufjs通讯

目前以我的感觉,坑差不多踩完了,所以记录下

客户端UniApp,刚开始用官方的protobufjs 生成pb文件,然后加载,uniapp发布web版本,是可以愉快的玩耍的
但是部署到微信开发者工具后,且上真机后,提示TypeError: Function(...) is not a function

根本原因是微信不允许动态加载function 什么的巴拉巴拉啦
反正就是不能直接用protobufjs

一系列的搜索,各个技术博客均指向 ProtobufForWechat  可行!

文中说要手动修改 pb.json 为 pb.js  且 要用 module.export 将json数据包裹。
每次修改要手动,其实我是极度抵触的。 随便生成几次,连代码都不想敲了

懒人懒办法,用Pyhton 模拟操作,贴上我的逻辑。

逻辑:
用上述pb库生成 .json文件后,用python,先获取当前文件夹下的所有.json文件
将里面内容取出来,再在内容之前 += module.exports=
再将修改的内容存入  xx.js 中,最后将原始json文件删除即可。

import os
def rewriteFile():
    all_file=[]
    path ="./"
    print(path)
    for fin os.listdir(path):
        f_name = os.path.join(path,f)
        file_extens = f_name.split('.')[2]
        if (file_extens =="json"):
            all_file.append(f_name)
        print(all_file)
        for fileItemin all_file:
        oldPath = fileItem
        newPath ="../../client/util/proto/json/"+fileItem.replace(".json",".js")
        oldFile = open(oldPath,"r")
        oldContent = oldFile.read()
        newFile = open(newPath,"w")
        newFile.write("module.exports=" + oldContent)
        oldFile.close()
        newFile.close()
        os.remove(oldPath)
rewriteFile()

你可能感兴趣的:(Uniapp与golang,使用Protobufjs通讯)