feat: 调整h5登陆
This commit is contained in:
55
app.py
55
app.py
@@ -160,10 +160,16 @@ SMS_SIGN_NAME = "价值前沿科技"
|
||||
SMS_TEMPLATE_REGISTER = "2386557" # 注册模板
|
||||
SMS_TEMPLATE_LOGIN = "2386540" # 登录模板
|
||||
|
||||
# 微信开放平台配置
|
||||
WECHAT_APPID = 'wxa8d74c47041b5f87'
|
||||
WECHAT_APPSECRET = 'eedef95b11787fd7ca7f1acc6c9061bc'
|
||||
WECHAT_REDIRECT_URI = 'http://valuefrontier.cn/api/auth/wechat/callback'
|
||||
# 微信开放平台配置(PC 扫码登录用)
|
||||
WECHAT_OPEN_APPID = 'wxa8d74c47041b5f87'
|
||||
WECHAT_OPEN_APPSECRET = 'eedef95b11787fd7ca7f1acc6c9061bc'
|
||||
|
||||
# 微信公众号配置(H5 网页授权用)
|
||||
WECHAT_MP_APPID = 'wx4e4b759f8fa9e43a'
|
||||
WECHAT_MP_APPSECRET = 'ef1ca9064af271bb0405330efbc495aa'
|
||||
|
||||
# 微信回调地址
|
||||
WECHAT_REDIRECT_URI = 'https://valuefrontier.cn/api/auth/wechat/callback'
|
||||
|
||||
# 邮件服务配置(QQ企业邮箱)
|
||||
MAIL_SERVER = 'smtp.exmail.qq.com'
|
||||
@@ -3382,12 +3388,18 @@ def register_with_email():
|
||||
return jsonify({'success': False, 'error': '注册失败,请重试'}), 500
|
||||
|
||||
|
||||
def get_wechat_access_token(code):
|
||||
"""通过code获取微信access_token"""
|
||||
def get_wechat_access_token(code, appid=None, appsecret=None):
|
||||
"""通过code获取微信access_token
|
||||
|
||||
Args:
|
||||
code: 微信授权后返回的 code
|
||||
appid: 微信 AppID(可选,默认使用开放平台配置)
|
||||
appsecret: 微信 AppSecret(可选,默认使用开放平台配置)
|
||||
"""
|
||||
url = "https://api.weixin.qq.com/sns/oauth2/access_token"
|
||||
params = {
|
||||
'appid': WECHAT_APPID,
|
||||
'secret': WECHAT_APPSECRET,
|
||||
'appid': appid or WECHAT_OPEN_APPID,
|
||||
'secret': appsecret or WECHAT_OPEN_APPSECRET,
|
||||
'code': code,
|
||||
'grant_type': 'authorization_code'
|
||||
}
|
||||
@@ -3449,10 +3461,10 @@ def get_wechat_qrcode():
|
||||
# URL编码回调地址
|
||||
redirect_uri = urllib.parse.quote_plus(WECHAT_REDIRECT_URI)
|
||||
|
||||
# 构建微信授权URL
|
||||
# 构建微信授权URL(PC 扫码登录使用开放平台 AppID)
|
||||
wechat_auth_url = (
|
||||
f"https://open.weixin.qq.com/connect/qrconnect?"
|
||||
f"appid={WECHAT_APPID}&redirect_uri={redirect_uri}"
|
||||
f"appid={WECHAT_OPEN_APPID}&redirect_uri={redirect_uri}"
|
||||
f"&response_type=code&scope=snsapi_login&state={state}"
|
||||
"#wechat_redirect"
|
||||
)
|
||||
@@ -3490,10 +3502,10 @@ def get_wechat_h5_auth_url():
|
||||
# 编码回调地址
|
||||
redirect_uri = urllib.parse.quote_plus(WECHAT_REDIRECT_URI)
|
||||
|
||||
# 构建授权 URL(使用 snsapi_userinfo 获取用户信息,仅限微信内 H5 使用)
|
||||
# 构建授权 URL(H5 网页授权使用公众号 AppID)
|
||||
auth_url = (
|
||||
f"https://open.weixin.qq.com/connect/oauth2/authorize?"
|
||||
f"appid={WECHAT_APPID}&redirect_uri={redirect_uri}"
|
||||
f"appid={WECHAT_MP_APPID}&redirect_uri={redirect_uri}"
|
||||
f"&response_type=code&scope=snsapi_userinfo&state={state}"
|
||||
"#wechat_redirect"
|
||||
)
|
||||
@@ -3527,10 +3539,10 @@ def get_wechat_bind_qrcode():
|
||||
# URL编码回调地址
|
||||
redirect_uri = urllib.parse.quote_plus(WECHAT_REDIRECT_URI)
|
||||
|
||||
# 构建微信授权URL
|
||||
# 构建微信授权URL(PC 扫码绑定使用开放平台 AppID)
|
||||
wechat_auth_url = (
|
||||
f"https://open.weixin.qq.com/connect/qrconnect?"
|
||||
f"appid={WECHAT_APPID}&redirect_uri={redirect_uri}"
|
||||
f"appid={WECHAT_OPEN_APPID}&redirect_uri={redirect_uri}"
|
||||
f"&response_type=code&scope=snsapi_login&state={state}"
|
||||
"#wechat_redirect"
|
||||
)
|
||||
@@ -3641,8 +3653,19 @@ def wechat_callback():
|
||||
session_data['status'] = 'scanned'
|
||||
print(f"✅ 微信扫码回调: state={state}, code={code[:10]}...")
|
||||
|
||||
# 步骤2: 获取access_token
|
||||
token_data = get_wechat_access_token(code)
|
||||
# 步骤2: 根据授权模式选择对应的 AppID/AppSecret
|
||||
# H5 模式使用公众号配置,PC 扫码和绑定模式使用开放平台配置
|
||||
if session_data.get('mode') == 'h5':
|
||||
appid = WECHAT_MP_APPID
|
||||
appsecret = WECHAT_MP_APPSECRET
|
||||
print(f"📱 H5 模式授权,使用公众号配置")
|
||||
else:
|
||||
appid = WECHAT_OPEN_APPID
|
||||
appsecret = WECHAT_OPEN_APPSECRET
|
||||
print(f"💻 PC 模式授权,使用开放平台配置")
|
||||
|
||||
# 步骤3: 获取access_token
|
||||
token_data = get_wechat_access_token(code, appid, appsecret)
|
||||
if not token_data:
|
||||
session_data['status'] = 'auth_failed'
|
||||
session_data['error'] = '获取访问令牌失败'
|
||||
|
||||
Reference in New Issue
Block a user