EquipmentManager.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. from django.views.decorators.csrf import csrf_exempt
  2. from django.core import serializers
  3. import traceback
  4. from Service.ModelService import ModelService
  5. from Model.models import Device_User, Device_Info
  6. from Service.CommonService import CommonService
  7. import simplejson as json
  8. from Object.TokenObject import TokenObject
  9. from Object.ResponseObject import ResponseObject
  10. import re
  11. from Ansjer.config import UID_TOKEN_KEY
  12. import jwt
  13. def addNewUserEquipment(userID, deviceContent, response):
  14. userIDValid = Device_User.objects.filter(userID=userID)
  15. if userIDValid:
  16. try:
  17. try:
  18. deviceData = json.loads(deviceContent)
  19. except Exception as e:
  20. return response.json(803, repr(e))
  21. else:
  22. UID = deviceData.get('UID', None)
  23. if UID != None:
  24. dValid = Device_Info.objects.filter(userID=userID, UID=UID)
  25. if dValid:
  26. return response.json(174)
  27. else:
  28. if 'NickName' in deviceData:
  29. nValid = Device_Info.objects.filter(userID=userID, NickName=deviceData['NickName'])
  30. if nValid.exists():
  31. return response.json(179)
  32. UID = deviceData['UID']
  33. re_uid = re.compile(r'^[A-Za-z0-9]{20}$')
  34. if re_uid.match(UID):
  35. is_bind = Device_Info.objects.filter(UID=UID, isShare=False)
  36. # 判断是否有绑定
  37. if is_bind:
  38. # return response.json(175)
  39. userDevice = Device_Info(id=CommonService.getUserID(getUser=False),isShare=True,userID_id=userID,**deviceData)
  40. userDevice.save()
  41. else:
  42. userDevice = Device_Info(id=CommonService.getUserID(getUser=False), userID_id=userID,
  43. **deviceData)
  44. userDevice.save()
  45. else:
  46. return response.json(180)
  47. else:
  48. return response.json(806)
  49. except Exception as e:
  50. errorInfo = traceback.format_exc()
  51. print('添加设备错误: %s ' % errorInfo)
  52. return response.json(178, repr(e))
  53. else:
  54. sqlJSON = serializers.serialize('json', [userDevice])
  55. sqlList = json.loads(sqlJSON)
  56. sqlDict = dict(zip(["datas"], [sqlList]))
  57. return response.json(0, sqlDict)
  58. else:
  59. return response.json(113)
  60. def delUserEquipment(userID, id, response):
  61. try:
  62. deviceValid = Device_Info.objects.filter(userID_id=userID, id=id)
  63. except Exception as e:
  64. errorInfo = traceback.format_exc()
  65. print('查询数据库错误: %s' % errorInfo)
  66. return response.json(500, repr(e))
  67. else:
  68. if deviceValid:
  69. try:
  70. Device_Info.objects.filter(userID_id=userID, id=id).delete()
  71. except Exception as e:
  72. errorInfo = traceback.format_exc()
  73. print('删除数据库记录错误: %s' % errorInfo)
  74. return response.json(176, repr(e))
  75. else:
  76. return response.json(0)
  77. else:
  78. UserValid = Device_User.objects.filter(userID=userID)
  79. if UserValid.exists():
  80. return response.json(172)
  81. else:
  82. return response.json(113)
  83. def modifyUserEquipment(userID, deviceContent, id, response):
  84. try:
  85. deviceValid = Device_Info.objects.filter(userID_id=userID, id=id)
  86. except Exception as e:
  87. errorInfo = traceback.format_exc()
  88. print('查询数据库错误: %s' % errorInfo)
  89. return response.json(500, repr(e))
  90. else:
  91. if deviceValid:
  92. deviceData = json.loads(deviceContent)
  93. try:
  94. userDevice = Device_Info.objects.filter(userID_id=userID, id=id).update(**deviceData)
  95. except Exception as e:
  96. errorInfo = traceback.format_exc()
  97. print('修改设备信息错误: %s ' % errorInfo)
  98. return response.json(177, repr(e))
  99. else:
  100. qs = Device_Info.objects.filter(userID_id=userID, id=id)
  101. res = CommonService.qs_to_dict(qs)
  102. return response.json(0, res)
  103. else:
  104. UserValid = Device_User.objects.filter(userID=userID)
  105. if UserValid.exists():
  106. return response.json(172)
  107. else:
  108. return response.json(113)
  109. def showAllUserEquipment(userID, response):
  110. try:
  111. userValid = Device_User.objects.filter(userID=userID).order_by('-data_joined')
  112. except Exception as e:
  113. errorInfo = traceback.format_exc()
  114. print('查询数据库错误: %s' % errorInfo)
  115. return response.json(500, repr(e))
  116. else:
  117. if userValid.exists():
  118. # own_permission = ModelService.check_permission(userID=userID,permID=30)
  119. if userValid[0].is_superuser != 100 and userValid[0].is_superuser != 1:
  120. return response.json(612)
  121. else:
  122. qs = Device_Info.objects.all()
  123. res = CommonService.qs_to_dict(qs)
  124. return response.json(0, res)
  125. else:
  126. return response.json(113)
  127. def showAllUserEquipmentPC(userID, fieldDict, response):
  128. try:
  129. user_valid = Device_User.objects.filter(userID=userID).order_by('-data_joined')
  130. except Exception as e:
  131. errorInfo = traceback.format_exc()
  132. print('查询数据库错误: %s' % errorInfo)
  133. return response.json(500, repr(e))
  134. else:
  135. if user_valid:
  136. own_permission = ModelService.check_permission(userID=userID, permID=30)
  137. if own_permission:
  138. page = int(fieldDict['page'])
  139. line = int(fieldDict['line'])
  140. device_info_query_set = Device_Info.objects.all()
  141. device_info_count = device_info_query_set.count()
  142. device_info_res = device_info_query_set[(page - 1) * line:page * line]
  143. sqlDict = CommonService.qs_to_dict(query_set=device_info_res)
  144. sqlDict['count'] = device_info_count
  145. return response.json(0, sqlDict)
  146. else:
  147. return response.json(612)
  148. else:
  149. return response.json(113)
  150. def findEquipmentInfo(content, type, fieldDict):
  151. if type == 1:
  152. Device_Info_QuerySet = Device_Info.objects.all()
  153. if type == 2:
  154. searchCondition = content
  155. kwargs = CommonService.get_kwargs(data=searchCondition)
  156. Device_Info_QuerySet = Device_Info.objects.filter(**kwargs)
  157. page = int(fieldDict['page'])
  158. line = int(fieldDict['line'])
  159. device_info_count = Device_Info_QuerySet.count()
  160. res = Device_Info_QuerySet[(page - 1) * line:page * line]
  161. send_dict = CommonService.qs_to_dict(query_set=res)
  162. for k, v in enumerate(send_dict["datas"]):
  163. for val in res:
  164. if v['pk'] == val.id:
  165. username = ModelService.get_user_name(send_dict["datas"][k]['fields']['userID'])
  166. send_dict["datas"][k]['fields']['username'] = username
  167. primary = ModelService.get_user_name(send_dict["datas"][k]['fields']['primaryUserID'])
  168. send_dict["datas"][k]['fields']['primaryusername'] = primary
  169. send_dict['count'] = device_info_count
  170. return send_dict
  171. @csrf_exempt
  172. def queryUserEquipmentInterface(request, *callback_args,
  173. **callback_kwargs):
  174. '''
  175. 查询用户设备
  176. :param request:
  177. :param callback_args:
  178. :param callback_kwargs:
  179. :return:
  180. '''
  181. response = ResponseObject()
  182. if request.method == 'POST':
  183. request.encoding = 'utf-8'
  184. token = request.POST.get('token', None)
  185. elif request.method == 'GET':
  186. request.encoding = 'gb2312'
  187. token = request.GET.get('token', None)
  188. else:
  189. return response.json(801)
  190. if token is not None:
  191. tko = TokenObject(token)
  192. tko.valid()
  193. response.lang = tko.lang
  194. if tko.code == 0:
  195. userID = tko.userID
  196. if userID is not None:
  197. try:
  198. deviceValid = Device_Info.objects.filter(userID_id=userID)
  199. except Exception as e:
  200. errorInfo = traceback.format_exc()
  201. print('查询数据库错误: %s' % errorInfo)
  202. return response.json(500, repr(e))
  203. else:
  204. if deviceValid.exists():
  205. res = CommonService.qs_to_dict(deviceValid)
  206. return response.json(0, res)
  207. else:
  208. UserValid = Device_User.objects.filter(userID=userID)
  209. if UserValid.exists():
  210. return response.json(172)
  211. else:
  212. return response.json(113)
  213. else:
  214. return response.json(309)
  215. else:
  216. return response.json(tko.code)
  217. else:
  218. return response.json(800)
  219. @csrf_exempt
  220. def addNewUserEquipmentInterface(request, *callback_args,
  221. **callback_kwargs):
  222. response = ResponseObject()
  223. if request.method == 'POST':
  224. request.encoding = 'utf-8'
  225. request_dict = request.POST
  226. elif request.method == 'GET':
  227. request.encoding = 'utf-8'
  228. request_dict = request.GET
  229. else:
  230. return response.json(801)
  231. token = request_dict.get('token', None)
  232. deviceContent = request_dict.get('content', None)
  233. if token is not None and deviceContent is not None:
  234. tko = TokenObject(token)
  235. tko.valid()
  236. response.lang = tko.lang
  237. if tko.code == 0:
  238. userID = tko.userID
  239. if userID is not None:
  240. return addNewUserEquipment(userID, deviceContent, response)
  241. else:
  242. return response.json(309)
  243. else:
  244. return response.json(tko.code)
  245. else:
  246. return response.json(444, 'token,content')
  247. @csrf_exempt
  248. def delUserEquipmentInterface(request, *callback_args,
  249. **callback_kwargs):
  250. '''
  251. 删除用户设备
  252. :param request:
  253. :param callback_args: 表示任何多个无名参数,tuple类型
  254. :param callback_kwargs: 表示关键字参数,dict类型
  255. :return:
  256. '''
  257. response = ResponseObject()
  258. if request.method == 'POST':
  259. request.encoding = 'utf-8'
  260. request_dict = request.POST
  261. elif request.method == 'GET':
  262. request.encoding = 'gb2312'
  263. request_dict = request.GET
  264. else:
  265. return response.json(801)
  266. token = request_dict.get('token', None)
  267. id = request_dict.get('id', None)
  268. if token is not None and id is not None:
  269. tko = TokenObject(token)
  270. tko.valid()
  271. response.lang = tko.lang
  272. if tko.code == 0:
  273. userID = tko.userID
  274. if userID is not None:
  275. return delUserEquipment(userID, id, response)
  276. else:
  277. return response.json(309)
  278. else:
  279. return response.json(tko.code)
  280. else:
  281. return response.json(800)
  282. @csrf_exempt
  283. def modifyUserEquipmentInterface(request, *callback_args,
  284. **callback_kwargs):
  285. '''
  286. 修改用户设备
  287. :param request:
  288. :param callback_args:
  289. :param callback_kwargs:
  290. :return:
  291. '''
  292. response = ResponseObject()
  293. if request.method == 'POST':
  294. request.encoding = 'utf-8'
  295. request_dict = request.POST
  296. elif request.method == 'GET':
  297. request.encoding = 'gb2312'
  298. request_dict = request.GET
  299. token = request_dict.get('token', None)
  300. deviceContent = request_dict.get('content', None)
  301. id = request_dict.get('id', None)
  302. if token is not None and deviceContent is not None and id is not None:
  303. tko = TokenObject(token)
  304. tko.valid()
  305. response.lang = tko.lang
  306. if tko.code == 0:
  307. userID = tko.userID
  308. if userID is not None:
  309. return modifyUserEquipment(userID, deviceContent, id, response)
  310. else:
  311. return response.json(309)
  312. else:
  313. return response.json(tko.code)
  314. else:
  315. return response.json(444, 'token,content,id')
  316. @csrf_exempt
  317. def showAllUserEquipmentInterface(request, *callback_args, **callback_kwargs):
  318. if request.method == 'POST':
  319. request.encoding = 'utf-8'
  320. fieldDict = request.POST
  321. if request.method == 'GET':
  322. request.encoding = 'utf-8'
  323. fieldDict = request.GET
  324. token = request.POST.get('token', None)
  325. type = request.POST.get('type', None)
  326. response = ResponseObject()
  327. if token != None:
  328. tko = TokenObject(token)
  329. tko.valid()
  330. response.lang = tko.lang
  331. if tko.code == 0:
  332. userID = tko.userID
  333. if userID:
  334. if type == 'PC':
  335. return showAllUserEquipmentPC(userID, fieldDict, response)
  336. else:
  337. return showAllUserEquipment(userID, response)
  338. else:
  339. return response.json(309)
  340. else:
  341. return response.json(tko.code)
  342. else:
  343. return response.json(800)
  344. @csrf_exempt
  345. def findEquipmentInfoInterface(request, *callback_args, **callback_kwargs):
  346. if request.method == 'GET':
  347. request.encoding = 'gb2312'
  348. fieldDict = request.GET
  349. if request.method == 'POST':
  350. request.encoding = 'utf-8'
  351. fieldDict = request.POST
  352. deviceContent = fieldDict.get('content', None)
  353. token = fieldDict.get('token', None)
  354. response = ResponseObject()
  355. if token != None:
  356. tko = TokenObject(token)
  357. tko.valid()
  358. response.lang = tko.lang
  359. if tko.code == 0:
  360. if deviceContent:
  361. try:
  362. deviceContent = json.loads(deviceContent)
  363. except Exception as e:
  364. print(repr(e))
  365. return response.json(10,repr(e))
  366. else:
  367. resultDict = findEquipmentInfo(content=deviceContent, type=2, fieldDict=fieldDict)
  368. else:
  369. resultDict = findEquipmentInfo(content='', type=1, fieldDict=fieldDict)
  370. return response.json(0, resultDict)
  371. else:
  372. return response.json(tko.code)
  373. else:
  374. return response.json(444,'token')