diff --git a/jituan/services/club_user.py b/jituan/services/club_user.py index fb0f210..9c2ffcd 100644 --- a/jituan/services/club_user.py +++ b/jituan/services/club_user.py @@ -151,10 +151,17 @@ def ensure_user_club_id(user, club_id): user.save(update_fields=['ClubID']) +def _looks_like_wx_openid(value): + """微信 openid 常见形态:以 o 开头、长度约 28。""" + v = (value or '').strip() + return len(v) >= 20 and v.startswith('o') + + def get_wx_openid_for_user(user, club_id=None): """ 按俱乐部取微信支付 JSAPI 用 openid。 - 多俱乐部用户 openid 在 user_wx_openid;xq 老用户可回落 User.OpenID。 + 多俱乐部用户 openid 在 user_wx_openid;xq 老用户可回落 User.OpenID; + 星之界等俱乐部用户常 UserName=openid 且 OpenID 为空,再回落 UserName。 """ if not user: return '' @@ -165,7 +172,12 @@ def get_wx_openid_for_user(user, club_id=None): if binding and binding.openid: return binding.openid legacy = getattr(user, 'OpenID', None) or getattr(user, 'openid', None) - return legacy or '' + if legacy: + return legacy.strip() + username = (getattr(user, 'UserName', None) or '').strip() + if _looks_like_wx_openid(username): + return username + return '' def get_payment_openid(request, user=None, club_id=None): diff --git a/users/tixian_shenhe_services.py b/users/tixian_shenhe_services.py index acb7e74..753493e 100644 --- a/users/tixian_shenhe_services.py +++ b/users/tixian_shenhe_services.py @@ -253,13 +253,19 @@ def validate_payment_info_for_withdraw(user_main): return True, '' +def get_auto_withdraw_openid(user_main): + """自动提现打款用 openid:兼容 user_wx_openid / User.OpenID / UserName(openid)。""" + from jituan.services.club_user import get_wx_openid_for_user + club_id = club_id_for_tixian_yonghuid(user_main.UserUID) + return (get_wx_openid_for_user(user_main, club_id) or '').strip() + + def validate_auto_withdraw_prerequisites(user_main): """ 自动提现(zddksh / tixiansq):微信企业付款到用户 OpenID。 - 不要求手机号、收款码、收款账号;须已绑定微信 OpenID。 + 不要求手机号、收款码、收款账号;须能解析到微信 OpenID。 """ - openid = (getattr(user_main, 'OpenID', None) or '').strip() - if not openid: + if not get_auto_withdraw_openid(user_main): return False, '请先使用微信登录后再申请自动提现' return True, '' diff --git a/users/tixian_shenhe_views.py b/users/tixian_shenhe_views.py index b6d9602..0bf8abb 100644 --- a/users/tixian_shenhe_views.py +++ b/users/tixian_shenhe_views.py @@ -201,13 +201,14 @@ def process_audit_collect(request): ) try: - if not user_main.OpenID: - return Response({'code': 10, 'msg': '用户未绑定微信,无法收款'}) - + from users.tixian_shenhe_services import get_auto_withdraw_openid from jituan.services.club_user import get_user_club_id from jituan.services.wechat_pay import get_wechat_v3_config payout_club_id = get_user_club_id(user_main) + payout_openid = get_auto_withdraw_openid(user_main) + if not payout_openid: + return Response({'code': 10, 'msg': '用户未绑定微信,无法收款'}) wx_cfg = get_wechat_v3_config(payout_club_id) if not wx_cfg.get('APPID') or not wx_cfg.get('MCHID') or not wx_cfg.get('TRANSFER_SCENE_ID'): logger.error('微信打款配置不完整 club=%s APPID=%s MCHID=%s', payout_club_id, wx_cfg.get('APPID'), wx_cfg.get('MCHID')) @@ -317,7 +318,7 @@ def process_audit_collect(request): audit.save(update_fields=['tixian_auto_id', 'UpdateTime']) body = _build_wx_transfer_body( - wx_cfg, tixian_id, user_main.OpenID, yonghuid, leixing, shijidaozhang, + wx_cfg, tixian_id, payout_openid, yonghuid, leixing, shijidaozhang, ) try: