赞
踩
微信小程序获取到的二维码图片,是一个很长的字符串,不好保存到数据库,前端页面也不好读取。
解决方案:
把图片串保存到本地,然后再去读取本地图片,具体写法如下:
微信小程序获取二维码,python写法:
- #获取token
- getUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + XCX_APP_ID + '&secret=' + XCX_APP_SECRET
- getReq = urllib2.Request(getUrl)
- getRes = urllib2.urlopen(getReq)
- getResObj = json.loads(getRes.read())
- access_token = getResObj["access_token"]
- if not access_token:
- return False
-
- #scene 是发给小程序接收的参数
- textmod = {"scene": scene, "page": XCX_MAIN_PAGE, "width": 900, "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 = urllib2.Request(url=url, data=textmod, headers=header_dict)
- res = urllib2.urlopen(req)
- res = res.read()
-
- #保存小程序二维码到指定路径
- b64str = base64.b64encode(res)
- imgFileName = taskId +"_QRcode.png"
- pathImg = IMG_UPLOAD_PATH + imgFileName
- with open(pathImg, 'wb') as f:
- f.write(base64.b64decode(b64str))
- return pathImg

通过 https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' 获取到的二维码参数是一段很长的字符串,如下图
可以通过
- with open(PATH + 'XX.png','wb') as f:
- f.write(base64.b64decode(b64str))
方法把图片写入指定的路径,就可以打开了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。