| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 | 
							- from django.views.decorators.csrf import csrf_exempt
 
- import traceback
 
- from Service.ModelService import ModelService
 
- from Model.models import Device_User, Device_Info, Device_Meal, UID_Bucket
 
- from Service.CommonService import CommonService
 
- import simplejson as json
 
- from Object.TokenObject import TokenObject
 
- from Object.ResponseObject import ResponseObject
 
- import re
 
- def addNewUserEquipment(userID, deviceContent, response):
 
-     userIDValid = Device_User.objects.filter(userID=userID)
 
-     if userIDValid:
 
-         try:
 
-             try:
 
-                 deviceData = json.loads(deviceContent)
 
-             except Exception as e:
 
-                 return response.json(803, repr(e))
 
-             else:
 
-                 UID = deviceData.get('UID', None)
 
-                 if UID != None:
 
-                     dValid = Device_Info.objects.filter(userID=userID, UID=UID)
 
-                     if dValid:
 
-                         return response.json(174)
 
-                     else:
 
-                         # 判断是否有同样昵称存在
 
-                         # if 'NickName' in deviceData:
 
-                         #     nValid = Device_Info.objects.filter(userID=userID, NickName=deviceData['NickName'])
 
-                         #     if nValid.exists():
 
-                         #         return response.json(179)
 
-                         UID = deviceData.get('UID', '')
 
-                         re_uid = re.compile(r'^[A-Za-z0-9]{20}$')
 
-                         if re_uid.match(UID):
 
-                             # is_bind = Device_Info.objects.filter(UID=UID, isShare=False)
 
-                             is_bind = ''
 
-                             # 判断是否有已绑定用户
 
-                             if is_bind:
 
-                                 # return response.json(175)
 
-                                 # 判断用户密码是否正确
 
-                                 # dev_user = deviceData.get('View_Account', '')
 
-                                 # dev_pass = deviceData.get('View_Password', '')
 
-                                 # if is_bind[0].View_Account == dev_user and is_bind[0].View_Password == dev_pass:
 
-                                 deviceData['NickName'] = is_bind[0].NickName
 
-                                 if 'isShare' in deviceData:
 
-                                     deviceData['isShare'] = True
 
-                                 userDevice = Device_Info(id=CommonService.getUserID(getUser=False),
 
-                                                          primaryUserID=is_bind[0].userID_id, userID_id=userID,
 
-                                                          **deviceData)
 
-                                 userDevice.save()
 
-                                 # else:
 
-                                 #     return response.json(111)
 
-                             else:
 
-                                 userDevice = Device_Info(id=CommonService.getUserID(getUser=False), userID_id=userID,
 
-                                                          **deviceData)
 
-                                 userDevice.save()
 
-                         else:
 
-                             return response.json(180)
 
-                 else:
 
-                     return response.json(806)
 
-         except Exception as e:
 
-             errorInfo = traceback.format_exc()
 
-             print('添加设备错误: %s ' % errorInfo)
 
-             return response.json(178, repr(e))
 
-         else:
 
-             sqlDict = CommonService.qs_to_dict([userDevice])
 
-             return response.json(0, sqlDict)
 
-     else:
 
-         return response.json(104)
 
- def delUserEquipment(userID, id, response):
 
-     try:
 
-         deviceValid = Device_Info.objects.filter(userID_id=userID, id=id)
 
-     except Exception as e:
 
-         errorInfo = traceback.format_exc()
 
-         print('查询数据库错误: %s' % errorInfo)
 
-         return response.json(500, repr(e))
 
-     else:
 
-         if deviceValid:
 
-             try:
 
-                 Device_Info.objects.filter(userID_id=userID, id=id).delete()
 
-             except Exception as e:
 
-                 errorInfo = traceback.format_exc()
 
-                 print('删除数据库记录错误: %s' % errorInfo)
 
-                 return response.json(176, repr(e))
 
-             else:
 
-                 return response.json(0)
 
-         else:
 
-             UserValid = Device_User.objects.filter(userID=userID)
 
-             if UserValid.exists():
 
-                 return response.json(172)
 
-             else:
 
-                 return response.json(104)
 
- def showAllUserEquipment(userID, response):
 
-     try:
 
-         userValid = Device_User.objects.filter(userID=userID).order_by('-data_joined')
 
-     except Exception as e:
 
-         errorInfo = traceback.format_exc()
 
-         print('查询数据库错误: %s' % errorInfo)
 
-         return response.json(500, repr(e))
 
-     else:
 
-         if not userValid.exists():
 
-             return response.json(104)
 
-         own_perm = ModelService.check_permission(userID=userID, permID=30)
 
-         # if userValid[0].is_superuser != 100 and userValid[0].is_superuser != 1:
 
-         if own_perm is not True:
 
-             return response.json(404)
 
-         qs = Device_Info.objects.all()
 
-         res = CommonService.qs_to_dict(qs)
 
-         return response.json(0, res)
 
- def showAllUserEquipmentPC(userID, fieldDict, response):
 
-     try:
 
-         user_valid = Device_User.objects.filter(userID=userID).order_by('-data_joined')
 
-     except Exception as e:
 
-         errorInfo = traceback.format_exc()
 
-         print('查询数据库错误: %s' % errorInfo)
 
-         return response.json(500, repr(e))
 
-     else:
 
-         if not user_valid:
 
-             return response.json(104)
 
-         own_permission = ModelService.check_permission(userID=userID, permID=30)
 
-         if not own_permission:
 
-             return response.json(404)
 
-         page = int(fieldDict['page'])
 
-         line = int(fieldDict['line'])
 
-         device_info_query_set = Device_Info.objects.all()
 
-         device_info_count = device_info_query_set.count()
 
-         device_info_res = device_info_query_set[(page - 1) * line:page * line]
 
-         sqlDict = CommonService.qs_to_dict(query_set=device_info_res)
 
-         sqlDict['count'] = device_info_count
 
-         return response.json(0, sqlDict)
 
- def findEquipmentInfo(content, type, fieldDict):
 
-     if type == 1:
 
-         Device_Info_QuerySet = Device_Info.objects.all()
 
-     if type == 2:
 
-         searchCondition = content
 
-         kwargs = CommonService.get_kwargs(data=searchCondition)
 
-         Device_Info_QuerySet = Device_Info.objects.filter(**kwargs)
 
-     page = int(fieldDict['page'])
 
-     line = int(fieldDict['line'])
 
-     device_info_count = Device_Info_QuerySet.count()
 
-     res = Device_Info_QuerySet[(page - 1) * line:page * line]
 
-     send_dict = CommonService.qs_to_dict(query_set=res)
 
-     for k, v in enumerate(send_dict["datas"]):
 
-         for val in res:
 
-             if v['pk'] == val.id:
 
-                 username = ModelService.get_user_name(send_dict["datas"][k]['fields']['userID'])
 
-                 send_dict["datas"][k]['fields']['username'] = username
 
-                 primary = ModelService.get_user_name(send_dict["datas"][k]['fields']['primaryUserID'])
 
-                 send_dict["datas"][k]['fields']['primaryusername'] = primary
 
-     send_dict['count'] = device_info_count
 
-     return send_dict
 
- @csrf_exempt
 
- def queryUserEquipmentInterface(request, *callback_args,
 
-                                 **callback_kwargs):
 
-     '''
 
-     查询用户设备
 
-     :param request:
 
-     :param callback_args:
 
-     :param callback_kwargs:
 
-     :return:
 
-     '''
 
-     response = ResponseObject()
 
-     if request.method == 'POST':
 
-         request.encoding = 'utf-8'
 
-         request_dict = request.POST
 
-     elif request.method == 'GET':
 
-         request.encoding = 'utf-8'
 
-         request_dict = request.GET
 
-     else:
 
-         return response.json(801)
 
-     token = request_dict.get('token', None)
 
-     tko = TokenObject(token)
 
-     response.lang = tko.lang
 
-     if tko.code != 0:
 
-         return response.json(tko.code)
 
-     userID = tko.userID
 
-     if not userID:
 
-         return response.json(104)
 
-     deviceValid = Device_Info.objects.filter(userID_id=userID)
 
-     res = CommonService.qs_to_dict(deviceValid)
 
-     datas = res['datas']
 
-     uid_list = []
 
-     for q in datas:
 
-         uid_list.append(q['fields']['UID'])
 
-     ubqs = UID_Bucket.objects.filter(uid__in=uid_list). \
 
-         values('bucket__content', 'status', 'channel', 'endTime', 'uid')
 
-     ubql = CommonService.qs_to_list(ubqs)
 
-     # print(uid_list)
 
-     res = []
 
-     for p in datas:
 
-         p['fields']['vod'] = []
 
-         for dm in ubql:
 
-             if p['fields']['UID'] == dm['uid']:
 
-                 p['fields']['vod'].append(dm)
 
-         res.append(p)
 
-     # print(res)
 
-     return response.json(0, {'datas': res})
 
- @csrf_exempt
 
- def addNewUserEquipmentInterface(request, *callback_args,
 
-                                  **callback_kwargs):
 
-     response = ResponseObject()
 
-     if request.method == 'POST':
 
-         request.encoding = 'utf-8'
 
-         request_dict = request.POST
 
-     elif request.method == 'GET':
 
-         request.encoding = 'utf-8'
 
-         request_dict = request.GET
 
-     else:
 
-         return response.json(801)
 
-     token = request_dict.get('token', None)
 
-     deviceContent = request_dict.get('content', None)
 
-     if token is not None and deviceContent is not None:
 
-         tko = TokenObject(token)
 
-         response.lang = tko.lang
 
-         if tko.code == 0:
 
-             userID = tko.userID
 
-             if userID is not None:
 
-                 return addNewUserEquipment(userID, deviceContent, response)
 
-             else:
 
-                 return response.json(309)
 
-         else:
 
-             return response.json(tko.code)
 
-     else:
 
-         return response.json(444, 'token,content')
 
- @csrf_exempt
 
- def delUserEquipmentInterface(request, *callback_args,
 
-                               **callback_kwargs):
 
-     '''
 
-     删除用户设备
 
-     :param request:
 
-     :param callback_args: 表示任何多个无名参数,tuple类型
 
-     :param callback_kwargs: 表示关键字参数,dict类型
 
-     :return:
 
-     '''
 
-     response = ResponseObject()
 
-     if request.method == 'POST':
 
-         request.encoding = 'utf-8'
 
-         request_dict = request.POST
 
-     elif request.method == 'GET':
 
-         request.encoding = 'gb2312'
 
-         request_dict = request.GET
 
-     else:
 
-         return response.json(801)
 
-     token = request_dict.get('token', None)
 
-     id = request_dict.get('id', None)
 
-     if token is not None and id is not None:
 
-         tko = TokenObject(token)
 
-         response.lang = tko.lang
 
-         if tko.code == 0:
 
-             userID = tko.userID
 
-             if userID is not None:
 
-                 return delUserEquipment(userID, id, response)
 
-             else:
 
-                 return response.json(309)
 
-         else:
 
-             return response.json(tko.code)
 
-     else:
 
-         return response.json(800)
 
- @csrf_exempt
 
- def modifyUserEquipmentInterface(request, *callback_args,
 
-                                  **callback_kwargs):
 
-     '''
 
-     修改用户设备
 
-     :param request:
 
-     :param callback_args:
 
-     :param callback_kwargs:
 
-     :return:
 
-     '''
 
-     response = ResponseObject()
 
-     if request.method == 'POST':
 
-         request.encoding = 'utf-8'
 
-         request_dict = request.POST
 
-     elif request.method == 'GET':
 
-         request.encoding = 'utf-8'
 
-         request_dict = request.GET
 
-     token = request_dict.get('token', None)
 
-     deviceContent = request_dict.get('content', None)
 
-     id = request_dict.get('id', None)
 
-     if not deviceContent or not id:
 
-         return response.json(444, 'content,id')
 
-     tko = TokenObject(token)
 
-     response.lang = tko.lang
 
-     if tko.code != 0:
 
-         return response.json(tko.code)
 
-     userID = tko.userID
 
-     if userID is None:
 
-         return response.json(309)
 
-     try:
 
-         deviceValid = Device_Info.objects.filter(userID_id=userID, id=id)
 
-     except Exception as e:
 
-         return response.json(500, repr(e))
 
-     else:
 
-         if deviceValid.exists():
 
-             deviceData = json.loads(deviceContent)
 
-             try:
 
-                 Device_Info.objects.filter(userID_id=userID, id=id).update(**deviceData)
 
-             except Exception as e:
 
-                 return response.json(177, repr(e))
 
-             else:
 
-                 qs = Device_Info.objects.filter(userID_id=userID, id=id)
 
-                 res = CommonService.qs_to_dict(qs)
 
-                 return response.json(0, res)
 
-         else:
 
-             UserValid = Device_User.objects.filter(userID=userID)
 
-             if UserValid.exists():
 
-                 return response.json(172)
 
-             else:
 
-                 return response.json(104)
 
- @csrf_exempt
 
- def showAllUserEquipmentInterface(request, *callback_args, **callback_kwargs):
 
-     if request.method == 'POST':
 
-         request.encoding = 'utf-8'
 
-         fieldDict = request.POST
 
-     if request.method == 'GET':
 
-         request.encoding = 'utf-8'
 
-         fieldDict = request.GET
 
-     token = request.POST.get('token', None)
 
-     type = request.POST.get('type', None)
 
-     response = ResponseObject()
 
-     if token != None:
 
-         tko = TokenObject(token)
 
-         response.lang = tko.lang
 
-         if tko.code == 0:
 
-             userID = tko.userID
 
-             if userID:
 
-                 if type == 'PC':
 
-                     return showAllUserEquipmentPC(userID, fieldDict, response)
 
-                 else:
 
-                     return showAllUserEquipment(userID, response)
 
-             else:
 
-                 return response.json(309)
 
-         else:
 
-             return response.json(tko.code)
 
-     else:
 
-         return response.json(800)
 
- @csrf_exempt
 
- def findEquipmentInfoInterface(request, *callback_args, **callback_kwargs):
 
-     if request.method == 'GET':
 
-         request.encoding = 'gb2312'
 
-         fieldDict = request.GET
 
-     if request.method == 'POST':
 
-         request.encoding = 'utf-8'
 
-         fieldDict = request.POST
 
-     deviceContent = fieldDict.get('content', None)
 
-     token = fieldDict.get('token', None)
 
-     response = ResponseObject()
 
-     if token != None:
 
-         tko = TokenObject(token)
 
-         response.lang = tko.lang
 
-         if tko.code == 0:
 
-             if deviceContent:
 
-                 try:
 
-                     deviceContent = json.loads(deviceContent)
 
-                 except Exception as e:
 
-                     print(repr(e))
 
-                     return response.json(10, repr(e))
 
-                 else:
 
-                     resultDict = findEquipmentInfo(content=deviceContent, type=2, fieldDict=fieldDict)
 
-             else:
 
-                 resultDict = findEquipmentInfo(content='', type=1, fieldDict=fieldDict)
 
-             return response.json(0, resultDict)
 
-         else:
 
-             return response.json(tko.code)
 
-     else:
 
-         return response.json(444, 'token')
 
 
  |