温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序中用Python生成二维码的方法有哪些

发布时间:2021-01-25 10:02:12 来源:亿速云 阅读:438 作者:小新 栏目:移动开发

小编给大家分享一下微信小程序中用Python生成二维码的方法有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

微信小程序生成二维码:

所用语言python,有两种方式:

1: 后端传一段字符串给前端, 前端显示

2: 后端直接生成图片

1: 后端传一段字符串给前端, 前端显示

def get_wxCode(Request, UserInfo):
    try:
        scene = Request["scene"]
        access_token = get_wxCode_token()
        if not access_token:
            return False
        textmod = {"scene": scene, "page": "pages/index/main", "width": 430, "auto_color": True, "is_hyaline": False}
        textmod = json.dumps(textmod).encode(encoding='utf-8')
        header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',
                       "Content-Type": "application/json"}
        url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + access_token
        req = request.Request(url=url, data=textmod, headers=header_dict)
        res = request.urlopen(req)
        res = res.read()
        b64str = base64.b64encode(res)
        return b64str
    except Exception as e:
        print(e)
        return False
var getWXcode2 = function(hostname){  //获取管理端小程序码

    //动态获取域名,若为本地环境,则默认携带参数为wx-test
    //示例:londex.i-plc.cn
    var hostname1 =  window.location.host;
    hostname1 = hostname1.split('.')[0];
    if(hostname1 == '127' || hostname1 == 'localhost'){
        hostname1 = hostname;
    }
    if(window.localStorage.getItem('wxcode2')){
        $('#wxcodeImg2').attr('src','data:image/png;base64,'+ window.localStorage.getItem('wxcode2'));
        $('#wxCodeModal2').modal('show');
        return;
    }
    var params = {
        "scene":hostname1,
    };
    $.ajax({
        type:'post',
        url:'/request?rname=i_plc.Page.wechat_api.wechat.get_wxCode',
        data:params,
        success:function (res) {
            console.log(res)

            if(res === false){
                $.MessageBox.notify('warn', '获取失败,请稍后再试!');
            }else{
                console.log(res)
                $('#wxcodeImg2').attr('src','data:image/png;base64,'+res);
                $('#wxCodeModal2').modal('show');
                window.localStorage.setItem('wxcode2',res)
            }

        }
    });
};

2: 后端直接生成图片

def get_wxCode(Request, UserInfo):
    """
        生成小程序二维码
    :param Request:
    :param UserInfo:
    :return:
    """
    result = {"success": False}
    try:
        # scene = Request["scene"]
        access_token = get_wxCode_token()
        if not access_token:
            raise Exception("access_token")
        compid = Request["compid"]
        sql = "select compIndex from company where operationFlag=9 and compID=%s" % compid
        Result = SqlRun(sql)
        if Result["Data"] and Result["Data"][0] and Result["Data"][0][0]:
            scene = Result["Data"][0][0]

            textmod = {"scene": scene, "page": "pages/index/main", "width": 430, "auto_color": True, "is_hyaline": False}
            textmod = json.dumps(textmod).encode(encoding='utf-8')
            header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',
                           "Content-Type": "application/json"}
            url = 'https://cache.yisu.com/upload/information/20201208/261/13969" % file_name
            file = open(os.path.join(settings.BASE_DIR, path,), 'wb+')
            file.write(imgdata)
            file.close()

            result["code_url"] = path
            result["success"] = True
    except Exception as e:
        result["error_msg"] = str(e)
    return json.dumps(result)


def get_wxCode_token():
    try:
        textmod = {"grant_type": "client_credential",
            "appid": "wx44a452fb08b0a990",
            "secret": "9aedb0a274027bdd09612fbde3298129"
        }
        textmod = parse.urlencode(textmod)
        header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}
        url = 'https://api.weixin.qq.com/cgi-bin/token'
        req = request.Request(url='%s%s%s' % (url, '?', textmod), headers=header_dict)
        res = request.urlopen(req)
        res = res.read().decode(encoding='utf-8')
        res = json.loads(res)
        access_token = res["access_token"]
        return access_token
    except Exception as e:
        print(e)
        return False

以上是“微信小程序中用Python生成二维码的方法有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI