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

172 lines
5.6KB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime, timedelta
  3. from random import weibullvariate
  4. from odoo import models, fields, api, _
  5. from odoo.addons.oec_im_wecom_api.api.wecom_abstract_api import ApiException
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. class ResConfigSettings(models.TransientModel):
  9. _inherit = "res.config.settings"
  10. # 基础
  11. is_wecom_organization = fields.Boolean(
  12. related="company_id.is_wecom_organization", readonly=False
  13. )
  14. company_name = fields.Char(
  15. related="company_id.name", string="Company Name", readonly=False
  16. )
  17. abbreviated_name = fields.Char(
  18. related="company_id.abbreviated_name", readonly=False
  19. )
  20. corpid = fields.Char(string="Corp ID", related="company_id.corpid", readonly=False)
  21. wecom_jsapi_ticket = fields.Char(
  22. string="JSAPI Ticket", related="company_id.wecom_jsapi_ticket", readonly=False
  23. )
  24. wecom_jsapi_ticket_expiration_time = fields.Datetime(
  25. string="Expiration time of JSAPI Ticket",
  26. related="company_id.wecom_jsapi_ticket_expiration_time",
  27. readonly=False,
  28. )
  29. debug_enabled = fields.Boolean("Turn on debug mode", default=True)
  30. resources_path = fields.Char(
  31. "WeCom resources storage path",
  32. config_parameter="wecom.resources_path",
  33. )
  34. global_error_code_url = fields.Char(
  35. "Global error code page URL",
  36. config_parameter="wecom.global_error_code_url",
  37. )
  38. global_error_code_item_selection_code = fields.Char(
  39. "Global error code item selection code",
  40. config_parameter="wecom.global_error_code_item_selection_code",
  41. ) # 错误码排查方法的选取属性。
  42. module_rainbow_community_theme = fields.Boolean("Rainbow Community Theme")
  43. module_oec_im_wecom_contacts = fields.Boolean("WeCom Contacts")
  44. @api.model
  45. def get_values(self):
  46. res = super(ResConfigSettings, self).get_values()
  47. ir_config = self.env["ir.config_parameter"].sudo()
  48. debug_enabled = (
  49. True if ir_config.get_param("wecom.debug_enabled") == "True" else False
  50. )
  51. res.update(
  52. debug_enabled=debug_enabled,
  53. )
  54. return res
  55. def set_values(self):
  56. super(ResConfigSettings, self).set_values()
  57. ir_config = self.env["ir.config_parameter"].sudo()
  58. ir_config.set_param("wecom.debug_enabled", self.debug_enabled or "False")
  59. def open_wecom_company(self):
  60. return {
  61. "type": "ir.actions.act_window",
  62. "name": "My Company",
  63. "view_mode": "form",
  64. "res_model": "res.company",
  65. "res_id": self.env.company.id,
  66. "target": "current",
  67. "context": {
  68. "form_view_initial_mode": "edit",
  69. },
  70. }
  71. @api.depends("company_id")
  72. def _compute_wecom_company_corpid(self):
  73. company_corpid = self.company_id.corpid if self.company_id.corpid else ""
  74. for record in self:
  75. record.wecom_company_corpid = company_corpid
  76. @api.model
  77. def open_wecom_settings(self):
  78. action = self.env["ir.actions.actions"]._for_xml_id(
  79. "oec_im_wecom_base.res_config_settings_view_form"
  80. )
  81. action["target"] = "new"
  82. return action
  83. def get_app_info(self):
  84. """
  85. 获取企业应用信息
  86. :param agentid:
  87. :return:
  88. """
  89. def get_corp_jsapi_ticket(self):
  90. """
  91. 获取企业的jsapi_ticket
  92. :return:
  93. """
  94. ir_config = self.env["ir.config_parameter"].sudo()
  95. debug = ir_config.get_param("wecom.debug_enabled")
  96. if debug:
  97. _logger.info(
  98. _("Start getting ticket for company [%s]") % (self.company_id.name)
  99. )
  100. try:
  101. if (
  102. self.wecom_jsapi_ticket_expiration_time
  103. and self.wecom_jsapi_ticket_expiration_time > datetime.now()
  104. ):
  105. # 未过期,
  106. # print("未过期")
  107. msg = {
  108. "title": _("Tips"),
  109. "message": _("Ticket is still valid, and no update is required!"),
  110. "sticky": False,
  111. }
  112. return self.env["wecomapi.tools.action"].WecomInfoNotification(msg)
  113. else:
  114. # print("过期")
  115. oec_im_wecom_api = self.env["wecom.service_api"].InitServiceApi(
  116. self.company_id.corpid, self.contacts_app_id.secret
  117. )
  118. response = oec_im_wecom_api.httpCall(
  119. self.env["wecom.service_api_list"].get_server_api_call(
  120. "GET_JSAPI_TICKET"
  121. ),
  122. {},
  123. )
  124. # print(response)
  125. except ApiException as ex:
  126. return self.env["wecomapi.tools.action"].ApiExceptionDialog(
  127. ex, raise_exception=True
  128. )
  129. else:
  130. if response["errcode"] == 0:
  131. self.write(
  132. {
  133. "wecom_jsapi_ticket": response["ticket"],
  134. "wecom_jsapi_ticket_expiration_time": datetime.now()
  135. + timedelta(seconds=response["expires_in"]),
  136. }
  137. )
  138. msg = {
  139. "title": _("Tips"),
  140. "message": _("Successfully obtained ticket!"),
  141. "sticky": False,
  142. "next": {
  143. "type": "ir.actions.client",
  144. "tag": "reload",
  145. },
  146. }
  147. return self.env["wecomapi.tools.action"].WecomSuccessNotification(msg)
上海开阖软件有限公司 沪ICP备12045867号-1