ModelService.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import json
  2. import time
  3. import requests
  4. from django.db.models import Q
  5. from Ansjer.config import BASE_DIR
  6. from Model.models import *
  7. from Service.EquipmentInfoService import EquipmentInfoService
  8. # 针对模型封装的复用性代码
  9. class ModelService:
  10. # UID Manage检测权限
  11. @staticmethod
  12. def check_perm_uid_manage(userID, permID):
  13. try:
  14. user_qs = UserModel.objects.filter(id=userID)
  15. if user_qs.exists():
  16. user = user_qs[0]
  17. if int(user.permission) != 0:
  18. return False
  19. else:
  20. return True
  21. else:
  22. return False
  23. except Exception as e:
  24. print(repr(e))
  25. # 获取当前用户角色名
  26. @staticmethod
  27. def getRole(rid):
  28. return Role.objects.get(rid=rid).roleName
  29. # 获取用户所有权限
  30. @staticmethod
  31. def own_permission(userID):
  32. permission = Device_User.objects.get(userID=userID).role.values_list('permission', flat=True)
  33. if permission:
  34. return list(permission)
  35. return []
  36. # 获取用户角色相关
  37. @staticmethod
  38. def own_role(userID):
  39. try:
  40. role_qs = Device_User.objects.get(userID=userID).role.values('rid', 'roleName')
  41. if role_qs.exists():
  42. return {'rid': role_qs[0]['rid'], 'roleName': role_qs[0]['roleName']}
  43. except Exception as e:
  44. pass
  45. return {'rid': '', 'roleName': ''}
  46. # 检测权限有无
  47. @staticmethod
  48. def check_perm(userID, permID):
  49. try:
  50. perm_list = Device_User.objects.get(userID=userID).role.values_list('permission', flat=True)
  51. if perm_list:
  52. if permID in perm_list:
  53. return True
  54. except Exception as e:
  55. return False
  56. return False
  57. # 根据设备主键ID判断是否拥有该设备
  58. @staticmethod
  59. def check_user_own_device(userID, deviceID):
  60. try:
  61. dvqs = Device_Info.objects.filter(userID_id=userID).values_list('id', flat=True)
  62. if dvqs:
  63. if deviceID in dvqs:
  64. return True
  65. except Exception as e:
  66. return False
  67. return False
  68. # 根据设设备唯一名称UID判断是否拥有该设备
  69. @staticmethod
  70. def check_own_device(userID, UID):
  71. dvqs = Device_Info.objects.filter(userID_id=userID, UID=UID)
  72. if dvqs.exists():
  73. return True
  74. return False
  75. # 根据userID获取用户名
  76. @staticmethod
  77. def get_user_name(userID):
  78. try:
  79. if userID:
  80. device_user = Device_User.objects.get(userID=userID)
  81. return device_user.username
  82. else:
  83. return ''
  84. except Exception as e:
  85. return ''
  86. @staticmethod
  87. def get_user_mark(userID):
  88. if userID:
  89. qs = Device_User.objects.filter(userID=userID).values('username', 'userEmail', 'phone')
  90. if qs[0]['username']:
  91. return qs[0]['username']
  92. elif qs[0]['userEmail']:
  93. return qs[0]['userEmail']
  94. elif qs[0]['phone']:
  95. return qs[0]['phone']
  96. else:
  97. return ''
  98. else:
  99. return ''
  100. # 根据username获取userID
  101. @staticmethod
  102. def get_userID_byname(username):
  103. try:
  104. device_user = Device_User.objects.get(Q(username=username) | Q(userEmail=username) | Q(phone=username))
  105. except Exception as e:
  106. return None
  107. else:
  108. return device_user.userID
  109. # 访问日志批量添加
  110. @staticmethod
  111. def add_batch_log(data_list):
  112. try:
  113. if data_list:
  114. querysetlist = []
  115. for i in data_list:
  116. data = json.loads(i.decode('utf-8'))
  117. querysetlist.append(Access_Log(**data))
  118. Access_Log.objects.bulk_create(querysetlist)
  119. else:
  120. return
  121. except Exception as e:
  122. print('ggga')
  123. print(repr(e))
  124. return False
  125. else:
  126. return True
  127. # 通过用户名获取userIDLIST
  128. @staticmethod
  129. def get_user_list_by_username(username):
  130. userID_list = Device_User.objects.filter(Q(username=username) | Q(userEmail=username) | Q(phone=username)). \
  131. values_list('userID', flat=True)
  132. return userID_list
  133. @staticmethod
  134. def del_eq_info(userID, uid):
  135. notify_alexa_delete(userID, uid)
  136. ei_qs = Equipment_Info.objects.filter(userID_id=userID, devUid=uid)
  137. ei_qs.delete()
  138. for i in range(1, 8):
  139. eq_list = EquipmentInfoService.get_equipment_info_model('', i)
  140. eq_list = eq_list.filter(device_user_id=userID, device_uid=uid)
  141. if eq_list.exists():
  142. eq_list.delete()
  143. @staticmethod
  144. def del_user_list_eq_info(user_id_list, uid):
  145. """
  146. 根据用户id列表删除设备推送消息
  147. @param user_id_list: 用户id列表
  148. @param uid:
  149. @return:
  150. """
  151. for i in range(1, 8):
  152. EquipmentInfoService.get_equipment_info_model('', i).\
  153. filter(device_user_id__in=user_id_list, device_uid=uid).delete()
  154. # 获取绑定用户设备列表
  155. @staticmethod
  156. def get_uid_list(userID):
  157. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  158. return list(uid_list)
  159. @staticmethod
  160. def notify_alexa_add(uid, userID, nickname, encrypt_pwd):
  161. url = 'https://www.zositech.xyz/deviceStatus/addOrUpdate'
  162. data = {
  163. 'UID': uid,
  164. 'userID': userID,
  165. 'uid_nick': nickname,
  166. 'password': encrypt_pwd,
  167. }
  168. try:
  169. res = requests.post(url, data=data, timeout=5)
  170. except Exception as e:
  171. print(repr(e))
  172. @staticmethod
  173. def add_log(ip, userID, operation):
  174. file_path = '/'.join((BASE_DIR, 'static/delete_device.log'))
  175. file = open(file_path, 'a+')
  176. file.write(ip + "; username:" + userID + "; time:" + time.strftime(
  177. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; " + operation)
  178. file.write('\n')
  179. file.flush()
  180. file.close()
  181. @staticmethod
  182. def update_log(ip, userID, operation, content, id):
  183. content['xid'] = id
  184. file_path = '/'.join((BASE_DIR, 'static/update_device.log'))
  185. file = open(file_path, 'a+')
  186. file.write(ip + "; username:" + userID + "; time:" + time.strftime(
  187. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; content:" + json.dumps(content) + "; " + operation)
  188. file.write('\n')
  189. file.flush()
  190. file.close()
  191. @staticmethod
  192. def add_ip_log(ip, info):
  193. file_path = '/'.join((BASE_DIR, 'static/get_timezone.log'))
  194. file = open(file_path, 'a+')
  195. file.write(ip + "; info:" + str(info) + "; time:" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  196. file.write('\n')
  197. file.flush()
  198. file.close()
  199. @staticmethod
  200. def add_tmp_log(info):
  201. file_path = '/'.join((BASE_DIR, 'static/tmp_test.log'))
  202. file = open(file_path, 'a+')
  203. file.write("info:" + str(info))
  204. file.write('\n')
  205. file.flush()
  206. file.close()
  207. @staticmethod
  208. def app_log_log(userID, UID):
  209. file_path = '/'.join((BASE_DIR, 'static/app_log.log'))
  210. file = open(file_path, 'a+')
  211. file.write("username:" + userID + "; time:" + time.strftime(
  212. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; " + "; uid:" + UID)
  213. file.write('\n')
  214. file.flush()
  215. file.close()
  216. def notify_alexa_delete(userID, UID):
  217. url = 'https://www.zositech.xyz/deviceStatus/delete'
  218. data = {
  219. 'userID': userID,
  220. 'UID': UID
  221. }
  222. try:
  223. requests.post(url=url, data=data, timeout=5)
  224. except Exception as e:
  225. print(repr(e))