HikAddcard.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import requests
  2. import json
  3. import datetime
  4. import random
  5. from Utils.HikAccessToken import hik_access_token
  6. def datetime_now():
  7. datetimenow = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  8. return datetimenow
  9. def getcardNo():
  10. all_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  11. captcha = ''
  12. for _ in range(15):
  13. a = random.choice(all_chars)
  14. captcha += a # 连接字符串
  15. return captcha
  16. def hik_getcard(jobids):
  17. card_lists = []
  18. for i in jobids:
  19. if i is not None:
  20. card = {
  21. "cardNo": getcardNo(),
  22. "cardType": "normalCard",
  23. "employeeNo": i
  24. }
  25. card_lists.append(card)
  26. return card_lists
  27. def hik_addcard(hik_accesstoken,jobids):
  28. headers = {"content-type": "application/json"}
  29. data = {
  30. "cards": hik_getcard(jobids)
  31. }
  32. print(data)
  33. response = requests.post(
  34. url="https://api2.hik-cloud.com/api/v1/open/basic/cards/batchCreate?access_token=%s" % hik_accesstoken,
  35. headers=headers,
  36. data=json.dumps(data)
  37. )
  38. print(response.json())
  39. if response.json()["code"] == 200:
  40. print("%s,创建卡片成功"%(datetime_now()))
  41. else:
  42. print(datetime_now(),response.json()["message"])
  43. # return response.text
  44. #if __name__ == '__main__':
  45. #with open("../config.json") as f:
  46. # data = json.load(f)
  47. # appkey = data["appkey"]
  48. # appsecret = data["appsecret"]
  49. # client_id = data["client_id"]
  50. # client_secret = data["client_secret"]
  51. # groupId = data["groupId"]
  52. # sleeptime = data["sleeptime"]
  53. # 获取Hik token
  54. # hik_accesstoken = hik_access_token(client_id, client_secret)
  55. # print(hik_accesstoken)
  56. # diff_value = ["2900","2893"]
  57. # hik_addcard(hik_accesstoken, list(diff_value))