From 48e0809bd735c05857a8cb6b08884fbd7de1b8ca Mon Sep 17 00:00:00 2001 From: mgw <1392924357@qq.com> Date: Wed, 31 May 2023 18:13:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=AE=A1=E6=89=B9=E6=A8=A1=E6=9D=BF~=E9=99=A4?= =?UTF-8?q?=E2=80=9D=E6=8E=A7=E4=BB=B6=E9=85=8D=E7=BD=AE=E2=80=9C=E5=A4=96?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sf_wxwork_approval/models/__init__.py | 1 + sf_wxwork_approval/models/wx_work_api.py | 177 ++++++++++-------- .../wxwork_approval_template_controls.py | 168 +++++++++-------- sf_wxwork_approval/models/wxwork_settings.py | 129 ++++++++++++- .../security/ir.model.access.csv | 4 + .../views/wxwork_approval_template_view.xml | 94 ++++++++-- 6 files changed, 392 insertions(+), 181 deletions(-) diff --git a/sf_wxwork_approval/models/__init__.py b/sf_wxwork_approval/models/__init__.py index 96798de7..9c7125e6 100644 --- a/sf_wxwork_approval/models/__init__.py +++ b/sf_wxwork_approval/models/__init__.py @@ -5,5 +5,6 @@ from . import wxwork_approval_template_controls from . import wxwork_settings from . import wxwork_approval_template_summary from . import we_approval_record +from . import wx_work_api # from . import res_config_setting diff --git a/sf_wxwork_approval/models/wx_work_api.py b/sf_wxwork_approval/models/wx_work_api.py index eb1b4130..c07c2a3f 100644 --- a/sf_wxwork_approval/models/wx_work_api.py +++ b/sf_wxwork_approval/models/wx_work_api.py @@ -19,6 +19,17 @@ class WxWorkAPI: else: raise Exception(f"获取AccessToken失败: {result}") + def get_template_detail(self, template_data): + url = f"https://qyapi.weixin.qq.com/cgi-bin/oa/gettemplatedetail?access_token={self.access_token}" + headers = {'Content-Type': 'application/json'} + response = requests.post(url, json=template_data, headers=headers) + result = response.json() + if result['errcode'] == 0: + # print(result) + return result + else: + raise Exception(f"获取模板详情请求失败: {result}") + def create_approval_request(self, approval_data): url = f"https://qyapi.weixin.qq.com/cgi-bin/oa/applyevent?access_token={self.access_token}" headers = {'Content-Type': 'application/json'} @@ -60,89 +71,89 @@ class WxWorkAPI: return result['media_id'] -template_id_dict = {"test": "ZvmJBAt7U2iUju9JLePxfkfuN22meQMVTAHymA"} -creator_userid = "MaGuangWei" -corid = 'wweaf7f1caab27a136' -secret = 'L6z-kjrUBOWfhBiLOmX_fNSY3jydDkLUNNWnemafn00' -wx = WxWorkAPI(corid, secret) -media_id = wx.upload_temp_file() -print(media_id) -# access = wx.get_access_token() -approval_datas = { - "creator_userid": creator_userid, - "template_id": template_id_dict['test'], - "use_template_approver": 0, - # "choose_department": 2, - "approver": [ - { - "attr": 2, - "userid": ["MaGuangWei", "YangJinLing"] - }, - { - "attr": 1, - "userid": ["MaGuangWei"] - } - ], - "notifyer": ["MaGuangWei", "PengYuBo"], - # "notifyer": ["MaGuangWei"], - "notify_type": 1, - "apply_data": { - "contents": [ - # 申请事项 - { - "control": "Text", - "id": "Text-1640339319582", - "value": { - "text": "测试测试测试测试测试" - } - }, - # 申请内容 - { - "control": "Textarea", - "id": "Textarea-1640339335659", - "value": { - "text": "odoo>>>企业微信审批" - } - }, - # 附件 - { - "control": "File", - "id": "File-1640339381728", - "value": { - "files": [ - { - "file_id": media_id - } - ] - } - - }, - - ] - }, - "summary_list": [ - { - "summary_info": [{ - "text": "测试附件上传", - "lang": "zh_CN" - }] - }, - # { - # "summary_info": [{ - # "text": "摘要第2行", - # "lang": "zh_CN" - # }] - # }, - # { - # "summary_info": [{ - # "text": "摘要第3行", - # "lang": "zh_CN" - # }] - # } - ] -} -wx.create_approval_request(approval_datas) -print('创建成功') +# template_id_dict = {"test": "ZvmJBAt7U2iUju9JLePxfkfuN22meQMVTAHymA"} +# creator_userid = "MaGuangWei" +# corid = 'wweaf7f1caab27a136' +# secret = 'L6z-kjrUBOWfhBiLOmX_fNSY3jydDkLUNNWnemafn00' +# wx = WxWorkAPI(corid, secret) +# media_id = wx.upload_temp_file() +# print(media_id) +# # access = wx.get_access_token() +# approval_datas = { +# "creator_userid": creator_userid, +# "template_id": template_id_dict['test'], +# "use_template_approver": 0, +# # "choose_department": 2, +# "approver": [ +# { +# "attr": 2, +# "userid": ["MaGuangWei", "YangJinLing"] +# }, +# { +# "attr": 1, +# "userid": ["MaGuangWei"] +# } +# ], +# "notifyer": ["MaGuangWei", "PengYuBo"], +# # "notifyer": ["MaGuangWei"], +# "notify_type": 1, +# "apply_data": { +# "contents": [ +# # 申请事项 +# { +# "control": "Text", +# "id": "Text-1640339319582", +# "value": { +# "text": "测试测试测试测试测试" +# } +# }, +# # 申请内容 +# { +# "control": "Textarea", +# "id": "Textarea-1640339335659", +# "value": { +# "text": "odoo>>>企业微信审批" +# } +# }, +# # 附件 +# { +# "control": "File", +# "id": "File-1640339381728", +# "value": { +# "files": [ +# { +# "file_id": media_id +# } +# ] +# } +# +# }, +# +# ] +# }, +# "summary_list": [ +# { +# "summary_info": [{ +# "text": "测试附件上传", +# "lang": "zh_CN" +# }] +# }, +# # { +# # "summary_info": [{ +# # "text": "摘要第2行", +# # "lang": "zh_CN" +# # }] +# # }, +# # { +# # "summary_info": [{ +# # "text": "摘要第3行", +# # "lang": "zh_CN" +# # }] +# # } +# ] +# } +# wx.create_approval_request(approval_datas) +# print('创建成功') aa = {"errcode": 0, "errmsg": "ok", "template_names": [{"text": "对接开发测试", "lang": "zh_CN"}], "template_content": { "controls": [{"property": {"control": "Text", "id": "Text-1640339319582", diff --git a/sf_wxwork_approval/models/wxwork_approval_template_controls.py b/sf_wxwork_approval/models/wxwork_approval_template_controls.py index cc5e4273..d903ab3f 100644 --- a/sf_wxwork_approval/models/wxwork_approval_template_controls.py +++ b/sf_wxwork_approval/models/wxwork_approval_template_controls.py @@ -6,97 +6,103 @@ from odoo.exceptions import UserError _logger = logging.getLogger(__name__) +# 控件模型 class WxWorkApprovalTemplate(models.Model): _name = 'wxwork.approval.template.controls' _description = "企业微信审批模板控件" + + template_id = fields.Many2one('wxwork.approval.template', string='模板') + property_id = fields.One2many('property.model', 'control_id', string='控件属性') + config_id = fields.One2many('config.model', 'control_id', string='控件配置') + + +# 控件属性模型 +class PropertyModel(models.Model): + _name = 'property.model' _rec_name = "title" - SELECTION_RELATION = { - '申请事项': {'id': 'Text-1640339319582', 'control': 'Text', 'value': '{"text": "文本填写的内容"}'}, - '申请内容': {'id': 'Textarea-1640339335659', 'control': 'Textarea', 'value': '{"text": "文本填写的内容"}'}, - '多行文本': {'id': 'Textarea-1684823453770', 'control': 'Textarea', 'value': '{"text": "文本填写的内容"}'}, - '金额': {'id': 'Money-1684823460232', 'control': 'Money', 'value': '{"new_money": "0"}'}, - '部门': {'id': 'Contact-1684823477216', 'control': 'Contact', - 'value': '{"departments":[{"openapi_id":"2","name":"销售部"},{"openapi_id":"3","name":"生产部"}]}'}, - '成员': {'id': 'Contact-1684823465317', 'control': 'Contact', - 'value': '{"members":[{"userid":"WuJunJie","name":"Jackie"},{"userid":"WangXiaoMing","name":"Tom"}]}'}, - '明细': {'id': 'Table-1684823492261', 'control': 'Table', - 'value': '{"children":[{"list":[' - '{"control":"Text","id":"Text-15111111111","title":' - '[{"text":"明细内文本控件","lang":"zh_CN"}],"value":{"text":"明细文本1"}},' - '{"control":"Money","id":"Text-15111111112","title":[{"text":"明细内金额控件","lang":"zh_CN"}],' - '"value":{"new_money":"700"}}]},' - '{"list":[{"control":"Text","id":"Text-15111111111",' - '"title":[{"text":"明细内文本控件","lang":"zh_CN"}],"value":{"text":"明细文本2"}},' - '{"control":"Money","id":"Text-15111111112","title":[{"text":"明细内金额控件","lang":"zh_CN"}],' - '"value":{"new_money":"900"}}]}]}'}, - '关联申请单': {'id': 'RelatedApproval-1684823485883', 'control': 'RelatedApproval', - 'value': '{"related_approval":[{"sp_no":"202011180001"}]}'}, - '附件': {'id': 'File-1640339381728', 'control': 'File', - 'value': '{"files":[{"file_id":"1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K1aaa"}]}'}, - '数字': {'id': 'File-1640339381728', 'control': 'Number', 'value': '{"new_number": "700"}'}, - '日期/日期+时间': {'id': 'File-1640339381728', 'control': 'Date', - 'value': '{"date": {"type": "day","s_timestamp": "1569859200"}}'}, - '单选/多选': {'id': 'File-1640339381728', 'control': 'Selector', - 'value': '{"selector": ' - '{"type": "multi","options": [{"key": "option-15111111111"},{"key": "option-15222222222"}]}}' - }, - '说明文字': {'id': 'File-1640339381728', 'control': 'Tips'}, - '位置': {'id': 'File-1640339381728', 'control': 'Location', - 'value': '{"location":{"latitude":"30.547239","longitude":"104.063291",' - '"title":"腾讯科技(成都)有限公司(腾讯成都大厦)",' - '"address":"四川省成都市武侯区天府三街198号腾讯成都大厦A座","time":1605690460}}'}, - '公式': {'id': 'File-1640339381728', 'control': 'Formula', 'value': '{"formula":{"value":"5.0"}}'}, - '时长': {'id': 'File-1640339381728', 'control': 'DateRange', - 'value': '{"date_range":{"new_begin":1570550400,"new_end":1570593600,"new_duration":86400}}'}, - '请假组件': {'id': 'File-1640339381728', 'control': 'Vacation', - 'value': '{"vacation":{"selector":{"type":"single","options":[{"key":"3",' - '"value":[{"text":"病假","lang":"zh_CN"}]}],"exp_type":0},' - '"attendance":{"date_range":{"type":"hour","new_begin":1568077200,' - '"new_end":1568368800,"new_duration":291600},"type":1}}}'}, - '出差/外出/加班组件': {'id': 'File-1640339381728', 'control': 'Attendance', - 'value': '{"attendance":{"date_range":{"type":"halfday","new_begin":1570550400,' - '"new_end":1570593600,"new_duration":86400},"type":4}}'}, - - } - - title = fields.Selection( - selection=[(key, key) for key in SELECTION_RELATION.keys()], - string='控件名称', - ) - template_id = fields.Many2one('wxwork.approval.template', string='模板名称') - control_id = fields.Char(string='控件ID') - control = fields.Char(string='控件类型') - value = fields.Char(string='内容') - - placeholder = fields.Char(string='占位符') - require = fields.Boolean(string='必填', compute='_compute_require') - un_print = fields.Boolean(string='不打印') + CONTROL_TYPE_SELECTION = [ + ('Text', '文本'), + ('Textarea', '多行文本'), + ('Number', '数字'), + ('Money', '金额'), + ('Date', '日期/日期+时间'), + ('Selector', '单选/多选'), + ('Contact', '成员/部门'), + ('Tips', '说明文字'), + ('File', '附件'), + ('Table', '明细'), + ('Attendance', '假勤控件'), + ('Vacation', '请假控件'), + ('Location', '位置'), + ('RelatedApproval', '关联审批单'), + ('Formula', '公式'), + ('DateRange', '时长') + ] + control_type = fields.Selection(CONTROL_TYPE_SELECTION, string='控件类型') + control_id = fields.Many2one('wxwork.approval.template.controls', string='控件') + unique_control_id = fields.Char(string='控件ID') + title = fields.Char(string='控件名称', translate=True) + placeholder = fields.Char(string='控件说明', translate=True) + require = fields.Boolean(string='是否必填') + un_print = fields.Boolean(string='是否参与打印') un_replace = fields.Boolean(string='不替换') - display = fields.Boolean(string='显示', compute='_compute_display') + display = fields.Boolean(string='显示') + child_control_id = fields.Many2one('config.model', string='明细子控件') - @api.onchange('title') - def _onchange_title(self): - if self.title: - related_values = WxWorkApprovalTemplate.SELECTION_RELATION[self.title] - self.control_id = related_values['id'] - self.control = related_values['control'] - # 特殊处理:对于"说明文字",将"value"设置为空字符串 - if self.control == 'Tips': - self.value = '' - else: - self.value = related_values['value'] +# 控件配置模型 +class ConfigModel(models.Model): + _name = 'config.model' + _rec_name = "title" - @api.depends('title') - def _compute_require(self): - for record in self: - record.require = record.title in ['申请事项', '申请内容'] + control_id = fields.Many2one('wxwork.approval.template.controls', string='控件') + # 在此添加其他字段以存储不同控件类型的附加类型和属性 + date_type = fields.Selection([('day', '日期'), ('hour', '日期+时间')], string='日期类型') - @api.depends('title') - def _compute_display(self): - for record in self: - record.display = record.title in ['申请事项', '申请内容'] + selector_type = fields.Selection([('single', '单选'), ('multi', '多选')], string='选择器类型') + options = fields.One2many('config.option', 'config_id', string='选项') + + contact_type = fields.Selection( + [('single', '单选'), ('multi', '多选')], + string='联系人类型') + contact_mode = fields.Selection( + [('user', '成员'), ('department', '部门')], + string='联系人模式') + + # table_children = fields.One2many('config.child', 'config_id', string='明细子控件') + table_children = fields.One2many('property.model', 'child_control_id', string='明细子控件') + + attendance_type = fields.Selection([('1', '请假'), ('3', '出差'), ('4', '外出'), ('5', '加班')], string='假勤组件类型') + attendance_date_range_type = fields.Selection([('hour', '分钟'), ('halfday', '上午/下午')], string='假勤时间刻度') + + vacation_list = fields.One2many('config.option', 'config_id', string='假期类型列表') + + +class ConfigOption(models.Model): + _name = 'config.option' + + key = fields.Char(string='选项Key') + text = fields.Char(string='选项Text') + lang = fields.Char(string='语言') + config_id = fields.Many2one('config.model', string='配置') + + +class ConfigChild(models.Model): + _name = 'config.child' + + control = fields.Char(string='控件类型') + control_id = fields.Char(string='控件ID') + # 其他属性... + + +class VacationType(models.Model): + _name = 'vacation.type' + + config_id = fields.Many2one('config.model', string='配置') + vacation_id = fields.Integer(string='假期ID') + name = fields.Char(string='假期名称') + lang = fields.Char(string='语言') class ApproverLine(models.Model): diff --git a/sf_wxwork_approval/models/wxwork_settings.py b/sf_wxwork_approval/models/wxwork_settings.py index 862bc97d..8b62e334 100644 --- a/sf_wxwork_approval/models/wxwork_settings.py +++ b/sf_wxwork_approval/models/wxwork_settings.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- +import json from odoo import api, fields, models +from odoo.addons.sf_wxwork_approval.models import wx_work_api class WxSettings(models.Model): @@ -10,7 +12,130 @@ class WxSettings(models.Model): wx_work_app = fields.Char(string='企业微信应用名称') wx_work_corp_id = fields.Char(string='企业微信CorpID') wx_work_secret = fields.Char(string='企业微信Secret') - wx_work_templates = fields.One2many('wxwork.template.settings', 'wxwork_id', string='审批模板') + wx_work_template_ids = fields.Many2many('wxwork.template.settings', string='审批模板') + + def update_template(self): + template_dict = {} + template_list = [] + print(self.wx_work_template_ids) + for template in self.wx_work_template_ids: + # template_dict[template.sp_name] = template.template_id + template_list.append(template.template_id) + # template_dict = {} + # print(template_list) + wx = wx_work_api.WxWorkAPI(self.wx_work_corp_id, self.wx_work_secret) + # print(wx) + # 获取模型中的所有记录 + all_records = self.env['wxwork.approval.template'].search([]) + all_records1 = self.env['wxwork.approval.template.controls'].search([]) + + # 删除所有记录 + all_records.unlink() + all_records1.unlink() + for template in template_list: + print('====================') + temp_dict = {'template_id': template} + # print(json.dumps(temp_dict)) + # aa = json.dumps(temp_dict) + template_detail = wx.get_template_detail(temp_dict) + # print('template_detail%s' % template_detail) + # + # # 准备用于创建记录的字段值 + # values = { + # 'process_code': template, + # 'name': template_detail['template_names'][0]['text'], + # # 如果需要设置content_ids,请按照正确格式添加关联数据 + # } + # # 使用create方法创建记录 + # new_record = self.env['wxwork.approval.template'].create(values) + + # 解析template_detail中的内容数据 + content_data_list = [] + # print('jjjjjjjj', template_detail['template_content']) + # print('jjjjjjjjjj', template_detail['template_content']['controls']) + for control_data in template_detail['template_content']['controls']: + print('control_data', control_data) + property_data = control_data['property'] + config_values = {} + + # print(property_data) + # 准备用于创建记录的字段值 + property_values = { + 'control_type': property_data.get('control', ''), + 'unique_control_id': property_data.get('id', ''), + 'title': property_data['title'][0]['text'] if 'title' in property_data else '', + 'placeholder': property_data['placeholder'][0]['text'] if 'placeholder' in property_data else '', + 'require': property_data.get('require', None), + 'un_print': property_data.get('un_print', None), + 'un_replace': property_data.get('un_replace', None), + 'display': property_data.get('display', None), + # 'un_replace': template_detail['template_names'][0]['text'], + # 如果需要设置content_ids,请按照正确格式添加关联数据 + } + # print('property_values', property_values) + # config_data = None + + if 'config' in control_data: + + config_data = control_data['config'] + # print('config_data===', config_data) + if '"date":' in config_data: + # config_values = { + # 'date_type': config_data.get('type', ''), + # } + config_values['date_type'] = config_data.get('type', '') + if '"selector":' in config_data: + # config_values = { + # 'selector_type': config_data.get('type', ''), + # # 'options': config_data['title'][0]['text'] if 'title' in property_data else '', + # } + config_values['selector_type'] = config_data.get('type', '') + if '"contact":' in config_data: + # config_values = { + # 'contact_type': config_data.get('type', ''), + # 'contact_mode': config_data.get('mode', ''), + # + # } + config_values['contact_type'] = config_data.get('type', '') + config_values['contact_mode'] = config_data.get('mode', '') + if '"table":' in config_data: + pass + # config_values = { + # 'table_children': config_data.get('un_print', None), + # } + if '"attendance":' in config_data: + pass + # config_values = { + # 'attendance_type': config_data.get('un_replace', None), + # 'attendance_date_range_type': config_data.get('display', None), + # # 如果需要设置content_ids,请按照正确格式添加关联数据 + # } + if '"vacation_list":' in config_data: + pass + # config_values = { + # 'vacation_list': config_data['template_names'][0]['text'], + # # 如果需要设置content_ids,请按照正确格式添加关联数据 + # } + + content_data_list.append((0, 0, { + 'property_id': [(0, 0, property_values)], + 'config_id': [(0, 0, config_values)], + })) + else: + content_data_list.append((0, 0, { + 'property_id': [(0, 0, property_values)], + # 'config_id': [(0, 0, config_values)], + })) + + # 准备用于创建记录的字段值,包括content_ids数据 + values = { + 'process_code': template, + 'name': template_detail['template_names'][0]['text'], + 'content_ids': content_data_list, + } + + # 使用create方法创建记录 + new_record = self.env['wxwork.approval.template'].create(values) class WxTemplateSettings(models.Model): @@ -19,4 +144,4 @@ class WxTemplateSettings(models.Model): sp_name = fields.Char(string='审批模板名称') template_id = fields.Char(string='审批模板ID') - wxwork_id = fields.Many2one('wxwork.settings', string='企业微信') + # wxwork_id = fields.Many2one('wxwork.settings', string='企业微信') diff --git a/sf_wxwork_approval/security/ir.model.access.csv b/sf_wxwork_approval/security/ir.model.access.csv index 6315b0d5..6374ff3a 100644 --- a/sf_wxwork_approval/security/ir.model.access.csv +++ b/sf_wxwork_approval/security/ir.model.access.csv @@ -7,6 +7,10 @@ access_wxwork_approval_approver_line,wxwork.approval.approver_line,model_wxwork_ access_wxwork_settings,wxwork.settings,model_wxwork_settings,base.group_user,1,1,1,1 access_wxwork_approval_template_summary,wxwork.approval.template.summary,model_wxwork_approval_template_summary,base.group_user,1,1,1,1 access_wxwork_template_settings,wxwork.template.settings,model_wxwork_template_settings,base.group_user,1,1,1,1 +access_property_model,property.model,model_property_model,base.group_user,1,1,1,1 +access_config_model,config.model,model_config_model,base.group_user,1,1,1,1 +access_config_option,config.option,model_config_option,base.group_user,1,1,1,1 +access_vacation_type,vacation.type,model_vacation_type,base.group_user,1,1,1,1 diff --git a/sf_wxwork_approval/views/wxwork_approval_template_view.xml b/sf_wxwork_approval/views/wxwork_approval_template_view.xml index 175ab1dc..ce1304bb 100644 --- a/sf_wxwork_approval/views/wxwork_approval_template_view.xml +++ b/sf_wxwork_approval/views/wxwork_approval_template_view.xml @@ -10,8 +10,8 @@ - + @@ -23,8 +23,8 @@
- + @@ -33,7 +33,7 @@ - +