chore: update auth and portal
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { request } from '../utils/request';
|
||||
import { request } from "../utils/request";
|
||||
|
||||
export const authApi = {
|
||||
sendOTP: (phone) => request('/auth/otp', { method: 'POST', body: { phone } }),
|
||||
login: (phone, otp) => request('/auth/login', { method: 'POST', body: { phone, otp } }),
|
||||
sendOTP: (phone) => request("/auth/otp", { method: "POST", body: { phone } }),
|
||||
login: (phone, otp) =>
|
||||
request("/auth/login", { method: "POST", body: { phone, otp } }),
|
||||
};
|
||||
|
||||
@@ -1,131 +1,131 @@
|
||||
import { request } from '../utils/request';
|
||||
import { getTenantCode } from '../utils/tenant';
|
||||
import { request } from "../utils/request";
|
||||
import { getTenantCode } from "../utils/tenant";
|
||||
|
||||
export const commonApi = {
|
||||
getOptions: () => request('/common/options'),
|
||||
checkHash: (hash) => request(`/upload/check?hash=${hash}`),
|
||||
deleteMedia: (id) => request(`/media-assets/${id}`, { method: 'DELETE' }),
|
||||
upload: (file, type) => {
|
||||
getOptions: () => request("/common/options"),
|
||||
checkHash: (hash) => request(`/upload/check?hash=${hash}`),
|
||||
deleteMedia: (id) => request(`/media-assets/${id}`, { method: "DELETE" }),
|
||||
upload: (file, type) => {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
formData.append("type", type);
|
||||
return request("/upload", { method: "POST", body: formData });
|
||||
},
|
||||
uploadMultipart: (file, hash, type, onProgress) => {
|
||||
const controller = new AbortController();
|
||||
const signal = controller.signal;
|
||||
|
||||
const promise = (async () => {
|
||||
// 1. Check Hash
|
||||
try {
|
||||
const res = await commonApi.checkHash(hash);
|
||||
if (res) {
|
||||
if (onProgress) onProgress(100);
|
||||
return res;
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore hash check errors
|
||||
}
|
||||
|
||||
if (signal.aborted) throw new Error("Aborted");
|
||||
|
||||
// 2. Init
|
||||
const initRes = await request("/upload/init", {
|
||||
method: "POST",
|
||||
body: {
|
||||
filename: file.name,
|
||||
size: file.size,
|
||||
mime_type: file.type,
|
||||
hash: hash,
|
||||
type: type,
|
||||
},
|
||||
signal,
|
||||
});
|
||||
|
||||
const { upload_id, chunk_size } = initRes;
|
||||
const totalChunks = Math.ceil(file.size / chunk_size);
|
||||
|
||||
// 3. Upload Parts
|
||||
for (let i = 0; i < totalChunks; i++) {
|
||||
if (signal.aborted) throw new Error("Aborted");
|
||||
|
||||
const start = i * chunk_size;
|
||||
const end = Math.min(start + chunk_size, file.size);
|
||||
const chunk = file.slice(start, end);
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('type', type);
|
||||
return request('/upload', { method: 'POST', body: formData });
|
||||
},
|
||||
uploadMultipart: (file, hash, type, onProgress) => {
|
||||
const controller = new AbortController();
|
||||
const signal = controller.signal;
|
||||
formData.append("file", chunk);
|
||||
formData.append("upload_id", upload_id);
|
||||
formData.append("part_number", i + 1);
|
||||
|
||||
const promise = (async () => {
|
||||
// 1. Check Hash
|
||||
try {
|
||||
const res = await commonApi.checkHash(hash);
|
||||
if (res) {
|
||||
if (onProgress) onProgress(100);
|
||||
return res;
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore hash check errors
|
||||
}
|
||||
|
||||
if (signal.aborted) throw new Error('Aborted');
|
||||
|
||||
// 2. Init
|
||||
const initRes = await request('/upload/init', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
filename: file.name,
|
||||
size: file.size,
|
||||
mime_type: file.type,
|
||||
hash: hash,
|
||||
type: type
|
||||
},
|
||||
signal
|
||||
});
|
||||
|
||||
const { upload_id, chunk_size } = initRes;
|
||||
const totalChunks = Math.ceil(file.size / chunk_size);
|
||||
|
||||
// 3. Upload Parts
|
||||
for (let i = 0; i < totalChunks; i++) {
|
||||
if (signal.aborted) throw new Error('Aborted');
|
||||
|
||||
const start = i * chunk_size;
|
||||
const end = Math.min(start + chunk_size, file.size);
|
||||
const chunk = file.slice(start, end);
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', chunk);
|
||||
formData.append('upload_id', upload_id);
|
||||
formData.append('part_number', i + 1);
|
||||
|
||||
// request helper with FormData handles content-type, but we need signal
|
||||
await request('/upload/part', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
signal
|
||||
});
|
||||
|
||||
if (onProgress) {
|
||||
const percent = Math.round(((i + 1) / totalChunks) * 100);
|
||||
onProgress(percent);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Complete
|
||||
return request('/upload/complete', {
|
||||
method: 'POST',
|
||||
body: { upload_id },
|
||||
signal
|
||||
});
|
||||
})();
|
||||
|
||||
return { promise, abort: () => controller.abort() };
|
||||
},
|
||||
uploadWithProgress: (file, type, onProgress) => {
|
||||
let xhr;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('type', type);
|
||||
|
||||
xhr = new XMLHttpRequest();
|
||||
const tenantCode = getTenantCode();
|
||||
if (!tenantCode) {
|
||||
reject(new Error('Tenant code missing in URL'));
|
||||
return;
|
||||
}
|
||||
xhr.open('POST', `/t/${tenantCode}/v1/upload`);
|
||||
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
|
||||
xhr.upload.onprogress = (event) => {
|
||||
if (event.lengthComputable && onProgress) {
|
||||
const percentComplete = (event.loaded / event.total) * 100;
|
||||
onProgress(percentComplete);
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
try {
|
||||
const response = JSON.parse(xhr.responseText);
|
||||
resolve(response);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
} else {
|
||||
reject(new Error(xhr.statusText || 'Upload failed'));
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = () => reject(new Error('Network Error'));
|
||||
xhr.onabort = () => reject(new Error('Aborted'));
|
||||
xhr.send(formData);
|
||||
// request helper with FormData handles content-type, but we need signal
|
||||
await request("/upload/part", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
signal,
|
||||
});
|
||||
|
||||
return { promise, abort: () => xhr.abort() };
|
||||
}
|
||||
|
||||
if (onProgress) {
|
||||
const percent = Math.round(((i + 1) / totalChunks) * 100);
|
||||
onProgress(percent);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Complete
|
||||
return request("/upload/complete", {
|
||||
method: "POST",
|
||||
body: { upload_id },
|
||||
signal,
|
||||
});
|
||||
})();
|
||||
|
||||
return { promise, abort: () => controller.abort() };
|
||||
},
|
||||
uploadWithProgress: (file, type, onProgress) => {
|
||||
let xhr;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
formData.append("type", type);
|
||||
|
||||
xhr = new XMLHttpRequest();
|
||||
const tenantCode = getTenantCode();
|
||||
if (!tenantCode) {
|
||||
reject(new Error("Tenant code missing in URL"));
|
||||
return;
|
||||
}
|
||||
xhr.open("POST", `/t/${tenantCode}/v1/upload`);
|
||||
|
||||
const token = localStorage.getItem("token");
|
||||
if (token) {
|
||||
xhr.setRequestHeader("Authorization", `Bearer ${token}`);
|
||||
}
|
||||
|
||||
xhr.upload.onprogress = (event) => {
|
||||
if (event.lengthComputable && onProgress) {
|
||||
const percentComplete = (event.loaded / event.total) * 100;
|
||||
onProgress(percentComplete);
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
try {
|
||||
const response = JSON.parse(xhr.responseText);
|
||||
resolve(response);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
} else {
|
||||
reject(new Error(xhr.statusText || "Upload failed"));
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = () => reject(new Error("Network Error"));
|
||||
xhr.onabort = () => reject(new Error("Aborted"));
|
||||
xhr.send(formData);
|
||||
});
|
||||
|
||||
return { promise, abort: () => xhr.abort() };
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
import { request } from '../utils/request';
|
||||
import { request } from "../utils/request";
|
||||
|
||||
export const contentApi = {
|
||||
list: (params) => {
|
||||
if (params.tenant_id) {
|
||||
const { tenant_id: tenantID, ...rest } = params;
|
||||
const qs = new URLSearchParams(rest).toString();
|
||||
return request(`/creators/${tenantID}/contents?${qs}`);
|
||||
}
|
||||
const qs = new URLSearchParams(params).toString();
|
||||
return request(`/contents?${qs}`);
|
||||
},
|
||||
get: (id) => request(`/contents/${id}`),
|
||||
listComments: (id, page) => request(`/contents/${id}/comments?page=${page || 1}`),
|
||||
createComment: (id, data) => request(`/contents/${id}/comments`, { method: 'POST', body: data }),
|
||||
likeComment: (id) => request(`/comments/${id}/like`, { method: 'POST' }),
|
||||
addLike: (id) => request(`/contents/${id}/like`, { method: 'POST' }),
|
||||
removeLike: (id) => request(`/contents/${id}/like`, { method: 'DELETE' }),
|
||||
addFavorite: (id) => request(`/contents/${id}/favorite`, { method: 'POST' }),
|
||||
removeFavorite: (id) => request(`/contents/${id}/favorite`, { method: 'DELETE' }),
|
||||
listTopics: () => request('/topics'),
|
||||
list: (params) => {
|
||||
if (params.tenant_id) {
|
||||
const { tenant_id: tenantID, ...rest } = params;
|
||||
const qs = new URLSearchParams(rest).toString();
|
||||
return request(`/creators/${tenantID}/contents?${qs}`);
|
||||
}
|
||||
const qs = new URLSearchParams(params).toString();
|
||||
return request(`/contents?${qs}`);
|
||||
},
|
||||
get: (id) => request(`/contents/${id}`),
|
||||
listComments: (id, page) =>
|
||||
request(`/contents/${id}/comments?page=${page || 1}`),
|
||||
createComment: (id, data) =>
|
||||
request(`/contents/${id}/comments`, { method: "POST", body: data }),
|
||||
likeComment: (id) => request(`/comments/${id}/like`, { method: "POST" }),
|
||||
addLike: (id) => request(`/contents/${id}/like`, { method: "POST" }),
|
||||
removeLike: (id) => request(`/contents/${id}/like`, { method: "DELETE" }),
|
||||
addFavorite: (id) => request(`/contents/${id}/favorite`, { method: "POST" }),
|
||||
removeFavorite: (id) =>
|
||||
request(`/contents/${id}/favorite`, { method: "DELETE" }),
|
||||
listTopics: () => request("/topics"),
|
||||
};
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { request } from '../utils/request';
|
||||
import { request } from "../utils/request";
|
||||
|
||||
export const orderApi = {
|
||||
create: (data) => request('/orders', { method: 'POST', body: data }),
|
||||
pay: (id, data) => request(`/orders/${id}/pay`, { method: 'POST', body: data }),
|
||||
status: (id) => request(`/orders/${id}/status`),
|
||||
create: (data) => request("/orders", { method: "POST", body: data }),
|
||||
pay: (id, data) =>
|
||||
request(`/orders/${id}/pay`, { method: "POST", body: data }),
|
||||
status: (id) => request(`/orders/${id}/status`),
|
||||
};
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { request } from '../utils/request';
|
||||
import { request } from "../utils/request";
|
||||
|
||||
export const tenantApi = {
|
||||
get: (id) => request(`/tenants/${id}`),
|
||||
list: (params) => {
|
||||
const qs = new URLSearchParams(params).toString();
|
||||
return request(`/tenants?${qs}`);
|
||||
},
|
||||
follow: (id) => request(`/tenants/${id}/follow`, { method: 'POST' }),
|
||||
unfollow: (id) => request(`/tenants/${id}/follow`, { method: 'DELETE' }),
|
||||
get: (id) => request(`/tenants/${id}`),
|
||||
list: (params) => {
|
||||
const qs = new URLSearchParams(params).toString();
|
||||
return request(`/tenants?${qs}`);
|
||||
},
|
||||
follow: (id) => request(`/tenants/${id}/follow`, { method: "POST" }),
|
||||
unfollow: (id) => request(`/tenants/${id}/follow`, { method: "DELETE" }),
|
||||
};
|
||||
|
||||
@@ -1,25 +1,38 @@
|
||||
import { request } from '../utils/request';
|
||||
import { request } from "../utils/request";
|
||||
|
||||
export const userApi = {
|
||||
getMe: () => request('/me'),
|
||||
updateMe: (data) => request('/me', { method: 'PUT', body: data }),
|
||||
realName: (data) => request('/me/realname', { method: 'POST', body: data }),
|
||||
getWallet: () => request('/me/wallet'),
|
||||
recharge: (data) => request('/me/wallet/recharge', { method: 'POST', body: data }),
|
||||
getOrders: (status) => request(`/me/orders?status=${status || 'all'}`),
|
||||
getOrder: (id) => request(`/me/orders/${id}`),
|
||||
getLibrary: () => request('/me/library'),
|
||||
getFavorites: () => request('/me/favorites'),
|
||||
addFavorite: (contentId) => request(`/me/favorites?contentId=${contentId}`, { method: 'POST' }),
|
||||
removeFavorite: (contentId) => request(`/me/favorites/${contentId}`, { method: 'DELETE' }),
|
||||
getLikes: () => request('/me/likes'),
|
||||
addLike: (contentId) => request(`/me/likes?contentId=${contentId}`, { method: 'POST' }),
|
||||
removeLike: (contentId) => request(`/me/likes/${contentId}`, { method: 'DELETE' }),
|
||||
getNotifications: (type, page) => request(`/me/notifications?type=${type || 'all'}&page=${page || 1}`),
|
||||
markNotificationRead: (id) => request(`/me/notifications/${id}/read`, { method: 'POST' }),
|
||||
markAllNotificationsRead: () => request('/me/notifications/read-all', { method: 'POST' }),
|
||||
getFollowing: () => request('/me/following'),
|
||||
getCoupons: (status) => request(`/me/coupons?status=${status || 'unused'}`),
|
||||
getAvailableCoupons: (amount) => request(`/me/coupons/available?amount=${amount}`),
|
||||
receiveCoupon: (couponId) => request('/me/coupons/receive', { method: 'POST', body: { coupon_id: couponId } }),
|
||||
getMe: () => request("/me"),
|
||||
updateMe: (data) => request("/me", { method: "PUT", body: data }),
|
||||
realName: (data) => request("/me/realname", { method: "POST", body: data }),
|
||||
getWallet: () => request("/me/wallet"),
|
||||
recharge: (data) =>
|
||||
request("/me/wallet/recharge", { method: "POST", body: data }),
|
||||
getOrders: (status) => request(`/me/orders?status=${status || "all"}`),
|
||||
getOrder: (id) => request(`/me/orders/${id}`),
|
||||
getLibrary: () => request("/me/library"),
|
||||
getFavorites: () => request("/me/favorites"),
|
||||
addFavorite: (contentId) =>
|
||||
request(`/me/favorites?contentId=${contentId}`, { method: "POST" }),
|
||||
removeFavorite: (contentId) =>
|
||||
request(`/me/favorites/${contentId}`, { method: "DELETE" }),
|
||||
getLikes: () => request("/me/likes"),
|
||||
addLike: (contentId) =>
|
||||
request(`/me/likes?contentId=${contentId}`, { method: "POST" }),
|
||||
removeLike: (contentId) =>
|
||||
request(`/me/likes/${contentId}`, { method: "DELETE" }),
|
||||
getNotifications: (type, page) =>
|
||||
request(`/me/notifications?type=${type || "all"}&page=${page || 1}`),
|
||||
markNotificationRead: (id) =>
|
||||
request(`/me/notifications/${id}/read`, { method: "POST" }),
|
||||
markAllNotificationsRead: () =>
|
||||
request("/me/notifications/read-all", { method: "POST" }),
|
||||
getFollowing: () => request("/me/following"),
|
||||
getCoupons: (status) => request(`/me/coupons?status=${status || "unused"}`),
|
||||
getAvailableCoupons: (amount) =>
|
||||
request(`/me/coupons/available?amount=${amount}`),
|
||||
receiveCoupon: (couponId) =>
|
||||
request("/me/coupons/receive", {
|
||||
method: "POST",
|
||||
body: { coupon_id: couponId },
|
||||
}),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user