// pages/posts/add/index.js
import {
formatTime
} from './../../../utils/util'
const app = getApp();
const postTbl = wx.cloud.database().collection("forum-post");
Page({
data: {
title: '',
keyword: '',
content: '',
type: '',
imgList: [],
videoList: [],
fileIDs: [],
videoIDs: [],
actionShow: false,
typeActions: [{
name: '经验分享',
}, {
name: '疑问交流',
}]
},
showAction() {
this.setData({
actionShow: true
});
},
onActionClose() {
this.setData({
actionShow: false
});
},
onActionSelect(event) {
this.setData({
type: event.detail.name
})
},
//选择图片
ChooseImage() {
wx.chooseMedia({
count: 6 - this.data.imgList.length - this.data.videoList.length, //默认6,我们这里最多选择9个文件
sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
success: (res) => {
console.log("选择成功", res)
if (res.type === 'image') {
let paths = res.tempFiles.map(x => x.tempFilePath);
this.setData({
imgList: [...this.data.imgList, ...paths]
})
} else {
this.setData({
videoList: [...this.data.videoList, ...res.tempFiles]
})
}
console.log("路径", this.data.imgList)
}
});
},
//删除图片
DeleteImg(e) {
wx.showModal({
title: '确定要删除该附件吗?',
content: '',
cancelText: '取消',
confirmText: '确定',
success: res => {
if (res.confirm && e.currentTarget.dataset.type === 'image') {
this.data.imgList.splice(e.currentTarget.dataset.index, 1);
this.setData({
imgList: this.data.imgList
})
} else if (res.confirm && e.currentTarget.dataset.type === 'video') {
this.data.videoList.splice(e.currentTarget.dataset.index, 1);
this.setData({
videoList: this.data.videoList
})
}
}
})
},
//上传数据
publish(e) {
let {
imgList,
videoList
} = this.data;
if (this.data.title == null || this.data.keyword == null || this.data.content == null || this.data.type == '' || (this.data.imgList.length === 0 && this.data.videoList.length === 0)) {
wx.showToast({
icon: "none",
title: '所有内容都不能为空哦!'
})
return
}
wx.showLoading({
title: '发布中...',
})
const promiseArr = []
//只能一张张上传 遍历临时的图片数组
for (let i = 0; i < this.data.imgList.length; i++) {
let filePath = this.data.imgList[i]
let suffix = /\.[^\.]+$/.exec(filePath)[0]; // 正则表达式,获取文件扩展名
//在每次上传的时候,就往promiseArr里存一个promise,只有当所有的都返回结果时,才可以继续往下执行
promiseArr.push(new Promise((reslove, reject) => {
wx.cloud.uploadFile({
cloudPath: new Date().getTime() + suffix,
filePath: filePath, // 文件路径
}).then(res => {
// get resource ID
console.log("上传结果", res.fileID)
this.setData({
fileIDs: this.data.fileIDs.concat(res.fileID)
})
reslove()
}).catch(error => {
console.log("上传失败", error)
})
}))
}
let videoIDs = [];
for (let i = 0; i < this.data.videoList.length; i++) {
videoIDs.push({
videoID: '',
coverID: ''
})
let videoPath = this.data.videoList[i].tempFilePath
let coverPath = this.data.videoList[i].thumbTempFilePath;
let suffix = /\.[^\.]+$/.exec(videoPath)[0]; // 正则表达式,获取文件扩展名
let coverSuffix = /\.[^\.]+$/.exec(coverPath)[0]; // 正则表达式,获取文件扩展名
//在每次上传的时候,就往promiseArr里存一个promise,只有当所有的都返回结果时,才可以继续往下执行
promiseArr.push(new Promise((reslove, reject) => {
wx.cloud.uploadFile({
cloudPath: new Date().getTime() + suffix,
filePath: videoPath, // 文件路径
}).then(res => {
// get resource ID
console.log("上传结果", res.fileID)
videoIDs[i].videoID = res.fileID;
reslove()
}).catch(error => {
console.log("上传失败", error)
})
}))
//在每次上传的时候,就往promiseArr里存一个promise,只有当所有的都返回结果时,才可以继续往下执行
promiseArr.push(new Promise((reslove, reject) => {
wx.cloud.uploadFile({
cloudPath: new Date().getTime() + coverSuffix,
filePath: coverPath, // 文件路径
}).then(res => {
// get resource ID
console.log("上传结果", res.fileID)
videoIDs[i].coverID = res.fileID;
reslove()
}).catch(error => {
console.log("上传失败", error)
})
}))
}
//保证所有图片都上传成功
let db = wx.cloud.database()
Promise.all(promiseArr).then(res => {
postTbl.add({
data: {
fileIDs: this.data.fileIDs,
videoIDs,
date: formatTime(new Date()),
createTime: db.serverDate(),
images: this.data.imgList,
title: this.data.title,
type: this.data.type,
content: this.data.content,
keyword: this.data.keyword,
createdBy: app.globalData.userInfo._id,
creator: app.globalData.userInfo.nickName
},
success: res => {
wx.hideLoading()
wx.showToast({
title: '发布成功',
})
console.log('发布成功', res)
wx.navigateBack({
delta: 1,
})
},
fail: err => {
wx.hideLoading()
wx.showToast({
icon: 'none',
title: '网络不给力....'
})
console.error('发布失败', err)
}
})
})
},
})
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
forum.zip (49个子文件)
sitemap.json 191B
pages
index
index.wxml 978B
index.js 1KB
index.json 27B
index.wxss 264B
posts
detail
index.wxml 2KB
index.js 3KB
index.json 27B
index.wxss 1KB
index.wxml 470B
add
index.wxml 2KB
index.js 7KB
index.json 27B
index.wxss 775B
tab
index.wxml 1KB
index.js 3KB
index.json 52B
index.wxss 910B
index.js 602B
index.json 55B
index.wxss 200B
mine
index.wxml 492B
index.js 608B
index.json 27B
index.wxss 249B
my-star
index.wxml 345B
index.js 372B
index.json 64B
index.wxss 30B
login
index.wxml 1KB
index.js 3KB
index.json 27B
index.wxss 295B
app.json 2KB
project.config.json 2KB
app.js 906B
utils
util.js 460B
app.wxss 194B
package.json 252B
package-lock.json 317B
.gitignore 161B
images
forum.png 2KB
photo.png 903B
mine.png 3KB
add.png 2KB
close.png 1KB
forum-o.png 3KB
mine-o.png 4KB
readme.text 31B
共 49 条
- 1
资源评论
- xdxd10182024-03-07资源有一定的参考价值,与资源描述一致,很实用,能够借鉴的部分挺多的,值得下载。
code.song
- 粉丝: 387
- 资源: 916
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功