feat: ai聊天开始提交测试

This commit is contained in:
常东方 2025-06-06 11:12:09 +08:00
parent af586485f0
commit 6296bc5e19
5 changed files with 1156 additions and 549 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,57 @@
import { getEnvBaseUrl } from '@/utils'
import {httpPost} from "@/utils/http"
import { TOKEN } from './test';
import {apis,uploadFile} from "@/utils/tools"
const baseUrl = getEnvBaseUrl();
// /artwork/get-chunk-list 获取文件分断数据
// /artwork/upload-chunk 分断上传画作图片
export const uploadFiles = (url,params)=>{
let token = uni.getStorageSync('authorization');
return new Promise((resolve, reject) => {
uni.uploadFile({
url: baseUrl+url,
filePath: params.tempFilePath,
name: "Chunk",
formData:params.formData,
header: {
'authorization': token,
},
complete: (res) => {
// console.log('res: ',res);
if(res.statusCode == 200) {
resolve(res)
} else {
reject(res)
}
}
})
})
}
const getChunkList=(file)=>{
return new Promise((resolve,reject)=>{
uni.request({
url:baseUrl+"/artwork/get-chunk-list",
data:file,
header:{
"Content-Type":"application/json", //"multipart/form-data"
},
success(res){
resolve(res)
},
fail(rej){
reject(Jrej)
}
})
})
}
export const uploadFileChunk=(data)=>{
const header={
authorization:TOKEN
}
let token = uni.getStorageSync('authorization');
return uploadFiles('/artwork/upload-chunk',data)
}

View File

@ -1,15 +1,21 @@
export const fileSuffix = (str) => {
if (!str) {
return str
}
let reg = /\.\w*$/
return str.match(reg)[0]
}
export const officeFileTypeList = ['.docx', '.doc', '.xls', '.xlsx', '.pdf', '.txt']
export const videoFileType = ['.mp4', '.mov', '.wmv']
export const picFileType = ['.jpg', '.png', '.jpeg']
export const formatParams = (uploadList) => {
// 上传文件formData类型
const uploadFileTypeEm = {
image: 'image',
video: 'video',
file: 'file',
text: 'text',
}
let mediaList = [] // 媒体文件 参数中的content
let photoList = [] // 媒体文件 参数中的content
let videoList = []
let fileList = [] // 文档文件
uploadList.forEach((item) => {
if (item.uploadFileType === uploadFileTypeEm.image) {
if (picFileType.includes(fileSuffix(item.ori_url))) {
// 图片
let media = {
type: 'image_url',
@ -17,26 +23,29 @@ export const formatParams = (uploadList) => {
url: item.ori_url,
},
}
mediaList.push(media)
} else if (item.uploadFileType === uploadFileTypeEm.video) {
photoList.push(media)
} else if (videoFileType.includes(fileSuffix(item.ori_url))) {
// 视频
let media = {
type: 'video_url',
video_url: {
url: item.url,
url: item.ori_url,
poster: item.url,
},
}
mediaList.push(media)
} else if (item.uploadFileType === uploadFileTypeEm.file) {
videoList.push(media)
} else if (officeFileTypeList.includes(fileSuffix(item.ori_url))) {
let file = {
role: 'system',
name: item.name,
content: item.url,
size: item.size,
mask: 'new',
}
fileList.push(file)
}
})
return { media: mediaList, file: fileList }
return { photoList: photoList, videoList: videoList, file: fileList }
}
export const calcFileSize = (size: number) => {
const type = ['B', 'KB', 'MB', 'GB', 'TB']
@ -50,3 +59,127 @@ export const calcFileSize = (size: number) => {
}
return `${Math.ceil(size)}${unit}`
}
// 格式化请求数据,从页面渲染的数据改为请求数据
export function formatData(list) {
let result = []
let msg = null
list.forEach((item, index) => {
// if (index === list.length - 1) {
// return
// }
if (item.type === 'text') {
result.push({
role: item.role,
content: item.content,
type: item.type,
mask: item.mask,
})
if (item.role !== 'assistant') {
msg = {
role: item.role,
content: item.content,
type: item.type,
}
}
} else if (item.type === 'image' || item.type === 'video') {
// 图片与视频混合在一起
const content = []
item.content.forEach((child) => {
if (child.type === 'image_url') {
content.push({
type: 'image_url',
image_url: {
url: child.image_url.url,
},
})
} else if (child.type === 'video_url') {
content.push({
type: 'video_url',
video_url: {
url: child.video_url.url,
},
})
}
})
if (msg) {
content.push({
type: 'text',
text: msg.content,
})
msg = null
}
result.push({
role: 'user',
content: content,
type: 'image',
mask: item.mask,
})
} else if (item.type === 'file') {
let content = []
item.content.forEach((child) => {
if (child.role === 'system') {
content.push({
role: 'system',
content: child.content,
type: 'file',
mask: item.mask,
})
} else {
console.log(child)
}
})
if (msg) {
content.push(msg)
msg = null
}
result = result.concat(content)
}
})
return result
}
function sliceFile(blob: Blob, chunkSize: number): Blob[] {
const chunks: Blob[] = []
let cursor = 0
while (cursor < blob.size) {
chunks.push(blob.slice(cursor, cursor + chunkSize))
cursor += chunkSize
}
return chunks
}
/**
* Base64
*/
export function sliceBase64(base64: string, chunkSize: number = 1024 * 1024): string[] {
// 提取实际 Base64 数据部分
// const base64Data = base64.split(',')[1]
const byteStringLength = base64.length
const slices = []
console.log(base64)
for (let i = 0; i < byteStringLength; i += chunkSize) {
const chunk = base64.slice(i, i + chunkSize)
slices.push(chunk) // 可选:保留前缀
}
return slices
}
export async function readFile(file, chunkSize = 10 * 1024 * 1024) {
const blob = await fetch(file.tempFilePath)
const buffer = await blob.blob()
return sliceFile(buffer, chunkSize)
}
function uploadChunkFile({ chunk, fileName }, index, total, fileId) {
const formData = new FormData()
formData.append('Chunk', chunk)
formData.append('ChunkFileName', `${fileName}_${index}`)
formData.append('total', total)
formData.append('UseType', 100)
formData.append('FileName', fileName)
formData.append('Source', 'aiChat')
formData.append('UseType', 100)
return
}

View File

@ -1,8 +1,9 @@
import {httpPost,httpGet} from "./http"
import {getEnvBaseUrl} from "./index"
const baseUrl=getEnvBaseUrl();
// 发送文本消息
const endMsg=async (params)=>{
return await httpGet(baseUrl+url,data)
}
}

View File

@ -1,89 +1,219 @@
import { getEnvBaseUrl } from "./index";
export const baseUrl =getEnvBaseUrl();
// 获取接口application/json带token
export const api = (url = '', params = {}, method = 'post') => {
let token = wx.getStorageSync('token');
return new Promise((resolve, reject) => {
wx.request({
url: baseUrl+url,
data: params,
method,
header: {
'Content-Type': "application/json",
'token': token,
},
complete: res => {
if(res.statusCode == 200) {
resolve(res.data)
} else {
reject(res)
}
}
})
})
}
// 获取接口(application/x-www-form-urlencoded,带token)
// let authorization = uni.getStorageSync('authorization');
// console.log(authorization);
return new Promise((resolve, reject) => {
uni.request({
url:baseUrl+url,
data: params,
method,
header: {
'Content-Type': 'application/x-www-form-urlencoded'
//'authorization': authorization,
},
complete: (res) => {
if (res.statusCode == 200) {
resolve(res.data);
} else {
reject(res);
}
}
});
});
};
export const api_form = (url = '', params = {}, method = 'post') => {
let token = wx.getStorageSync('token');
return new Promise((resolve, reject) => {
wx.request({
url: baseUrl+url,
data: params,
method,
header: {
'Content-Type':"application/x-www-form-urlencoded",
'token': token,
},
complete: res => {
if(res.statusCode == 200) {
resolve(res.data)
} else {
reject(res)
}
}
})
})
}
// 获取接口(application/x-www-form-urlencoded,不带token)
let authorization = uni.getStorageSync('authorization');
return new Promise((resolve, reject) => {
uni.request({
url: baseUrl + url,
data: params,
method,
header: {
// 'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': 'application/json',
'authorization': authorization,
},
complete: (res) => {
if (res.statusCode == 200) {
resolve(res.data);
} else {
reject(res);
}
}
});
});
};
// 接口获取
export const apis = (url = '', params = {}, method = 'post') => {
return new Promise((resolve, reject) => {
wx.request({
url: baseUrl+url,
data: params,
method,
header: {
'Content-Type':"application/x-www-form-urlencoded",
},
complete: res => {
if(res.statusCode == 200) {
resolve(res.data)
} else {
reject(res)
}
}
})
})
let authorization = uni.getStorageSync('authorization');
return new Promise((resolve, reject) => {
uni.request({
url: baseUrl + url,
data: params,
method,
header: {
// 'Content-Type':'multipart/form-data',//application/x-www-form-urlencoded',
'authorization': authorization,
},
complete: (res) => {
if (res.statusCode == 200) {
resolve(res.data);
} else {
reject(res);
}
}
});
});
};
// 错误toast提示
export const showToastErr = (title) => {
uni.showToast({
title: title,
icon: 'none',
duration: 2000,
mask: true
});
}
// 时间戳转日期(年月日)
export const time_format = (time) =>{
// 判断时间戳是否为13位数如果不是则*1000时间戳只有13位数(带毫秒)和10(不带毫秒)位数的
if(time.toString().length == 13){
var tme = new Date(time);
}else{
var tme = new Date(time * 1000);
}
var Y = tme.getFullYear();
var M = (tme.getMonth() + 1 < 10 ? '0' + (tme.getMonth() + 1) : tme.getMonth() + 1);
var D = tme.getDate();
var h = tme.getHours();
var m = tme.getMinutes();
var s = tme.getSeconds();
var tem1 = Y + '-' + M + '-' + D
// + h + '时' + m + '分'
// + s +'秒'
return tem1;
// 成功toast提示
export const showToastOk = (title) => {
uni.showToast({
title: title,
icon: 'success',
duration: 2000,
mask: true
});
}
export const showToastOkMask = (title) => {
uni.showToast({
title: title,
icon: 'success',
duration: 2000,
mask: false
});
}
export const showloading=(title)=>{
uni.showLoading({
mask:true,
title,
});
}
export const hideloading=()=>{
uni.hideLoading();
}
export const navigateTo = (url) => {
uni.navigateTo({
url: url
})
}
export const redirectTo = (url) => {
uni.redirectTo({
url: url
})
}
export const reLaunch = (url) => {
uni.reLaunch({
url: url
})
}
// 上传图片(选择图片)
export const selectPic = () => {
return new Promise((resolve, reject) => {
uni.chooseImage({
count: 9, //默认9
sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
sourceType: ["album", "camera"], //从相册选择
success: function(res) {
resolve(res)
},
fail: function() {
reject("选择文件失败")
}
})
})
}
// 上传图片
export const uploadFile = (url,file) => {
let authorization = uni.getStorageSync('authorization');
return new Promise((resolve, reject) => {
uni.uploadFile({
url: baseUrl + url,
// filePath: file.tempFilePaths[0],
name: 'file',
header:{
authorization: authorization
},
formData: {
from: file,
},
complete: (res) => {
if (res.statusCode == 200) {
resolve(res)
} else {
reject(res)
}
}
})
})
}
//滚动到元素位置
export const smoothScroll = (element) => {
setTimeout(() => {
document.querySelector(element).scrollIntoView({
behavior: "smooth",
});
}, 1000)
}
export function getCurrentTime() {
var gettime = this
const yy = new Date().getFullYear()
const mm = new Date().getMonth() + 1
const dd = new Date().getDate()
const hh = new Date().getHours()
const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
return gettime
}
export function getCurrentTime1() {
var gettime = this
const yy = new Date().getFullYear()
const mm = new Date().getMonth() + 1
const dd = new Date().getDate()
const hh = new Date().getHours()
const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
gettime = yy + '-' + mm + '-' + dd
return gettime
}
// 时间戳转日期(年月日)
export const time_format = (time) => {
// 判断时间戳是否为13位数如果不是则*1000时间戳只有13位数(带毫秒)和10(不带毫秒)位数的
if (time.toString().length == 13) {
var tme = new Date(time);
} else {
var tme = new Date(time * 1000);
}
export const time_format3 = (time) =>{
var Y = tme.getFullYear();
var M = (tme.getMonth() + 1 < 10 ? '0' + (tme.getMonth() + 1) : tme.getMonth() + 1);
var D = tme.getDate();
var h = tme.getHours();
var m = tme.getMinutes();
var s = tme.getSeconds();
if(D<10){
D='0'+D;
}
var tem1 = Y + '/' + M + '/' + D
// + h + '时' + m + '分'
// + s +'秒'
return tem1;
}
export const time_format3 = (time,seq="-") =>{
// 判断时间戳是否为13位数如果不是则*1000时间戳只有13位数(带毫秒)和10(不带毫秒)位数的
if(time.toString().length == 13){
var tme = new Date(time);
@ -105,7 +235,7 @@ export const apis = (url = '', params = {}, method = 'post') => {
if(s<10){
s='0'+s;
}
var tem1 = Y + '-' + M + '-' + D +' '+h+':'+m+':'+s
var tem1 = Y + seq + M + seq + D +' '+h+':'+m
// + h + '时' + m + '分'
// + s +'秒'
return tem1;
@ -136,300 +266,16 @@ export const apis = (url = '', params = {}, method = 'post') => {
time = (hours < 10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? ('0' + minutes) : minutes) ;
return time;
}
// 错误toast提示
export const showToastErr = (title,mask=false) =>{
wx.showToast({
title: title,
icon: 'none',
duration: 1500,
mask:mask
});
}
export function showToastErrMask(title){
wx.showToast({
title:title,
icon:"none",
duration:1500,
mask:true
})
}
// export function showToastErrMask(title){
// wx.showToast({
// title: title,
// icon:"none",
// duration:1500,
// mask:false
// })
// }
// 成功toast提示
export const showToastOk = (title,mask=false) =>{
wx.showToast({
title: title,
icon: 'success',
duration: 1500,
mask:mask
});
}
export function showToastOkMask(title){
// 用于不需要控制用户点击频率时
wx.showToast({
title:title,
icon:"success",
duration:1500,
mask:true
})
}
// 上传图片
export const uploadFile = (url,file)=>{
let token = wx.getStorageSync('token');
return new Promise((resolve, reject) => {
wx.uploadFile({
url: baseUrl+ url,
filePath: file,
name: "file",
header: {
"Content-Type": "application/x-www-form-urlencoded",
'token': token,
},
complete: (res) => {
if(res.statusCode == 200) {
resolve(res)
} else {
reject(res)
}
}
})
})
}
// 上传图片(选择图片)
export const selectPic = ()=>{
return new Promise((resolve,reject) => {
wx.chooseImage({
count: 4, //默认9
sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
sourceType: ["album", "camera"], //从相册选择
success: function (res) {
resolve(res)
},
fail:function(){
reject("选择文件失败")
}
})
})
}
// 日期字符串添加年月日
export const times=(strTime, type)=> {
// 字符串转数组
var strTimes = strTime.split(type)
strTimes[0] = strTimes[0]
strTimes[1] = strTimes[1]
strTimes[2] = strTimes[2]
// 数组转字符串
strTimes = strTimes.join(type)
return strTimes
// 日期转时间戳(10)
export const date_stamp = (time) => new Date(time).getTime() / 1000
// 日期转时间戳(13)
export const date_stamp1 = (time) => new Date(time).getTime()
// 树节点
export function travelTree(tree, arr) {
for (let item of tree) {
arr.push(item.label);
if (item.children && item.children.length) travelTree(item.children, arr);
}
export const times1=(strTime, type)=> {
// 字符串转数组
var strTimes = strTime.split(type)
strTimes[0] = strTimes[0] + '年'
strTimes[1] = strTimes[1] + '月'
strTimes[2] = strTimes[2] + '日'
strTimes[2] = strTimes[3] + '时'
strTimes[2] = strTimes[4] + '分'
strTimes[2] = strTimes[5] + '秒'
// 数组转字符串
strTimes = strTimes.join(type)
return strTimes
}
// 登录
export const login = () => {
return new Promise(function (resolve, reject) {
wx.login({
success: function (res) {
if (res.code) {
resolve(res);
} else {
reject(res);
}
},
fail: function (err) {
reject(err);
},
});
});
};
// 模态框
export const Modal=(title,content)=> {
return new Promise((resolve,reject)=>{
wx.showModal({
title: title,
content: content,
success: function(res) {
if(res.confirm) {
resolve(res.confirm)
} else if(res.cancel) {
reject(res.cancel)
}
},
fail: function(rej){
reject(rej)
}
})
})
}
// 获取个人信息
export const getUserInfo=()=> {
return new Promise((resolve, reject) => {
wx.getUserInfo({
desc: '用于完善会员资料',
success: function (res) {
if (res) {
resolve(res);
} else {
reject(res);
}
},
fail: function (err) {
reject(err);
},
})
})
};
export function navigateTo(url) {
wx.navigateTo({
url: `/pages/${url}/${url}`
})
}
export function reLaunch(url) {
wx.reLaunch({
url: `/pages/${url}/${url}`
})
}
// 日期转时间戳
export const date_stamp=(time)=> new Date(time).getTime()/1000
export function travelTree(tree,arr) {
for (let item of tree) {
arr.push(item.label);
if (item.children&& item.children.length) travelTree(item.children,arr);
}
return arr;
}
export function getToday(){
let date=new Date();
let year=date.getFullYear();
let month=date.getMonth();
let day=date.getDate();
return `${year}-${month}-${day}`;
return arr;
}
// 重定向到某个页面
export function redirectTo(nextUrl){
wx.redirectTo({
url: nextUrl,
})
}
export function showloading(title=""){
wx.showLoading({
title:title,
mask:true
})
}
export function hideloading(){
wx.hideLoading()
}
// 获取用户授权权限
export function getLimit(){
return new Promise((resolve,reject)=>{
wx.getSetting({
withSubscriptions:true,
success:function(res){
if(res){
resolve(res)
}else{
reject(res)
}
},
fail:(rej)=>{
reject(rej)
}
})
})
}
// 开启订阅
export function subscribe(id){
return new Promise((resolve,reject)=>{
wx.requestSubscribeMessage({
tmplIds: id,
success:function(res){
if(res){
resolve(res)
}else{
reject(res)
}
},
fail:function(rej){
reject(rej)
}
})
})
}
export const chooseMsgFile=()=>{
return new Promise((resolve,reject)=>{
wx.chooseMessageFile({
count: 100,
type:"all",
success(res){
if(res){
resolve(res)
}else{
reject(res)
}
},
fail(rej){
reject(rej)
},
complete(res){
if(res){
resolve(res)
}else{
reject(res)
}
}
})
})
}
// 拍摄或者从相册上传
export const chooseMedia=(count=9)=>{
return new Promise((resolve,reject)=>{
wx.chooseMedia({
count:count,
mediaType:["image"],
sourceType:['album', 'camera'],
camera:"back",
success(res){
if(res){
resolve(res)
}else{
reject(res)
}
},
fail(rej){
reject(rej)
},
complete(res){
if(res){
resolve(res)
}else{
reject(res)
}
}
})
})
}