Просмотр исходного кода

Merge remote-tracking branch 'remotes/origin/dev'

# Conflicts:
#	Ansjer/urls.py
tanghongbin 5 лет назад
Родитель
Сommit
69609a2232

+ 10 - 3
Ansjer/urls.py

@@ -9,9 +9,11 @@ from Controller import FeedBack, EquipmentOTA, EquipmentInfo, AdminManage, AppIn
     UserController, CloudVod, OrderContrller, VodBucket, DetectController, DeviceShare, UserBrandController, \
     StsOssController, UIDPreview, OssCrd, SysMsg, UidUser, EquipmentManagerV2, EquipmentManagerV3, PushDeploy, \
     AppSetController, \
-    ApplicationController, UserExController, CloudStorage, TestApi, UserBrandControllerV2
+    ApplicationController, UserExController, CloudStorage, TestApi, UserBrandControllerV2, \
+    StatisticsController
 
 urlpatterns = [
+    url(r'^testApi/(?P<operation>.*)$', TestApi.testView.as_view()),
 
     url(r'^account/authcode$', UserController.authCodeView.as_view()),
     url(r'^v3/account/generatepictureCodeView/$', UserController.generatePictureCodeView.as_view()),
@@ -120,6 +122,11 @@ urlpatterns = [
     url(r'^v2/account/logout$', UserController.V2LogoutView.as_view()),
     url(r'^v2/account/login$', UserController.v3LoginView.as_view()),
     url(r'^v3/account/login$', UserController.v3LoginView.as_view()),
+
+    # 验证码登录
+    url(r'^account/loginCode$', UserController.loginCodeView.as_view()),
+    url(r'^v3/account/loginByCode$', UserController.v3LoginByCodeView.as_view()),
+
     # 新增
     url(r'^detect/detect_group_push$',DetectController.NotificationView.detect_group_push),
     url(r'^detect/add$', DetectController.PushNotificationView.as_view()),
@@ -202,11 +209,11 @@ urlpatterns = [
     # 判断节假日接口
     url(r'^date/(?P<operation>.*)$', DateController.DateConView.as_view()),
 
-    url(r'^equipment/flow_test$', EquipmentManager.uid_status_test),
     url(r'^equipment/flow_test$', EquipmentManager.uid_status_test),
     url(r'^account/appFrequency/(?P<operation>.*)$', UserController.UserAppFrequencyView.as_view()),
     url(r'^v2/userbrand/(?P<operation>.*)$', UserBrandControllerV2.UserBrandV2.as_view()),
-    url(r'^testApi/(?P<operation>.*)$', TestApi.testView.as_view()),
+    # url(r'^statistcs/appFrequencyMonth$', StatisticsController.statistcsAppFrequency),
+    # url(r'^statistcs/appFrequencyYear$', StatisticsController.statistcsAppFrequencyYear),
     # app 设备消息模板
     # 路由加参数参考
     # url(r'^(?P<path>.*)/(?P<UID>.*)/lls$', Test.Test.as_view(), name=u'gg'),

+ 89 - 2
Controller/AdminManage.py

@@ -1,5 +1,7 @@
 # -*- coding: utf-8 -*-
-from django.db.models import Count
+import time
+
+from django.db.models import Count,Q
 from django.views.decorators.csrf import csrf_exempt
 from django.views.generic import TemplateView
 from django.utils.decorators import method_decorator
@@ -71,6 +73,10 @@ class AdminManage(TemplateView):
             return self.getAreaDeviceType(userID, response)
         if operation == 'getDeviceType':
             return self.getDeviceType(userID, response)
+        # if operation == 'getAppFrequency':
+        #     return self.getAppFrequency(userID, request_dict, response)
+        # if operation == 'getHistoryAppFrequency':
+        #     return self.getAllAppFrequency(userID, response)
 
     def resetUserPwd(self, request_dict, userID, response):
         own_permission = ModelService.check_perm(userID=userID, permID=50)
@@ -335,9 +341,89 @@ class AdminManage(TemplateView):
             data.append(us)
             quantity += us['quantity']
         model_data['quantity'] = quantity
-
         return response.json(0, res)
 
+    # def getAppFrequency(self, userID, request_dict, response):
+    #     own_permission = ModelService.check_perm(userID=userID, permID=30)
+    #     if own_permission is not True:
+    #         return response.json(404)
+    #
+    #     # 当前的年份
+    #     current_time = int(time.time())
+    #     localtime = time.localtime(current_time)
+    #     current_year = localtime.tm_year
+    #     current_month = localtime.tm_mon
+    #
+    #     start_year = end_year = current_year
+    #     end_month = current_month
+    #     start_month = 1
+    #
+    #     result = []
+    #     if end_month != 12:
+    #         start_month = end_month + 1
+    #         start_year = current_year - 1
+    #
+    #     time_struct = [start_year, start_month, 0, 0, 0, 0, 0, 0, 0]
+    #     key_formal = '{year}{month}'
+    #     for i in range(12):
+    #         result.append({
+    #             'date_time': key_formal.format(year=time_struct[0], month=str(time_struct[1]).zfill(2)),
+    #             'data': None
+    #         })
+    #
+    #         month = time_struct[1] + 1
+    #         if month > 12:
+    #             time_struct[0] = time_struct[0] + 1
+    #             time_struct[1] = 1
+    #         else:
+    #             time_struct[1] = month
+    #     #
+    #     afs_qs = {}
+    #     if start_year == end_year:
+    #         afs_qs = list(AppFrequencyStatisticsModel.objects.filter(year=start_year, month__gte=start_month, month__lte=end_month).values().order_by('-year', 'month'))
+    #     else:
+    #         afs_qs = list(AppFrequencyStatisticsModel.objects.filter(year=start_year, month__gte=start_month).values().order_by('-year', 'month'))
+    #         tmps_qs = list(AppFrequencyStatisticsModel.objects.filter(year=end_year, month__lte=end_month).values().order_by('-year', 'month'))
+    #         for tmp in tmps_qs:
+    #             afs_qs.append(tmp)
+    #
+    #     tmp_dict = {}
+    #
+    #     for afs in afs_qs:
+    #         key = key_formal.format(year=afs['year'], month=str(afs['month']).zfill(2))
+    #         tmp_dict[key] = json.loads(afs['data'])
+    #
+    #     for res in result:
+    #         if tmp_dict.__contains__(res['date_time']):
+    #             res['data'] = tmp_dict[res['date_time']]
+    #     print(result)
+    #
+    #     return response.json(0, result)
+    #
+    # def getAllAppFrequency(self, userID, response):
+    #     own_permission = ModelService.check_perm(userID=userID, permID=30)
+    #     if own_permission is not True:
+    #         return response.json(404)
+    #
+    #     # 取出请求年份的统计好的数据
+    #     print('start')
+    #     time_struct = time.localtime()
+    #     current_year = time_struct.tm_year
+    #     start_year = current_year - 5 + 1
+    #     afs_qs = AppFrequencyYearStatisticsModel.objects.filter(year__gte=start_year, year__lt=current_year).order_by(
+    #         '-year')
+    #     if afs_qs.exists():
+    #         afs_qs = afs_qs.values('id', 'data', 'num', 'year')
+    #         res = []
+    #         for afs in afs_qs:
+    #             res.append({
+    #                 'year': afs['year'],
+    #                 'data': json.loads(afs['data'])
+    #             })
+    #         return response.json(0, res)
+    #     else:
+    #         return response.json(0, [])
+
 
 @require_http_methods(["GET"])
 def getUserIds(request):
@@ -424,3 +510,4 @@ def search_user_by_content(request):
         sqlDict['count'] = count
         return response.json(0, sqlDict)
     return response.json(0, {'datas': [], 'count': 0})
+

+ 2 - 2
Controller/EquipmentManagerV3.py

@@ -304,7 +304,7 @@ class EquipmentManagerV3(View):
                         p['ucode'] = ''
                     data.append(p)
                 # 缓存时间为一个钟
-                redisObj.set_data(key='uid_qs_'+userID, val=data, expire=3600)
+                redisObj.set_data(key='uid_qs_'+userID, val=json.dumps(data), expire=3600)
                 result = data
                 print('分页')
                 # items=[]
@@ -326,7 +326,7 @@ class EquipmentManagerV3(View):
                     redis_value = json.loads(redis_value)
                 except Exception as e:
                     print(repr(e))
-                    return response.json(0, {'data': redis_value})
+                    return response.json(0, redis_value)
                 result = redis_value
 
             if NickName:

+ 137 - 0
Controller/StatisticsController.py

@@ -0,0 +1,137 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+import json
+import time
+
+from django.db.models import Count
+from django.views.decorators.csrf import csrf_exempt
+
+from Model.models import UserAppFrequencyModel, AppFrequencyStatisticsModel, Device_User, \
+    AppFrequencyYearStatisticsModel
+from Object.ResponseObject import ResponseObject
+from Object.TokenObject import TokenObject
+
+
+@csrf_exempt
+def statistcsAppFrequency(request):
+    request.encoding = 'utf-8'
+    response = ResponseObject()
+    if request.method == 'POST':
+        request_dict = request.POST
+    elif request.method == 'GET':
+        request_dict = request.GET
+    else:
+        return response.json(444)
+
+    token = TokenObject(request_dict.get('token', None))
+    if token.code != 0:
+        return response.json(token.code)
+
+    target_year = request_dict.get('year', None)
+    target_month = request_dict.get('month', None)
+
+    localtime = time.localtime()
+    current_month = localtime.tm_mon
+    current_year = localtime.tm_year
+
+    if target_year and target_month:
+        current_year = int(target_year)
+        current_month = int(target_month)
+
+    time_struct = [current_year, 1, 1, 0, 0, 0, 0, 0, 0]
+
+    # 该月的开始时间
+    time_struct[1] = current_month
+    start_time = time.mktime(tuple(time_struct))
+
+    # 该月的结束时间
+    time_struct[1] = current_month + 1
+    end_time = time.mktime(tuple(time_struct))
+
+    # 统计该月的数据
+    uaf_qs = UserAppFrequencyModel.objects.filter(data_time__gte=start_time, data_time__lt=end_time) \
+        .values('type').annotate(quantity=Count('id')).order_by()
+
+    afs_qs = AppFrequencyStatisticsModel.objects.filter(year=current_year, month=current_month)
+
+    if afs_qs.exists():
+        afs_qs.update(data=json.dumps(uaf_qs))
+    else:
+        data = {
+            'data': json.dumps(list(uaf_qs)),
+            'month': current_month,
+            'year': current_year
+        }
+        AppFrequencyStatisticsModel.objects.create(**data)
+    return response.json(0)
+
+
+@csrf_exempt
+def statistcsAppFrequencyYear(request):
+    request.encoding = 'utf-8'
+    response = ResponseObject()
+    if request.method == 'POST':
+        request_dict = request.POST
+    elif request.method == 'GET':
+        request_dict = request.GET
+    else:
+        return response.json(444)
+
+    token = TokenObject(request_dict.get('token', None))
+    if token.code != 0:
+        return response.json(token.code)
+
+    year = request_dict.get('year', None)
+
+    localtime = time.localtime()
+
+    current_year = localtime.tm_year
+    if year:
+        current_year = int(year)
+
+    afs_qs = AppFrequencyStatisticsModel.objects.filter(year=current_year).values('data', 'month')
+    # num = Device_User.objects.count()
+    num = 300000
+
+    res = []
+    result = {
+        0: 0,
+        1: 0,
+        2: 0,
+        3: 0,
+        4: 0,
+        5: 0,
+        6: 0
+    }
+    quantity = 0
+    for afs in afs_qs:
+        data = json.loads(afs['data'])
+        for item in data:
+            result[item['type']] += item['quantity']
+            quantity += item['quantity']
+
+    tmps = []
+    for k, v in result.items():
+        tmp = {
+            'type': k,
+            'value': round(v / 12 / num, 3)
+        }
+        tmps.append(tmp)
+    res.append({
+        'year': current_year,
+        'data': tmps
+    })
+    print(res)
+
+    afys_qs = AppFrequencyYearStatisticsModel.objects.filter(year=current_year)
+    #
+    if afys_qs.exists():
+        afys_qs.update(data=json.dumps(res))
+    else:
+        data = {
+            'data': json.dumps(res),
+            'year': current_year,
+            'num': num
+        }
+        AppFrequencyYearStatisticsModel.objects.create(**data)
+    return response.json(0)

+ 250 - 95
Controller/UserController.py

@@ -27,8 +27,7 @@ from ratelimit.decorators import ratelimit
 
 from Ansjer.config import AuthCode_Expire, SERVER_DOMAIN, APNS_CONFIG, JPUSH_CONFIG, FCM_CONFIG, TUTK_PUSH_DOMAIN
 from Controller.CheckUserData import DataValid, date_handler, RandomStr
-from Model.models import Device_User, Role, UidPushModel, UserOauth2Model, UserExModel, Device_Info, UidSetModel, \
-    UserAppFrequencyModel
+from Model.models import Device_User, Role, UidPushModel, UserOauth2Model, UserExModel, Device_Info, UidSetModel
 from Object.AWS.SesClassObject import SesClassObject
 from Object.AliSmsObject import AliSmsObject
 from Object.RedisObject import RedisObject
@@ -51,7 +50,7 @@ class authCodeView(TemplateView):
 
     @method_decorator(csrf_exempt)
     def dispatch(self, *args, **kwargs):
-		# testtest11111111111111
+        # testtest11111111111111
         return super(authCodeView, self).dispatch(*args, **kwargs)
 
     @ratelimit(key='ip', rate='2/m')
@@ -441,7 +440,6 @@ class v3ChangePwdView(TemplateView):
             return response.json(177)
 
 
-
 class ForgetPwdView(TemplateView):
     '''
     忘记密码
@@ -973,7 +971,6 @@ class v2registerView(TemplateView):
             return self.do_login(email_qs, response)
 
 
-
 # 验证码注册v3
 class v3registerView(TemplateView):
     @method_decorator(csrf_exempt)
@@ -1149,7 +1146,6 @@ class v3registerView(TemplateView):
             return self.do_login(email_qs, response)
 
 
-
 # 重置密码
 # 忘记密码获取验证码v2
 class v2forgetPwdCodeView(TemplateView):
@@ -1415,7 +1411,6 @@ class v2resetPwdByCodeView(TemplateView):
         return response.json(0, res)
 
 
-
 # 忘记密码v3
 class v3resetPwdByCodeView(TemplateView):
     @method_decorator(csrf_exempt)
@@ -1573,7 +1568,6 @@ class v3resetPwdByCodeView(TemplateView):
         return response.json(0, res)
 
 
-
 # 登录
 class v2LoginView(TemplateView):
     @method_decorator(csrf_exempt)  # @csrf_exempt
@@ -1673,7 +1667,7 @@ class v2LoginView(TemplateView):
 class v3LoginView(TemplateView):
     @method_decorator(csrf_exempt)  # @csrf_exempt
     def dispatch(self, *args, **kwargs):
-        #chong 
+        # chong
         return super(v3LoginView, self).dispatch(*args, **kwargs)
 
     @ratelimit(key='ip', rate='5/m')
@@ -1715,13 +1709,13 @@ class v3LoginView(TemplateView):
                     password = password.decode('utf-8')
                     # 截去第一位,最后一位
                     password = password[1:-1]
-                if i==2:
+                if i == 2:
                     # 第2次先解密
                     password = base64.b64decode(password)
                     password = password.decode('utf-8')
                     # 去前2位,后2位
                     password = password[2:-2]
-                if i==3:
+                if i == 3:
                     # 第3次先解密
                     password = base64.b64decode(password)
                     password = password.decode('utf-8')
@@ -1820,7 +1814,7 @@ class InitInfoView(View):
         tz = request_dict.get('tz', '0')
         lang = request_dict.get('lang', '')  # 语言区域
         now_time = int(time.time())
-        if all([token_val, push_type, appBundleId,userID]):
+        if all([token_val, push_type, appBundleId, userID]):
             push_type = int(push_type)
             if push_type == 0:
                 if appBundleId not in APNS_CONFIG.keys():
@@ -2581,11 +2575,12 @@ class alexaAuthView(TemplateView):
         #     uid_arr.append({'uid': uid_q['UID'], 'nick': uid_q['NickName'], 'password': uid_q['View_Password']})
         res = {
             'userID': userID,
-        #     'uid_arr': uid_arr
+            #     'uid_arr': uid_arr
         }
-        return response.json(0,res)
+        return response.json(0, res)
         # return response.json(0, res)
 
+
 class alexaUidView(TemplateView):
     def post(self, request, *args, **kwargs):
         request.encoding = 'utf-8'
@@ -2610,11 +2605,11 @@ class alexaUidView(TemplateView):
             uid_list = []
             for uid_q in uid_qs:
                 uid_list.append(uid_q['UID'])
-                uid_arr.append({'uid': uid_q['UID'], 'nick': uid_q['NickName'], 'password': uid_q['View_Password'],})
+                uid_arr.append({'uid': uid_q['UID'], 'nick': uid_q['NickName'], 'password': uid_q['View_Password'], })
             res = {
                 'uid_arr': uid_arr
             }
-            return response.json(0,res)
+            return response.json(0, res)
         else:
             return response.json(107)
 
@@ -2627,16 +2622,16 @@ class alexaUidView(TemplateView):
             uid_dict = {}
             uid_list = []
             for uid_q in uid_qs:
-                #追加
+                # 追加
                 uid_list.append(uid_q['UID'])
-                #给uid_q['UID']赋值
+                # 给uid_q['UID']赋值
                 uid_dict[uid_q['UID']] = {'nick': uid_q['NickName'], 'password': uid_q['View_Password']}
             us_qs = UidSetModel.objects.filter(uid__in=uid_list, is_alexa=1).values('uid', 'region_alexa')
             # uid,password,region的列表
             uid_arr = []
             for us in us_qs:
                 uid = us['uid']
-                #设备alexa区域
+                # 设备alexa区域
                 region_alexa = us['region_alexa']
                 if region_alexa == '':
                     region_alexa = "en"
@@ -2651,6 +2646,7 @@ class alexaUidView(TemplateView):
         else:
             return response.json(107)
 
+
 # 登出
 class V2LogoutView(TemplateView):
     @method_decorator(csrf_exempt)
@@ -2812,14 +2808,14 @@ class Image_Code_RegisterView(TemplateView):
         request_dict = request.GET
         return self.validates(request_dict)
 
-    #检测验证码,并注册
-    def validates(self,request_dict):
+    # 检测验证码,并注册
+    def validates(self, request_dict):
         print("__________request_dict:%s" % request_dict)
-        userEmail = request_dict.get('userEmail',None)
-        password = request_dict.get('userPwd',None)
-        lang = request_dict.get('lang',None)
-        #前端传进来的uuid
-        imageCodeId = request_dict.get('imageCodeId',None)
+        userEmail = request_dict.get('userEmail', None)
+        password = request_dict.get('userPwd', None)
+        lang = request_dict.get('lang', None)
+        # 前端传进来的uuid
+        imageCodeId = request_dict.get('imageCodeId', None)
         # 页面输入的验证码
         response = ResponseObject(lang)
         valid_code = request_dict.get('id_v_code', None)
@@ -2840,7 +2836,7 @@ class Image_Code_RegisterView(TemplateView):
                     password = base64.b64decode(password)
                     password = password.decode('utf-8')
                     password = password[3:-3]
-            print("password%s"%password)
+            print("password%s" % password)
         except Exception as e:
             return response.json(111)
         try:
@@ -2857,7 +2853,7 @@ class Image_Code_RegisterView(TemplateView):
                     valid_code = base64.b64decode(valid_code)
                     valid_code = valid_code.decode('utf-8')
                     valid_code = valid_code[3:-3]
-            print("valid_code:%s"%valid_code)
+            print("valid_code:%s" % valid_code)
         except Exception as e:
             return response.json(121)
         if not userEmail:
@@ -2871,16 +2867,16 @@ class Image_Code_RegisterView(TemplateView):
             emailValid = Device_User.objects.filter(userEmail=userEmail)
             if emailValid:
                 return response.json(103)
-        #根据uuid拼接的key
-        image_code_key = "image_code_%s" %imageCodeId
-        #判断验证码是否过期
+        # 根据uuid拼接的key
+        image_code_key = "image_code_%s" % imageCodeId
+        # 判断验证码是否过期
         if image_code_key is None:
             return response.json(120)
         redisObj = RedisObject(db=6)
-        #redis里面的验证码
+        # redis里面的验证码
         redis_image_code = redisObj.get_data(key=image_code_key)
-        #验证用户输入的验证码和redis中的验证码
-        if valid_code.lower()!=redis_image_code.lower():
+        # 验证用户输入的验证码和redis中的验证码
+        if valid_code.lower() != redis_image_code.lower():
             return response.json(121)
         # 删除redis中的图片验证码,防止用户使用同一个图片验证码验证多次
         redisObj.del_data(key=image_code_key)
@@ -2931,81 +2927,240 @@ class Image_Code_RegisterView(TemplateView):
         return response.json(0, res)
 
 
-class UserAppFrequencyView(TemplateView):
-    @method_decorator(csrf_exempt)
+# class UserAppFrequencyView(TemplateView):
+#     @method_decorator(csrf_exempt)
+#     def dispatch(self, *args, **kwargs):
+#         return super(UserAppFrequencyView, self).dispatch(*args, **kwargs)
+#
+#     def post(self, request, *args, **kwargs):
+#         request.encoding = 'utf-8'
+#         request_dict = request.POST
+#         operation = kwargs.get('operation')
+#         return self.validates(request_dict, operation)
+#
+#     def get(self, request, *args, **kwargs):
+#         request.encoding = 'utf-8'
+#         request_dict = request.GET
+#         operation = kwargs.get('operation')
+#         return self.validates(request_dict, operation)
+#
+#     def validates(self, request_dict, operation):
+#         token = request_dict.get('token', None)
+#         response = ResponseObject()
+#
+#         token = TokenObject(token)
+#         if token.code != 0:
+#             return response.json(token.code)
+#
+#         if operation == 'refresh':
+#             return self.do_refresh(request_dict, token.userID, response)
+#         else:
+#             return response.json(404)
+#
+#     def do_refresh(self, request_dict, userID, response):
+#         # return response.json(0)
+#         type = request_dict.get('type', None)
+#         month = request_dict.get('month', None)
+#         if not type or not month:
+#             return response.json(444, 'type')
+#         else:
+#             type = int(type)
+#             now_time = int(time.time())
+#             month = int(month)
+#             uaf_qs = UserAppFrequencyModel.objects.filter(user__userID=userID)
+#
+#             if not uaf_qs.exists():
+#                 user = Device_User.objects.filter(userID=userID)[0]
+#                 data = {
+#                     'user': user,
+#                     'type': type,
+#                     'data_time': month,
+#                     'add_time': now_time,
+#                     'update_time': now_time,
+#                 }
+#                 UserAppFrequencyModel.objects.create(**data)
+#                 return response.json(0)
+#             else:
+#                 updateMonth = time.strftime('%m', time.localtime(month))
+#                 uaf = uaf_qs.values('id', 'type', 'data_time')[0]
+#                 dbMonth = time.strftime('%m', time.localtime(int(uaf['data_time'])))
+#                 print('update month is ' + updateMonth)
+#                 print('db month is ' + dbMonth)
+#                 if updateMonth == dbMonth:
+#                     UserAppFrequencyModel.objects.filter(id=uaf['id']).update(type=type)
+#                     return response.json(0)
+#                 elif updateMonth > dbMonth:
+#                     user = Device_User.objects.filter(userID=userID)[0]
+#                     data = {
+#                         'user': user,
+#                         'type': type,
+#                         'data_time': month,
+#                         'add_time': now_time,
+#                         'update_time': now_time,
+#                     }
+#                     UserAppFrequencyModel.objects.create(**data)
+#                     return response.json(0)
+#                 else:
+#                     return response.json(444, 'month')
+
+
+class loginCodeView(View):
+    @method_decorator(csrf_exempt)  # @csrf_exempt
     def dispatch(self, *args, **kwargs):
-        return super(UserAppFrequencyView, self).dispatch(*args, **kwargs)
+        return super(loginCodeView, self).dispatch(*args, **kwargs)
 
+    @ratelimit(key='ip', rate='2/m')
     def post(self, request, *args, **kwargs):
         request.encoding = 'utf-8'
+        lang = request.POST.get('lang', None)
+        if not lang:
+            lang = request.POST.get('language', None)
+        response = ResponseObject(lang)
         request_dict = request.POST
-        operation = kwargs.get('operation')
-        return self.validates(request_dict, operation)
+        phone = request_dict.get('phone', None)
+        if phone is not None:
+            was_limited = getattr(request, 'limited', False)
+            if was_limited is True:
+                return response.json(5)
+        return self.validate(request_dict, response)
 
+    @ratelimit(key='ip', rate='2/m')
     def get(self, request, *args, **kwargs):
         request.encoding = 'utf-8'
+        lang = request.GET.get('lang', None)
+        if not lang:
+            lang = request.GET.get('language', None)
+        response = ResponseObject(lang)
+        was_limited = getattr(request, 'limited', False)
+        if was_limited is True:
+            return response.json(5)
         request_dict = request.GET
-        operation = kwargs.get('operation')
-        return self.validates(request_dict, operation)
+        return self.validate(request_dict, response)
 
-    def validates(self, request_dict, operation):
-        token = request_dict.get('token', None)
-        response = ResponseObject()
+    def validate(self, request_dict, response):
+        phone = request_dict.get('phone', None)
+        country_code = request_dict.get('country_code', None)
+        sign_name = request_dict.get('sign_name', None)
 
-        token = TokenObject(token)
-        if token.code != 0:
-            return response.json(token.code)
-
-        if operation == 'refresh':
-            return self.do_refresh(request_dict, token.userID, response)
-        else:
-            return response.json(404)
-
-    def do_refresh(self, request_dict, userID, response):
-        # return response.json(0)
-        type = request_dict.get('type', None)
-        month = request_dict.get('month', None)
-        if not type or not month:
-            return response.json(444, 'type')
-        else:
-            type = int(type)
-            now_time = int(time.time())
-            month = int(month)
-            uaf_qs = UserAppFrequencyModel.objects.filter(user__userID=userID)
-
-            if not uaf_qs.exists():
-                user = Device_User.objects.filter(userID=userID)[0]
-                data = {
-                    'user': user,
-                    'type': type,
-                    'data_time': month,
-                    'add_time': now_time,
-                    'update_time': now_time,
-                }
-                UserAppFrequencyModel.objects.create(**data)
-                return response.json(0)
+        if phone and sign_name:
+            du_qs = Device_User.objects.filter(username=phone)
+            if not du_qs.exists():
+                return response.json(104)
             else:
-                updateMonth = time.strftime('%m', time.localtime(month))
-                uaf = uaf_qs.values('id', 'type', 'data_time')[0]
-                dbMonth = time.strftime('%m', time.localtime(int(uaf['data_time'])))
-                print('update month is ' + updateMonth)
-                print('db month is ' + dbMonth)
-                if updateMonth == dbMonth:
-                    UserAppFrequencyModel.objects.filter(id=uaf['id']).update(type=type)
-                    return response.json(0)
-                elif updateMonth > dbMonth:
-                    user = Device_User.objects.filter(userID=userID)[0]
-                    data = {
-                        'user': user,
-                        'type': type,
-                        'data_time': month,
-                        'add_time': now_time,
-                        'update_time': now_time,
-                    }
-                    UserAppFrequencyModel.objects.create(**data)
+                redisObject = RedisObject()
+                login_code_key = '{phone}_login_code'.format(phone=phone)
+                login_code = redisObject.get_data(key=login_code_key)
+                login_code_ttl = redisObject.get_ttl(key=login_code_key)
+                if login_code_ttl > 240 and login_code:
+                    return response.json(90)
+                login_code = RandomStr(6, True)
+                aliSms = AliSmsObject()
+                if sign_name == 'zosi':
+                    sign_sms = '周视'
+                else:
+                    sign_sms = 'Ansjer'
+
+                res = aliSms.send_code_sms(phone=phone, code=login_code, sign_name=sign_sms, temp_msg='SMS_151600991')
+
+                if res['Code'] == 'OK':
+                    if redisObject.set_data(key=login_code_key, val=login_code, expire=300) is not True:
+                        return response.json(48)
                     return response.json(0)
                 else:
-                    return response.json(444, 'month')
+                    return response.json(10, res['Message'])
+        else:
+            return response.json(444)
+
+
+class v3LoginByCodeView(View):
+    @method_decorator(csrf_exempt)  # @csrf_exempt
+    def dispatch(self, *args, **kwargs):
+        return super(v3LoginByCodeView, self).dispatch(*args, **kwargs)
+
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        lang = request.POST.get('lang', None)
+        if not lang:
+            lang = request.POST.get('language', None)
+        response = ResponseObject(lang)
+        request_dict = request.POST
+        phone = request_dict.get('phone', None)
+        if phone is not None:
+            was_limited = getattr(request, 'limited', False)
+            if was_limited is True:
+                return response.json(5)
+        return self.validate(request_dict, response)
+
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        lang = request.GET.get('lang', None)
+        if not lang:
+            lang = request.GET.get('language', None)
+        response = ResponseObject(lang)
+        was_limited = getattr(request, 'limited', False)
+        if was_limited is True:
+            return response.json(5)
+        request_dict = request.GET
+        return self.validate(request_dict, response)
+
+    def validate(self, request_dict, response):
+        phone = request_dict.get('phone', None)
+        code = request_dict.get('code', None)
+
+        if phone and code:
+            redisObject = RedisObject()
+            login_code_key = '{phone}_login_code'.format(phone=phone)
+            login_code = redisObject.get_data(key=login_code_key)
+
+            if login_code is not False:
+                print(code)
+                code = CommonService.decode_data(code)
+                print(code)
+                if login_code == code:
+                    if response.lang is None:
+                        response.lang = 'en'
+                    return self.do_phone_login(phone, response)
+                else:
+                    return response.json(121)
+            else:
+                return response.json(120)
 
+    def do_phone_login(self, phone, response):
+        user_qs = Device_User.objects.filter(Q(phone=phone) | Q(username=phone), is_active=True,
+                                             user_isValid=True)
+        return self.valid_login(user_qs, response)
 
+    def valid_login(self, user_qs, response):
+        if not user_qs.exists():
+            return response.json(104)
+        # users = user_qs.values('role__rid', 'role__roleName', 'userID', 'role', 'NickName', 'username', 'userEmail',
+        #                        'phone', 'password', 'userIconPath', 'user_isValid', 'is_active')[0]
+        users = user_qs.values('role__rid', 'role__roleName', 'userID', 'NickName', 'username', 'userEmail',
+                               'phone', 'password', 'userIconPath')[0]
 
+        userID = users['userID']
+        tko = TokenObject()
+        res = tko.generate(
+            data={'userID': userID, 'lang': response.lang, 'user': users['username'],
+                  'm_code': '123413243214'})
+        if tko.code == 0:
+            now_time = datetime.datetime.utcnow().replace(tzinfo=utc).astimezone(utc)
+            user_qs.update(last_login=now_time, language=response.lang)
+            res['rid'] = users['role__rid']
+            res['roleName'] = users['role__roleName']
+            res['permList'] = ModelService.own_permission(userID)
+            res['userID'] = userID
+            # 昵称,邮箱,电话,刷新,头像
+            userIconPath = str(users['userIconPath'])
+            if userIconPath and userIconPath.find('static/') != -1:
+                userIconPath = userIconPath.replace('static/', '').replace('\\', '/')
+                res['userIconUrl'] = SERVER_DOMAIN + 'account/getAvatar/' + userIconPath
+            else:
+                res['userIconUrl'] = ''
+            res['NickName'] = users['NickName'] if users['NickName'] is not None else ''
+            res['username'] = users['username'] if users['username'] is not None else ''
+            res['userEmail'] = users['userEmail'] if users['userEmail'] is not None else ''
+            res['phone'] = users['phone'] if users['phone'] is not None else ''
+            return response.json(0, res)
+        else:
+            return response.json(tko.code)

+ 25 - 1
Model/models.py

@@ -922,4 +922,28 @@ class UserAppFrequencyModel(models.Model):
         verbose_name = '用户使用APP频率表'
         verbose_name_plural = verbose_name
         db_table = 'user_app_frequency'
-        ordering = ('-add_time',)
+        ordering = ('-add_time',)
+
+
+class AppFrequencyStatisticsModel(models.Model):
+    id = models.AutoField(primary_key=True)
+    data = models.TextField(default='', verbose_name='统计好的数据')
+    month = models.IntegerField(default=0, verbose_name='月份')
+    year = models.IntegerField(default=0, verbose_name='年份')
+
+    class Meta:
+        verbose_name = 'app月使用频率统计表'
+        verbose_name_plural = verbose_name
+        db_table = 'app_frequency_statistics'
+
+
+class AppFrequencyYearStatisticsModel(models.Model):
+    id = models.AutoField(primary_key=True)
+    data = models.TextField(default='', verbose_name='统计好的数据')
+    year = models.IntegerField(default=0, verbose_name='年份')
+    num = models.IntegerField(default=0, verbose_name='总人数')
+
+    class Meta:
+        verbose_name = 'app使用频率统计表,年度统计'
+        verbose_name_plural = verbose_name
+        db_table = 'app_frequency_year_statistics'

+ 24 - 1
Service/CommonService.py

@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+import base64
 import datetime
 import time
 from pathlib import Path
@@ -221,4 +222,26 @@ class CommonService:
         random = Random()
         for index in range(randomlength):
             str += characterSet[random.randint(0, length)]
-        return str
+        return str
+
+    @staticmethod
+    def decode_data(content):
+        try:
+            for i in range(1, 4):
+                if i == 1:
+                    content = base64.b64decode(content)
+                    content = content.decode('utf-8')
+                    content = content[1:-1]
+                if i == 2:
+                    content = base64.b64decode(content)
+                    content = content.decode('utf-8')
+                    content = content[2:-2]
+                if i == 3:
+                    content = base64.b64decode(content)
+                    content = content.decode('utf-8')
+                    content = content[3:-3]
+
+            return content
+        except Exception as e:
+            print(e)
+            return None