@ -12,10 +12,10 @@ from rest_framework.response import Response
from rest_framework . views import APIView
from rest_framework_xml . parsers import XMLParser
from api . models import ThirdWeChatUserInfo , UserInfo , UserCertificationInfo
from api . utils . modelutils import PageNumber
from api . models import ThirdWeChatUserInfo , UserInfo , UserCertificationInfo , WeChatInfo
from api . utils . modelutils import PageNumber , get_wx_nickname
from api . utils . response import BaseResponse
from api . utils . serializer import ThirdWx Serializer
from api . utils . serializer import WeixinInfo Serializer
from api . views . login import get_login_type
from common . core . auth import ExpiringTokenAuthentication
from common . libs . mp . chat import reply , receive
@ -47,7 +47,7 @@ def reply_login_msg(rec_msg, to_user, from_user):
content = f ' 还未绑定用户,请通过手机或者邮箱登录账户之后进行绑定 '
u_data_id = - 1
ids = [ ]
wx_user_obj_queryset = ThirdWeChatUserInfo . objects . filter ( openid = to_user , enable_login = True ) . all ( )
wx_user_obj_queryset = ThirdWeChatUserInfo . objects . filter ( weixin__ openid= to_user , enable_login = True ) . all ( )
wx_ticket_info = get_wx_ticket_login_info_cache ( rec_msg . Ticket )
@ -55,12 +55,13 @@ def reply_login_msg(rec_msg, to_user, from_user):
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 ( )
user_obj = wx_user_obj . user_id
u_data_id = user_obj . pk
content = f ' 用户 { user_obj . first_name } 登录成功 '
WxTemplateMsg ( to_user , get_wx_nickname ( to_user ) ) . login_success_msg ( user_obj . 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
@ -75,36 +76,37 @@ def reply_login_msg(rec_msg, to_user, from_user):
def update_or_create_wx_userinfo ( to_user , user_obj = None , w_type = ' ' ) :
code , wx_user_ info = get_userinfo_from_openid ( to_user )
code , wx_info = get_userinfo_from_openid ( to_user )
logger . info ( f " get openid: { to_user } info: { to_user } code: { code } " )
if code :
wx_user_ info = {
' openid ' : wx_user_ info . get ( ' openid ' ) ,
wx_info = {
' openid ' : wx_info . get ( ' openid ' ) ,
# 'nickname': wx_user_info.get('nickname'), # 最新微信接口已经取消该字段
# 'sex': wx_user_info.get('sex'), # 最新微信接口已经取消该字段
' subscribe_time ' : wx_user_ info . get ( ' subscribe_time ' ) ,
' subscribe_time ' : wx_info . get ( ' subscribe_time ' ) ,
# 'head_img_url': wx_user_info.get('headimgurl'), # 最新微信接口已经取消该字段
' address ' : f " { wx_user_ info . get ( ' country ' ) } - { wx_user_ info . get ( ' province ' ) } - { wx_user _info . get ( ' city ' ) } " ,
' subscribe ' : wx_user_ info . get ( ' subscribe ' ) ,
' address ' : f " { wx_info . get ( ' country ' ) } - { wx_info . get ( ' province ' ) } - { wx_info . get ( ' city ' ) } " ,
' subscribe ' : wx_info . get ( ' subscribe ' ) ,
}
if user_obj :
wx_obj , _ = WeChatInfo . objects . update_or_create ( openid = to_user , defaults = wx_info )
wx_user_info = { ' weixin ' : wx_obj }
if w_type == ' login ' :
wx_user_info [ ' enable_login ' ] = True
if w_type == ' notify ' :
wx_user_info [ ' enable_notify ' ] = True
ThirdWeChatUserInfo . objects . update_or_create ( user_id = user_obj , openid = to_user , defaults = wx_user_info )
return wx_user_info
ThirdWeChatUserInfo . objects . update_or_create ( user_id = user_obj , weixin = wx_obj , defaults = wx_user_info )
return wx_obj
def wx_bind_utils ( rec_msg , to_user , from_user , content ) :
w_type = rec_msg . Eventkey . split ( ' . ' ) [ - 1 ]
uid = rec_msg . Eventkey . split ( ' . ' ) [ - 2 ]
user_obj = UserInfo . objects . filter ( uid = uid ) . first ( )
wx_user_obj = ThirdWeChatUserInfo . objects . filter ( openid = to_user , user_id__uid = uid ) . first ( )
wx_user_obj = ThirdWeChatUserInfo . objects . filter ( weixin__ openid= to_user , user_id__uid = uid ) . first ( )
if wx_user_obj :
wx_template_msg_obj = WxTemplateMsg ( to_user , wx_user_obj . nickname )
wx_template_msg_obj = WxTemplateMsg ( to_user , get_wx_nickname ( to_user ) )
if user_obj and user_obj . uid == wx_user_obj . user_id . uid :
content = f ' 账户 { wx_user_obj . user_id . first_name } 已经绑定成功,感谢您的使用 '
update_or_create_wx_userinfo ( to_user , user_obj , w_type )
@ -114,9 +116,9 @@ def wx_bind_utils(rec_msg, to_user, from_user, content):
wx_template_msg_obj . bind_failed_msg ( content )
else :
if user_obj :
wx_user_info = update_or_create_wx_userinfo ( to_user , user_obj , w_type )
update_or_create_wx_userinfo ( to_user , user_obj , w_type )
content = f ' 账户绑定 { user_obj . first_name } 成功 '
WxTemplateMsg ( to_user , wx_user_info . get ( ' nickname ' , ' ' ) ) . bind_success_msg ( user_obj . first_name )
WxTemplateMsg ( to_user , get_wx_nickname ( to_user ) ) . bind_success_msg ( user_obj . first_name )
if user_obj :
set_wx_ticket_login_info_cache ( rec_msg . Ticket , { ' pk ' : user_obj . pk , ' w_type ' : w_type , ' to_user ' : to_user } )
reply_msg = reply . TextMsg ( to_user , from_user , content )
@ -152,7 +154,8 @@ class ValidWxChatToken(APIView):
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 ( )
wx_user_obj = ThirdWeChatUserInfo . objects . filter ( weixin__openid = to_user ,
user_id__uid = uid ) . first ( )
data = { }
w_msg = [ ]
if msg . startswith ( ' 解除登录绑定 ' ) :
@ -162,14 +165,15 @@ class ValidWxChatToken(APIView):
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 (
ThirdWeChatUserInfo . objects . filter ( weixin__openid = to_user , user_id__uid = uid ) . update (
* * data )
WxTemplateMsg ( to_user , get_wx_nickname ( to_user ) ) . 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 )
reply_msg = reply . TextMsg ( to_user , from_user , f ' { get_wx_nickname ( to_user ) } ♥ { content } ' )
result = reply_msg . send ( )
elif rec_msg . MsgType == ' image ' :
@ -186,7 +190,7 @@ class ValidWxChatToken(APIView):
if rec_msg . Event == ' CLICK ' : # 公众号点击事件
if rec_msg . Eventkey == ' good ' :
content = random . choices ( GOOD_XX ) [ 0 ]
reply_msg = reply . TextMsg ( to_user , from_user , content )
reply_msg = reply . TextMsg ( to_user , from_user , f ' { get_wx_nickname ( to_user ) } ♥ { content } ' )
result = reply_msg . send ( )
logger . info ( f " replay msg: { result } " )
return HttpResponse ( result )
@ -196,7 +200,7 @@ class ValidWxChatToken(APIView):
logger . info ( f " replay msg: { result } " )
return HttpResponse ( result )
elif rec_msg . Eventkey in [ ' query_bind ' , ' unbind ' , ' unbind_all ' ] :
wx_user_obj_queryset = ThirdWeChatUserInfo . objects . filter ( openid = to_user ) . filter (
wx_user_obj_queryset = ThirdWeChatUserInfo . objects . filter ( weixin__ openid= to_user ) . filter (
Q ( enable_login = True ) | Q ( enable_notify = True ) ) . all ( )
if rec_msg . Eventkey == ' query_bind ' :
for wx_user_obj in wx_user_obj_queryset :
@ -220,33 +224,30 @@ class ValidWxChatToken(APIView):
description + = " 消息通知 "
description + = " 功能。 "
WxTemplateMsg ( to_user , wx_user_obj . nickname ) . bind_query_success_msg (
WxTemplateMsg ( to_user , get_wx_nickname ( to_user ) ) . 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 )
WxTemplateMsg ( to_user , get_wx_nickname ( to_user ) ) . query_bind_info_failed_msg (
" 查询绑定 " , ' 暂无绑定信息 ' )
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 (
WxTemplateMsg ( to_user , get_wx_nickname ( to_user ) ) . unbind_success_msg (
wx_user_obj . user_id . first_name ,
" 解除绑定成功,您将无法使用微信扫描登录平台和微信消息推送功能 " ,
" 如需重新绑定,请登陆平台,在个人资料进行登录绑定。感谢您的关注 " )
ThirdWeChatUserInfo . objects . filter ( openid = to_user ) . update ( enable_login = False ,
enable_notify = False )
ThirdWeChatUserInfo . objects . filter ( weixin__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 )
WxTemplateMsg ( to_user , get_wx_nickname ( to_user ) ) . query_bind_info_failed_msg (
" 解除绑定 " , ' 暂无绑定信息 ' )
elif rec_msg . Eventkey == ' unbind ' :
if wx_user_obj_queryset . count ( ) == 0 :
content = f ' 暂无绑定信息 '
@ -284,7 +285,7 @@ class ValidWxChatToken(APIView):
elif rec_msg . Eventkey . startswith ( ' qrscene_web.bind. ' ) :
result = wx_bind_utils ( rec_msg , to_user , from_user , content )
if rec_msg . Event == ' unsubscribe ' :
Third WeChatUser Info. objects . filter ( openid = to_user ) . update ( subscribe = False )
WeChatInfo . objects . filter ( openid = to_user ) . update ( subscribe = False )
elif rec_msg . Event == ' SCAN ' :
if rec_msg . Eventkey == ' web.login ' : # 已经关注,然后再次扫码,登录认证操作
@ -305,12 +306,13 @@ class ThirdWxAccount(APIView):
def get ( self , request ) :
res = BaseResponse ( )
if get_login_type ( ) . get ( ' third ' , ' ' ) . get ( ' wxp ' ) :
wx_obj_lists = ThirdWeChatUserInfo . objects . filter ( user_id = request . user , enable_login = True )
wx_obj_lists = WeChatInfo . objects . filter ( thirdwechatuserinfo__user_id = request . user ,
thirdwechatuserinfo__enable_login = True )
page_obj = PageNumber ( )
info_serializer = page_obj . paginate_queryset ( queryset = wx_obj_lists . order_by ( " -subscribe_time " ) ,
request = request ,
view = self )
wx_user_info = ThirdWx Serializer( info_serializer , many = True , )
wx_user_info = WeixinInfo Serializer( info_serializer , many = True , )
res . data = wx_user_info . data
res . count = wx_obj_lists . count ( )
return Response ( res . dict )
@ -320,7 +322,8 @@ class ThirdWxAccount(APIView):
openid = data . get ( " openid " )
if get_login_type ( ) . get ( ' third ' , ' ' ) . get ( ' wxp ' ) and openid :
if request . user . check_password ( data . get ( ' confirm_pwd ' , ' ' ) ) :
ThirdWeChatUserInfo . objects . filter ( user_id = request . user , openid = openid ) . update ( enable_login = False )
ThirdWeChatUserInfo . objects . filter ( user_id = request . user , weixin__openid = openid ) . update (
enable_login = False )
else :
res = BaseResponse ( )
res . code = 1001