仿照微信朋友圈做了一个界面如下,先看效果:
1、点开界面
2、选择图片
3、点击上传
4、动态显示
第一个页面的wxml:
发布
第一个页面的wcss:
.page{
padding: 20px
}
.text{
width: 100%;
margin-bottom: 40px;
font-size: 20px;
padding: 5px
}
.image{
width:31%;
height: 100px;
padding: 2px
}
.moment_img{
width: 98px;
height: 98px;
}
.image_content{
width: 100%;
display: flex;
flex-wrap: wrap;
margin-bottom: 20px
}
第一个页面的js:
Page({
data: {
img_url: [],
content:''
},
onLoad: function (options) {
},
input:function(e){
this.setData({
content:e.detail.value
})
},
chooseimage:function(){
var that = this;
wx.chooseImage({
count: 9, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
if (res.tempFilePaths.length>0){
//图如果满了9张,不显示加图
if (res.tempFilePaths.length == 9){
that.setData({
hideAdd:1
})
}else{
that.setData({
hideAdd: 0
})
}
//把每次选择的图push进数组
let img_url = that.data.img_url;
for (let i = 0; i < res.tempFilePaths.length; i++) {
img_url.push(res.tempFilePaths[i])
}
that.setData({
img_url: img_url
})
}
}
})
},
//发布按钮事件
send:function(){
var that = this;
var user_id = wx.getStorageSync('userid')
wx.showLoading({
title: '上传中',
})
that.img_upload()
},
//图片上传
img_upload: function () {
let that = this;
let img_url = that.data.img_url;
let img_url_ok = [];
//由于图片只能一张一张地上传,所以用循环
for (let i = 0; i < img_url.length; i++) {
wx.uploadFile({
//路径填你上传图片方法的地址
url: 'http://wechat.homedoctor.com/Moments/upload_do',
filePath: img_url[i],