ModelService.py 9.6 KB

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