Skip to content

Commit 7dfbc2d

Browse files
committed
feat: change experiment's cover image
1 parent fb30ddc commit 7dfbc2d

4 files changed

Lines changed: 93 additions & 23 deletions

File tree

src/services/api/getData.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export async function getData(path: string, body: unknown) {
3636
"Content-Type": "application/json",
3737
"x-API-Token": token,
3838
"x-API-AuthCode": authCode,
39+
"x-API-Version": "2502", // F**K! When was it updated from 2411 to 2502?
3940
},
4041
}).then((response) => {
4142
if (!response.ok) {

src/services/i18n/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export default {
9191
copyID: "Copy Work ID",
9292
copyInternalLink: "Copy Internal Link",
9393
copyExternalLink: "Copy External Link",
94+
changeCover: "Change Cover",
9495
},
9596
date: {
9697
justNow: "Just now",

src/services/i18n/zh.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export default {
9090
copyID: "复制作品ID",
9191
copyInternalLink: "复制内部链接",
9292
copyExternalLink: "复制外部链接",
93+
changeCover: "更换封面",
9394
},
9495
date: {
9596
justNow: "刚刚",

src/views/ExperimentSummary.vue

Lines changed: 90 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ import "../layout/AdaptationView.css";
184184
import { useI18n } from "vue-i18n";
185185
import showActionSheet from "../popup/actionSheet.ts";
186186
import Emitter from "../services/eventEmitter.ts";
187+
import storageManager from "../services/storage.ts";
187188
188189
const comment = ref("");
189190
const isLoading = ref(false);
@@ -193,7 +194,7 @@ const selectedTab = ref("Intro");
193194
const route = useRoute();
194195
const { t } = useI18n();
195196
const returnImagePath = ref(
196-
window.$getPath("/@base/assets/library/Navigation-Return.png"),
197+
window.$getPath("/@base/assets/library/Navigation-Return.png")
197198
);
198199
199200
const data = ref({
@@ -232,7 +233,7 @@ const data = ref({
232233
});
233234
234235
let coverUrl = ref(
235-
window.$getPath("/@base/assets/messages/Experiment-Default.png"),
236+
window.$getPath("/@base/assets/messages/Experiment-Default.png")
236237
);
237238
let avatarUrl = getUserUrl(data.value.User);
238239
@@ -264,7 +265,7 @@ async function handleEnter() {
264265
route.params.category as string,
265266
route.params.id as string,
266267
replyID,
267-
upDate,
268+
upDate
268269
);
269270
}
270271
@@ -284,26 +285,92 @@ function copy(text: string) {
284285
}
285286
286287
function copySubject() {
287-
showActionSheet(
288-
[
289-
{ label: t("expeSummary.copyID") },
290-
{ label: t("expeSummary.copyInternalLink") },
291-
{ label: t("expeSummary.copyExternalLink") },
292-
],
293-
(idx) => {
294-
if (idx === 0) {
295-
copy(data.value.ID);
296-
} else if (idx === 1) {
297-
copy(
298-
`<${(route.params.category as string).toLowerCase()}=${route.params.id}>${data.value.Subject}</${(route.params.category as string).toLowerCase()}>`,
299-
);
300-
} else if (idx === 2) {
301-
copy(
302-
`<external=${window.location.href}>${data.value.Subject}[web]</external>`,
303-
);
304-
}
305-
},
306-
);
288+
let list = [
289+
{ label: t("expeSummary.copyID") },
290+
{ label: t("expeSummary.copyInternalLink") },
291+
{ label: t("expeSummary.copyExternalLink") },
292+
];
293+
if (data.value.User.ID === storageManager.getObj("userInfo")?.value?.id) {
294+
list.push({ label: t("expeSummary.changeCover") });
295+
}
296+
showActionSheet(list, (idx) => {
297+
if (idx === 0) {
298+
copy(data.value.ID);
299+
} else if (idx === 1) {
300+
copy(
301+
`<${(route.params.category as string).toLowerCase()}=${route.params.id}>${data.value.Subject}</${(route.params.category as string).toLowerCase()}>`
302+
);
303+
} else if (idx === 2) {
304+
copy(
305+
`<external=${window.location.href}>${data.value.Subject}[web]</external>`
306+
);
307+
} else if (idx === 3) {
308+
try {
309+
// ask user to select an image
310+
const input = document.createElement("input");
311+
input.type = "file";
312+
input.accept = "image/*";
313+
input.onchange = async (e: any) => {
314+
try {
315+
const file = e.target.files && e.target.files[0];
316+
if (!file) return;
317+
const summaryRes = await getData(`/Contents/GetSummary`, {
318+
ContentID: route.params.id,
319+
Category: route.params.category,
320+
});
321+
const imageIndex = (summaryRes.Data.Image || 0) + 1;
322+
await getData(`/Contents/ConfirmExperiment`, {
323+
Category: route.params.category,
324+
SummaryID: route.params.id,
325+
Image: imageIndex,
326+
Extension: ".png",
327+
});
328+
const submitRes = await getData(`/Contents/SubmitExperiment`, {
329+
Request: {
330+
FileSize: 0 - Math.abs(file.size),
331+
Extension: ".jpg",
332+
},
333+
Summary: summaryRes.Data,
334+
});
335+
try {
336+
const form = new FormData();
337+
form.append(
338+
"authorization",
339+
submitRes.Data?.Token?.Authorization || ""
340+
);
341+
form.append("policy", submitRes.Data?.Token?.Policy || "");
342+
form.append("file", file, "cover.jpg");
343+
await fetch("https://v0.api.upyun.com/qphysics", {
344+
method: "POST",
345+
body: form,
346+
});
347+
await getData(`/Contents/ConfirmExperiment`, {
348+
Category: route.params.category,
349+
SummaryID: route.params.id,
350+
Image: imageIndex,
351+
Extension: ".png",
352+
});
353+
} catch (upErr) {
354+
Emitter.emit("error", "Failed to upload file", 2, upErr);
355+
return;
356+
}
357+
Emitter.emit("success", "Cover changed successfully", 2);
358+
// refresh current cover (using existing utility function)
359+
setTimeout(async () => {
360+
const refreshed = await getData(`/Contents/GetSummary`, {
361+
ContentID: route.params.id,
362+
Category: route.params.category,
363+
});
364+
coverUrl.value = getCoverUrl(refreshed.Data);
365+
}, 800);
366+
} catch (err) {
367+
Emitter.emit("error", "Failed to change cover, please try again later", 2, err);
368+
}
369+
};
370+
input.click();
371+
} catch (error) {}
372+
}
373+
});
307374
}
308375
309376
onActivated(() => {

0 commit comments

Comments
 (0)