feat: UFO H5 服务号 OAuth 登录与同商户公众号 JSAPI 支付

This commit is contained in:
XingQue
2026-07-30 00:12:37 +08:00
parent 531639731a
commit 148a35f04b
11 changed files with 449 additions and 64 deletions

View File

@@ -143,6 +143,62 @@ def wechat_jscode2session_for_request(request, code):
return jscode2session(code, club_id=club_id)
def get_club_official_credentials(club_id):
"""服务号 AppID/SecretH5 网页授权 + 公众号 JSAPI"""
club_id = club_id or CLUB_ID_DEFAULT
club = None
try:
club = Club.query.get(club_id=club_id)
except Club.DoesNotExist:
club = None
except Exception:
logger.exception('读取俱乐部失败 club_id=%s', club_id)
club = None
if not club:
return '', '', None
appid = (club.official_appid or '').strip()
secret = (club.official_secret or '').strip()
return appid, secret, club
def oauth2_access_token(code, club_id=None):
"""
服务号网页授权code → openidsns/oauth2/access_token
"""
appid, secret, club = get_club_official_credentials(club_id)
if not appid or not secret:
return {'errmsg': '俱乐部服务号配置未设置official_appid/official_secret'}
url = 'https://api.weixin.qq.com/sns/oauth2/access_token'
params = {
'appid': appid,
'secret': secret,
'code': code,
'grant_type': 'authorization_code',
}
try:
response = requests.get(url, params=params, timeout=10)
result = response.json()
if 'openid' in result:
return {
'openid': result['openid'],
'access_token': result.get('access_token', ''),
'refresh_token': result.get('refresh_token', ''),
'scope': result.get('scope', ''),
'unionid': result.get('unionid', ''),
'club_id': (club.club_id if club else None) or club_id or CLUB_ID_DEFAULT,
'wx_appid': appid,
}
errcode = result.get('errcode', 'unknown')
errmsg = result.get('errmsg', '未知错误')
return {'errmsg': f'[{errcode}]{errmsg}'}
except requests.exceptions.Timeout:
return {'errmsg': '请求微信接口超时'}
except Exception as e:
logger.exception('oauth2_access_token 异常 club_id=%s', club_id)
return {'errmsg': f'请求微信接口异常: {str(e)}'}
def jscode2session(code, club_id=None, app_id=None):
"""
按俱乐部微信配置换取 openid。