apply Network.ts
This commit is contained in:
@ -4,9 +4,10 @@ import Time from "../../../utils/Time";
|
||||
import Toolkit from "../../../utils/Toolkit";
|
||||
import config from "../../../config/index";
|
||||
import { Location, MediaItem, MediaItemType, WechatMediaItem } from "../../../types/UI";
|
||||
import { Journal, JournalType } from "../../../types/Journal";
|
||||
import { JournalType } from "../../../types/Journal";
|
||||
import { MediaAttachExt, MediaAttachType } from "../../../types/Attachment";
|
||||
import IOSize, { Unit } from "../../../utils/IOSize";
|
||||
import { JournalApi } from "../../../api/JournalApi";
|
||||
|
||||
interface JournalEditorData {
|
||||
/** 模式:create 或 edit */
|
||||
@ -148,25 +149,8 @@ Page({
|
||||
},
|
||||
/** 加载日记详情(编辑模式) */
|
||||
async loadJournalDetail(id: number) {
|
||||
wx.showLoading({ title: "加载中...", mask: true });
|
||||
try {
|
||||
const journal: Journal = await new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `${config.url}/journal/${id}`,
|
||||
method: "POST",
|
||||
header: {
|
||||
Key: wx.getStorageSync("key")
|
||||
},
|
||||
success: (res: any) => {
|
||||
if (res.data.code === 20000) {
|
||||
resolve(res.data.data);
|
||||
} else {
|
||||
reject(new Error(res.data.message || "加载失败"));
|
||||
}
|
||||
},
|
||||
fail: reject
|
||||
});
|
||||
});
|
||||
const journal = await JournalApi.getDetail(id);
|
||||
|
||||
const items = journal.items || [];
|
||||
const thumbItems = items.filter((item) => item.attachType === MediaAttachType.THUMB);
|
||||
@ -309,11 +293,11 @@ Page({
|
||||
// 编辑模式:mediaList + newMediaList
|
||||
const sources = (this.data.mediaList as MediaItem[]).map(item => ({
|
||||
url: item.sourceURL,
|
||||
type: MediaItemType[item.type].toLowerCase()
|
||||
type: item.type
|
||||
}));
|
||||
const newSources = this.data.newMediaList.map(item => ({
|
||||
url: item.path,
|
||||
type: MediaItemType[item.type].toLowerCase()
|
||||
type: item.type
|
||||
}));
|
||||
const allSources = [...sources, ...newSources];
|
||||
const itemIndex = isNewMedia ? this.data.mediaList.length + index : index;
|
||||
@ -392,45 +376,21 @@ Page({
|
||||
this.executeDelete();
|
||||
},
|
||||
/** 执行删除 */
|
||||
executeDelete() {
|
||||
wx.showLoading({ title: "删除中...", mask: true });
|
||||
wx.request({
|
||||
url: `${config.url}/journal/delete`,
|
||||
method: "POST",
|
||||
header: {
|
||||
Key: wx.getStorageSync("key"),
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
data: {
|
||||
id: this.data.id
|
||||
},
|
||||
success: (res: any) => {
|
||||
wx.hideLoading();
|
||||
if (res.data.code === 20000 || res.statusCode === 200) {
|
||||
Events.emit("JOURNAL_REFRESH");
|
||||
Events.emit("JOURNAL_LIST_REFRESH");
|
||||
wx.showToast({
|
||||
title: "删除成功",
|
||||
icon: "success"
|
||||
});
|
||||
setTimeout(() => {
|
||||
wx.navigateBack();
|
||||
}, 1000);
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: res.data.message || "删除失败",
|
||||
icon: "error"
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
wx.hideLoading();
|
||||
wx.showToast({
|
||||
title: "删除失败",
|
||||
icon: "error"
|
||||
});
|
||||
}
|
||||
});
|
||||
async executeDelete() {
|
||||
try {
|
||||
await JournalApi.delete(this.data.id!);
|
||||
Events.emit("JOURNAL_REFRESH");
|
||||
Events.emit("JOURNAL_LIST_REFRESH");
|
||||
wx.showToast({
|
||||
title: "删除成功",
|
||||
icon: "success"
|
||||
});
|
||||
setTimeout(() => {
|
||||
wx.navigateBack();
|
||||
}, 1000);
|
||||
} catch (error) {
|
||||
console.error("删除日记失败:", error);
|
||||
}
|
||||
},
|
||||
/** 提交/保存 */
|
||||
submit() {
|
||||
@ -441,10 +401,9 @@ Page({
|
||||
}
|
||||
},
|
||||
/** 创建日记 */
|
||||
createJournal() {
|
||||
async createJournal() {
|
||||
const handleFail = () => {
|
||||
wx.showToast({ title: "上传失败", icon: "error" });
|
||||
wx.hideLoading();
|
||||
this.setData({
|
||||
isSaving: false
|
||||
});
|
||||
@ -452,83 +411,64 @@ Page({
|
||||
this.setData({
|
||||
isSaving: true
|
||||
});
|
||||
// 获取 openId
|
||||
const getOpenId = new Promise<string>((resolve, reject) => {
|
||||
wx.login({
|
||||
success: (res) => {
|
||||
if (res.code) {
|
||||
wx.request({
|
||||
url: `${config.url}/journal/openid`,
|
||||
method: "POST",
|
||||
header: {
|
||||
Key: wx.getStorageSync("key")
|
||||
},
|
||||
data: {
|
||||
code: res.code
|
||||
},
|
||||
success: (resp) => {
|
||||
const data = resp.data as any;
|
||||
if (data.code === 20000) {
|
||||
resolve(data.data);
|
||||
} else {
|
||||
reject(new Error("获取 openId 失败"));
|
||||
}
|
||||
},
|
||||
fail: () => reject(new Error("获取 openId 请求失败"))
|
||||
});
|
||||
} else {
|
||||
reject(new Error("获取登录凭证失败"));
|
||||
}
|
||||
},
|
||||
fail: () => reject(new Error("登录失败"))
|
||||
try {
|
||||
// 获取 openId
|
||||
const getOpenId = new Promise<string>((resolve, reject) => {
|
||||
wx.login({
|
||||
success: async (res) => {
|
||||
if (res.code) {
|
||||
try {
|
||||
const openId = await JournalApi.getOpenId(res.code);
|
||||
resolve(openId);
|
||||
} catch (error) {
|
||||
reject(new Error("获取 openId 失败"));
|
||||
}
|
||||
} else {
|
||||
reject(new Error("获取登录凭证失败"));
|
||||
}
|
||||
},
|
||||
fail: () => reject(new Error("登录失败"))
|
||||
});
|
||||
});
|
||||
});
|
||||
// 文件上传
|
||||
const uploadFiles = this.uploadMediaFiles(this.data.mediaList as WechatMediaItem[]);
|
||||
// 并行执行获取 openId 和文件上传
|
||||
Promise.all([getOpenId, uploadFiles]).then(([openId, tempFileIds]) => {
|
||||
wx.showLoading({ title: "正在保存..", mask: true });
|
||||
wx.request({
|
||||
url: `${config.url}/journal/create`,
|
||||
method: "POST",
|
||||
header: {
|
||||
Key: wx.getStorageSync("key")
|
||||
},
|
||||
data: {
|
||||
idea: this.data.idea,
|
||||
type: this.data.type,
|
||||
lat: this.data.location?.lat,
|
||||
lng: this.data.location?.lng,
|
||||
location: this.data.location?.text,
|
||||
pusher: openId,
|
||||
createdAt: Date.parse(`${this.data.date} ${this.data.time}`),
|
||||
tempFileIds
|
||||
},
|
||||
success: async () => {
|
||||
Events.emit("JOURNAL_REFRESH");
|
||||
wx.showToast({ title: "提交成功", icon: "success" });
|
||||
this.setData({
|
||||
idea: "",
|
||||
mediaList: [],
|
||||
isSaving: false,
|
||||
uploaded: "0",
|
||||
uploadTotal: "0 MB",
|
||||
uploadProgress: 0
|
||||
});
|
||||
await Toolkit.sleep(1000);
|
||||
wx.switchTab({
|
||||
url: "/pages/main/journal/index"
|
||||
});
|
||||
},
|
||||
fail: handleFail
|
||||
// 文件上传
|
||||
const uploadFiles = this.uploadMediaFiles(this.data.mediaList as WechatMediaItem[]);
|
||||
// 并行执行获取 openId 和文件上传
|
||||
const [openId, tempFileIds] = await Promise.all([getOpenId, uploadFiles]);
|
||||
|
||||
await JournalApi.create({
|
||||
idea: this.data.idea,
|
||||
type: this.data.type,
|
||||
lat: this.data.location?.lat,
|
||||
lng: this.data.location?.lng,
|
||||
location: this.data.location?.text,
|
||||
pusher: openId,
|
||||
createdAt: Date.parse(`${this.data.date} ${this.data.time}`),
|
||||
tempFileIds
|
||||
});
|
||||
}).catch(handleFail);
|
||||
|
||||
Events.emit("JOURNAL_REFRESH");
|
||||
wx.showToast({ title: "提交成功", icon: "success" });
|
||||
this.setData({
|
||||
idea: "",
|
||||
mediaList: [],
|
||||
isSaving: false,
|
||||
uploaded: "0",
|
||||
uploadTotal: "0 MB",
|
||||
uploadProgress: 0
|
||||
});
|
||||
await Toolkit.sleep(1000);
|
||||
wx.switchTab({
|
||||
url: "/pages/main/journal/index"
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("创建日记失败:", error);
|
||||
handleFail();
|
||||
}
|
||||
},
|
||||
/** 更新日记 */
|
||||
updateJournal() {
|
||||
async updateJournal() {
|
||||
const handleFail = () => {
|
||||
wx.showToast({ title: "保存失败", icon: "error" });
|
||||
wx.hideLoading();
|
||||
this.setData({
|
||||
isSaving: false
|
||||
});
|
||||
@ -536,53 +476,39 @@ Page({
|
||||
this.setData({
|
||||
isSaving: true
|
||||
});
|
||||
// 收集保留的附件 ID(缩略图 ID)
|
||||
const attachmentIds = (this.data.mediaList as MediaItem[]).map(item => item.attachmentId);
|
||||
// 上传新媒体文件
|
||||
const uploadFiles = this.uploadMediaFiles(this.data.newMediaList);
|
||||
try {
|
||||
// 收集保留的附件 ID(缩略图 ID)
|
||||
const attachmentIds = (this.data.mediaList as MediaItem[]).map(item => item.attachmentId);
|
||||
// 上传新媒体文件
|
||||
const tempFileIds = await this.uploadMediaFiles(this.data.newMediaList);
|
||||
|
||||
// 提交保存
|
||||
uploadFiles.then((tempFileIds) => {
|
||||
wx.showLoading({ title: "正在保存..", mask: true });
|
||||
wx.request({
|
||||
url: `${config.url}/journal/update`,
|
||||
method: "POST",
|
||||
header: {
|
||||
Key: wx.getStorageSync("key")
|
||||
},
|
||||
data: {
|
||||
id: this.data.id,
|
||||
idea: this.data.idea,
|
||||
type: this.data.type,
|
||||
lat: this.data.location?.lat,
|
||||
lng: this.data.location?.lng,
|
||||
location: this.data.location?.text,
|
||||
createdAt: new Date(`${this.data.date}T${this.data.time}:00`).getTime(),
|
||||
// 保留的现有附件 ID
|
||||
attachmentIds,
|
||||
// 新上传的临时文件 ID
|
||||
tempFileIds
|
||||
},
|
||||
success: async (resp: any) => {
|
||||
if (resp.data.code === 20000 || resp.statusCode === 200) {
|
||||
Events.emit("JOURNAL_REFRESH");
|
||||
Events.emit("JOURNAL_LIST_REFRESH");
|
||||
wx.showToast({ title: "保存成功", icon: "success" });
|
||||
this.setData({
|
||||
isSaving: false,
|
||||
uploaded: "0",
|
||||
uploadTotal: "0 MB",
|
||||
uploadProgress: 0
|
||||
});
|
||||
await Toolkit.sleep(1000);
|
||||
wx.navigateBack();
|
||||
} else {
|
||||
handleFail();
|
||||
}
|
||||
},
|
||||
fail: handleFail
|
||||
await JournalApi.update({
|
||||
id: this.data.id!,
|
||||
idea: this.data.idea,
|
||||
type: this.data.type,
|
||||
lat: this.data.location?.lat,
|
||||
lng: this.data.location?.lng,
|
||||
location: this.data.location?.text,
|
||||
createdAt: new Date(`${this.data.date}T${this.data.time}:00`).getTime(),
|
||||
attachmentIds,
|
||||
tempFileIds
|
||||
});
|
||||
}).catch(handleFail);
|
||||
|
||||
Events.emit("JOURNAL_REFRESH");
|
||||
Events.emit("JOURNAL_LIST_REFRESH");
|
||||
wx.showToast({ title: "保存成功", icon: "success" });
|
||||
this.setData({
|
||||
isSaving: false,
|
||||
uploaded: "0",
|
||||
uploadTotal: "0 MB",
|
||||
uploadProgress: 0
|
||||
});
|
||||
await Toolkit.sleep(1000);
|
||||
wx.navigateBack();
|
||||
} catch (error) {
|
||||
console.error("更新日记失败:", error);
|
||||
handleFail();
|
||||
}
|
||||
},
|
||||
/** 上传媒体文件 */
|
||||
uploadMediaFiles(mediaList: WechatMediaItem[]): Promise<string[]> {
|
||||
|
||||
Reference in New Issue
Block a user