297 lines
9.8 KiB
Python
297 lines
9.8 KiB
Python
# !/usr/bin/env python
|
|
# -*- coding:utf-8 -*-
|
|
import base64
|
|
import re
|
|
import requests
|
|
|
|
from ai import agent
|
|
from jimeng import VolcEngineAPIClient
|
|
from router import Router, Response
|
|
from esmart import transform_data, SmartSheet, ESmart, push_message
|
|
from lib import parse_data, get_approval, generate_approval_report_html, new_product_print, SalesTools, CalendarEventDB
|
|
|
|
st = SalesTools()
|
|
ceb = CalendarEventDB()
|
|
esmart = ESmart()
|
|
router = Router()
|
|
vea = VolcEngineAPIClient('AKLTZjg1MGViN2E4ODU1NGY4MzlmYTg2ODRjMzBlYmQ4NTk',
|
|
'WlRoak1qZzJPR1EzTm1JeU5EQmhOamswWWpSa09UbGhNek5pT0RkbE1tRQ==')
|
|
|
|
|
|
def old_handle(request):
|
|
data = request.body
|
|
template_id = data.get("template_id", "C4ZT8ZuPRbKC76KmWCrmRAKyVdneoq9wh1ESaUrrJ")
|
|
if data.get("action") == "get_approval":
|
|
push_message("get_approval数据迁移", f"有人正在使用[action:get_approval]接口", "")
|
|
result = get_approval(template_id)
|
|
result = generate_approval_report_html(result)
|
|
address = data.get("address", "lychang@genesysinfo.net")
|
|
st.send_company_mail(address, "客户拜访审批次数报表", result)
|
|
return Response({"code": 200, "msg": "success", "update": "迁移到[GET](/approval/sales_call)路径访问"}, 304)
|
|
elif template_id == "C4ZT8ZuPRbKC76KmWCrmRAKyVdneoq9wh1ESaUrrJ":
|
|
push_message("approval接口迁移", f"有人正在使用[approval:C4ZT8ZuPRbKC76KmWCrmRAKyVdneoq9wh1ESaUrrJ]接口", "")
|
|
info = parse_data(data, {"Number-1697103952657": "company_code", "Text-1697103919621": "company_name",
|
|
"Text-1728374569134": "company_address", "Date-1728370374837": "visit_time",
|
|
"Date-1728370485478": "end_time"})
|
|
ceb.login(info["_uid"])
|
|
ceb.add(info)
|
|
return Response({"code": 200, "msg": "success", "update": "迁移到[POST](/approval)路径访问"}, 304)
|
|
elif template_id == "3WMWPcxrt28TRT72wqH74KSjMDBhmZPJguv1VEZZ":
|
|
push_message("approval接口迁移", f"有人正在使用[approval:3WMWPcxrt28TRT72wqH74KSjMDBhmZPJguv1VEZZ]接口", "")
|
|
info = parse_data(data, {
|
|
"Selector-1688785038239": "service_name",
|
|
"Number-1661195930200": "company_code",
|
|
"Number-1727161672205": "user_id"
|
|
})
|
|
new_product_print(info)
|
|
return Response({"code": 200, "msg": "success", "update": "迁移到[POST](/approval)路径访问"}, 304)
|
|
else:
|
|
return Response("error", 400)
|
|
|
|
|
|
@router.route('/', methods=['POST'])
|
|
def index(request):
|
|
return old_handle(request)
|
|
|
|
|
|
@router.route('/hello', methods=['GET'])
|
|
def hello(request):
|
|
return Response({"message": "hello", "today": str(esmart.today)})
|
|
|
|
|
|
@router.route('/otp', methods=['POST'])
|
|
def save_otp_data(request):
|
|
# One-Time Password
|
|
data_list = request.body.get("data", [{}])
|
|
sta = SmartSheet(
|
|
"dcqzSDHPl2ZWoLkEB3Id1wMMaW0meMSwGLjfnuHD0VMh0DVidKdal-3wYfsh7Zb1pGn9moDuzjvhtlRZAXoO-Hjg",
|
|
"g6dTI2")
|
|
mapping = {
|
|
"userName": "用户名称",
|
|
"secCode": "公司代码",
|
|
"companyName": "公司名称",
|
|
"count": "使用动态口令次数",
|
|
"date": "时间",
|
|
"goodName": "产品"
|
|
}
|
|
data_list = [transform_data(i, mapping) for i in data_list]
|
|
if sta.batch_add(data_list):
|
|
return Response("success")
|
|
else:
|
|
return Response("error", 500)
|
|
|
|
|
|
@router.route('/approval', methods=['POST'])
|
|
def callback_approval(request):
|
|
template_id = request.body.get("template_id")
|
|
if template_id == "C4ZT8ZuPRbKC76KmWCrmRAKyVdneoq9wh1ESaUrrJ":
|
|
info = parse_data(request.body, {"Number-1697103952657": "company_code", "Text-1697103919621": "company_name",
|
|
"Text-1728374569134": "company_address", "Date-1728370374837": "visit_time",
|
|
"Date-1728370485478": "end_time"})
|
|
ceb.login(info["_uid"])
|
|
ceb.add(info)
|
|
return Response("success")
|
|
elif template_id == "3WMWPcxrt28TRT72wqH74KSjMDBhmZPJguv1VEZZ":
|
|
info = parse_data(request.body, {
|
|
"Selector-1688785038239": "service_name",
|
|
"Number-1661195930200": "company_code",
|
|
"Number-1727161672205": "user_id"
|
|
})
|
|
new_product_print(info)
|
|
return Response("success")
|
|
else:
|
|
return Response("error", 400)
|
|
|
|
|
|
@router.route('/approval/sales_call', methods=['GET'])
|
|
def approval_sales_call_info(request):
|
|
result = get_approval("C4ZT8ZuPRbKC76KmWCrmRAKyVdneoq9wh1ESaUrrJ")
|
|
result = generate_approval_report_html(result)
|
|
address = request.body.get("address", "lychang@genesysinfo.net")
|
|
st.send_company_mail(address, "客户拜访审批次数报表", result)
|
|
return Response("success")
|
|
|
|
|
|
@router.route('/tools/ip', methods=['GET'])
|
|
def tools_ip(request):
|
|
resp = requests.get("http://cip.cc")
|
|
ip = re.search(r"\d+\.\d+\.\d+\.\d+", resp.text).group()
|
|
return Response({"ip": ip})
|
|
|
|
|
|
@router.route('/tools/push_message', methods=['POST'])
|
|
def tools_push_message(request):
|
|
"""发送消息"""
|
|
title = request.body.get('title')
|
|
message = request.body.get('message')
|
|
url = request.body.get('url', '')
|
|
|
|
if not title or not message:
|
|
return Response({
|
|
'success': False,
|
|
'message': '标题和消息内容不能为空'
|
|
}, status_code=400)
|
|
|
|
try:
|
|
push_message(title, message, url)
|
|
return Response({
|
|
'success': True,
|
|
'message': '消息已发送'
|
|
})
|
|
except Exception as e:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'发送消息失败: {str(e)}'
|
|
}, status_code=500)
|
|
|
|
|
|
@router.route('/esmart/refresh', methods=['GET'])
|
|
def esmart_refresh(request):
|
|
"""刷新数据"""
|
|
try:
|
|
esmart.refresh()
|
|
return Response({
|
|
'success': True,
|
|
'message': '数据已成功刷新'
|
|
})
|
|
except Exception as e:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'刷新数据失败: {str(e)}'
|
|
}, status_code=500)
|
|
|
|
|
|
@router.route('/esmart/prepared', methods=['GET'])
|
|
def esmart_prepared(request):
|
|
"""生成待发送数据"""
|
|
try:
|
|
esmart.prepared_message()
|
|
return Response({
|
|
'success': True,
|
|
'message': '待发送数据已成功生成'
|
|
})
|
|
except Exception as e:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'生成数据失败: {str(e)}'
|
|
}, status_code=500)
|
|
|
|
|
|
@router.route('/esmart/push', methods=['GET'])
|
|
def esmart_push(request):
|
|
"""刷新数据"""
|
|
try:
|
|
esmart.push()
|
|
return Response({
|
|
'success': True,
|
|
'message': '数据已成功刷新'
|
|
})
|
|
except Exception as e:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'刷新数据失败: {str(e)}'
|
|
}, status_code=500)
|
|
|
|
|
|
@router.route('/esmart/base_data', methods=['GET'])
|
|
def esmart_base_data(request):
|
|
"""获取基础数据"""
|
|
return Response(esmart.base_data)
|
|
|
|
|
|
@router.route('/esmart/user_info', methods=['GET'])
|
|
def esmart_user_info(request):
|
|
"""获取用户信息"""
|
|
return Response(esmart.user_info)
|
|
|
|
|
|
@router.route('/esmart/security_mapping', methods=['GET'])
|
|
def esmart_security_mapping(request):
|
|
"""获取证券映射信息"""
|
|
return Response(esmart.security_mapping)
|
|
|
|
|
|
@router.route('/ai/image/generate', methods=['POST'])
|
|
@router.route('/image/generate', methods=['POST'])
|
|
def image_generate(request):
|
|
"""绘制相关图像"""
|
|
prompt = request.body.get('prompt', "")
|
|
source = request.body.get('source', "url")
|
|
user = request.body.get('user', {})
|
|
security_code = user.get('seccode', "")
|
|
year = user.get('year', "2023")
|
|
if not security_code:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'user.seccode 不能为空'
|
|
}, 400)
|
|
if not prompt:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'prompt 不能为空'
|
|
}, 400)
|
|
prompt = agent.get_image_prompt(prompt,security_code,year)
|
|
result = vea.run(prompt, source, 1)
|
|
if source != "url":
|
|
result = str(base64.b64encode(result))[2:-1]
|
|
return Response(result)
|
|
|
|
|
|
@router.route('/ai/image/prompt', methods=['POST'])
|
|
@router.route('/image/prompt', methods=['POST'])
|
|
def image_prompt(request):
|
|
"""获取绘制图像详细提示词"""
|
|
prompt = request.body.get('prompt', "")
|
|
if prompt:
|
|
prompt = agent.get_image_prompt(prompt)
|
|
return Response(prompt)
|
|
else:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'prompt 不能为空'
|
|
}, 400)
|
|
|
|
|
|
@router.route('/ai/report/generate', methods=['POST'])
|
|
def report_generate(request):
|
|
"""生成相关报告"""
|
|
security_code = request.body.get('seccode', "")
|
|
year = request.body.get('year', "2023")
|
|
if not security_code:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'seccode 不能为空'
|
|
})
|
|
result = agent.get_report(security_code, year)
|
|
if result:
|
|
return Response(result)
|
|
else:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'无相关报告'
|
|
}, 400)
|
|
|
|
|
|
@router.route('/ai/report/prompt', methods=['POST'])
|
|
def report_prompt(request):
|
|
"""查询相关报告的提示词"""
|
|
security_code = request.body.get('seccode', "")
|
|
year = request.body.get('year', "2023")
|
|
if not security_code:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'seccode 不能为空'
|
|
})
|
|
result = agent.get_report_template(security_code, year)
|
|
if result:
|
|
return Response(result)
|
|
else:
|
|
return Response({
|
|
'success': False,
|
|
'message': f'无相关报告'
|
|
}, 400)
|
|
|
|
|
|
def main_handler(event, context):
|
|
return router.handle_request(event)
|