中国本土应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.7KB

  1. # -*- coding: utf-8 -*-
  2. from odoo import api, fields, models, tools, _
  3. import logging
  4. import hashlib
  5. import random
  6. import hashlib
  7. from Crypto.Cipher import AES
  8. from passlib.context import CryptContext
  9. import xml.etree.cElementTree as ET
  10. import hashlib
  11. _logger = logging.getLogger(__name__)
  12. NUMLIST = ["0","1","2","3","4","5","6","7","8","9","q","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","W","R","S","T","U","V","W","X","Y","Z",]
  13. class WecomApiToolsSecurity(models.AbstractModel):
  14. _name = "wecomapi.tools.security"
  15. _description = "Wecom API Tools - Security"
  16. def random_str(self, num):
  17. """
  18. 生成随机字符串
  19. :return:
  20. """
  21. rang = num
  22. if rang == None:
  23. random_str = "".join(random.choice(NUMLIST) for i in range(8))
  24. else:
  25. random_str = "".join(random.choice(NUMLIST) for i in range(int(rang)))
  26. return random_str
  27. def random_passwd(self, num):
  28. """
  29. 生成随机密码
  30. :return:
  31. """
  32. rang = num
  33. if rang == None:
  34. passwd = "".join(random.choice(NUMLIST) for i in range(8))
  35. else:
  36. passwd = "".join(random.choice(NUMLIST) for i in range(int(rang)))
  37. crypt_context = CryptContext(
  38. schemes=["pbkdf2_sha512", "plaintext"], deprecated=["plaintext"]
  39. )
  40. hash_password = (
  41. crypt_context.hash
  42. if hasattr(crypt_context, "hash")
  43. else crypt_context.encrypt
  44. )
  45. return hash_password(passwd)
  46. def generate_jsapi_signature(self, jsapi_ticket, nonceStr, timestamp):
  47. """
  48. 使用sha1加密算法,生成JSAPI的签名
  49. ------------------------------
  50. company: 公司
  51. nonceStr: 生成签名的随机串
  52. timestamp: 生成签名的时间戳
  53. url: 当前网页的URL, 不包含#及其后面部分
  54. """
  55. # 生成签名前,刷新 ticke .search([(("is_wecom_organization", "=", True))])
  56. # company.sudo().get_jsapi_ticket()
  57. ICP = self.env["ir.config_parameter"].sudo()
  58. web_base_url = ICP.get_param("web.base.url", default="http://localhost:8069")
  59. if web_base_url[-1] == "/":
  60. web_base_url = web_base_url + "web"
  61. else:
  62. web_base_url = web_base_url + "/web"
  63. jsapi_ticket_str = ("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s") % (
  64. jsapi_ticket,
  65. nonceStr,
  66. timestamp,
  67. web_base_url,
  68. )
  69. encrypts = hashlib.sha1(jsapi_ticket_str.encode("utf-8")).hexdigest()
  70. return encrypts
上海开阖软件有限公司 沪ICP备12045867号-1