中国本土应用
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.

104 lines
3.7KB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime, timedelta
  3. from odoo.http import request
  4. from odoo import api, fields, models, tools, _
  5. from odoo.addons.oec_im_wecom_api.api.wecom_abstract_api import ApiException
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. class Company(models.Model):
  9. _inherit = "res.company"
  10. # 通讯录
  11. contacts_app_id = fields.Many2one(
  12. "wecom.apps",
  13. string="Contacts Application",
  14. # required=True,
  15. # default=lambda self: self.env.company,
  16. domain="[('company_id', '=', current_company_id)]",
  17. )
  18. oec_im_wecom_contacts_join_qrcode_enabled = fields.Boolean(
  19. string="Enable to join the enterprise QR code",
  20. default=True,
  21. copy=False,
  22. )
  23. oec_im_wecom_contacts_join_qrcode = fields.Char(
  24. string="Join enterprise wechat QR code",
  25. copy=False,
  26. readonly=True,
  27. )
  28. oec_im_wecom_contacts_join_qrcode_size_type = fields.Selection(
  29. [
  30. ("1", "171px x 171px"),
  31. ("2", "399px x 399px"),
  32. ("3", "741px x 741px"),
  33. ("4", "2052px x 2052px"),
  34. ],
  35. string="Join enterprise wechat QR code size type",
  36. default="2",
  37. required=True,
  38. )
  39. oec_im_wecom_contacts_join_qrcode_last_time = fields.Datetime(
  40. string="Get the last time of QR code (UTC)",
  41. copy=False,
  42. )
  43. def cron_get_corp_jsapi_ticket(self):
  44. """
  45. 定时任务,每隔两小时更新企业的jsapi_ticket
  46. """
  47. for company in self:
  48. if (
  49. company.is_wecom_organization
  50. and company.corpid
  51. and company.contacts_app_id
  52. ):
  53. _logger.info(
  54. _("Automatic tasks:Start getting JSAPI ticket for company [%s]")
  55. % (company.name)
  56. )
  57. if (
  58. company.wecom_jsapi_ticket_expiration_time
  59. and company.wecom_jsapi_ticket_expiration_time > datetime.now()
  60. ):
  61. _logger.info(
  62. _(
  63. "The company [%s] ticket is still valid and does not need to be updated!"
  64. )
  65. % (company.name)
  66. )
  67. else:
  68. try:
  69. wecom_api = self.env["wecom.service_api"].InitServiceApi(
  70. self.company_id.corpid, self.contacts_app_id.secret
  71. )
  72. response = wecom_api.httpCall(
  73. self.env["wecom.service_api_list"].get_server_api_call(
  74. "GET_JSAPI_TICKET"
  75. ),
  76. {},
  77. )
  78. except ApiException as ex:
  79. _logger.error(
  80. _("Error in obtaining company [%s] ticket, reason: %s")
  81. % (company.name, ex)
  82. )
  83. else:
  84. if response["errcode"] == 0:
  85. company.write(
  86. {
  87. "wecom_jsapi_ticket": response["ticket"],
  88. "wecom_jsapi_ticket_expiration_time": datetime.now()
  89. + timedelta(seconds=response["expires_in"]),
  90. }
  91. )
  92. finally:
  93. _logger.info(
  94. _("Automatic tasks:End of company [%s] JSAPI ticket update")
  95. % (company.name)
  96. )
上海开阖软件有限公司 沪ICP备12045867号-1