ModelService.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. # ei_count = ei_qs.count()
  144. # while (ei_count > 1000):
  145. # ei_qs[0:1000].delete()
  146. # 根据设备主键ID判断是否拥有该设备
  147. # 获取绑定用户设备列表
  148. @staticmethod
  149. def get_uid_list(userID):
  150. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  151. return list(uid_list)
  152. @staticmethod
  153. def notify_alexa_add(uid, userID, nickname, encrypt_pwd):
  154. url = 'https://www.zositech.xyz/deviceStatus/addOrUpdate'
  155. data = {
  156. 'UID': uid,
  157. 'userID': userID,
  158. 'uid_nick': nickname,
  159. 'password': encrypt_pwd,
  160. }
  161. try:
  162. res = requests.post(url, data=data, timeout=5)
  163. except Exception as e:
  164. print(repr(e))
  165. @staticmethod
  166. def add_log(ip, userID, operation):
  167. file_path = '/'.join((BASE_DIR, 'static/delete_device.log'))
  168. file = open(file_path, 'a+')
  169. file.write(ip + "; username:" + userID + "; time:" + time.strftime(
  170. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; " + operation)
  171. file.write('\n')
  172. file.flush()
  173. file.close()
  174. @staticmethod
  175. def update_log(ip, userID, operation, content, id):
  176. content['xid'] = id
  177. file_path = '/'.join((BASE_DIR, 'static/update_device.log'))
  178. file = open(file_path, 'a+')
  179. file.write(ip + "; username:" + userID + "; time:" + time.strftime(
  180. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; content:" + json.dumps(content) + "; " + operation)
  181. file.write('\n')
  182. file.flush()
  183. file.close()
  184. @staticmethod
  185. def add_ip_log(ip, info):
  186. file_path = '/'.join((BASE_DIR, 'static/get_timezone.log'))
  187. file = open(file_path, 'a+')
  188. file.write(ip + "; info:" + str(info) + "; time:" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  189. file.write('\n')
  190. file.flush()
  191. file.close()
  192. @staticmethod
  193. def add_tmp_log(info):
  194. file_path = '/'.join((BASE_DIR, 'static/tmp_test.log'))
  195. file = open(file_path, 'a+')
  196. file.write("info:" + str(info))
  197. file.write('\n')
  198. file.flush()
  199. file.close()
  200. @staticmethod
  201. def app_log_log(userID, UID):
  202. file_path = '/'.join((BASE_DIR, 'static/app_log.log'))
  203. file = open(file_path, 'a+')
  204. file.write("username:" + userID + "; time:" + time.strftime(
  205. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; " + "; uid:" + UID)
  206. file.write('\n')
  207. file.flush()
  208. file.close()
  209. def notify_alexa_delete(userID, UID):
  210. url = 'https://www.zositech.xyz/deviceStatus/delete'
  211. data = {
  212. 'userID': userID,
  213. 'UID': UID
  214. }
  215. try:
  216. requests.post(url=url, data=data, timeout=5)
  217. except Exception as e:
  218. print(repr(e))