小編給大家分享一下微信小程序中用Python生成二維碼的方法有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)公司專注于企業(yè)成都營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、建華網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、商城網(wǎng)站制作、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為建華等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
微信小程序生成二維碼:
所用語(yǔ)言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){ //獲取管理端小程序碼 //動(dòng)態(tài)獲取域名,若為本地環(huán)境,則默認(rèn)攜帶參數(shù)為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', '獲取失敗,請(qǐng)稍后再試!'); }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 = '/upload/otherpica41/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生成二維碼的方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文題目:微信小程序中用Python生成二維碼的方法有哪些
文章URL:http://aaarwkj.com/article12/ispjdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站導(dǎo)航、靜態(tài)網(wǎng)站、小程序開(kāi)發(fā)、App設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)