Bläddra i källkod

登出开启事务

locky 2 år sedan
förälder
incheckning
3407a218bb
1 ändrade filer med 37 tillägg och 29 borttagningar
  1. 37 29
      Controller/UserController.py

+ 37 - 29
Controller/UserController.py

@@ -12,6 +12,7 @@ import simplejson
 import simplejson as json
 from PIL import Image, ImageDraw, ImageFont
 from django.contrib.auth.hashers import make_password, check_password  # 对密码加密模块
+from django.db import transaction
 from django.db.models import Q
 from django.http import HttpResponseRedirect
 from django.shortcuts import HttpResponse
@@ -311,16 +312,18 @@ class LogoutView(TemplateView):
         if tko.code != 0:
             return response.json(tko.code)
 
-        Device_User.objects.filter(userID=tko.userID).update(online=False)
-        redisObj = RedisObject(db=3)
-        redisObj.del_data(key=tko.userID)
-        Device_Info.objects.filter(userID=tko.userID).update(NotificationMode=0)
         m_code = request_dict.get('m_code', None)
-        if m_code:
-            userID = tko.userID
-            UidPushModel.objects.filter(userID_id=userID, m_code=m_code).delete()
-            GatewayPush.objects.filter(user_id=userID, m_code=m_code).update(logout=True)
-        return response.json(0)
+        try:
+            with transaction.atomic():
+                Device_User.objects.filter(userID=tko.userID).update(online=False)
+                Device_Info.objects.filter(userID=tko.userID).update(NotificationMode=0)
+                if m_code:
+                    userID = tko.userID
+                    UidPushModel.objects.filter(userID_id=userID, m_code=m_code).delete()
+                    GatewayPush.objects.filter(user_id=userID, m_code=m_code).update(logout=True)
+            return response.json(0)
+        except Exception as e:
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
 
 # 修改密码
@@ -425,6 +428,7 @@ class v3ChangePwdView(TemplateView):
                         newPwd = newPwd.decode('utf-8')
                         newPwd = newPwd[3:-3]
         except Exception as e:
+            print(repr(e))
             return response.json(111)
         else:
             if oldPwd is None or newPwd is None or len(newPwd) < 6:
@@ -531,8 +535,7 @@ class ForgetPwdView(TemplateView):
         return self.ValidationError(userName, response)
 
     def ValidationError(self, userName, response):
-        # return response.json(475)
-        if userName != None:
+        if userName is not None:
             userName = userName.strip()
             return self.ForgetPwd(userName, response)
         else:
@@ -669,8 +672,7 @@ class refreshTokenView(TemplateView):
                         }
                         user_ex_qs.update(**update_dict)
                 except Exception as e:
-                    pass
-                # # #
+                    print(repr(e))
                 return response.json(0, res)
             else:
                 return response.json(tko.code)
@@ -1074,7 +1076,7 @@ class v2registerView(TemplateView):
             if number:
                 create_data["region_country"] = number
 
-            users = Device_User.objects.create(**create_data)
+            Device_User.objects.create(**create_data)
         except Exception as e:
             errorInfo = traceback.format_exc()
             print(errorInfo)
@@ -1148,7 +1150,7 @@ class v2registerView(TemplateView):
             }
             if number:
                 create_data["region_country"] = number
-            users = Device_User.objects.create(**create_data)
+            Device_User.objects.create(**create_data)
         except Exception as e:
             errorInfo = traceback.format_exc()
             print(errorInfo)
@@ -1220,6 +1222,7 @@ class v3registerView(TemplateView):
                         password = password[3:-3]
                 print(password)
         except Exception as e:
+            print(repr(e))
             return response.json(111)
         try:
             for i in range(1, 4):
@@ -1235,8 +1238,8 @@ class v3registerView(TemplateView):
                     authcode = base64.b64decode(authcode)
                     authcode = authcode.decode('utf-8')
                     authcode = authcode[3:-3]
-            print(authcode)
         except Exception as e:
+            print(repr(e))
             return response.json(121)
         else:
             if password is None:
@@ -1798,8 +1801,8 @@ class v3resetPwdByCodeView(TemplateView):
                         password = base64.b64decode(password)
                         password = password.decode('utf-8')
                         password = password[3:-3]
-                print(password)
         except Exception as e:
+            print(repr(e))
             return response.json(111)
         try:
             for i in range(1, 4):
@@ -1815,8 +1818,8 @@ class v3resetPwdByCodeView(TemplateView):
                     authcode = base64.b64decode(authcode)
                     authcode = authcode.decode('utf-8')
                     authcode = authcode[3:-3]
-            print(authcode)
         except Exception as e:
+            print(repr(e))
             return response.json(121)
         if phone is not None:
             phone = phone.strip()
@@ -2166,6 +2169,7 @@ class v3LoginView(TemplateView):
                         # 去前3位,后3位
                         password = password[3:-3]
         except Exception as e:
+            print(repr(e))
             return response.json(111)
         else:
             data_valid = DataValid()
@@ -2656,8 +2660,8 @@ class wxAuthSignView(TemplateView):
                 appid = app_config[appBundleID]['appid']
                 secret = app_config[appBundleID]['secret']
                 # 获取access_token请求
-                at_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={secret}&code={code}&grant_type=authorization_code'.format(
-                    appid=appid, secret=secret, code=grant_code)
+                at_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={secret}&code={code}' \
+                         '&grant_type=authorization_code'.format(appid=appid, secret=secret, code=grant_code)
                 res_req = requests.get(url=at_url)
                 res_json = res_req.json()
                 print(res_json)
@@ -2667,8 +2671,8 @@ class wxAuthSignView(TemplateView):
                 access_token = res_json['access_token']
                 openid = res_json['openid']
                 if access_token and openid:
-                    info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token={access_token}&openid={openid}'.format(
-                        access_token=access_token, openid=openid)
+                    info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token={access_token}&openid={openid}'.\
+                        format(access_token=access_token, openid=openid)
                     res_req = requests.get(url=info_url)
                     res_req.encoding = res_req.apparent_encoding
                     res_json = res_req.json()
@@ -2807,8 +2811,9 @@ class wxPerfectView(TemplateView):
                     appid = app_config[appBundleID]['appid']
                     secret = app_config[appBundleID]['secret']
                     # 获取access_token请求
-                    at_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={secret}&code={code}&grant_type=authorization_code'.format(
-                        appid=appid, secret=secret, code=grant_code)
+                    at_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={secret}&' \
+                             'code={code}&grant_type=authorization_code'.\
+                        format(appid=appid, secret=secret, code=grant_code)
                     res_req = requests.get(url=at_url)
                     res_json = res_req.json()
                     print(res_json)
@@ -2819,8 +2824,8 @@ class wxPerfectView(TemplateView):
                     openid = res_json['openid']
 
                     if access_token and openid:
-                        info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token={access_token}&openid={openid}'.format(
-                            access_token=access_token, openid=openid)
+                        info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token={access_token}&' \
+                                   'openid={openid}'.format(access_token=access_token, openid=openid)
                         res_req = requests.get(url=info_url)
                         res_req.encoding = res_req.apparent_encoding
                         res_json = res_req.json()
@@ -3472,7 +3477,7 @@ class V2LogoutView(TemplateView):
                 try:
                     UidPushModel.objects.filter(userID_id=userID, m_code=m_code).delete()
                 except Exception as e:
-                    pass
+                    print(repr(e))
                 else:
                     pass
             os_type = request_dict.get('os_type', None)
@@ -3650,6 +3655,7 @@ class Image_Code_RegisterView(TemplateView):
                         password = password[3:-3]
                 password = make_password(password)
         except Exception as e:
+            print(repr(e))
             return response.json(111)
         try:
             for i in range(1, 4):
@@ -3666,6 +3672,7 @@ class Image_Code_RegisterView(TemplateView):
                     valid_code = valid_code.decode('utf-8')
                     valid_code = valid_code[3:-3]
         except Exception as e:
+            print(repr(e))
             return response.json(121)
         if not userEmail:
             return response.json(105)
@@ -3850,7 +3857,7 @@ class loginCodeView(View):
 
     def validate(self, request_dict, response):
         phone = request_dict.get('phone', None)
-        country_code = request_dict.get('country_code', None)
+        request_dict.get('country_code', None)
         sign_name = request_dict.get('sign_name', None)
 
         if phone and sign_name:
@@ -4258,7 +4265,7 @@ class LocalUserView(View):
         request_dict = request.POST
         operation = kwargs.get('operation', None)
         print('start_time=' + str((time.time())))
-        ip = CommonService.get_ip_address(request)
+        CommonService.get_ip_address(request)
         print('end_time=' + str((time.time())))
         return self.validate(request_dict, operation)
 
@@ -4461,6 +4468,7 @@ def deleteAccount(request):
                     # 去前3位,后3位
                     password = password[3:-3]
     except Exception as e:
+        print(repr(e))
         return response.json(111)
     else:
         if token is None: