From 880e702288e4d456fe70211d4b620efaf0145c7e Mon Sep 17 00:00:00 2001 From: Timi Date: Fri, 12 Dec 2025 17:49:20 +0800 Subject: [PATCH] merge journal-creater, journal-editor --- miniprogram/app.json | 1 - .../pages/main/journal-creater/index.json | 10 - .../pages/main/journal-creater/index.less | 146 ----- .../pages/main/journal-creater/index.ts | 376 ------------- .../pages/main/journal-creater/index.wxml | 118 ---- .../pages/main/journal-editor/index.less | 2 + .../pages/main/journal-editor/index.ts | 512 +++++++++++++----- .../pages/main/journal-editor/index.wxml | 194 ++++--- miniprogram/pages/main/journal/index.ts | 2 +- miniprogram/pages/main/moment/index.ts | 2 +- 10 files changed, 505 insertions(+), 858 deletions(-) delete mode 100644 miniprogram/pages/main/journal-creater/index.json delete mode 100644 miniprogram/pages/main/journal-creater/index.less delete mode 100644 miniprogram/pages/main/journal-creater/index.ts delete mode 100644 miniprogram/pages/main/journal-creater/index.wxml diff --git a/miniprogram/app.json b/miniprogram/app.json index a3bf344..01c178a 100644 --- a/miniprogram/app.json +++ b/miniprogram/app.json @@ -2,7 +2,6 @@ "pages": [ "pages/index/index", "pages/main/journal/index", - "pages/main/journal-creater/index", "pages/main/journal-search/index", "pages/main/journal-editor/index", "pages/main/journal-map/index", diff --git a/miniprogram/pages/main/journal-creater/index.json b/miniprogram/pages/main/journal-creater/index.json deleted file mode 100644 index 919a3f4..0000000 --- a/miniprogram/pages/main/journal-creater/index.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "component": true, - "usingComponents": { - "t-icon": "tdesign-miniprogram/icon/icon", - "t-radio": "tdesign-miniprogram/radio/radio", - "t-button": "tdesign-miniprogram/button/button", - "t-navbar": "tdesign-miniprogram/navbar/navbar", - "t-radio-group": "tdesign-miniprogram/radio-group/radio-group" - } -} \ No newline at end of file diff --git a/miniprogram/pages/main/journal-creater/index.less b/miniprogram/pages/main/journal-creater/index.less deleted file mode 100644 index 69f747b..0000000 --- a/miniprogram/pages/main/journal-creater/index.less +++ /dev/null @@ -1,146 +0,0 @@ -/* pages/main/journal-creater/index.wxss */ -.container { - - .content { - width: calc(100% - 64px); - padding: 0 32px 32px 32px; - display: flex; - align-items: center; - flex-direction: column; - - .label { - color: var(--theme-text-secondary); - } - - .section { - width: 100%; - margin-top: 1.5rem; - - &.type { - display: flex; - - .radio { - margin-right: 1em; - } - } - - &.time { - display: flex; - - .picker { - margin-right: .25rem; - } - } - - &.media { - - .ctrl { - display: flex; - - .clear { - width: 100px; - padding-left: 0; - padding-right: 0; - } - } - - .gallery { - gap: 10rpx; - display: grid; - grid-template-columns: repeat(3, 1fr); - - .item { - height: 200rpx; - position: relative; - overflow: hidden; - background: var(--theme-bg-card); - box-shadow: 1px 1px 6px var(--theme-shadow-light); - border-radius: 2rpx; - - &.add { - color: var(--theme-wx); - margin: 0; - font-size: 80rpx; - background: transparent; - } - - .thumbnail { - width: 200rpx; - height: 200rpx; - display: block; - } - - .video-container { - position: relative; - - - .play-icon { - top: 50%; - left: 50%; - color: rgba(255, 255, 255, .8); - z-index: 2; - position: absolute; - font-size: 128rpx; - transform: translate(-50%, -50%); - text-shadow: 4rpx 4rpx 0 rgba(0, 0, 0, .5); - pointer-events: none; - } - } - - .delete { - top: 10rpx; - right: 10rpx; - color: rgba(255, 255, 255, .6); - z-index: 3; - position: absolute; - font-size: 45rpx; - - &::after { - content: ""; - top: 0; - width: 100%; - height: 100%; - z-index: -1; - position: absolute; - background: rgba(0, 0, 0, .6); - border-radius: 50%; - } - } - } - } - } - } - - .uploading { - width: 100%; - margin-top: 1rem; - - .progress { - margin-bottom: 8rpx; - } - - .text { - color: var(--theme-text-secondary); - display: flex; - font-size: .8rem; - justify-content: space-between; - } - } - - .ctrl { - width: 100%; - display: flex; - margin-top: 1rem; - align-items: center; - - .clear { - width: 200rpx; - } - - .submit { - flex: 1; - margin-left: 12rpx; - } - } - } -} \ No newline at end of file diff --git a/miniprogram/pages/main/journal-creater/index.ts b/miniprogram/pages/main/journal-creater/index.ts deleted file mode 100644 index f292f82..0000000 --- a/miniprogram/pages/main/journal-creater/index.ts +++ /dev/null @@ -1,376 +0,0 @@ -// pages/main/journal-creater/index.ts -import Events from "../../../utils/Events"; -import Time from "../../../utils/Time"; -import Toolkit from "../../../utils/Toolkit"; -import config from "../../../config/index"; -import { JournalType } from "../../../types/Journal"; -import IOSize, { Unit } from "../../../utils/IOSize"; - -enum MediaItemType { - IMAGE, - VIDEO -} - -type MediaItem = { - type: MediaItemType; - path: string; - thumbPath: string; - size: number; - duration: number | undefined; - raw: any; -} - -export type Location = { - lat: number; - lng: number; - text?: string; -} - -interface JournalEditorData { - idea: string; - date: string; - time: string; - type: JournalType; - mediaList: MediaItem[]; - location?: Location; - qqMapSDK?: any; - isAuthLocation: boolean; - uploaded: string; - uploadTotal: string; - uploadSpeed: string; - uploadProgress: number; - isSubmitting: boolean; -} - -Page({ - data: { - idea: "", - date: "2025-06-28", - time: "16:00", - type: JournalType.NORMAL, - mediaList: [], - location: undefined, - isAuthLocation: false, - uploaded: "0", - uploadTotal: "0 MB", - uploadSpeed: "0 MB / s", - uploadProgress: 0, - isSubmitting: false, - mediaItemTypeEnum: { - ...MediaItemType - } - }, - async onLoad() { - // 授权定位 - const setting = await wx.getSetting(); - wx.setStorageSync("isAuthLocation", setting.authSetting["scope.userLocation"]); - let isAuthLocation = JSON.parse(wx.getStorageSync("isAuthLocation")); - this.setData({ isAuthLocation }); - if (!isAuthLocation) { - wx.authorize({ - scope: "scope.userLocation" - }).then(() => { - isAuthLocation = true; - this.setData({ isAuthLocation }); - }); - } - - const unixTime = new Date().getTime(); - this.setData({ - date: Time.toDate(unixTime), - time: Time.toTime(unixTime) - }); - // 获取默认定位 - wx.getLocation({ - type: "gcj02" - }).then(resp => { - this.setData({ - location: { - lat: resp.latitude, - lng: resp.longitude - }, - }); - const argLoc = `location=${this.data.location!.lat},${this.data.location!.lng}`; - const argKey = "key=WW5BZ-J4LCM-UIT6I-65MXY-Z5HDT-VRFFU"; - wx.request({ - url: `https://apis.map.qq.com/ws/geocoder/v1/?${argLoc}&${argKey}`, - success: res => { - if (res.statusCode === 200) { - this.setData({ - location: { - lat: this.data.location!.lat, - lng: this.data.location!.lng, - text: (res.data as any).result?.formatted_addresses?.recommend - } - }); - } - } - }); - }); - }, - onChangeType(e: any) { - const { value } = e.detail; - this.setData({ type: value }); - }, - async chooseLocation() { - const location = await wx.chooseLocation({}); - this.setData({ - location: { - lat: location.latitude, - lng: location.longitude, - text: location.name - } - }); - }, - addMedia() { - const that = this; - wx.chooseMedia({ - mediaType: ["mix"], - sourceType: ["album", "camera"], - camera: "back", - success(res) { - wx.showLoading({ - title: "加载中..", - mask: true - }) - const tempFiles = res.tempFiles; - const mediaList = tempFiles.map(item => { - return { - type: (MediaItemType)[item.fileType.toUpperCase()], - path: item.tempFilePath, - thumbPath: item.thumbTempFilePath, - size: item.size, - duration: item.duration, - raw: item - } as MediaItem; - }); - that.setData({ - mediaList: [...that.data.mediaList, ...mediaList] - }); - wx.hideLoading(); - } - }) - }, - clearMedia() { - wx.showModal({ - title: "提示", - content: "确认清空已选照片或视频吗?", - confirmText: "清空", - confirmColor: "#E64340", - cancelText: "取消", - success: res => { - if (res.confirm) { - this.setData({ - mediaList: [] - }); - } - } - }) - }, - preview(e: WechatMiniprogram.BaseEvent) { - const itemIndex = e.currentTarget.dataset.index; - const total = this.data.mediaList.length; - - const startIndex = Math.max(0, itemIndex - 25); - const endIndex = Math.min(total, startIndex + 50); - const newCurrentIndex = itemIndex - startIndex; - - const sources = this.data.mediaList.slice(startIndex, endIndex).map(item => { - return { - url: item.path, - type: MediaItemType[item.type].toLowerCase() - } as WechatMiniprogram.MediaSource; - }); - wx.previewMedia({ - current: newCurrentIndex, - sources - }); - }, - deleteMedia(e: WechatMiniprogram.BaseEvent) { - const index = e.currentTarget.dataset.index; - const mediaList = [...this.data.mediaList]; - mediaList.splice(index, 1); - this.setData({ - mediaList - }); - }, - cancel() { - wx.switchTab({ - url: "/pages/main/journal/index", - }) - }, - submit() { - const handleFail = () => { - wx.showToast({ title: "上传失败", icon: "error" }); - wx.hideLoading(); - this.setData({ - isSubmitting: false - }); - }; - - this.setData({ - isSubmitting: true - }); - - // 获取 openId - const getOpenId = new Promise((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("登录失败")) - }); - }); - // 文件上传 - const uploadFiles = new Promise((resolve, reject) => { - const mediaList = this.data.mediaList || []; - if (mediaList.length === 0) { - resolve([]); - return; - } - - wx.showLoading({ title: "正在上传..", mask: true }); - - // 计算总大小 - const sizePromises = mediaList.map(item => { - return new Promise((sizeResolve, sizeReject) => { - wx.getFileSystemManager().getFileInfo({ - filePath: item.path, - success: (res) => sizeResolve(res.size), - fail: (err) => sizeReject(err) - }); - }); - }); - Promise.all(sizePromises).then(fileSizes => { - const totalSize = fileSizes.reduce((acc, size) => acc + size, 0); - const uploadTasks: WechatMiniprogram.UploadTask[] = []; - let uploadedSize = 0; - let lastUploadedSize = 0; - - this.setData({ - uploadTotal: IOSize.format(totalSize, 2, Unit.MB) - }); - // 计算上传速度 - const speedUpdateInterval = setInterval(() => { - const chunkSize = uploadedSize - lastUploadedSize; - this.setData({ - uploadSpeed: `${IOSize.format(chunkSize)} / s` - }); - lastUploadedSize = uploadedSize; - }, 1000); - // 上传文件 - const uploadPromises = mediaList.map(item => { - return new Promise((uploadResolve, uploadReject) => { - const task = wx.uploadFile({ - url: `${config.url}/temp/file/upload`, - filePath: item.path, - name: "file", - success: (resp) => { - const result = JSON.parse(resp.data); - if (result && result.code === 20000) { - uploadResolve(result.data[0].id); - } else { - uploadReject(new Error(`文件上传失败: ${result?.message || "未知错误"}`)); - } - }, - fail: (err) => uploadReject(new Error(`文件上传失败: ${err.errMsg}`)) - }); - - // 监听上传进度 - let prevProgress = 0; - task.onProgressUpdate((res) => { - const fileUploaded = (res.totalBytesExpectedToSend * res.progress) / 100; - const delta = fileUploaded - prevProgress; - uploadedSize += delta; - prevProgress = fileUploaded; - - // 更新进度条 - this.setData({ - uploaded: IOSize.formatWithoutUnit(uploadedSize, 2, Unit.MB), - uploadProgress: Math.round((uploadedSize / totalSize) * 10000) / 100 - }); - }); - uploadTasks.push(task); - }); - }); - - Promise.all(uploadPromises).then((tempFileIds) => { - // 清除定时器 - clearInterval(speedUpdateInterval); - uploadTasks.forEach(task => task.offProgressUpdate()); - this.setData({ - uploadProgress: 100, - uploadSpeed: "0 MB / s" - }); - resolve(tempFileIds); - }).catch((e: Error) => { - // 取消所有上传任务 - uploadTasks.forEach(task => task.abort()); - clearInterval(speedUpdateInterval); - reject(e); - }); - }).catch(reject); - }); - - // 并行执行获取 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: [], - isSubmitting: false, - uploaded: "0", - uploadTotal: "0 MB", - uploadProgress: 0 - }); - await Toolkit.sleep(1000); - wx.switchTab({ - url: "/pages/main/journal/index" - }); - }, - fail: handleFail - }); - }).catch(handleFail); - } -}); diff --git a/miniprogram/pages/main/journal-creater/index.wxml b/miniprogram/pages/main/journal-creater/index.wxml deleted file mode 100644 index e3f3f98..0000000 --- a/miniprogram/pages/main/journal-creater/index.wxml +++ /dev/null @@ -1,118 +0,0 @@ - - - 取消 - - - - -