You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git/scr/food/pages/submit/submit.js

412 lines
9.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

Page({
/**
* 页面的初始数据
*/
//分类1
data: {
title: '',
content: '',
question_id:'',
feed_source_img:'',
feed_source_name:'',
tempFilePaths: [],
nowCount:0,//当前的图片上传个数
index:0,
nickName:"",
avatarUrl:"",
feed:[],
pinglun:[],
comment_num:0,
good_num:0
},
bindPickerChange:function(e){
console.log(e)
this.setData({
index:e.detail.value
})
},
//图片的上传
chooseImage:function(e){
let that = this;
wx.chooseImage({
count: 3, // 默认最多3张图片可自行更改
sizeType: ['original', 'compressed'],// 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: res => {
wx.showToast({
title: '正在上传...',
icon: 'loading',
mask: true,
duration: 1000
})
// 返回选定照片的本地文件路径列表tempFilePath可以作为img标签的src属性显示图片
let tempFilePath = res.tempFilePaths;
console.log(tempFilePath);
let nowCount = that.data.nowCount;
let tempFilePaths = that.data.tempFilePaths;
if(that.data.nowCount + tempFilePath.length >=3){
let i = 0;
while(nowCount<3){//还可以继续上传图片
tempFilePaths.push(tempFilePath[i]);
i++;
nowCount++;
}
that.setData({
nowCount:3,
tempFilePaths:tempFilePaths
})
}else{
let i = 0;
while(i<tempFilePath.length){
tempFilePaths.push(tempFilePath[i]);
i++;
}
that.setData({
nowCount:that.data.nowCount + tempFilePath.length,
tempFilePaths:tempFilePaths
})
}
console.log(that.data.nowCount,that.data.tempFilePaths);
}
})
},
//长按删除图片
DeleteImg: function (e) {
let that = this;
let tempFilePaths = that.data.tempFilePaths;
let index = e.currentTarget.dataset.index;//获取当前长按图片下标
wx.showModal({
title: '提示',
content: '确定要删除此图片吗?',
success: function (res) {
if (res.confirm) {
//console.log('点击确定了');
let nowCount = that.data.nowCount;
that.setData({
nowCount:nowCount-1
})
tempFilePaths.splice(index, 1);
} else if (res.cancel) {
//console.log('点击取消了');
return false;
}
that.setData({
tempFilePaths:tempFilePaths
});
}
})
},
//上传文件到云端
submit:function(e){
let i;
let count=0;
let that = this;
let url = [];
let pl=[];
console.log(e);
let title = e.detail.value.name;
let content = e.detail.value.content;
if(title.length<3){
wx.showToast({
title: '标题少于3个字',
icon:"none"
})
return false
}
if(content.length<5){
wx.showToast({
title: '内容少于5个字',
icon:"none"
})
return false
}
wx.showLoading({
title: '发布中',
})
if(that.data.nowCount==0){
/*wx.cloud.callFunction({
name:'love_upload',
data:{
title:title,
content:content,
openid:that.data.openid,
url:[],
},success:function(e){
console.log(e);
wx.hideLoading({
success: (res) => {
that.setData({
value1:'',
value2:'',
tempFilePaths:[],
nowCount:0
})
wx.showToast({
title: '发布成功',
})
},
})
},fail:function(e){
wx.hideLoading({
success: (res) => {
wx.showToast({
title: '网络异常',
})
that.setData({
value1:'',
value2:'',
tempFilePaths:[],
nowCount:0
})
},
})
console.log(e);
}
})*/
let a=wx.getStorageSync ("nickName")
let b=wx.getStorageSync ("avatarUrl")
wx.cloud.database().collection('tiezi').add({
data: {
title:title,
content:content,
openid:that.data.openid,
url:url,
createTime: wx.cloud.database().serverDate(),
feed_source_img:b,
feed_source_name:a,
pinglun:pl,
comment_num:0,
good_num:0
}
})
wx.hideLoading({
success: (res) => {
that.setData({
value1:'',
value2:'',
tempFilePaths:[],
nowCount:0
})
wx.showToast({
title: '发布成功',
})}})
}else{
//将所有的内容上传到云端去
for(i=0;i<that.data.nowCount;i++){
console.log(1);
let extName = that.data.tempFilePaths[i].split(".").pop();
let cloudPath = "love/" + new Date().getTime() + "." + extName;
wx.cloud.uploadFile({
cloudPath: cloudPath,
filePath: that.data.tempFilePaths[i], // 文件路径
success: res => {
count++;
url.push(res.fileID);
console.log('上传图片');
console.log(i,url);
if(count==that.data.nowCount){
console.log(url);
/*
wx.cloud.callFunction({
name:'love_upload',
data:{
title:title,
content:content,
openid:that.data.openid,
url:url,
type:parseInt(that.data.index)+1
},success:function(e){
console.log(e);
wx.hideLoading({
success: (res) => {
that.setData({
value1:'',
value2:'',
tempFilePaths:[],
nowCount:0
})
wx.showToast({
title: '发布成功',
})
},
})
},fail:function(e){
wx.hideLoading({
success: (res) => {
wx.showToast({
title: '网络异常',
})
that.setData({
value1:'',
value2:'',
tempFilePaths:[],
nowCount:0
})
},
})
console.log(e);
}
})*/
let a=wx.getStorageSync ("nickName")
let b=wx.getStorageSync ("avatarUrl")
let pl=[];
wx.cloud.database().collection('tiezi').add({
data: {
title:title,
content:content,
openid:that.data.openid,
url:url,
createTime: wx.cloud.database().serverDate(),
feed_source_img:b,
feed_source_name:a,
pinglun:pl,
comment_num:0,
good_num:0
}
})
wx.hideLoading({
success: (res) => {
that.setData({
value1:'',
value2:'',
tempFilePaths:[],
nowCount:0
})
wx.showToast({
title: '发布成功',
})}})
}
},fail:res=>{
console.log(res);
}
})
}
}
wx.switchTab({
url: '../forum/forum',
})
},
blur:function(e){
console.log(e);
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let loveSort = wx.getStorageSync('loveSort')
console.log(loveSort)
this.setData({
loveSort:loveSort
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
onAdd_s: function () {
wx.cloud.init({
env: 'cloud1-8g5wmepxce8a3b8a'
});
const db = wx.cloud.database()
db.collection('tiezi').add({
data: {
title:title,
content:content,
openid:that.data.openid,
url:[],
},
success: res => {
// 在返回结果中会包含新创建的记录的 _id
this.setData({
counterId: res._id,
count: 1
})
wx.showToast({
title: '成功扔进树洞~',
})
console.log('[数据库] [留言] 成功,记录 _id: ', res._id)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '种种原因树洞拒绝了~'
})
console.error('[数据库] [新增记录] 失败:', err)
}
})
}
})