ea
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
|
||||
export const client = (function () {
|
||||
const hosts = 'http://192.168.200.129:6604/',
|
||||
uploaderUrl = 'http://192.168.200.129:6604/api/upload',
|
||||
loginPath = 'oauth',
|
||||
serverId = 0,
|
||||
clientVersion = '';
|
||||
|
||||
let pool: any[] = [];
|
||||
let waite: any[] = [];
|
||||
let isLogin = false;
|
||||
|
||||
const clearQueue = () => {
|
||||
pool = [];
|
||||
waite = [];
|
||||
};
|
||||
|
||||
const requestApi = (url: string, method: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT', data: any, resolve: (res: any) => void, subject: (res: any) => void, retryOn401 = true) => {
|
||||
wx.request({
|
||||
url: hosts + url,
|
||||
header: {
|
||||
'AppId': serverId,
|
||||
'x-token': wx.getStorageSync('access_token'),
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Scene': wx.getStorageSync('scene'),
|
||||
'Version': clientVersion
|
||||
},
|
||||
method: method,
|
||||
data: data,
|
||||
dataType: 'json',
|
||||
success(token) {
|
||||
if (+token.statusCode === 401) {
|
||||
console.log(token, retryOn401);
|
||||
if (retryOn401) {
|
||||
make(url, method, data, resolve, subject);
|
||||
} else {
|
||||
subject(token);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (+token.statusCode === 200) {
|
||||
resolve(token.data);
|
||||
return;
|
||||
}
|
||||
|
||||
subject(token);
|
||||
},
|
||||
fail(fai) {
|
||||
subject(fai);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const flushQueue = () => {
|
||||
const pending = pool.slice();
|
||||
clearQueue();
|
||||
|
||||
console.log(pending);
|
||||
|
||||
pending.forEach((item) => {
|
||||
request(item.url, item.method, item.data).then((data) => {
|
||||
item.resolve(data);
|
||||
}).catch((err) => {
|
||||
item.subject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const rejectQueue = (error: any) => {
|
||||
const pending = pool.slice();
|
||||
console.log(pending);
|
||||
clearQueue();
|
||||
pending.forEach((item) => {
|
||||
item.subject(error);
|
||||
});
|
||||
};
|
||||
|
||||
const login = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (isLogin === true) {
|
||||
return;
|
||||
}
|
||||
isLogin = true;
|
||||
|
||||
const success = (value: any) => {
|
||||
if (value.code != 0) {
|
||||
rejectQueue(value);
|
||||
reject(value);
|
||||
|
||||
wx.showModal({
|
||||
title: '温馨提示',
|
||||
content: '登录失败,请稍后再试',
|
||||
showCancel: false
|
||||
});
|
||||
return
|
||||
}
|
||||
wx.setStorageSync('access_token', value.data.token);
|
||||
setTimeout(() => {
|
||||
flushQueue();
|
||||
resolve(value);
|
||||
}, 50);
|
||||
};
|
||||
|
||||
const fail = (err: any) => {
|
||||
rejectQueue(err);
|
||||
reject(err);
|
||||
};
|
||||
|
||||
wx.login({
|
||||
timeout: 3000,
|
||||
success: (res) => {
|
||||
isLogin = false;
|
||||
const options = wx.getLaunchOptionsSync();
|
||||
requestApi(
|
||||
loginPath,
|
||||
'POST',
|
||||
{
|
||||
code: res.code,
|
||||
query: JSON.stringify(options.query),
|
||||
scene: options.scene
|
||||
},
|
||||
success,
|
||||
fail,
|
||||
false
|
||||
);
|
||||
},
|
||||
fail: (res) => {
|
||||
isLogin = false;
|
||||
rejectQueue(res);
|
||||
reject(res);
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
});
|
||||
};
|
||||
|
||||
const make = (url: string, method: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT', data: any, resolve: (res: any) => void, subject: (res: any) => void) => {
|
||||
const obj = {
|
||||
url: url,
|
||||
method: method,
|
||||
data: data,
|
||||
resolve: resolve,
|
||||
subject: subject
|
||||
};
|
||||
const key = method + url;
|
||||
if (waite.indexOf(key) === -1) {
|
||||
waite.push(key);
|
||||
pool.push(obj);
|
||||
}
|
||||
login().catch((res: any) => {
|
||||
console.log('catch', res);
|
||||
rejectQueue(res);
|
||||
subject(res);
|
||||
}).then((res: any) => {
|
||||
console.log('then', res);
|
||||
});
|
||||
};
|
||||
|
||||
const request = (url: string, method: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT', data = {}) => {
|
||||
return new Promise((resolve, subject) => {
|
||||
const token = wx.getStorageSync('access_token');
|
||||
if (token !== null && token !== '') {
|
||||
requestApi(url, method, data, resolve, subject);
|
||||
} else {
|
||||
make(url, method, data, resolve, subject);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const upload = (imageUrl: string, data: any, path: string = '/api/upload/default') => {
|
||||
return new Promise((sCall, eCall) => {
|
||||
wx.uploadFile({
|
||||
filePath: imageUrl,
|
||||
name: 'file',
|
||||
url: 'http://192.168.200.129:6604' + path,
|
||||
header: {
|
||||
AppId: serverId,
|
||||
'x-token': wx.getStorageSync('access_token'),
|
||||
Version: clientVersion
|
||||
},
|
||||
formData: data,
|
||||
success(token) {
|
||||
try {
|
||||
const json = JSON.parse(token.data);
|
||||
if (token.statusCode === 401) {
|
||||
eCall(json);
|
||||
} else {
|
||||
sCall(json);
|
||||
}
|
||||
} catch (e) {
|
||||
eCall(e);
|
||||
}
|
||||
},
|
||||
fail(res) {
|
||||
wx.showModal({
|
||||
title: '错误提示',
|
||||
content: '网络异常,请稍后再试',
|
||||
showCancel: false
|
||||
});
|
||||
eCall(res);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
request,
|
||||
upload
|
||||
};
|
||||
})()
|
||||
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
* 优化后的事件系统
|
||||
* 使用 WeakMap 来追踪监听器,避免内存泄漏
|
||||
* 使用数组存储监听器,确保正确注册和移除
|
||||
*/
|
||||
export const customEvent = (function () {
|
||||
let events: { [key: string]: any } = {};
|
||||
|
||||
// 用于追踪监听器的唯一标识,避免重复注册
|
||||
let locked: { [key: string]: any } = {};
|
||||
|
||||
/**
|
||||
* 检查监听器是否已经注册
|
||||
*/
|
||||
function isListenerRegistered(listeners: any, listener: any) {
|
||||
if (!Array.isArray(listeners)) {
|
||||
return false;
|
||||
}
|
||||
// 使用严格相等检查
|
||||
return listeners.some(item => item === listener);
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* 注册事件监听器
|
||||
* @param {string} name - 事件名称
|
||||
* @param {Function} callback - 回调函数
|
||||
*/
|
||||
on: function (name: string, callback: Function) {
|
||||
if (typeof name !== 'string' || !name) {
|
||||
return;
|
||||
}
|
||||
if (typeof callback !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化事件数组
|
||||
if (!events[name]) {
|
||||
events[name] = [];
|
||||
}
|
||||
|
||||
// 检查是否已经注册过
|
||||
if (isListenerRegistered(events[name], callback)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 注册监听器
|
||||
events[name].push({
|
||||
once: false,
|
||||
handler: callback
|
||||
});
|
||||
},
|
||||
/**
|
||||
* 注册事件监听器
|
||||
* @param {string} name - 事件名称
|
||||
* @param {Function} callback - 回调函数
|
||||
*/
|
||||
once: function (name: string, callback: Function) {
|
||||
if (typeof name !== 'string' || !name) {
|
||||
return;
|
||||
}
|
||||
if (typeof callback !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化事件数组
|
||||
if (!events[name]) {
|
||||
events[name] = [];
|
||||
}
|
||||
|
||||
// 检查是否已经注册过
|
||||
if (isListenerRegistered(events[name], callback)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 注册监听器
|
||||
events[name].push({
|
||||
once: true,
|
||||
handler: callback
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 取消事件监听器
|
||||
* @param {string} name - 事件名称
|
||||
* @param {Function} callback - 要移除的回调函数,如果为 null 则移除该事件的所有监听器
|
||||
*/
|
||||
off: function (name: string, callback: null | Function = null) {
|
||||
if (typeof name !== 'string' || !name) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果事件不存在,直接返回
|
||||
if (!events[name] || !Array.isArray(events[name])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果没有指定回调,移除该事件的所有监听器
|
||||
if (callback === null) {
|
||||
delete events[name];
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找并移除指定的监听器
|
||||
const index = events[name].findIndex((listener: { handler: Function }) => listener.handler === callback);
|
||||
if (index !== -1) {
|
||||
events[name].splice(index, 1);
|
||||
}
|
||||
|
||||
// 如果该事件没有监听器了,清理事件对象
|
||||
if (events[name].length === 0) {
|
||||
delete events[name];
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 触发事件
|
||||
* @param {string} name - 事件名称
|
||||
* @param {Object} data - 事件数据
|
||||
*/
|
||||
emmit: function (name: string, data: any) {
|
||||
if (typeof name !== 'string' || !name) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果事件不存在或没有监听器,直接返回
|
||||
if (!events[name] || !Array.isArray(events[name]) || events[name].length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((locked[name] || false) === true) {
|
||||
return;
|
||||
}
|
||||
locked[name] = true;
|
||||
|
||||
// 创建 SocketMessage 实例
|
||||
|
||||
// 复制监听器数组,避免在执行过程中数组被修改
|
||||
const listeners = [...events[name]];
|
||||
|
||||
// 触发所有监听器
|
||||
listeners.forEach((listener, index) => {
|
||||
try {
|
||||
if (listener.once) {
|
||||
events[name].splice(index, 1)
|
||||
}
|
||||
listener.handler(data);
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
});
|
||||
|
||||
locked[name] = false;
|
||||
|
||||
delete locked[name]
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取事件监听器数量(用于调试)
|
||||
* @param {string} name - 事件名称,如果为空则返回所有事件的信息
|
||||
*/
|
||||
getListenerCount: function (name: string) {
|
||||
if (name) {
|
||||
return events[name] ? events[name].length : 0;
|
||||
}
|
||||
const result: { [key: string]: any } = {};
|
||||
Object.keys(events).forEach(eventName => {
|
||||
result[eventName] = events[eventName].length;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,110 @@
|
||||
import { client } from "./api"
|
||||
import { customEvent } from "./event";
|
||||
|
||||
export const resetuserinfocache = (that?: any) => {
|
||||
userInfo().then((res: any) => {
|
||||
if (!res.code) {
|
||||
wx.setStorageSync('userInfo', res.data);
|
||||
if (that) {
|
||||
that.setData({ userInfo: res.Data });
|
||||
}
|
||||
customEvent.emmit('update:userinfo', [res.data]);
|
||||
}
|
||||
})
|
||||
}
|
||||
export const userInfo = () => {
|
||||
return client.request('api/info', 'GET');
|
||||
}
|
||||
export const login = () => {
|
||||
return client.request('oauth', 'GET');
|
||||
}
|
||||
|
||||
export const diamonds = () => {
|
||||
return client.request('api/diamond/confs', 'GET');
|
||||
}
|
||||
export const vips = () => {
|
||||
return client.request('api/vip/confs', 'GET');
|
||||
}
|
||||
export const updateuserinfo = (nickname: string, avatar: string) => {
|
||||
return client.request('api/update', 'POST', { nickname, avatar });
|
||||
}
|
||||
export const analysis = (content: string) => {
|
||||
return client.request('api/analysis', 'POST', { content });
|
||||
}
|
||||
export const tasksearch = (taskId: string) => {
|
||||
return client.request('api/task', 'GET', { taskId });
|
||||
}
|
||||
export const createTask = (parseId: string, content: string) => {
|
||||
return client.request('api/task/create', 'POST', { parseId, content });
|
||||
}
|
||||
export const polishScript = (taskId: string, content: string, style = 'optimize') => {
|
||||
return client.request('api/script/polish', 'POST', { taskId, content, style });
|
||||
}
|
||||
export const saveScript = (taskId: string, content: string) => {
|
||||
return client.request('api/script/save', 'POST', { taskId, content });
|
||||
}
|
||||
export const diamondVirtualPayment = (configId: number) => {
|
||||
return client.request('api/virtual/diamond', 'POST', { configId });
|
||||
}
|
||||
export const vipVirtualPayment = (configId: number) => {
|
||||
return client.request('api/virtual/vip', 'POST', { configId });
|
||||
}
|
||||
export const taskinfo = (taskId: string) => {
|
||||
return client.request('api/task/search', 'GET', { taskId });
|
||||
}
|
||||
export const feedback = (data: any) => {
|
||||
return client.request('api/feedback', 'POST', { data });
|
||||
}
|
||||
export const updateTaskAvatar = (taskId: string, avatarId: number) => {
|
||||
return client.request('api/task/avatar', 'POST', { taskId, avatarId });
|
||||
}
|
||||
export const updateTaskVoice = (taskId: string, voiceId: number) => {
|
||||
return client.request('api/task/voice', 'POST', { taskId, voiceId });
|
||||
}
|
||||
export const updatesessionkey = (code: string) => {
|
||||
return client.request('api/update/session-key', 'POST', { code });
|
||||
}
|
||||
export const uploadavatar = (file: any, data: any, path: string) => {
|
||||
return client.upload(file, data, path);
|
||||
}
|
||||
export const checkavatar = (url: string) => {
|
||||
return client.request('api/upload/avatar/check', 'POST', { url });
|
||||
}
|
||||
export const checkvoice = (url: string, asrTaskId = '') => {
|
||||
return client.request('api/upload/voice/check', 'POST', { url, asrTaskId });
|
||||
}
|
||||
export const saveHumanAssets = (data: { taskId: string, avatarUrl: string, voiceUrl: string, voiceText: string, durationMs?: number, asrTaskId?: string }) => {
|
||||
return client.request('api/human-assets/save', 'POST', data);
|
||||
}
|
||||
export const backgroundTemplates = () => {
|
||||
return client.request('api/background/templates', 'GET');
|
||||
}
|
||||
export const updateTaskBackground = (taskId: string, backgroundId: number) => {
|
||||
return client.request('api/task/background', 'POST', { taskId, backgroundId });
|
||||
}
|
||||
export const historyList = (status = 'all', page = 1, limit = 10) => {
|
||||
return client.request('api/history', 'GET', { status, page, limit });
|
||||
}
|
||||
export const historyDetail = (taskId: string) => {
|
||||
return client.request('api/history/detail', 'GET', { taskId });
|
||||
}
|
||||
export const historyDelete = (taskId: string) => {
|
||||
return client.request('api/history/delete', 'POST', { taskId });
|
||||
}
|
||||
export const historyDownload = (taskId: string) => {
|
||||
return client.request('api/history/download', 'POST', { taskId });
|
||||
}
|
||||
export const historyCancel = (taskId: string) => {
|
||||
return client.request('api/history/cancel', 'POST', { taskId });
|
||||
}
|
||||
export const sign = () => {
|
||||
return client.request('api/sign', 'POST', {});
|
||||
}
|
||||
export const uploadrecord = (file: string) => {
|
||||
return client.upload(file, {}, '/api/upload/voice');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { userInfo } from "./request"
|
||||
|
||||
export const formatTime = (date: Date) => {
|
||||
const year = date.getFullYear()
|
||||
const month = date.getMonth() + 1
|
||||
const day = date.getDate()
|
||||
const hour = date.getHours()
|
||||
const minute = date.getMinutes()
|
||||
const second = date.getSeconds()
|
||||
|
||||
return (
|
||||
[year, month, day].map(formatNumber).join('/') +
|
||||
' ' +
|
||||
[hour, minute, second].map(formatNumber).join(':')
|
||||
)
|
||||
}
|
||||
|
||||
const formatNumber = (n: number) => {
|
||||
const s = n.toString()
|
||||
return s[1] ? s : '0' + s
|
||||
}
|
||||
|
||||
|
||||
export const event = {
|
||||
user: {
|
||||
update: () => {
|
||||
userInfo().then((res: any) => {
|
||||
if (!res.code) {
|
||||
wx.setStorageSync('userInfo', res.data);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user