微信公众号多域名解决方案---附python源码 办公学习

教程/书籍 2023-06-19 15:02   2046 1
import json
import os
from urllib import parse
from flask import request, redirect, session, Flask

app = Flask(__name__)
app.config ['SECRET_KEY'] = os.urandom(24)


@app.route('/', methods= ['GET'])
def index():
return '''<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>微信公众号多域名回调系统</title>
<style>
body{
font-family:'微软雅黑','Helvetica Neue',Helvetica,Arial,sans-serif;
margin: 0;
text-align: center;

background-size:100% auto;
}
.title_logo{
opacity: 0.5;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="container">
<div class="title_logo"><svg t="1666179437018" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2569" width="200" height="200"><path d="M308.73856 119.23456C23.65696 170.15296-71.37024 492.23936 155.392 639.66464c12.43392 7.99232 12.43392 7.104-6.21824 62.76096l-15.98464 47.65952 57.43104-30.784 57.43104-30.78656 30.49216 7.40096c31.96928 7.99232 72.82432 13.61664 100.0576 13.61664l16.28416 0-5.62688-21.61152c-44.70016-164.5952 109.82912-327.71072 310.8352-327.71072l27.2384 0-5.62432-19.53792C677.59616 186.43456 491.392 86.67136 308.73856 119.23456zM283.87072 263.40352c30.1952 20.4288 31.97184 64.5376 2.95936 83.48416-47.06816 30.78656-102.1312-23.38816-70.45632-69.57056C230.28736 256.59648 263.74144 249.78688 283.87072 263.40352zM526.62016 263.40352c49.73568 33.45408 12.43392 110.71744-43.22304 89.40288-40.25856-15.39328-44.99712-70.75072-7.40096-90.5856C490.79808 254.22848 513.88928 254.81984 526.62016 263.40352zM636.44928 385.37216c-141.2096 25.7536-239.19872 132.91776-233.57184 256.06656 7.40096 164.89472 200.71168 278.56896 386.32448 227.65312l21.90592-5.92128 46.1824 24.8704c25.4592 13.9136 46.77376 23.97696 47.36512 22.79168 0.59392-1.47968-4.43648-19.24352-10.95168-39.6672-14.79936-45.59104-15.09632-42.33472 4.73856-56.54272C1121.64864 654.464 925.67552 332.97408 636.44928 385.37216zM630.82496 518.28992c12.4288 8.28928 18.944 29.01248 13.61408 44.1088-11.24864 32.26624-59.49952 34.63424-72.52992 3.55328C557.10976 530.13248 597.9648 496.97536 630.82496 518.28992zM828.57472 521.84576c19.53792 18.64704 16.2816 50.32448-6.51264 62.16448-34.93376 17.76128-71.63904-17.76128-53.58336-51.80416C780.32128 510.2976 810.81344 504.97024 828.57472 521.84576z" p-id="2570" data-spm-anchor-id="a313x.7781069.0.i0" class="" fill="#07c160"></path></svg></div>
</div>
</body>
</html>'''


@app.route('/connect/oauth2/authorize', methods= ['GET'])
def get_code():
# redirect_uri放到session
redirect_uri = request.args.get('redirect_uri')
session ['redirect_uri'] = redirect_uri
print(request.args.get('path'),'12321312321')
# 获取相关参数
appid = request.args.get('appid')
if appid == '' or appid is None:
return json.dumps({'code': 500, 'data': 'appid不能为空'}, ensure_ascii=False)
response_type = request.args.get('response_type')
if response_type == '' or response_type is None:
response_type = 'code'
scope = request.args.get('scope')
if scope == '' or scope is None:
scope = 'snsapi_base'
state = request.args.get('state')
if state == '' or state is None:
state = ''
host = request.host
http = ''
if host.split(':') [1] == '443':
http = 'https://'
host = host.split(':') [0]
elif host.split(':') [1] == '80':
http = 'http://'
host = host.split(':') [0]
uri = 'https://open.weixin.qq.com/connect/oauth2/authorize?'
data = {
'appid': appid,
'redirect_uri': http + host + '/get_code',
'response_type': response_type,
'scope': scope,
'state': state,
}
url = uri + parse.urlencode(data) + '#wechat_redirect'
return redirect(url)


@app.route('/get_code', methods= ['GET'])
def info():
# 获取微信code
code = request.args.get('code')
state = request.args.get('state')
# session获取实际重定向地址
redirect_uri = session.get('redirect_uri')
# 拼接实际重定向地址
url = redirect_uri + '?code=' + code + '&state=' + state
return redirect(url)


if __name__ == '__main__':
app.debug = False
app.run(host='0.0.0.0', port=8080)
最新回复 (1)
  • MCES
    2023-09-05 11:06
    这个资源我很需要,谢谢盘主
    • 网盘资源社-免费优质云盘网盘资源分享社区论坛!
      3
        立即登录 免费注册
关于盘主
这家伙太懒了,什么也没留下。

关注公众号
防丢失不迷路