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

253 lines
9.4KB

  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. from odoo import models, fields, api, _
  4. from odoo.exceptions import UserError, ValidationError
  5. import time
  6. from odoo.addons.oec_im_wecom_api.api.wecom_abstract_api import ApiException
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. class ResConfigSettings(models.TransientModel):
  10. _inherit = "res.config.settings"
  11. company_id = fields.Many2one(
  12. "res.company",
  13. string="Company",
  14. required=True,
  15. default=lambda self: self.env.company,
  16. )
  17. wecom_api_domain_ip = fields.Char(
  18. "Wecom API Domain IP",
  19. config_parameter="wecom.api_domain_ip",
  20. )
  21. # 加入企微微信二维码
  22. contacts_join_qrcode_enabled = fields.Boolean(
  23. related="company_id.oec_im_wecom_contacts_join_qrcode_enabled", readonly=False
  24. )
  25. contacts_join_qrcode = fields.Char(
  26. related="company_id.oec_im_wecom_contacts_join_qrcode", readonly=False
  27. )
  28. contacts_join_qrcode_size_type = fields.Selection(
  29. related="company_id.oec_im_wecom_contacts_join_qrcode_size_type", readonly=False
  30. )
  31. contacts_join_qrcode_last_time = fields.Datetime(
  32. related="company_id.oec_im_wecom_contacts_join_qrcode_last_time", readonly=False
  33. )
  34. # 通讯录应用
  35. contacts_app_id = fields.Many2one(
  36. related="company_id.contacts_app_id", readonly=False
  37. )
  38. contacts_secret = fields.Char(related="contacts_app_id.secret", readonly=False)
  39. # contacts_access_token = fields.Char(related="contacts_app_id.access_token")
  40. contacts_app_config_ids = fields.One2many(
  41. related="contacts_app_id.app_config_ids",
  42. readonly=False,
  43. )
  44. contacts_app_callback_service_ids = fields.One2many(
  45. related="contacts_app_id.app_callback_service_ids", readonly=False
  46. )
  47. # 其他应用
  48. module_oec_im_wecom_contacts_sync = fields.Boolean("WeCom Contacts Synchronized")
  49. module_oec_im_wecom_material = fields.Boolean("WeCom Material")
  50. module_oec_im_wecom_auth_oauth = fields.Boolean("WeCom Authentication")
  51. module_oec_im_wecom_message = fields.Boolean("WeCom Message")
  52. module_oec_im_wecom_portal = fields.Boolean("Wecom Portal")
  53. module_oec_im_wecom_msgaudit = fields.Boolean("Wecom Session Content Archive")
  54. module_oec_im_wecom_attendance = fields.Boolean("WeCom Attendances")
  55. module_oec_im_wecom_approval = fields.Boolean("WeCom Approvals")
  56. def cron_get_join_qrcode(self):
  57. """
  58. 自动任务获取加入企业二维码
  59. """
  60. companies = self.env["res.company"].search(
  61. [
  62. ("is_wecom_organization", "=", True),
  63. ("oec_im_wecom_contacts_join_qrcode_enabled", "=", True),
  64. ]
  65. )
  66. for company in companies:
  67. _logger.info(
  68. _("Automatic task:Start to get join enterprise QR code of company [%s]")
  69. % (company.name)
  70. )
  71. if not company.contacts_app_id:
  72. _logger.info(
  73. _("Automatic task:Please bind the contact app of company [%s]!")
  74. % (company.name)
  75. )
  76. elif not company.oec_im_wecom_contacts_join_qrcode_enabled:
  77. _logger.info(
  78. _(
  79. "Automatic task:Please enable the company [%s] to join the enterprise wechat QR code function!"
  80. )
  81. % (company.name)
  82. )
  83. else:
  84. try:
  85. wecomapi = self.env["wecom.service_api"].InitServiceApi(
  86. company.corpid, company.contacts_app_id.secret
  87. )
  88. last_time = company.oec_im_wecom_contacts_join_qrcode_last_time
  89. size_type = company.oec_im_wecom_contacts_join_qrcode_size_type
  90. # 超期
  91. overdue = False
  92. if last_time:
  93. overdue = self.env[
  94. "wecomapi.tools.datetime"
  95. ].cheeck_days_overdue(last_time, 7)
  96. if not last_time or overdue:
  97. response = wecomapi.httpCall(
  98. self.env["wecom.service_api_list"].get_server_api_call(
  99. "GET_JOIN_QRCODE"
  100. ),
  101. {"size_type": size_type},
  102. )
  103. if response["errcode"] == 0:
  104. company.write(
  105. {
  106. "oec_im_wecom_contacts_join_qrcode": response[
  107. "join_qrcode"
  108. ],
  109. "oec_im_wecom_contacts_join_qrcode_last_time": datetime.datetime.now(),
  110. }
  111. )
  112. except ApiException as ex:
  113. error = self.env["wecom.service_api_error"].get_error_by_code(
  114. ex.errCode
  115. )
  116. _logger.warning(
  117. _(
  118. "Automatic task:Error in obtaining the QR code of joining company [%s],error code: %s, error name: %s, error message: %s"
  119. )
  120. % (company.name, str(ex.errCode), error["name"], ex.errMsg)
  121. )
  122. _logger.info(
  123. _(
  124. "Automatic task:End obtaining joining enterprise QR code of company [%s]"
  125. )
  126. % (company.name)
  127. )
  128. def get_join_qrcode(self):
  129. """
  130. 获取加入企业二维码
  131. """
  132. # self.contacts_app_id.get_join_qrcode()
  133. ir_config = self.env["ir.config_parameter"].sudo()
  134. debug = ir_config.get_param("wecom.debug_enabled")
  135. if not self.contacts_app_id:
  136. raise ValidationError(_("Please bind contact app!"))
  137. if not self.contacts_join_qrcode_enabled:
  138. raise ValidationError(
  139. _("Please enable the function of join enterprise QR code!")
  140. )
  141. if debug:
  142. _logger.info(
  143. _("Start getting join enterprise QR code of company [%s]")
  144. % (self.company_id.name)
  145. )
  146. try:
  147. wecomapi = self.env["wecom.service_api"].InitServiceApi(
  148. self.company_id.corpid, self.contacts_app_id.secret
  149. )
  150. last_time = self.contacts_join_qrcode_last_time
  151. size_type = self.contacts_join_qrcode_size_type
  152. # 超期
  153. overdue = False
  154. if last_time:
  155. overdue = self.env["wecomapi.tools.datetime"].cheeck_days_overdue(
  156. last_time, 7
  157. )
  158. if not last_time or overdue:
  159. response = wecomapi.httpCall(
  160. self.env["wecom.service_api_list"].get_server_api_call(
  161. "GET_JOIN_QRCODE"
  162. ),
  163. {"size_type": size_type},
  164. )
  165. if response["errcode"] == 0:
  166. self.company_id.write(
  167. {
  168. "oec_im_wecom_contacts_join_qrcode": response[
  169. "join_qrcode"
  170. ],
  171. "oec_im_wecom_contacts_join_qrcode_last_time": datetime.datetime.now(),
  172. }
  173. )
  174. return {"type": "ir.actions.client", "tag": "reload"} # 刷新页面
  175. # self.contacts_join_qrcode=response["join_qrcode"]
  176. # self.contacts_join_qrcode_last_time = datetime.datetime.now()
  177. except ApiException as ex:
  178. return self.env["wecomapi.tools.action"].ApiExceptionDialog(
  179. ex, raise_exception=True
  180. )
  181. finally:
  182. if debug:
  183. _logger.info(
  184. _("End getting join enterprise QR code of company [%s]")
  185. % (self.company_id.name)
  186. )
  187. # TODO: 使用任务 获取IP
  188. def get_wecom_api_domain_ip(self):
  189. """
  190. 获取企业微信API域名IP段
  191. """
  192. ir_config = self.env["ir.config_parameter"].sudo()
  193. debug = ir_config.get_param("wecom.debug_enabled")
  194. if not self.contacts_app_id:
  195. raise ValidationError(_("Please bind contact app!"))
  196. if debug:
  197. _logger.info(_("Start to get enterprise wechat API domain name IP segment"))
  198. try:
  199. wecomapi = self.env["wecom.service_api"].InitServiceApi(
  200. self.company_id.corpid, self.contacts_app_id.secret
  201. )
  202. response = wecomapi.httpCall(
  203. self.env["wecom.service_api_list"].get_server_api_call(
  204. "GET_API_DOMAIN_IP"
  205. ),
  206. {},
  207. )
  208. if response["errcode"] == 0:
  209. self.env["ir.config_parameter"].set_param(
  210. "wecom.api_domain_ip", response["ip_list"]
  211. )
  212. return {"type": "ir.actions.client", "tag": "reload"} # 刷新页面
  213. except ApiException as ex:
  214. return self.env["wecomapi.tools.action"].ApiExceptionDialog(
  215. ex, raise_exception=True
  216. )
  217. finally:
  218. if debug:
  219. _logger.info(
  220. _("End obtaining enterprise wechat API domain name IP segment")
  221. )
上海开阖软件有限公司 沪ICP备12045867号-1