Wire real WeChat app config, prefer activity cover over posters, and add admin cover cropping for the home banner. Co-authored-by: Cursor <cursoragent@cursor.com>
140 lines
3.2 KiB
JavaScript
140 lines
3.2 KiB
JavaScript
const api = require('../../utils/api')
|
|
const media = require('../../utils/media')
|
|
const activityUtil = require('../../utils/activity')
|
|
|
|
function chooseOne(mediaType) {
|
|
return new Promise((resolve, reject) => {
|
|
wx.chooseMedia({
|
|
count: 1,
|
|
mediaType: [mediaType],
|
|
sourceType: ['album', 'camera'],
|
|
maxDuration: 60,
|
|
success: (res) => {
|
|
const file = res.tempFiles && res.tempFiles[0]
|
|
if (file && file.tempFilePath) resolve(file.tempFilePath)
|
|
else reject(new Error('未选择文件'))
|
|
},
|
|
fail: reject
|
|
})
|
|
})
|
|
}
|
|
|
|
// Crop to a wide banner ratio before upload (closest built-in scale is 16:9).
|
|
function cropCover(filePath) {
|
|
return new Promise((resolve, reject) => {
|
|
if (!wx.cropImage) {
|
|
resolve(filePath)
|
|
return
|
|
}
|
|
wx.cropImage({
|
|
src: filePath,
|
|
cropScale: '16:9',
|
|
success: (res) => resolve(res.tempFilePath || filePath),
|
|
fail: () => resolve(filePath)
|
|
})
|
|
})
|
|
}
|
|
|
|
function uploadWithLoading(filePath) {
|
|
wx.showLoading({ title: '上传中', mask: true })
|
|
return api.uploadMedia(filePath).then(
|
|
(data) => {
|
|
wx.hideLoading()
|
|
return data
|
|
},
|
|
(error) => {
|
|
wx.hideLoading()
|
|
throw error
|
|
}
|
|
)
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
title: '',
|
|
startAt: '',
|
|
pinned: false,
|
|
cover: '',
|
|
coverPreview: '',
|
|
video: '',
|
|
saving: false
|
|
},
|
|
|
|
onLoad() {
|
|
api
|
|
.profile()
|
|
.then((profile) => {
|
|
if (profile && profile.isAdmin) return
|
|
wx.showToast({ title: '仅管理员可操作', icon: 'none' })
|
|
setTimeout(() => wx.navigateBack(), 400)
|
|
})
|
|
.catch((error) => {
|
|
activityUtil.toast(error)
|
|
setTimeout(() => wx.navigateBack(), 400)
|
|
})
|
|
},
|
|
|
|
onTitle(event) {
|
|
this.setData({ title: event.detail.value })
|
|
},
|
|
|
|
onTime(event) {
|
|
this.setData({ startAt: event.detail.value })
|
|
},
|
|
|
|
togglePinned() {
|
|
this.setData({ pinned: !this.data.pinned })
|
|
},
|
|
|
|
pickCover() {
|
|
chooseOne('image')
|
|
.then(cropCover)
|
|
.then(uploadWithLoading)
|
|
.then((data) => this.setData({ cover: data.url, coverPreview: media.absUrl(data.url) }))
|
|
.catch((error) => {
|
|
if (error && error.message) activityUtil.toast(error)
|
|
})
|
|
},
|
|
|
|
clearCover() {
|
|
this.setData({ cover: '', coverPreview: '' })
|
|
},
|
|
|
|
pickVideo() {
|
|
chooseOne('video')
|
|
.then(uploadWithLoading)
|
|
.then((data) => this.setData({ video: data.url }))
|
|
.catch((error) => {
|
|
if (error && error.message) activityUtil.toast(error)
|
|
})
|
|
},
|
|
|
|
clearVideo() {
|
|
this.setData({ video: '' })
|
|
},
|
|
|
|
save() {
|
|
if (this.data.saving) return
|
|
const title = this.data.title.trim()
|
|
const startAt = this.data.startAt.trim()
|
|
if (!title || !startAt) {
|
|
wx.showToast({ title: '请填写名称和时间', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ saving: true })
|
|
api
|
|
.createPast({
|
|
title: title,
|
|
startAt: startAt,
|
|
pinned: this.data.pinned,
|
|
cover: this.data.cover,
|
|
video: this.data.video
|
|
})
|
|
.then(() => wx.navigateBack())
|
|
.catch((error) => {
|
|
this.setData({ saving: false })
|
|
activityUtil.toast(error)
|
|
})
|
|
}
|
|
})
|