def get_user_share_qrcode(): openid = session.get('openid') if not openid: return redirect(weixin_oauth2_url()) user = g.service.find_user(openid) rsp = dict(ret=SUCCESS, msg='ok') if not user['share_qrcode']: qr_file = randstr() auth_url = weixin.oauth2_url( app.config['RESTFUL_ROOT'] + '/auth/redirect', user['id']) short_url = weixin.url_to_short(auth_url) make_qrcode(short_url['short_url'], '%s/%s.png' % (app.config['QRCODE_HOME'], qr_file)) user['share_qrcode'] = qr_file + '.png' g.service.update_user(user) rsp['qrcode'] = user['share_qrcode'] app.logger.debug("user: %s, get qrcode return: %s", openid, rsp) return json_dumps(rsp)
def generate_signal_qrcode(uuid='', id=None, env='', url=None): """ 生成一张商户付款二维码 :param uuid: 付款码uuid :param id: 付款码id :param env: 环境变量 :param url: 背景图片url :return: qrcode bytes """ if not uuid and not id and not env: return b'' pay_url_map = { 'prod': 'http://api.mishitu.com/api/user/payment/entry?uuid={}', 'test': 'http://api-alpha.mishitu.com/api/user/payment/entry?uuid={}', } default_url = ('https://ss-alpha.mishitu.com/payunion/' 'f8743e3e-52d1-48d2-9b7f-3e0a8415536d.jpg') url = url if url else default_url # 背景图片 resp = requests.get(url) f = io.BytesIO(resp.content) back_img = Image.open(f) if back_img.mode != 'RGBA': back_img = back_img.convert('RGBA') back_img = back_img.resize(BACK_IMG_SIZE, Image.ANTIALIAS) # 生成二维码链接 data = pay_url_map[env].format(uuid) # 生成二维码扫码区 img = make_qrcode(data, QRCODE_SIZE) # 卡片 cav_img = Image.new('RGBA', BACK_IMG_SIZE, (220, 220, 220)) # 给卡片增加背景图片 cav_img = Image.alpha_composite(cav_img, back_img) img_w, _ = img.size cav_img_w, _ = cav_img.size w = int((cav_img_w - img_w) / 2) cav_img.paste(img, (w, QRCODE_START_HEIGHT)) # 生成画布 draw = ImageDraw.Draw(cav_img) # 写入编号 number = gen_number(id) fnt = ImageFont.truetype("msyh.ttf", size=64, encoding='utf-8') fnt_width, _ = draw.textsize(number, fnt) if fnt_width > cav_img_w: font_width = 0 else: font_width = int((cav_img_w - fnt_width) / 2) draw.text((font_width, QRCODE_ID_START_HEIGHT), number, font=fnt, fill='#fff') buf = io.BytesIO() cav_img.save(buf, 'png') return buf.getvalue()
def generate_auth_url_qrcode(scenario_type, state, env, version, redirect_url=None): """ 生成一张授权二维码(微信、支付宝) :param scenario_type: marketer / merchant / alipay :param state: 微信重定向参数 :param env: 环境变量 :param version: api版本 :param redirect_url: 微信重定向url :return: qrcode bytes """ scenario = { 'marketer': 'wx4897f192a4e54979', 'merchant': 'wx3f75ca357f606548', 'alipay_test': '2016091800543597', 'alipay_prod': '2016071401615878', 'user_test': 'wx550ba0dd3e19d5f6', } redirect_url_map = { 'test': 'http://api-alpha.mishitu.com/', 'prod': 'http://api.mishitu.com/' } if version and not redirect_url: version = '/{}'.format(version) redirect_map = { 'marketer': 'api/marketer{}/get-merchant-wechat-info/'.format(version), 'merchant': 'api/merchant{}/add_cashier/wechat_auth_redirect/'.format(version), 'alipay': 'api/marketer{}/get-merchant-alipay-info/'.format(version) } if not redirect_url: if state.startswith('alipay_'): redirect_url = redirect_url_map[env] + redirect_map['alipay'] elif state.startswith('marketer_'): redirect_url = redirect_url_map[env] + redirect_map['marketer'] else: redirect_url = redirect_url_map[env] + redirect_map['merchant'] # url encode the redirect_url redirect_url = quote(redirect_url, safe='') if state.startswith('alipay_'): auth_host = 'https://openauth.alipay.com' if env == 'test': auth_host = 'https://openauth.alipaydev.com' auth_url = ( '{}/oauth2/publicAppAuthorize.htm?app_id={}&scope=auth_user' '&state={}&redirect_uri={}'.format(auth_host, scenario[scenario_type], state, redirect_url)) else: auth_url = ('https://open.weixin.qq.com/connect/oauth2/authorize?' 'appid={}&redirect_uri={}&response_type=code&' 'scope=snsapi_userinfo&state={}' '#wechat_redirect'.format(scenario[scenario_type], redirect_url, state)) # 生成授权二维码 img = make_qrcode(auth_url) buf = io.BytesIO() img.save(buf, 'png') return buf.getvalue()
os.system('lessc {}/static/style.less > {}/static/style.css'.format( homedir, homedir)) # 获取主页短链接 url = weixin.url_to_short(main.weixin_oauth2_url()) print('get short url return: ' + url['errmsg']) if url['errcode'] != 0: exit(1) os.system("sed -i '/AUTH2_SHORT_URL/c\AUTH2_SHORT_URL = \"{}\"\' {}/{}".format( url['short_url'], homedir, '/conf/config.py')) main.create_menu() # 生成分享二维码 make_qrcode(url['short_url'], 'qrcode.png') from PIL import Image qr = Image.open('qrcode.png') bg = Image.open('../static/images/img_share_picture.png') qr = qr.convert('RGB') bg = bg.convert('RGB') qr_w, qr_h = qr.size bg_w, bg_h = bg.size qr_x = (bg_w - qr_w) // 2 qr_y = bg_h // 3 bg.paste(qr, (qr_x, qr_y, qr_x + qr_w, qr_y + qr_h)) bg = bg.convert('P') bg.save('../static/images/entry_qrcode.png') os.system('rm qrcode.png') print( 'get app entry qrcode from: http://test.boostfield.com/images/entry_qrcode.png'
def generate_qrcode(uuids, env, url=None): """ 生成二维码压缩包 :param uuids: uuid 数组 :param url: 二维码背景 :param env: 当前环境 :return: 二维码压缩包bytes """ if not uuids: return b'' default_url = ('https://ss1.mvpalpha.muchbo.com/payunion/' 'c3a531a5-9610-43d5-bdcc-0cd782eba491.jpg') pay_url_map = { 'prod': 'http://api.mishitu.com/api/user/payment/entry?uuid={}', 'test': 'http://api-alpha.mishitu.com/api/user/payment/entry?uuid={}', } # 背景图片url url = url if url else default_url # 背景图片 resp = requests.get(url) f = io.BytesIO(resp.content) back_img = Image.open(f) if back_img.mode != 'RGBA': back_img = back_img.convert('RGBA') back_img = back_img.resize(BACK_IMG_SIZE, Image.ANTIALIAS) # 生成一个内存zip对象 imz = InMemoryZIP() for item in uuids: # 二维码data data = pay_url_map[env].format(item[1]) # 生成扫码区 img = make_qrcode(data, QRCODE_SIZE) # 卡片 cav_img = Image.new('RGBA', BACK_IMG_SIZE, (220, 220, 220)) # 给卡片增加背景图片 cav_img = Image.alpha_composite(cav_img, back_img) img_w, _ = img.size cav_img_w, _ = cav_img.size width_start = int((cav_img_w - img_w) / 2) # 将二维码添加到背景 cav_img.paste(img, (width_start, QRCODE_START_HEIGHT)) # 将编号插入背景图 number = gen_number(item[0]) draw = ImageDraw.Draw(cav_img) fnt = ImageFont.truetype("msyh.ttf", size=64, encoding='utf-8') fnt_width, _ = draw.textsize(number, fnt) if fnt_width > cav_img_w: font_width = 0 else: font_width = int((cav_img_w - fnt_width) / 2) draw.text((font_width, QRCODE_ID_START_HEIGHT), number, font=fnt, fill='#fff') buf = io.BytesIO() cav_img.save(buf, 'png') imz.appendfile('{}.jpg'.format(str(item[1])), buf.getvalue()) return imz.read()