本篇文章给大家带来的内容是关于微信小程序中用Python生成二维码的两种方式 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
微信小程序生成二维码:
所用语言python,有两种方式:
1: 后端传一段字符串给前端, 前端显示
立即学习“Python免费学习笔记(深入)”;
2: 后端直接生成图片
1: 后端传一段字符串给前端, 前端显示
立即学习“Python免费学习笔记(深入)”;
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= #39;utf-8 #39;) header_dict = { #39;User-Agent #39;: #39;Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko #39;, Content-Type : application/json } url = #39;https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token= #39; + 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( #39;. #39;)[0]; if(hostname1 == #39;127 #39; || hostname1 == #39;localhost #39;){ hostname1 = hostname; if(window.localStorage.getItem( #39;wxcode2 #39;)){ $( #39;#wxcodeImg2 #39;).attr( #39;src #39;, #39;data:image/png;base64, #39;+ window.localStorage.getItem( #39;wxcode2 #39;)); $( #39;#wxCodeModal2 #39;).modal( #39;show #39;); return; var params = { scene :hostname1, $.ajax({ type: #39;post #39;, url: #39;/request?rname=i_plc.Page.wechat_api.wechat.get_wxCode #39;, data:params, success:function (res) { console.log(res) if(res === false){ $.MessageBox.notify( #39;warn #39;, #39;获取失败,请稍后再试! #39;); }else{ console.log(res) $( #39;#wxcodeImg2 #39;).attr( #39;src #39;, #39;data:image/png;base64, #39;+res); $( #39;#wxCodeModal2 #39;).modal( #39;show #39;); window.localStorage.setItem( #39;wxcode2 #39;,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= #39;utf-8 #39;) header_dict = { #39;User-Agent #39;: #39;Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko #39;, Content-Type : application/json } url = #39;https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token= #39; + 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,), #39;wb+ #39;) 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)textmod = parse.urlencode(textmod) header_dict = { #39;User-Agent #39;: #39;Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko #39;} url = #39;https://api.weixin.qq.com/cgi-bin/token #39; req = request.Request(url= #39;%s%s%s #39; % (url, #39;? #39;, textmod), headers=header_dict) res = request.urlopen(req) res = res.read().decode(encoding= #39;utf-8 #39;) res = json.loads(res) access_token = res[ access_token ] return access_token except Exception as e: print(e) return False
相关推荐:
微信小程序PHP生成带参数二维码
微信小程序用户点击按钮生成带参二维码的示例代码
以上就是微信小程序中用Python生成二维码的两种方式的详细内容,更多请关注php中文网其它相关文章!
微信app下载
微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!