浏览器大全:是一个提供流行浏览器教程、在线学习分享的学习平台!

微信小程序中用Python生成二维码的2种方式

微信小程序,简称小程序,英文名Mini Program,是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或搜一下即可打开应用。小程序是一种不用下载就能使用的应用,也是一项门槛非常高的创新,经过将近两年的发展,已经构造了新的小程序开发环境和开发者生态。

本篇文章给大家带来的内容是关于微信小程序中用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://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)
            imgdata=base64.b64decode(b64str)

            path = "static/tmpfiles/scan_%s.png" % 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

相关推荐:

微信小程序PHP生成带参数二维码

微信小程序用户点击按钮生成带参二维码的示例代码

以上就是微信小程序中用Python生成二维码的两种方式的详细内容,更多请关注php中文网其它相关文章!


小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜一下即可打开应用。



相关软件

2345加速浏览器官方版

2345加速浏览器官方版 | 56.2MB

2345加速浏览器官方版

新一代2345加速浏览器采用Chromium和IE双内核,主打极速与安全特性。基于Chromium深度定制,引入网页智能预加载技术,访问网页更快速..

QQ浏览器官方正式版

QQ浏览器官方正式版 | 49.67MB

QQ浏览器官方正式版

QQ浏览器秉承TT浏览器1-4系列方便易用的特点,但技术架构不同,交互和视觉表现也重新设计,采用Chromium内核+IE双内核,让浏览快速稳定...

百度浏览器最新版下载

百度浏览器最新版下载 | 13.3MB

百度浏览器最新版下载

q百度浏览器,是一款简洁轻快、智能懂你的浏览器。依靠百度强大的搜索平台,在满足用户浏览网页的基础上,它整合百度体系业务优势,带给用户更方便的浏览方式功能...

UC浏览器官方正式版

UC浏览器官方正式版 | 44.2MB

UC浏览器官方正式版

UC浏览器(UC Browser)是UC Mobile Limited在2004年8月开发的一款软件,分uc手机浏览器和uc浏览器电脑版。UC浏览器是全球使用量最大的第三方手机浏览器...

猎豹浏览器2022最新版下载

猎豹浏览器2022下载 | 45MB

猎豹浏览器2022最新版下载

猎豹安全浏览器对Chrome的Webkit内核进行了超过100项的技术优化,访问网页速度更快。其具有首创的智能切换引擎,动态选择内核匹配不同网页...

360安全浏览器官方版下载

360安全浏览器下载 | 21.4MB

360安全浏览器官方版下载

360安全浏览器拥有全国最大的恶意网址库,采用恶意网址拦截技术,可自动拦截挂马、欺诈、网银仿冒等恶意网址。独创沙箱技术,在隔离模式即使访问****也不会感染...