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

75 lines
2.8KB

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import time
  4. from datetime import datetime, timedelta
  5. from odoo import _, api, fields, models
  6. from odoo.exceptions import UserError
  7. from odoo.addons.oec_im_wecom_api.api.wecom_abstract_api import ApiException
  8. _logger = logging.getLogger(__name__)
  9. class WeComApps(models.Model):
  10. _inherit = "wecom.apps"
  11. def cron_get_app_jsapi_ticket(self):
  12. """
  13. 定时任务,每隔两小时更新应用的jsapi_ticket
  14. """
  15. for app in self:
  16. if (
  17. app.company_id.is_wecom_organization
  18. and app.company_id.corpid
  19. and app.secret
  20. ):
  21. _logger.info(
  22. _("Automatic tasks:Start getting JSAPI ticket for app [%s]")
  23. % (app.name)
  24. )
  25. if (
  26. app.jsapi_ticket_expiration_time
  27. and app.jsapi_ticket_expiration_time > datetime.now()
  28. ):
  29. _logger.info(
  30. _(
  31. "Automatic tasks:JSAPI ticket for app [%s] is not expired, no need to update"
  32. )
  33. % (app.name)
  34. )
  35. else:
  36. try:
  37. wecom_api = self.env["wecom.service_api"].InitServiceApi(
  38. app.company_id.corpid, app.secret
  39. )
  40. response = wecom_api.httpCall(
  41. self.env["wecom.service_api_list"].get_server_api_call(
  42. "AGENT_GET_TICKET"
  43. ),
  44. {"type": "agent_config"},
  45. )
  46. except ApiException as e:
  47. _logger.error(
  48. _(
  49. "Automatic tasks:Failed to get JSAPI ticket for app [%s], error: %s"
  50. )
  51. % (app.name, e)
  52. )
  53. else:
  54. if response["errcode"] == 0:
  55. app.write(
  56. {
  57. "jsapi_ticket": response["ticket"],
  58. "jsapi_ticket_expiration_time": datetime.now()
  59. + timedelta(seconds=response["expires_in"]),
  60. }
  61. )
  62. _logger.info(
  63. _("Automatic tasks:Successfully get JSAPI ticket for app [%s]")
  64. % (app.name)
  65. )
  66. _logger.info(
  67. _("Automatic tasks:Start getting app [%s] ticket for company [%s]")
  68. % (app.name, app.company_id.name)
  69. )
上海开阖软件有限公司 沪ICP备12045867号-1