实战项目展示
- 需要用户授权登录来获取用户名(暂时没有获取其他信息,如有需要自己在后续denglu页面中进行设置)
- 登录成功后进入云相册界面。点击右下角可上传图片(暂时设置为只能同时上传一张),其他用户上传的图片也可显示并带有用户名。点击图片可显示大图,用来保存转发等。
项目代码(复制可用,需配置云数据库和云存储)
1.云数据库数据存储格式
2.云存储格式
- 图标资源放页面中的图标数据,大家可以自己更换自己喜欢的图标
- 上传图片为用户上传到云相册的图片
3.部分代码展示(整个项目代码已上传至资源)
3-1. 用户登录验证部分页面
{
"usingComponents": {},
"enablePullDownRefresh": false
}
<!--pages/denglu/denglu.wxml-->
<view class="content">
<text class="title">香猪科技云相册</text>
<button type="primary" class="btn_denglu" bindtap="denglu">用户登录</button>
</view>
Page({
onLoad: function (options) {
this.denglu()
},
denglu() {
let user = wx.getStorageSync('user')
if (user == "") {
wx.showModal({
title: '温馨提示',
content: '亲,授权微信登录后才能正常使用小程序功能',
success: res => {
if (res.confirm) {
wx.getUserProfile({
desc: '获取你的昵称、头像、地区及性别',
success: res => {
console.log("获取到了用户信息", res)
let user = res.userInfo
wx.setStorageSync('user', user)
wx.redirectTo({
url: '/pages/list/list?userinfo=' + res.userInfo.nickName,
})
return res
},
fail: err => {
wx.showToast({
title: '您拒绝了请求,不能正常使用小程序',
icon: 'error',
duration: 2000
})
return -1
}
});
} else if (res.cancel) {
wx.showToast({
title: '您拒绝了请求,不能正常使用小程序',
icon: 'error',
duration: 2000
})
return -1
}
}
})
}else{
console.log("已存在用户数据")
wx.reLaunch({
url: '/pages/list/list?userinfo=' + user.nickName,
})
}
},
})
.title{
font-size: 80rpx;
text-align: right;
}
.content{
text-align: center;
}
.btn_denglu{
margin-top: 50rpx;
}
3-2.用户上传图片页面
{
"usingComponents": {},
"enablePullDownRefresh": false
}
<!--pages/home/home.wxml-->
<button bindtap='upload'>上传图片</button>
<image src='{{imgUrl}}'></image>
let username = null
Page({
onLoad: function (options) {
username = options.username
},
data: {
imgUrl: ""
},
upload() {
let timestamp = Date.parse(new Date());
console.log("当前时间戳", timestamp)
console.log("点击了图片上传")
wx.chooseImage({
count: 1,
success: chooseResult => {
wx.showLoading({
title: '上传中。。。',
})
wx.cloud.uploadFile({
cloudPath: "实战资源/云相册/U上传图片/" + timestamp + '.png',
filePath: chooseResult.tempFilePaths[0],
success: res => {
wx.hideLoading()
console.log('上传成功', res)
this.setData({
imgUrl: res.fileID
})
this.addImgList(res.fileID)
wx.showToast({
title: '图片上传成功',
duration: 2000
})
setTimeout(function () {
wx.reLaunch({
url: '/pages/list/list',
})
}, 2000);
},
})
},
})
},
addImgList(imgurl) {
wx.cloud.database().collection('imagelist').add({
data: {
name: username,
imgUrl: imgurl,
time: this.getNowFormatDate()
},
success(res) {
console.log("添加成功", res)
},
fail(res) {
console.log("添加失败", res)
}
})
},
getNowFormatDate: function () {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
" " + date.getHours() + seperator2 + date.getMinutes() +
seperator2 + date.getSeconds();
return currentdate;
},
})
3-3.云相册数据展示页面
<!-- 列表 -->
<block wx:for="{{dataList}}" wx:key="item">
<view class='item-container'>
<text class='item-name'>上传人:{{item.name}}</text>
<text class='item-name'>上传时间:{{item.time}}</text>
<image class='img' src='{{item.imgUrl}}' bindtap="showdatu" data-imgurl="{{item.imgUrl}}"></image>
<image class='shanchu' bindtap='delete2' data-id="{{item._id}}" data-imgurl="{{item.imgUrl}}" src="https://7869-xiaoxiangzhu-0g50plnie0fa8913-1307819210.tcb.qcloud.la/%E5%AE%9E%E6%88%98%E8%B5%84%E6%BA%90/%E4%BA%91%E7%9B%B8%E5%86%8C/M%E5%9B%BE%E6%A0%87%E8%B5%84%E6%BA%90/delete.png"></image>
</view>
</block>
<image bindtap='qufabu' class='fabu' src='https://7869-xiaoxiangzhu-0g50plnie0fa8913-1307819210.tcb.qcloud.la/%E5%AE%9E%E6%88%98%E8%B5%84%E6%BA%90/%E4%BA%91%E7%9B%B8%E5%86%8C/M%E5%9B%BE%E6%A0%87%E8%B5%84%E6%BA%90/fabu.png'></image>
let username = null
Page({
data: {
dataList: [],
userInfo: ""
},
onLoad: function (options) {
username = options.userinfo
console.log("用户名为:", username)
},
onShow: function () {
this.getImageList();
},
onReady: function () {
this.getImageList();
},
getImageList() {
wx.cloud.database().collection('imagelist').get({
success: res => {
wx.stopPullDownRefresh()
this.setData({
dataList: res.data
})
}
})
},
qufabu() {
wx.navigateTo({
url: '../home/home?username=' + username,
})
},
delete2(event) {
let id = event.currentTarget.dataset.id;
console.log("点击了删除按钮", id)
wx.showModal({
title: '警告!',
content: '您确定要删除吗?',
success: res => {
if (res.confirm) {
console.log("点击了确定按钮")
wx.cloud.database()
.collection('imagelist')
.doc(id)
.remove({
success: res => {
console.log("删除成功", res)
wx.cloud.deleteFile({
fileList: [event.currentTarget.dataset.imgurl],
success(res) {
console.log(res, '删除文件成功')
console.log("删除图片的链接为", event.currentTarget.dataset.imgurl)
},
fail(err) {
console.log("删除文件失败", err)
}
})
this.getImageList();
}
})
} else {
console.log("点击了取消按钮")
}
}
})
},
showdatu(event) {
wx.previewImage({
urls: [event.currentTarget.dataset.imgurl],
showmenu: true,
})
},
onPullDownRefresh: function () {
console.log('下拉刷新的监听')
this.getImageList()
}
})
page {
background: #2db7f5;
}
.item-container {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
width: 92%;
margin: 4%;
display: flex;
flex-direction: column;
background: white;
padding-top: 5pt;
padding-bottom: 5pt;
border-radius: 5px;
}
.item-name {
font-size: 12px;
margin-left: 15px;
}
.img {
width: 100px;
height: 100px;
margin-top: 10px;
margin-left: 20px;
}
.fabu{
width: 40px;
height: 40px;
position: fixed;
bottom: 30px;
right: 15px;
}
.shanchu{
width: 20px;
height: 20px;
position: absolute;
right: 20px;
}