词条信息

admin
admin
超级管理员
词条创建者 发短消息   

相关词条

热门词条

更多>>
什么是端口?到底是做什么的呢?
端口一般指两种,一种是硬件比如路由器或者交换机的插网线的端口,一种是软件的逻辑的概念,比如http的80端口!...
7种进阶方法让你快速测试端口连通性
Ping是Windows、Linux和Unix系统下的一个检查网络连通性的命令工具,对于大部分互联网用户来说很...
电脑开机,总需要按F1,是什么原因造成的?
一.主板掉电这个说法是行业内的叫法了,一般是主板的CMOS电池没电了导致的。也是最常见的一种提示你按F1的提示...
社保降费对个人有什么影响?
下调城镇职工基本养老保险单位缴费比例是政府给企业发的一个大红包,特别是对于企业来说是一个利好,但是对个人来说有...
车辆“出险”对下年保费的影响,到底有多大?
【出险对交强险的影响】【出险对商业险的影响】车辆“出险”对下年保费的影响,到底有多大?这里有必要先提下车险第三...

精选图集

更多>>
简易百科旧版 >>所属分类 >> 源码天下    Python    小程序    微信小程序   

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

标签: 微信小程序 Python 二维码

顶[0] 发表评论(0) 编辑词条

本篇文章给大家带来的内容是关于微信小程序中用Python生成二维码的两种方式 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。


微信小程序生成二维码:


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


目录

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

 

 

附件列表


按字母顺序浏览:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

→我们致力于为广大网民解决所遇到的各种电脑技术问题
 如果您认为本词条还有待完善,请 编辑词条

上一篇小程序如何自定义下拉刷新源码
下一篇微信专项清理色情恶意账号

0
1. 本站部分内容来自互联网,如有任何版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
2. 本站内容仅供参考,如果您需要解决具体问题,建议您咨询相关领域专业人士。
3. 如果您没有找到需要的百科词条,您可以到百科问答提问或创建词条,等待高手解答。

关于本词条的提问

查看全部/我要提问>>