import base64 import urllib.parse from odoo.http import Controller, request, route from datetime import datetime, timedelta from odoo import models, fields, api, http, SUPERUSER_ID, _ import logging _logger = logging.getLogger(__name__) class BusController(Controller): @route('/api/v1/addFace', methods=['POST'], type='json', auth='none', csrf=False) def get_model_kaoqin(self): """ 获取用户录入考勤机数据,和系统内用户绑定关联 :return: """ _logger.info("----------------/api/v1/addFace---------接口收到响应-------------------------------") data = request.httprequest.json user_name = data.get('name') name = data.get('sn') attendance_iot_id = request.env(user=SUPERUSER_ID)["attendance.iot"].search([('name', '=', name)]) if not attendance_iot_id: request.env(user=SUPERUSER_ID)["attendance.iot"].create({'name': name,}) employee = request.env(user=SUPERUSER_ID)["hr.employee"].search([('name', '=', user_name)]) # 判断员工是否绑定多台设备 if employee and attendance_iot_id not in employee.attendance_iot_ids.mapped('attendance_iot_id'): employee.write({ 'image_1920': data.get('photo'), 'attendance_iot_ids': [(0, 0, { 'employee_id': employee.id, 'attendance_address': '', 'attendance_iot_id': attendance_iot_id.id })] }) _logger.info("-------------------------%s创建成功-------------------------------" % (employee.name)) return {"Result": 0, "Msg": "添加成功"} @route('/api/v1/record/face', methods=['POST'], type='json', auth='none', csrf=False) def get_model_identify(self): """ 获取上传识别成功记录 :return: """ _logger.info("----------------/api/v1/record/face---------接口收到响应-------------------------------") data = request.httprequest.json user_id = data.get('logs')[0].get('user_id') employee_id = request.env(user=SUPERUSER_ID)["hr.employee"].search([('mobile_phone', '=', user_id)]) recog_time_str = data.get('logs')[0].get("recog_time") # checkin_time = datetime.strptime(recog_time_str, "%Y-%m-%d %H:%M:%S") + timedelta(hours=8) checkin_time = datetime.strptime(recog_time_str, "%Y-%m-%d %H:%M:%S") attendance_iot_id = request.env(user=SUPERUSER_ID)["attendance.iot"].search([('name', '=', data.get('sn'))]) if employee_id: request.env(user=SUPERUSER_ID)["hr.attendance.detail"].create({ 'employee_id': employee_id.id, # 员工 'checkin_time': checkin_time, # 打卡时间 'source': 'iot', # 打卡来源 'attendance_iot_id': attendance_iot_id.id, # 打卡设备 }) return {"Result": 0, "Msg": "添加成功"} @route('/api/v1/user/inf_photo', methods=['POST'], type='json', auth='none', csrf=False) def inf_photo(self): """ 更新用户信息 :return: """ _logger.info("----------------/api/v1/user/inf_photo---------接口收到响应-------------------------------") data = request.httprequest.json user_data = data['content'][0] user_id = user_data.get('user_id') vl_photo = user_data.get('vl_photo') sn = data.get('sn') # 设备号 # 查找员工 employee = request.env(user=SUPERUSER_ID)['hr.employee'].search([('mobile_phone', '=', user_id)], limit=1) iot_device = request.env(user=SUPERUSER_ID)['attendance.iot'].search([('name', '=', sn)], limit=1) if not iot_device: request.env(user=SUPERUSER_ID)["attendance.iot"].create({'name': sn}) iot_users = request.env(user=SUPERUSER_ID)['attendance.iot.user'].search([('employee_id', '=', employee.id), ('attendance_iot_id', '=', iot_device.id)]) # employee.write({'image_1920': base64.b64encode(urllib.parse.unquote(vl_photo))}) if not iot_users: request.env(user=SUPERUSER_ID)['attendance.iot.user'].create({ 'employee_id': employee.id, 'attendance_iot_id': iot_device.id, }) return {"Result": 0, "Msg": "添加成功"}