From d24ade9e55501e74591717f8938ac35f5089eca2 Mon Sep 17 00:00:00 2001 From: "jinling.yang" Date: Tue, 11 Oct 2022 21:54:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=B7=A5=E5=8E=82API?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=88=9B=E5=BB=BA=E9=94=80=E5=94=AE=E8=AE=A2?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sf_base/__manifest__.py | 2 +- sf_bpm_api/__init__.py | 2 +- sf_bpm_api/controllers/controllers.py | 21 ++++------- sf_bpm_api/models/__init__.py | 2 + sf_bpm_api/models/http.py | 43 ++++++++++++++++++++++ sf_bpm_api/models/models.py | 28 ++++++++++++++ sf_bpm_api/models/product_template.py | 23 ++++++------ sf_bpm_api/models/sale_order.py | 14 +++++-- sf_bpm_api/views/product_template_view.xml | 24 ++++++++---- 9 files changed, 120 insertions(+), 39 deletions(-) create mode 100644 sf_bpm_api/models/http.py create mode 100644 sf_bpm_api/models/models.py diff --git a/sf_base/__manifest__.py b/sf_base/__manifest__.py index 41d98d4a..0e23a23a 100644 --- a/sf_base/__manifest__.py +++ b/sf_base/__manifest__.py @@ -16,7 +16,7 @@ 'security/ir.model.access.csv', 'views/mrs_base_view.xml', 'views/mrs_common_view.xml', - # 'views/menu_view.xml' + 'views/menu_view.xml' ], 'demo': [ diff --git a/sf_bpm_api/__init__.py b/sf_bpm_api/__init__.py index f9944209..3834eca4 100644 --- a/sf_bpm_api/__init__.py +++ b/sf_bpm_api/__init__.py @@ -1,3 +1,3 @@ # -*-coding:utf-8-*- -# from . import controllers +from . import controllers from . import models diff --git a/sf_bpm_api/controllers/controllers.py b/sf_bpm_api/controllers/controllers.py index 57bea8bf..dd6162c7 100644 --- a/sf_bpm_api/controllers/controllers.py +++ b/sf_bpm_api/controllers/controllers.py @@ -8,7 +8,7 @@ from odoo.http import request class Sf_Bf_Connect(http.Controller): - @http.route('/api/bfm_process_order/list', type='json', auth='none', methods=['GET', 'POST'], csrf=False, + @http.route('/api/bfm_process_order/list', type='json', auth='sf_token', methods=['GET', 'POST'], csrf=False, cors="*") def get_bfm_process_order_list(self, **kw): """ @@ -16,17 +16,10 @@ class Sf_Bf_Connect(http.Controller): :param kw: :return: """ - result = json.loads('result') + datas = request.httprequest.data + ret = json.loads(datas) + ret = json.loads(ret['result']) + request.env['sale.order'].sudo().sale_order_create(ret['delivery_end_date']) order_line_ids = [] - for item in result['bfm_process_order_list']: - order_line_ids.append({ - "long": item['model_length'], - "width": item['model_width'], - "height": item['model_height'], - "volume": item['model_volume'], - "materials_id": item['texture_id'], - "unit_price": item['unit_price'] - # "barcode": item['barcode'] - }) - self.env['sale.order'].sudo().sale_order_create(result['result']) - + # for item in ret['bfm_process_order_list']: + diff --git a/sf_bpm_api/models/__init__.py b/sf_bpm_api/models/__init__.py index e843bf92..4c183668 100644 --- a/sf_bpm_api/models/__init__.py +++ b/sf_bpm_api/models/__init__.py @@ -1,4 +1,6 @@ from . import sale_order from . import product_template +from . import http +from . import models diff --git a/sf_bpm_api/models/http.py b/sf_bpm_api/models/http.py new file mode 100644 index 00000000..dfe6c75c --- /dev/null +++ b/sf_bpm_api/models/http.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +import json +import logging +import base64 +import datetime +import time +import hashlib +from odoo import fields, models, api +from odoo.http import request, AuthenticationError +import redis + +__author__ = 'jinling.yang' + +_logger = logging.getLogger(__name__) + + +class Http(models.AbstractModel): + _inherit = 'ir.http' + + @classmethod + def _auth_method_sf_token(cls): + # 从headers.environ中获取对方传过来的token,timestamp,加密的校验字符串 + datas = request.httprequest.headers.environ + if 'HTTP_TOKEN' in datas: + _logger.info('token:%s' % datas['HTTP_TOKEN']) + # 查询密钥 + factory_secret = request.env['res.partner'].sudo().search( + [('sf_token', '=', datas['HTTP_TOKEN'])], limit=1) + if not factory_secret: + raise AuthenticationError('无效的token') + timestamp_str = int(time.time()) + # 设置API接口请求时间,不能超过5秒 + deltime = datetime.timedelta(seconds=60) + if abs(int(datas['HTTP_TIMESTAMP'])-timestamp_str) > deltime.seconds: + raise AuthenticationError('请求已过期') + # 获得sha1_str加密字符串 + post_time = int(datas['HTTP_TIMESTAMP']) + check_str = '%s%s%s' % (datas['HTTP_TOKEN'], post_time, factory_secret.sf_secret_key) + check_sf_str = hashlib.sha1(check_str.encode('utf-8')).hexdigest() + if check_sf_str != datas['HTTP_CHECKSTR']: + raise AuthenticationError('数据校验不通过') + else: + raise AuthenticationError('请求参数中无token') diff --git a/sf_bpm_api/models/models.py b/sf_bpm_api/models/models.py new file mode 100644 index 00000000..3eeac229 --- /dev/null +++ b/sf_bpm_api/models/models.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +import logging +import uuid +import string +import random + + +from odoo import fields, models, api + +__author__ = 'jinling.yang' + +_logger = logging.getLogger(__name__) + + +class ResPartner(models.Model): + _inherit = 'res.partner' + + # 获取token,token自动生成且唯一 + def get_token(self): + return uuid.uuid1() + + # 获取密钥(大小字母+数字随机) + def get_secret(self): + ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 16)) + return ran_str + + sf_token = fields.Char(u'Token', default=get_token) + sf_secret_key = fields.Char(u'密钥', default=get_secret) \ No newline at end of file diff --git a/sf_bpm_api/models/product_template.py b/sf_bpm_api/models/product_template.py index 177351df..3d6b1247 100644 --- a/sf_bpm_api/models/product_template.py +++ b/sf_bpm_api/models/product_template.py @@ -9,11 +9,11 @@ class ResProductTemplate(models.Model): detailed_type = fields.Selection(default='product') list_price = fields.Float(digits=(16, 3)) invoice_policy = fields.Selection(default='delivery') - long = fields.Float('长[mm]', digits=(16, 3)) - width = fields.Float('宽[mm]', digits=(16, 3)) - height = fields.Float('高[mm]', digits=(16, 3)) - volume = fields.Float('体积[mm³]', digits=(16, 3)) - precision = fields.Float('精度要求', digits=(16, 3)) + model_long = fields.Float('长[mm]', digits=(16, 3)) + model_width = fields.Float('宽[mm]', digits=(16, 3)) + model_height = fields.Float('高[mm]', digits=(16, 3)) + model_volume = fields.Float('体积[mm³]', digits=(16, 3)) + model_precision = fields.Float('精度要求', digits=(16, 3)) materials_id = fields.Many2one('mrs.production.materials', string='材料') # materials_type_id = fields.Many2one('mrs.materials.model', string='型号') # surface_technics_id = fields.Many2one('mrs.production.process', string='表面工艺') @@ -21,15 +21,16 @@ class ResProductTemplate(models.Model): unit_price = fields.Float('单价') amount = fields.Integer('数量') - # 业务平台分配工厂时调用该方法创建产品,(调用该方法时用prototype) + # 业务平台分配工厂时调用该方法创建产品 def product_create(self, products): self.env['product.template'].create({ 'name': products['order_number'], - 'length': products['length'], - 'width': products['width'], - 'height': products['height'], - 'volume': products['volume'], + 'model_long': products['length'], + 'model_width': products['width'], + 'model_height': products['height'], + 'model_volume': products['volume'], 'materials_id': products['materials_id'], 'unit_price': products['unit_price'], - 'amount': products['amount'] + 'amount': products['amount'], + 'barcode':products['barcode'], }) diff --git a/sf_bpm_api/models/sale_order.py b/sf_bpm_api/models/sale_order.py index 7faba3f0..e502c388 100644 --- a/sf_bpm_api/models/sale_order.py +++ b/sf_bpm_api/models/sale_order.py @@ -1,5 +1,6 @@ from odoo import models, fields from odoo.exceptions import ValidationError +import datetime class ReSaleOrder(models.Model): @@ -7,8 +8,13 @@ class ReSaleOrder(models.Model): deadline_of_delivery = fields.Date('交货截止日期') - # 业务平台分配工厂时调用该方法先创建销售订单(调用该方法时用prototype) - def sale_order_create(self, order): - self.env['sale.order'].create({ - 'deadline_of_delivery': order['deadline_of_delivery'] + # 业务平台分配工厂时调用该方法先创建销售订单 + def sale_order_create(self, deadline_of_delivery): + now = datetime.datetime.now() + self.env['sale.order'].sudo().create({ + 'company_id': 1, + 'date_order': now, + 'name': '12', + 'partner_id': 1, + 'deadline_of_delivery': deadline_of_delivery }) diff --git a/sf_bpm_api/views/product_template_view.xml b/sf_bpm_api/views/product_template_view.xml index 9754f938..92620d4e 100644 --- a/sf_bpm_api/views/product_template_view.xml +++ b/sf_bpm_api/views/product_template_view.xml @@ -6,14 +6,22 @@ product.template - - - - - - - - + + + + + + + + + + + + + + + +