|
|
|
@ -46,23 +46,30 @@ class TextXMLParser(XMLParser): |
|
|
|
|
def reply_login_msg(rec_msg, to_user, from_user): |
|
|
|
|
content = f'还未绑定用户,请通过手机或者邮箱登录账户之后进行绑定' |
|
|
|
|
u_data_id = -1 |
|
|
|
|
wx_user_obj = ThirdWeChatUserInfo.objects.filter(openid=to_user, enable_login=True).first() |
|
|
|
|
ids = [] |
|
|
|
|
wx_user_obj_queryset = ThirdWeChatUserInfo.objects.filter(openid=to_user, enable_login=True).all() |
|
|
|
|
|
|
|
|
|
wx_ticket_info = get_wx_ticket_login_info_cache(rec_msg.Ticket) |
|
|
|
|
|
|
|
|
|
if wx_user_obj: |
|
|
|
|
u_data_id = wx_user_obj.user_id.pk |
|
|
|
|
content = f'用户 {wx_user_obj.user_id.first_name} 登录成功' |
|
|
|
|
WxTemplateMsg(to_user, wx_user_obj.nickname).login_success_msg(wx_user_obj.user_id.first_name) |
|
|
|
|
else: |
|
|
|
|
wx_user_info = update_or_create_wx_userinfo(to_user) |
|
|
|
|
WxTemplateMsg(to_user, wx_user_info.get('nickname', '')).login_failed_msg() |
|
|
|
|
|
|
|
|
|
wx_user_count = wx_user_obj_queryset.count() |
|
|
|
|
if 0 <= wx_user_count <= 1: |
|
|
|
|
wx_user_obj = wx_user_obj_queryset.first() |
|
|
|
|
if wx_user_obj: |
|
|
|
|
u_data_id = wx_user_obj.user_id.pk |
|
|
|
|
content = f'用户 {wx_user_obj.user_id.first_name} 登录成功' |
|
|
|
|
WxTemplateMsg(to_user, wx_user_obj.nickname).login_success_msg(wx_user_obj.user_id.first_name) |
|
|
|
|
else: |
|
|
|
|
wx_user_info = update_or_create_wx_userinfo(to_user) |
|
|
|
|
WxTemplateMsg(to_user, wx_user_info.get('nickname', '')).login_failed_msg() |
|
|
|
|
elif wx_user_count > 1: |
|
|
|
|
ids = [x['user_id__id'] for x in wx_user_obj_queryset.values('user_id__id')] |
|
|
|
|
u_data_id = wx_user_obj_queryset.first().user_id.pk |
|
|
|
|
content = f'您刚才进行了扫码登录,多用户登录中' |
|
|
|
|
if wx_ticket_info and wx_ticket_info.get('ip_addr'): |
|
|
|
|
ip_addr = wx_ticket_info.get('ip_addr') |
|
|
|
|
logger.info(f"{content} ip:{ip_addr}") |
|
|
|
|
|
|
|
|
|
set_wx_ticket_login_info_cache(rec_msg.Ticket, {'pk': u_data_id, 'to_user': to_user}) |
|
|
|
|
set_wx_ticket_login_info_cache(rec_msg.Ticket, {'pk': u_data_id, 'to_user': to_user, "ids": ids}) |
|
|
|
|
reply_msg = reply.TextMsg(to_user, from_user, content) |
|
|
|
|
return reply_msg.send() |
|
|
|
|
|
|
|
|
@ -93,8 +100,9 @@ def update_or_create_wx_userinfo(to_user, user_obj=None, w_type=''): |
|
|
|
|
def wx_bind_utils(rec_msg, to_user, from_user, content): |
|
|
|
|
w_type = rec_msg.Eventkey.split('.')[-1] |
|
|
|
|
uid = rec_msg.Eventkey.split('.')[-2] |
|
|
|
|
wx_user_obj = ThirdWeChatUserInfo.objects.filter(openid=to_user).first() |
|
|
|
|
user_obj = UserInfo.objects.filter(uid=uid).first() |
|
|
|
|
wx_user_obj = ThirdWeChatUserInfo.objects.filter(openid=to_user, user_id__uid=uid).first() |
|
|
|
|
|
|
|
|
|
if wx_user_obj: |
|
|
|
|
wx_template_msg_obj = WxTemplateMsg(to_user, wx_user_obj.nickname) |
|
|
|
|
if user_obj and user_obj.uid == wx_user_obj.user_id.uid: |
|
|
|
@ -133,11 +141,33 @@ class ValidWxChatToken(APIView): |
|
|
|
|
if ret == 0: |
|
|
|
|
content = '欢迎使用fly应用分发平台,感谢您的关注' |
|
|
|
|
rec_msg = receive.parse_xml(encrypt_xml) |
|
|
|
|
logger.info(f"code:{ret}, parse_xml result {rec_msg.__dict__ if rec_msg else rec_msg}") |
|
|
|
|
if isinstance(rec_msg, receive.Msg): |
|
|
|
|
to_user = rec_msg.FromUserName |
|
|
|
|
from_user = rec_msg.ToUserName |
|
|
|
|
|
|
|
|
|
if rec_msg.MsgType == 'text': |
|
|
|
|
msg = rec_msg.Content.decode('utf-8') |
|
|
|
|
if msg and (msg.startswith('解除登录绑定') or msg.startswith('解除消息绑定')): |
|
|
|
|
msg_list = msg.split(' ') |
|
|
|
|
if len(msg_list) == 2: |
|
|
|
|
uid = msg_list[-1] |
|
|
|
|
wx_user_obj = ThirdWeChatUserInfo.objects.filter(openid=to_user, user_id__uid=uid).first() |
|
|
|
|
data = {} |
|
|
|
|
w_msg = [] |
|
|
|
|
if msg.startswith('解除登录绑定'): |
|
|
|
|
data.update({'enable_login': False}) |
|
|
|
|
w_msg.append('扫码登录') |
|
|
|
|
if msg.startswith('解除消息绑定'): |
|
|
|
|
w_msg.append('消息推送') |
|
|
|
|
data.update({'enable_notify': False}) |
|
|
|
|
if data: |
|
|
|
|
ThirdWeChatUserInfo.objects.filter(openid=to_user, user_id__uid=uid).update(**data) |
|
|
|
|
WxTemplateMsg(to_user, wx_user_obj.nickname).unbind_success_msg( |
|
|
|
|
wx_user_obj.user_id.first_name, f"{' '.join(w_msg)} 解绑成功", |
|
|
|
|
"如需重新绑定,请登陆平台,进行绑定。感谢您的关注") |
|
|
|
|
return HttpResponse("success") |
|
|
|
|
|
|
|
|
|
content = random.choices([*GOOD_XX, content, rec_msg.Content.decode('utf-8')])[0] |
|
|
|
|
reply_msg = reply.TextMsg(to_user, from_user, content) |
|
|
|
|
result = reply_msg.send() |
|
|
|
@ -165,56 +195,85 @@ class ValidWxChatToken(APIView): |
|
|
|
|
result = reply_msg.send() |
|
|
|
|
logger.info(f"replay msg: {result}") |
|
|
|
|
return HttpResponse(result) |
|
|
|
|
elif rec_msg.Eventkey in ['query_bind', 'unbind']: |
|
|
|
|
wx_user_obj = ThirdWeChatUserInfo.objects.filter(openid=to_user).filter( |
|
|
|
|
Q(enable_login=True) | Q(enable_notify=True)).first() |
|
|
|
|
elif rec_msg.Eventkey in ['query_bind', 'unbind', 'unbind_all']: |
|
|
|
|
wx_user_obj_queryset = ThirdWeChatUserInfo.objects.filter(openid=to_user).filter( |
|
|
|
|
Q(enable_login=True) | Q(enable_notify=True)).all() |
|
|
|
|
if rec_msg.Eventkey == 'query_bind': |
|
|
|
|
if wx_user_obj: |
|
|
|
|
user_obj = wx_user_obj.user_id |
|
|
|
|
content = f'绑定用户 {user_obj.first_name} ' |
|
|
|
|
if user_obj.email: |
|
|
|
|
content += f' 登录邮箱:{user_obj.email} ' |
|
|
|
|
if user_obj.mobile: |
|
|
|
|
content += f' 登录手机:{user_obj.mobile}' |
|
|
|
|
user_cert_obj = UserCertificationInfo.objects.filter(user_id=user_obj, |
|
|
|
|
status=1).first() |
|
|
|
|
if user_cert_obj: |
|
|
|
|
name = user_cert_obj.name |
|
|
|
|
else: |
|
|
|
|
name = user_obj.first_name |
|
|
|
|
description = f'绑定了' |
|
|
|
|
if wx_user_obj.enable_login: |
|
|
|
|
description += " 微信登录 " |
|
|
|
|
if wx_user_obj.enable_notify: |
|
|
|
|
description += " 消息通知 " |
|
|
|
|
description += "功能。" |
|
|
|
|
|
|
|
|
|
WxTemplateMsg(to_user, wx_user_obj.nickname).bind_query_success_msg(user_obj.first_name, |
|
|
|
|
name, |
|
|
|
|
user_obj.mobile, |
|
|
|
|
user_obj.email, |
|
|
|
|
description) |
|
|
|
|
else: |
|
|
|
|
content = '暂无登录绑定信息' |
|
|
|
|
for wx_user_obj in wx_user_obj_queryset: |
|
|
|
|
if wx_user_obj: |
|
|
|
|
user_obj = wx_user_obj.user_id |
|
|
|
|
content = f'绑定用户 {user_obj.first_name} ' |
|
|
|
|
if user_obj.email: |
|
|
|
|
content += f' 登录邮箱:{user_obj.email} ' |
|
|
|
|
if user_obj.mobile: |
|
|
|
|
content += f' 登录手机:{user_obj.mobile}' |
|
|
|
|
user_cert_obj = UserCertificationInfo.objects.filter(user_id=user_obj, |
|
|
|
|
status=1).first() |
|
|
|
|
if user_cert_obj: |
|
|
|
|
name = user_cert_obj.name |
|
|
|
|
else: |
|
|
|
|
name = user_obj.first_name |
|
|
|
|
description = f'绑定了' |
|
|
|
|
if wx_user_obj.enable_login: |
|
|
|
|
description += " 微信登录 " |
|
|
|
|
if wx_user_obj.enable_notify: |
|
|
|
|
description += " 消息通知 " |
|
|
|
|
description += "功能。" |
|
|
|
|
|
|
|
|
|
WxTemplateMsg(to_user, wx_user_obj.nickname).bind_query_success_msg( |
|
|
|
|
user_obj.first_name, |
|
|
|
|
name, |
|
|
|
|
user_obj.mobile, |
|
|
|
|
user_obj.email, |
|
|
|
|
description) |
|
|
|
|
if wx_user_obj_queryset.count() == 0: |
|
|
|
|
content = '暂无绑定信息' |
|
|
|
|
wx_user_info = update_or_create_wx_userinfo(to_user) |
|
|
|
|
WxTemplateMsg(to_user, wx_user_info.get('nickname')).query_bind_info_failed_msg( |
|
|
|
|
"查询登录绑定", content) |
|
|
|
|
|
|
|
|
|
elif rec_msg.Eventkey == 'unbind': |
|
|
|
|
if wx_user_obj: |
|
|
|
|
content = f'解绑用户 {wx_user_obj.user_id.first_name} 成功' |
|
|
|
|
WxTemplateMsg(to_user, wx_user_obj.nickname).unbind_success_msg( |
|
|
|
|
wx_user_obj.user_id.first_name) |
|
|
|
|
ThirdWeChatUserInfo.objects.filter(openid=to_user).delete() |
|
|
|
|
else: |
|
|
|
|
content = f'暂无登录绑定信息' |
|
|
|
|
"查询绑定", content) |
|
|
|
|
|
|
|
|
|
elif rec_msg.Eventkey == 'unbind_all': |
|
|
|
|
for wx_user_obj in wx_user_obj_queryset: |
|
|
|
|
if wx_user_obj: |
|
|
|
|
content = f'解绑用户 {wx_user_obj.user_id.first_name} 成功' |
|
|
|
|
WxTemplateMsg(to_user, wx_user_obj.nickname).unbind_success_msg( |
|
|
|
|
wx_user_obj.user_id.first_name, |
|
|
|
|
"解除绑定成功,您将无法使用微信扫描登录平台和微信消息推送功能", |
|
|
|
|
"如需重新绑定,请登陆平台,在个人资料进行登录绑定。感谢您的关注") |
|
|
|
|
ThirdWeChatUserInfo.objects.filter(openid=to_user).update(enable_login=False, |
|
|
|
|
enable_notify=False) |
|
|
|
|
if wx_user_obj_queryset.count() == 0: |
|
|
|
|
content = f'暂无绑定信息' |
|
|
|
|
wx_user_info = update_or_create_wx_userinfo(to_user) |
|
|
|
|
WxTemplateMsg(to_user, wx_user_info.get('nickname')).query_bind_info_failed_msg( |
|
|
|
|
"解除登录绑定", content) |
|
|
|
|
|
|
|
|
|
"解除绑定", content) |
|
|
|
|
elif rec_msg.Eventkey == 'unbind': |
|
|
|
|
if wx_user_obj_queryset.count() == 0: |
|
|
|
|
content = f'暂无绑定信息' |
|
|
|
|
else: |
|
|
|
|
content = '' |
|
|
|
|
for wx_user_obj in wx_user_obj_queryset: |
|
|
|
|
user_obj = wx_user_obj.user_id |
|
|
|
|
user_content = f'用户昵称 {user_obj.first_name} ' |
|
|
|
|
if user_obj.email: |
|
|
|
|
user_content += f' 登录邮箱:{user_obj.email} ' |
|
|
|
|
if user_obj.mobile: |
|
|
|
|
user_content += f' 登录手机:{user_obj.mobile}' |
|
|
|
|
|
|
|
|
|
content += f'♥{user_content}♥\n' |
|
|
|
|
if wx_user_obj.enable_login: |
|
|
|
|
login_info = f'解除登录绑定 {user_obj.uid}' |
|
|
|
|
content += f'<a href="weixin://bizmsgmenu?msgmenuid=1&msgmenucontent={login_info}">【解除登录绑定】</a>' |
|
|
|
|
if wx_user_obj.enable_notify: |
|
|
|
|
notify_info = f'解除消息绑定 {user_obj.uid}' |
|
|
|
|
content += f'<a href="weixin://bizmsgmenu?msgmenuid=1&msgmenucontent={notify_info}">【解除消息绑定】</a>' |
|
|
|
|
content += '\n\n' |
|
|
|
|
content += '请点击您要解除绑定的信息\n' |
|
|
|
|
logger.info(f"to_user:{to_user} from_user:{from_user} reply msg: {content}") |
|
|
|
|
reply_msg = reply.TextMsg(to_user, from_user, content) |
|
|
|
|
result = reply_msg.send() |
|
|
|
|
if rec_msg.Eventkey == 'unbind': |
|
|
|
|
return HttpResponse(result) |
|
|
|
|
|
|
|
|
|
elif rec_msg.Event in ['subscribe', 'unsubscribe']: # 订阅 |
|
|
|
|
reply_msg = reply.TextMsg(to_user, from_user, content) |
|
|
|
@ -235,6 +294,8 @@ class ValidWxChatToken(APIView): |
|
|
|
|
else: |
|
|
|
|
logger.error('密文解密失败') |
|
|
|
|
logger.info(f"replay msg: {result}") |
|
|
|
|
# 直接回复修改为微信模板消息回复 |
|
|
|
|
# return HttpResponse(result) |
|
|
|
|
return HttpResponse("success") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|