最近折腾了一下微信小程序,发现小程序开发起来有它自己独特的魅力。很多常用的功能,如加载提示(wx.showLoading),都可以用一句代码实现。配合上云开发,整个开发速度得到了有力的提升。本文章,介绍如何开发一个相册小程序。
以下是小程序的GitHub链接:
先看一下效果图:
-
上传照片效果展示,最多支持9张照片上传。
-
查看我的照片墙效果展示,下拉刷新重载,上拉分页加载,一次5条:
-
删除某张照片,无需重新加载照片列表,即删除一张,再加载一张新的(如果有的话)。
- 最下面的标签实现:
"tabBar": {
"list": [
{
"pagePath": "pages/index/index",
"text": "上传照片",
"iconPath": "assets/pics1.png",
"selectedIconPath": "assets/pics2.png"
},
{
"pagePath": "pages/album/album",
"text": "我的照片墙",
"iconPath": "assets/self1.png",
"selectedIconPath": "assets/self2.png"
}
],
"selectedColor": "#3399FF"
}
- app.js,初始化云,并获取用户基本信息与openId(为了之后标识是该用户的照片)。
//app.js
App({
onLaunch: function () {
//初始化云
wx.cloud.init({
env: "***",
traceUser: true
});
//获取openId
this.getOpenId();
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: function (res) {
}
});
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
getOpenId() {
// 获取用户openid
let that = this;
wx.cloud.callFunction({
name: 'getOpenId',
complete: res => {
console.