pages/upVideo.wxml
<view class="headerBox">
<image src="{
{userInfo.weixinInfo.avatarUrl}}"></image>
<text>{
{
userInfo.weixinInfo.nickName}}</text>
</view>
<view class="classifyBox">
<input type="text" placeholder="课程标题(20个字以内)" value="{
{title}}" maxlength="20" bindblur="titleBlur" bindinput="getInput"></input>
<input type="digit" placeholder="课程价格" value="{
{price}}" bindblur="priceBlur" bindinput="getPrice"></input>
<select prop-array='{
{selectList}}' selectText="选择课程分类" selectShow="{
{select}}" bind:m_select_touch="m_select_touch"></select>
</view>
<view class="desc">
<textarea focus="true" placeholder="课程简介" value="{
{desc}}" maxlength="-1" placeholder-style="font-size:14px;color:#666" bindblur="descBlur" bindinput="getTextarea" />
</view>
<!-- 敏感内容警告提示节点 -->
<van-notify id="van-notify" />
<view class="imageRootAll">
<van-uploader
file-list="{
{ fileList }}"
accept="{
{accept}}"
multiple="true"
compressed="false"
bind:delete="Delete"
bind:after-read="afterRead"
/>
</view>
<button type="primary" bindtap="publish">确认上传</button>
pages/upVideo.wxss
.headerBox{
height:80px;
width:100%;
background:#fff;
display: flex;
align-items: center;
}
.headerBox image{
height: 60px;
width:60px;
border-radius: 50%;
margin:0px 20px;
}
.headerBox text{
font-size:18px;
color:#999;
}
.classifyBox{
width: 100%;
height: 110px;
background: #fff;
border-top:1px solid #b1b1b1;
}
.classifyBox input{
height: 30px;
width: 90%;
margin:5px 5%;
font-size: 14px;
border-bottom:1px solid #b1b1b1;
}
.desc{
width:100%;
height:200px;
background:#fff;
}
.desc textarea{
width:90%;
margin:10px 5%;
height:100%;
}
.imageRootAll{
background:#fff;
width:100%;
}
.imageRootAll van-uploader{
width:98%;
margin:1%;
background:#fff;
}
button{
margin:30px 10%;
width:80%;
height: 40px;
}
.showChoose{
height: 200px;
width:200px;
margin-left:5%;
background:#cdcdcd;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.showChoose image{
width:60px;
height:60px;
}
.showChoose view{
color:#efefef;
}
van-popup{
height: 100%;
width:100%;
}
van-popup view{
height: 20%;
width:100%;
text-align: center;
}
van-popup .chooseImg{
margin-top:2%;
}
pages/upVideo.json 。用到的vant 组件都是统一在全局app.json中引入的,这里就没必要重复引入了
{
"usingComponents": {
"select": "/components/select/select"
}
}
到官网中下载,然后引入就可以,主要是引入路径的修改。
pages/upVideo.js。图片上传和视频上传其实都是差不多的,定义一个临时存储的数组,然后对数组进行遍历,上传到云存储,保存云存储的存储路径保存到云数据库,改一下返回的一些参数就可以了。
const app=getApp();
const {
days,TurnSecondPoints}=require('../../utils/getNowFormatDate.js');
const {
hasSensitiveWords}=require('../../utils/contentdetection.js')
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
wx.cloud.init();
Page({
data: {
userInfo: [],
title: '',
desc: '',
classifyName: '',
price:"",
accept:'video',
fileList: [],
fileIDs: [],
selectList: [{
name: '历史文化',
id: 1
},
{
name: '英语二外',
id: 2
},
{
name: '创课编程',
id: 3
},
{
name: '游学研学',
id: 4
},
{
name: '好书推荐',
id: 5
},
{
name: '健身瑜伽',
id: 6
},
{
name: '生活点滴',
id: 7
},
{
name: '成长点滴',
id: 8
},
{
name: '亲子关系',
id: 9
},
{
name: '职场技能',
id: 10
},
{
name: '学考公考',
id: 11
},
{
name: '家庭教育',
id: 12
},
{
name: '早教托班',
id: 13
},
{
name: '亲子关系',
id: 14
},
{
name: '职场技能',
id: 15
},
{
name: '感统训练',
id: 16
},
{
name: '复读学习',
id: 17
},
{
name: '语文',
id: 18
},
{
name: '数学',
id: 19
},
{
name: '英语',
id: 20
},
{
name: '音乐',
id: 21
},
{
name: '美术',
id: 22
},
{
name: '夏令营',
id: 23
},
{
name: '体适能',
id: 24
},
{
name: '专注力',
id: 25
},
{
name: '主持',
id: 26
},
{
name: '其他',
id: 27
}
],
select: false,
select_value1: {
"id": "0",
"text": "未选择"
},
show: false,
},
showPopup() {
this.setData({
show: true
});
},
onClose() {
this.setData({
show: false
});
},
m_select_touch(e) {
let that = this;
let selectIndex = e.detail.selIndex;
console.log(that.data.selectList[selectIndex].name)
that.setData({
classifyName: that.data.selectList[selectIndex].name
})
},
getInput(event) {
let textVal = event.detail.value;
if (hasSensitiveWords(textVal)) {
Notify({
type: 'warning', message: '含有敏感词,敏感词将会用*号处理' });
this.setData({
title: textVal.replace(textVal, "***")
})
console.log(this.data.title);
} else {
this.setData({
title: textVal
})
console.log(this.data.title);
}
},
titleBlur:function(){
let that=this;
wx.cloud.callFunction({
name: 'msgSecCheck',
data: {
text: that.data.title
},
success(res){
if(res.result.code == 500){
Notify({
type: 'warning', message: '内容包含违法违规词汇,禁止发布' });
that.setData({
title:''
})
}
},
fail(err){
console.log(err)
}
})
},
priceBlur:function(){
let that=this;
wx.cloud.callFunction({
name: 'msgSecCheck',
data: {
text: that.data.price
},
success(res){
console.log(res)
if(res.result.code == 500){
Notify({
type: 'warning', message: '内容包含违法违规词汇,禁止发布' });
that.setData({
price:''
})
}
},
fail(err){
console.log(err)
}
})
},
getPrice:function(event){
let textVal = event.detail.value;
if (hasSensitiveWords(textVal)) {
Notify({
type: 'warning', message: '含有敏感词,敏感词将会用*号处理' });
this.setData({
price: textVal.replace(textVal, "***")
})
console.log(this.data.price);
} else {
this.setData({
price: textVal
})
console.log(this.data.price);
}
},
getTextarea(event) {
let textVal = event.detail.value;
if (hasSensitiveWords(textVal)) {
Notify({
type: 'warning', message: '含有敏感词,敏感词将会用*号处理' });
this.setData({
desc: textVal.replace(textVal, "***")
})
console.log(this.data.desc);
} else {
this.setData({
desc: textVal
})
console.log(this.data.desc);
}
},
descBlur:function(){
let that=this;
wx.cloud.callFunction({
name: 'msgSecCheck',
data: {
text: that.data.desc
},
success(res){
if(res.result.code == 500){
Notify({
type: 'warning', message: '内容包含违法违规词汇,禁止发布' });
that.setData({
desc:''
})
}
},
fail(err){
console.log(err)
}
})
},
afterRead(event) {
let that = this;
let obj = event.detail
let NewfileList = {
}
NewfileList.url = obj.file.tempFilePath
NewfileList.name = "video"+obj.index
NewfileList.index=obj.index
NewfileList.type = 'video'
NewfileList.duration = obj.file.duration
NewfileList.size=obj.file.size
let fileList = []
fileList.push(...that.data.fileList, NewfileList);
that.setData({
fileList
})
console.log(that.data.fileList)
},
Delete(event) {
let that = this
console.log(event)
console.log(event.detail.index)
wx.showModal({
title: '要删除这个文件吗?',
content: '',
cancelText: '取消',
confirmText: '确定',
success: res => {
if (res.confirm) {
that.data.fileList.splice(event.detail.index, 1);
that.setData({
fileList: that.data.fileList
})
}
}
})
},
publish() {
let that=this
var title=that.data.title;
var price=that.data.price;
var classifyName=that.data.classifyName;
var desc = that.data.desc;
var fileList = that.data.fileList;
if (!title) {
wx.showToast({
icon: "none",
title: '请输入课程标题'
})
return
}
if(!price) {
wx.showToast({
icon: "none",
title: '请输入课程价格'
})
return
}
if(!classifyName) {
wx.showToast({
icon: "none",
title: '请选择课程分类'
})
return
}
if (!desc || desc.length < 6) {
wx.showToast({
icon: "none",
title: '请输入课程简介'
})
return
}
wx.showLoading({
title: '正在上传...',
})
const promiseArr = []
for (let i = 0; i < that.data.fileList.length; i++) {
let filePath = that.data.fileList[i].url
let type=that.data.fileList[i].type
let duration=TurnSecondPoints(that.data.fileList[i].duration)
let title=that.data.fileList[i].name
let index=that.data.fileList[i].index
let suffix = /\.[^\.]+$/.exec(filePath)[0];
promiseArr.push(new Promise((reslove, reject) => {
wx.cloud.uploadFile({
cloudPath:'seriesLesson/'+new Date().getTime() + suffix,
filePath: filePath,
}).then(res => {
that.setData({
fileIDs: that.data.fileIDs.concat({
type:type,url:res.fileID,duration:duration,title:title,index:index})
})
reslove()
}).catch(error => {
console.log("上传失败", error)
})
}))
}
Promise.all(promiseArr).then(res => {
var timestamp=(new Date()).getTime()
wx.cloud.callFunction({
name:'regist',
data:{
$url:'UploadVideo',
avatarUrl:that.data.userInfo.weixinInfo.avatarUrl,
nickName:that.data.userInfo.weixinInfo.nickName,
level:that.data.userInfo.level,
openId:that.data.userInfo.openId,
company:that.data.userInfo.company,
createTime:days(),
timestamp:timestamp,
className:classifyName,
coverTitle:title,
coverImg:that.data.fileIDs[0]?that.data.fileIDs[0].url:'',
text:desc,
imgUrls:that.data.fileIDs?that.data.fileIDs:[],
pay:true,
price:Number(price)
},
success: res => {
wx.hideLoading()
wx.cloud.callFunction({
name:'regist',
data:{
$url:'selectMycourses',
timestamp:timestamp
},
success:res=>{
wx.cloud.callFunction({
name:'regist',
data:{
$url:'addMycourses',
openId:res.result.result.data[0].openId,
_id:res.result.result.data[0]._id
},
success(res){
}
})
}
})
wx.showModal({
title: '提示',
content: '上传成功,是否需要返回首页?',
success (res) {
if (res.confirm) {
wx.switchTab({
url: '../home/home',
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
},
fail: err => {
wx.hideLoading()
wx.showToast({
icon: 'none',
title: '网络不给力....'
})
}
})
})
},
onLoad: function (options) {
let that = this;
wx.getStorage({
key: 'userInfo',
success(res) {
console.log(res)
that.setData({
userInfo: res.data
})
}
})
},
onReady: function () {
console.log(this.data)
},
onShow: function () {
console.log(this.data.fileList)
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})
云函数 regist/index.js UploadVideo函数,云函数路由的使用在其他文章中有写
app.router('UploadVideo', async (ctx) => {
await db.collection('seriesLessons').add({
data: {
avatarUrl: event.avatarUrl,
nickName: event.nickName,
openId: event.openId,
level: event.level,
company: event.company,
createTime: event.createTime,
timestamp: event.timestamp,
className: event.className,
coverTitle: event.coverTitle,
coverImg: event.coverImg,
text: event.text,
media: {
imgWidth: event.imgWidth,
imgUrls: event.imgUrls
},
pay: event.pay,
price:event.price,
collection: [],
dianzan: [],
pinglun: [],
zhuanfa: []
},
success(res) {
ctx.body = {
event,
message: '动态发布成功'
}
}
})
});
- select 组件是一个自定义下拉选择内容的组件,根据个人情况使用。
- upVideo.js文件引入的敏感内容检测js在另一篇专门写这个的有写。
- 所有关于微信小程序云开发的文章都是在做了一个微信小程序云开发项目后的笔记总结,在开发过程中也有百度查资料,当时只顾着用来写项目,也就没有记下是参照了哪位大佬的文章了,厚颜说是原创,希望那些大佬们不要介意。
- 记录下这些东西一方面是做一下笔记,以后可能还用的上,另一方面也希望能帮到别人和得到大佬的指点。欢迎各位的指点和留言。