first commit
This commit is contained in:
5
code/system/apis/vdes/chat/__init__.py
Normal file
5
code/system/apis/vdes/chat/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022/5/9 23:26
|
||||
# @Author : 臧成龙
|
||||
# @FileName: __init__.py.py
|
||||
# @Software: PyCharm
|
||||
25
code/system/apis/vdes/chat/common.py
Normal file
25
code/system/apis/vdes/chat/common.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from ninja import Router
|
||||
from ninja import Router, UploadedFile, File
|
||||
import os
|
||||
from fuadmin import settings
|
||||
|
||||
router = Router()
|
||||
|
||||
# @router.get("/uploadFile")
|
||||
# def upload_file(request, files: list[UploadedFile] = File(...)):
|
||||
# file_list = []
|
||||
# upload_dir = os.path.join(settings.BASE_DIR, 'upload')
|
||||
# os.makedirs(upload_dir, exist_ok=True)
|
||||
|
||||
# for file in files:
|
||||
# new_path = os.path.join(upload_dir, file.name)
|
||||
# with open(new_path, 'wb+') as destination:
|
||||
# for chunk in file.chunks():
|
||||
# destination.write(chunk)
|
||||
# file_list.append({
|
||||
# 'name': file.name,
|
||||
# 'path': new_path,
|
||||
# 'size': file.size
|
||||
# })
|
||||
|
||||
# return file_list
|
||||
102
code/system/apis/vdes/chat/contacts.py
Normal file
102
code/system/apis/vdes/chat/contacts.py
Normal file
@@ -0,0 +1,102 @@
|
||||
from system.models import Contacts, HistorySession
|
||||
from ninja import Router, Schema, Field
|
||||
|
||||
router = Router()
|
||||
|
||||
class addContactSchema(Schema):
|
||||
MMSI: str = Field(None, alias="mmsi")
|
||||
name: str = Field(None, alias="name")
|
||||
phone: str = Field(None, alias="phone")
|
||||
region: str = Field(None, alias="region")
|
||||
|
||||
class deleteContactSchema(Schema):
|
||||
MMSI: str = Field(None, alias="mmsi")
|
||||
|
||||
class editContactSchema(Schema):
|
||||
oldMMSI: str = Field(None, alias="old_mmsi")
|
||||
newMMSI: str = Field(None, alias="new_mmsi")
|
||||
name: str = Field(None, alias="name")
|
||||
phone: str = Field(None, alias="phone")
|
||||
region: str = Field(None, alias="region")
|
||||
|
||||
|
||||
@router.get('/contactsList')
|
||||
def get_contacts_list(request):
|
||||
try:
|
||||
contacts = Contacts.objects.all().values()
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
"data": list(contacts)
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
@router.post('/deleteContact')
|
||||
def delete_contact(request, data: deleteContactSchema):
|
||||
try:
|
||||
if not data.MMSI:
|
||||
return {'code': 400, 'message': 'MMSI不能为空'}
|
||||
contact = Contacts.objects.filter(MMSI=data.MMSI).first()
|
||||
if not contact:
|
||||
return {'code': 404, 'message': '未找到对应联系人'}
|
||||
# 删除关联的历史会话记录
|
||||
HistorySession.objects.filter(FormId=contact.Id).delete()
|
||||
contact.delete()
|
||||
return {'code': 200, 'message': '删除成功'}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
|
||||
@router.post('/editContact')
|
||||
def edit_contact(request, data: editContactSchema):
|
||||
try:
|
||||
if not data.oldMMSI:
|
||||
return {'code': 400, 'message': '原MMSI不能为空'}
|
||||
|
||||
contact = Contacts.objects.filter(MMSI=data.oldMMSI).first()
|
||||
if not contact:
|
||||
return {'code': 404, 'message': '未找到对应联系人'}
|
||||
# 更新联系人信息
|
||||
contact.MMSI = data.newMMSI if data.newMMSI else contact.MMSI
|
||||
contact.Name = data.name if data.name else contact.Name
|
||||
contact.Mobile = data.phone if data.phone else contact.Mobile
|
||||
contact.Region = data.region if data.region else contact.Region
|
||||
contact.save()
|
||||
|
||||
# 更新关联的历史会话记录
|
||||
if data.newMMSI or data.name:
|
||||
HistorySession.objects.filter(FormId=contact.Id).update(
|
||||
MMSI=data.newMMSI if data.newMMSI else contact.MMSI,
|
||||
Name=data.name if data.name else contact.Name
|
||||
)
|
||||
|
||||
return {
|
||||
'code': 200,
|
||||
'message': '修改成功',
|
||||
'data': {
|
||||
'id': contact.Id,
|
||||
'MMSI': contact.MMSI
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
@router.post('/addContact')
|
||||
def add_contact(request, data: addContactSchema):
|
||||
try:
|
||||
if not data.MMSI:
|
||||
return {'code': 400, 'message': 'MMSI不能为空'}
|
||||
name = data.name if data.name else data.MMSI
|
||||
contact = Contacts.objects.create(
|
||||
MMSI=data.MMSI,
|
||||
Name=name,
|
||||
Mobile=data.phone,
|
||||
Region=data.region
|
||||
)
|
||||
return {
|
||||
'code': 200,
|
||||
'message': '成功',
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
53
code/system/apis/vdes/chat/content.py
Normal file
53
code/system/apis/vdes/chat/content.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from system.models import Content
|
||||
from ninja import Router, Schema, Field
|
||||
from datetime import datetime
|
||||
from vdes_utils.vdes_message_packaged import VdesMessagePackaged
|
||||
|
||||
router = Router()
|
||||
|
||||
@router.get('/getContent')
|
||||
def get_content(request):
|
||||
try:
|
||||
contacts = Content.objects.all().order_by('Id').values()
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
"data": list(contacts)
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
@router.post('/sendMessage')
|
||||
def send_message(request):
|
||||
# try:
|
||||
# 解析JSON数据
|
||||
import json
|
||||
data = json.loads(request.body)
|
||||
MMSI = data['mmsi']
|
||||
# 创建并保存Content记录
|
||||
content = Content.objects.create(
|
||||
SendId=data['SendId'],
|
||||
ReciverId=data['ReciverId'],
|
||||
Content=data['Content'],
|
||||
Type=data['Type'],
|
||||
# State=data['State'],
|
||||
State=1, # 设置为已发送状态
|
||||
NoCode=data['NoCode'],
|
||||
CreateDateUtc=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
||||
Title=data['Title'],
|
||||
Description=data['Description'],
|
||||
Label=data['Label'],
|
||||
Thumbnail=data['Thumbnail'],
|
||||
ReadFlag=data['ReadFlag'],
|
||||
Avatar=data['Avatar'],
|
||||
SoundStatus=0,
|
||||
|
||||
)
|
||||
message = data['Content'].replace('<p>', '').replace('</p>', '')
|
||||
# message = "这是一条长消息测试消息1111111111111111111111111111111111111111111111111111111"
|
||||
vdes_message_packaged = VdesMessagePackaged(message, MMSI)
|
||||
vdes_message_packaged.send_message()
|
||||
|
||||
return { "code": 200, "message": "消息保存成功" }
|
||||
# except Exception as e:
|
||||
# return {'code': 500, 'message': str(e)}
|
||||
50
code/system/apis/vdes/chat/historySession.py
Normal file
50
code/system/apis/vdes/chat/historySession.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from system.models import HistorySession
|
||||
from ninja import Router, Schema, Field
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
class HistorySessionSchema(Schema):
|
||||
FormId: str = Field(None, alias="formId")
|
||||
Name: str = Field(None, alias="name")
|
||||
MMSI: str = Field(None, alias="mmsi")
|
||||
Mobile: str = Field(None, alias="mobile")
|
||||
Region: str = Field(None, alias="region")
|
||||
ById: str = Field(None, alias="byId")
|
||||
|
||||
|
||||
@router.get('/getHistorySession')
|
||||
def get_history_session(request):
|
||||
try:
|
||||
contacts = HistorySession.objects.all().values()
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
"data": list(contacts)
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
|
||||
@router.post('/insertHistorySession')
|
||||
def insert_history_session(request, data: HistorySessionSchema):
|
||||
try:
|
||||
existing_record = HistorySession.objects.filter(
|
||||
FormId=data.FormId,
|
||||
ById=data.ById
|
||||
).first()
|
||||
if not existing_record:
|
||||
history_session = HistorySession.objects.create(
|
||||
FormId=data.FormId,
|
||||
Name=data.Name,
|
||||
MMSI=data.MMSI,
|
||||
Mobile=data.Mobile,
|
||||
Region=data.Region,
|
||||
ById=data.ById
|
||||
)
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
50
code/system/apis/vdes/chat/historysession_bk.py
Normal file
50
code/system/apis/vdes/chat/historysession_bk.py
Normal file
@@ -0,0 +1,50 @@
|
||||
# from system.models import HistorySession
|
||||
# from ninja import Router, Schema, Field
|
||||
|
||||
# router = Router()
|
||||
|
||||
|
||||
# class HistorySessionSchema(Schema):
|
||||
# FormId: str = Field(None, alias="formId")
|
||||
# Name: str = Field(None, alias="name")
|
||||
# MMSI: str = Field(None, alias="mmsi")
|
||||
# Mobile: str = Field(None, alias="mobile")
|
||||
# Region: str = Field(None, alias="region")
|
||||
# ById: str = Field(None, alias="byId")
|
||||
|
||||
|
||||
# @router.get('/getHistorySession')
|
||||
# def get_history_session(request):
|
||||
# try:
|
||||
# contacts = HistorySession.objects.all().values()
|
||||
# return {
|
||||
# "code": 200,
|
||||
# "message": "成功",
|
||||
# "data": list(contacts)
|
||||
# }
|
||||
# except Exception as e:
|
||||
# return {'code': 500, 'message': str(e)}
|
||||
|
||||
|
||||
# @router.post('/insertHistorySession')
|
||||
# def insert_history_session(request, data: HistorySessionSchema):
|
||||
# try:
|
||||
# existing_record = HistorySession.objects.filter(
|
||||
# FormId=data.FormId,
|
||||
# ById=data.ById
|
||||
# ).first()
|
||||
# if not existing_record:
|
||||
# history_session = HistorySession.objects.create(
|
||||
# FormId=data.FormId,
|
||||
# Name=data.Name,
|
||||
# MMSI=data.MMSI,
|
||||
# Mobile=data.Mobile,
|
||||
# Region=data.Region,
|
||||
# ById=data.ById
|
||||
# )
|
||||
# return {
|
||||
# "code": 200,
|
||||
# "message": "成功",
|
||||
# }
|
||||
# except Exception as e:
|
||||
# return {'code': 500, 'message': str(e)}
|
||||
Reference in New Issue
Block a user