This commit is contained in:
aaron 2026-05-12 23:10:14 +08:00
parent 1d8b815f95
commit cc97f6aebb
5 changed files with 159 additions and 0 deletions

View File

@ -0,0 +1,3 @@
{
"enablePullDownRefresh": true
}

View File

@ -0,0 +1,74 @@
const { postForm, uploadFile } = require("../../utils/api");
const { getActiveClassId, showError } = require("../../utils/page-helpers");
Page({
data: {
title: "",
content: "",
images: [],
loading: false
},
onLoad() {
wx.setNavigationBarTitle({ title: "发布动态" });
},
onTitleInput(event) {
this.setData({ title: event.detail.value });
},
onContentInput(event) {
this.setData({ content: event.detail.value });
},
chooseImages() {
wx.chooseMedia({
count: 9 - this.data.images.length,
mediaType: ["image"],
sourceType: ["album", "camera"],
success: (res) => {
const paths = (res.tempFiles || []).map((item) => item.tempFilePath);
this.setData({ images: [...this.data.images, ...paths].slice(0, 9) });
}
});
},
removeImage(event) {
const index = Number(event.currentTarget.dataset.index);
this.setData({
images: this.data.images.filter((_, itemIndex) => itemIndex !== index)
});
},
async submit() {
const title = this.data.title.trim();
const content = this.data.content.trim();
if (!title) {
wx.showToast({ title: "请输入标题", icon: "none" });
return;
}
this.setData({ loading: true });
try {
const classId = getActiveClassId();
const created = await postForm("/api/timeline/", {
title,
content,
class_id: String(classId || "")
});
for (let index = 0; index < this.data.images.length; index += 1) {
await uploadFile(`/api/timeline/${created.id}/images`, this.data.images[index]);
}
wx.showToast({ title: "已发布", icon: "success" });
const pages = getCurrentPages();
const previousPage = pages[pages.length - 2];
if (previousPage?.setData) {
previousPage.setData({ needsRefresh: true });
}
setTimeout(() => wx.navigateBack(), 500);
} catch (error) {
showError(error, "发布失败");
} finally {
this.setData({ loading: false });
}
}
});

View File

@ -0,0 +1,3 @@
{
"usingComponents": {}
}

View File

@ -0,0 +1,33 @@
<view class="page">
<view class="hero">
<view class="eyebrow">CLASS FEED</view>
<view class="hero-title">发布班级动态</view>
<view class="hero-subtitle">分享课程现场、活动照片、学习收获或班级提醒。</view>
</view>
<view class="section">
<view class="card">
<view class="muted">标题</view>
<input value="{{title}}" bindinput="onTitleInput" placeholder="一句话概括这条动态" />
</view>
<view class="card">
<view class="muted">正文</view>
<textarea value="{{content}}" bindinput="onContentInput" placeholder="写下想和同学分享的内容" />
</view>
<view class="card">
<view class="section-head">
<view class="card-title">图片</view>
<view class="section-action" bindtap="chooseImages">添加</view>
</view>
<view wx:if="{{images.length}}" class="image-grid">
<view wx:for="{{images}}" wx:key="*this" class="image-cell">
<image src="{{item}}" mode="aspectFill" />
<view class="image-remove" data-index="{{index}}" bindtap="removeImage">×</view>
</view>
</view>
<view wx:else class="muted">最多可添加 9 张图片</view>
</view>
</view>
<button class="button" loading="{{loading}}" bindtap="submit">发布动态</button>
</view>

View File

@ -0,0 +1,46 @@
input {
margin-top: 12rpx;
height: 72rpx;
font-size: 30rpx;
}
textarea {
width: 100%;
min-height: 220rpx;
margin-top: 12rpx;
font-size: 28rpx;
}
.image-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12rpx;
margin-top: 18rpx;
}
.image-cell {
position: relative;
overflow: hidden;
height: 180rpx;
border-radius: 20rpx;
background: #efe0ca;
}
.image-cell image {
width: 100%;
height: 100%;
}
.image-remove {
position: absolute;
top: 8rpx;
right: 8rpx;
width: 42rpx;
height: 42rpx;
border-radius: 999rpx;
background: rgba(47, 33, 28, 0.72);
color: #fff;
font-size: 32rpx;
line-height: 38rpx;
text-align: center;
}