122 lines
2.7 KiB
JavaScript
122 lines
2.7 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)
|
||
|
|
},
|
||
|
|
fail: reject
|
||
|
|
})
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
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(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)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
})
|