请勿使用本文章及源码作为商业用途!

前言

当初做这个小程序是为了婚礼前的需要,结婚之后,希望这个小程序能够留存下来,特地花了一些空闲时间将小程序转化成为“相册类小程序”

体验码

准备工作

注意:使用mpvue前,首先你得先熟悉vue框架的基本使用

项目结构介绍

注意:接下来展示的代码,有几个对比,分别是本人优化前和优化后的代码对比,感兴趣的可以着重看一下优化后的成熟写法。

common目录: 放一些公共资源,如js,css,json

components目录:组件相关的.vue文件都放在这里

pages目录:所有页面都放在这个目录

utils目录:使用mpvue时自动生成,可忽略

app.json文件:

{

"pages": [

"pages/index/main",

"pages/photo/main",

"pages/map/main",

"pages/greet/main",

"pages/message/main"

],

"window": {

"backgroundTextStyle": "light",

"navigationBarBackgroundColor": "#fff",

"navigationBarTitleText": "WeChat",

"navigationBarTextStyle": "black"

},

"tabBar": {

"color": "#ccc",

"selectedColor": "#ff4c91",

"borderStyle": "white",

"backgroundColor": "#ffffff",

"list": [

{

"pagePath": "pages/index/main",

"iconPath": "static/images/1-1.png",

"selectedIconPath": "static/images/1-2.png",

"text": "邀请函"

},

{

"pagePath": "pages/photo/main",

"iconPath": "static/images/2-1.png",

"selectedIconPath": "static/images/2-2.png",

"text": "甜蜜相册"

},

{

"pagePath": "pages/map/main",

"iconPath": "static/images/3-1.png",

"selectedIconPath": "static/images/3-2.png",

"text": "酒店导航"

},

{

"pagePath": "pages/greet/main",

"iconPath": "static/images/4-1.png",

"selectedIconPath": "static/images/4-2.png",

"text": "好友祝福"

},

{

"pagePath": "pages/message/main",

"iconPath": "static/images/5-1.png",

"selectedIconPath": "static/images/5-2.png",

"text": "留言评论"

}

]

},

"requiredBackgroundModes": ["audio"]

}

App.vue文件 (本人主要是为了增加项目更新后的提醒),所以在这个文件加了些相关内容,内容如下:

export default {

onLaunch () {

// 检测小程序是否有新版本更新

if (wx.canIUse('getUpdateManager')) {

const updateManager = wx.getUpdateManager()

updateManager.onCheckForUpdate(function (res) {

// 请求完新版本信息的回调

if (res.hasUpdate) {

updateManager.onUpdateReady(function () {

wx.showModal({

title: '更新提示',

content: '新版本已经准备好,是否重启应用?',

success: function (res) {

if (res.confirm) {

// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启

updateManager.applyUpdate()

}

}

})

})

// 小程序有新版本,会主动触发下载操作(无需开发者触发)

wx.getUpdateManager().onUpdateFailed(function () {

// 当新版本下载失败,会进行回调

wx.showModal({

title: '提示',

content: '检查到有新版本,下载失败,请检查网络设置',

showCancel: false

})

})

}

})

} else { // 版本过低则无法使用该方法

wx.showModal({

title: '提示',

confirmColor: '#5BB53C',

content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'

})

}

}

}

page

height 100%

image

display block

main.js文件:

import Vue from 'vue'

import App from './App'

Vue.config.productionTip = false

App.mpType = 'app'

wx.cloud.init({

env: '云开发环境ID'

})

const app = new Vue(App)

app.$mount()

functions目录:主要放一些云函数,这里不清楚云函数的文章后面会提及

images目录:主要放一些静态资源图片

页面介绍

首页——邀请函

首页着重和大家讲解下背景音乐的实现方法

const audioCtx = wx.createInnerAudioContext()

接着,通过实例的相关方法来实现音乐的播放与暂停功能

具体代码如下:

import IndexSwiper from 'components/indexSwiper'

import tools from 'common/js/h_tools'

const audioCtx = wx.createInnerAudioContext()

export default {

name: 'Index',

components: {

IndexSwiper

},

data () {

return {

isPlay: true,

list: []

}

},

onShow () {

const that = this

that.isPlay = true

that.getMusicUrl()

},

methods: {

audioPlay () {

const that = this

if (that.isPlay) {

audioCtx.pause()

that.isPlay = false

tools.showToast('您已暂停音乐播放~')

} else {

audioCtx.play()

that.isPlay = true

tools.showToast('背景音乐已开启~')

}

},

getList () {

const that = this

const db = wx.cloud.database()

const banner = db.collection('banner')

banner.get().then(res => {

that.list = res.data[0].bannerList

})

},

getMusicUrl () {

const that = this

const db = wx.cloud.database()

const music = db.collection('music')

music.get().then(res => {

let musicUrl = res.data[0].musicUrl

audioCtx.src = musicUrl

audioCtx.loop = true

audioCtx.play()

that.getList()

})

}

},

onShareAppMessage: function (res) {

return {

path: '/pages/index/main'

}

}

}

以上代码中使用到了云开发相关功能,文章后面会介绍,请大家稍安勿躁

相册页——就一个轮播图,这里就不过多介绍

地图页——这里着重讲一下地图标签map

这里讲一下标记点markers:

data () {

return {

// qqSdk: '',

markers: [{

iconPath: '../../static/images/nav.png',

id: 0,

latitude: 30.08059,

longitude: 115.93027,

width: 50,

height: 50

}]

}

}

呼叫新郎

呼叫新娘

祝福页——也是云开发相关内容,后面会介绍

留言页——也是云开发相关内容,后面会介绍

云开发介绍

project.config.json文件:

"cloudfunctionRoot": "static/functions/"

进行云开发首先我们需要找到上面这个文件,在上面这个json文件中加上上面这行代码

cloudfunctionRoot 用于指定存放云函数的目录

app.json文件:

"window": {

"backgroundTextStyle": "light",

"navigationBarBackgroundColor": "#fff",

"navigationBarTitleText": "WeChat",

"navigationBarTextStyle": "black"

},

"cloud": true

增加字段 "cloud": true实现云开发能力的兼容性

开通云开发

在开发者工具工具栏左侧,点击 “云开发” 按钮即可开通云开发

云开发控制台

数据库

云开发提供了一个 JSON 数据库

存储

云开发提供了一块存储空间,提供了上传文件到云端、带权限管理的云端下载能力,开发者可以在小程序端和云函数端通过 API 使用云存储功能。

云函数

云函数是一段运行在云端的代码,无需管理服务器,在开发工具内编写、一键上传部署即可运行后端代码。

使用云开发

云能力初始化

在小程序端开始使用云能力前,需先调用 wx.cloud.init 方法完成云能力初始化

import Vue from 'vue'

import App from './App'

Vue.config.productionTip = false

App.mpType = 'app'

wx.cloud.init({

env: '云开发环境ID'

})

const app = new Vue(App)

app.$mount()

数据库的使用

在开始使用数据库 API 进行增删改查操作之前,需要先获取数据库的引用。以下调用获取默认环境的数据库的引用:

const db = wx.cloud.database()

要操作一个集合,需先获取它的引用:

const todos = db.collection('todos')

操作数据库的相关示例

例:首页获取背景音乐资源

getMusicUrl () {

const that = this

const db = wx.cloud.database()

const music = db.collection('music')

music.get().then(res => {

let musicUrl = res.data[0].musicUrl

audioCtx.src = musicUrl

audioCtx.loop = true

audioCtx.play()

that.getList()

})

}

例:首页获取轮播图数组

getList () {

const that = this

const db = wx.cloud.database()

const banner = db.collection('banner')

banner.get().then(res => {

that.list = res.data[0].bannerList

})

}

例:祝福页,用户送上祝福存储用户

import tools from 'common/js/h_tools'

export default {

name: 'Greet',

data () {

return {

userList: [],

openId: '',

userInfo: ''

}

},

onShow () {

const that = this

that.getUserList()

},

methods: {

scroll (e) {

console.log(e)

},

sendGreet (e) {

const that = this

if (e.target.errMsg === 'getUserInfo:ok') {

wx.getUserInfo({

success: function (res) {

that.userInfo = res.userInfo

that.getOpenId()

}

})

}

},

addUser () {

const that = this

const db = wx.cloud.database()

const user = db.collection('user')

user.add({

data: {

user: that.userInfo

}

}).then(res => {

that.getUserList()

})

},

getOpenId () {

const that = this

wx.cloud.callFunction({

name: 'user',

data: {}

}).then(res => {

that.openId = res.result.openid

that.getIsExist()

})

},

getIsExist () {

const that = this

const db = wx.cloud.database()

const user = db.collection('user')

user.where({

_openid: that.openId

}).get().then(res => {

if (res.data.length === 0) {

that.addUser()

} else {

tools.showToast('您已经送过祝福了~')

}

})

},

getUserList () {

const that = this

wx.cloud.callFunction({

name: 'userList',

data: {}

}).then(res => {

that.userList = res.result.data.reverse()

})

}

}

}

获取送祝福的好友列表

getUserList () {

const that = this

wx.cloud.callFunction({

name: 'userList',

data: {}

}).then(res => {

that.userList = res.result.data.reverse()

})

}

这里用到了云函数,之所以用云函数是因为小程序端在获取集合数据时服务器一次默认并且最多返回 20 条记录,云函数端这个数字则是 100。

云函数的使用方法

上面我们讲过在project.config.json文件中配置云函数存放位置

下面是云函数messageList的index.js文件:

不成熟写法:

const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()

const MAX_LIMIT = 100

exports.main = async (event, context) => {

// 先取出集合记录总数

const countResult = await db.collection('message').count()

const total = countResult.total

// 计算需分几次取

const batchTimes = Math.ceil(total / 100)

// 承载所有读操作的 promise 的数组

const tasks = []

for (let i = 0; i < batchTimes; i++) {

const promise = db.collection('message').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()

tasks.push(promise)

}

// 等待所有

return (await Promise.all(tasks)).reduce((acc, cur) => ({

data: acc.data.concat(cur.data),

errMsg: acc.errMsg

}))

}

成熟写法(分页查询):

const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()

exports.main = async (event, context) => {

const wxContext = cloud.getWXContext()

const dbName = 'message'

const filter = event.filter ? event.filter : null

const pageNum = event.pageNum ? event.pageNum : 1

const pageSize = event.pageSize ? event.pageSize : 10

const countResult = await db.collection(dbName).where(filter).count()

const total = countResult.total

const totalPage = Math.ceil(total / pageSize)

let hasMore

if (pageNum >= totalPage) {

hasMore = false

} else {

hasMore = true

}

return db.collection(dbName).orderBy('time', 'desc').where(filter).skip((pageNum - 1) * pageSize).limit(pageSize).get().then(res => {

res.hasMore = hasMore

res.total = total

res.openId = wxContext.OPENID

return res

})

}

使用云函数前,在开发者工具上,找到messageList目录,右键如图:

点击上传并部署:云端安装依赖(不上传node_modules)

得到如图的提示:

安装完点击完成就能使用当前云函数了,使用方法即:

getUserList () {

const that = this

wx.cloud.callFunction({

name: 'userList',

data: {}

}).then(res => {

that.userList = res.result.data.reverse()

})

}

数组之所以要倒序是因为希望新祝福的的用户在最前面显示

用户送上祝福的时候存储用户

这里我们用到了云函数获取用户信息,

当小程序端调用云函数时,云函数的传入参数中会被注入小程序端用户的 openid,开发者无需校验 openid 的正确性,因为微信已经完成了这部分鉴权,开发者可以直接使用该 openid

不成熟写法:

下面是云函数user的index.js文件:

// 云函数入口文件

const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数

exports.main = async (event, context) => {

const wxContext = cloud.getWXContext()

return {

event,

openid: wxContext.OPENID,

appid: wxContext.APPID,

unionid: wxContext.UNIONID

}

}

主要是为了获取当前操作用户的openid,获取当前用户的openid方法:

getOpenId () {

const that = this

wx.cloud.callFunction({

name: 'user',

data: {}

}).then(res => {

that.openId = res.result.openid

that.getIsExist()

})

}

接着判断当前用户是否已经存在于数据库中,即getIsExist()方法:

getIsExist () {

const that = this

const db = wx.cloud.database()

const user = db.collection('user')

user.where({

_openid: that.openId

}).get().then(res => {

if (res.data.length === 0) {

that.addUser()

} else {

tools.showToast('您已经送过祝福了~')

}

})

}

如果得到的数组长度为零则添加改用户到数据库,否则则提醒当前用户已经送过祝福

接下来介绍存储用户信息的方法,即addUser():

addUser () {

const that = this

const db = wx.cloud.database()

const user = db.collection('user')

user.add({

data: {

user: that.userInfo

}

}).then(res => {

that.getUserList()

})

}

存入到数据库的信息是这样的:

成熟写法(使用云函数一次搞定):

// 云函数入口文件

const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()

exports.main = async (event, context) => {

const wxContext = cloud.getWXContext()

const dbName = 'user'

const filter = {

_openid: wxContext.OPENID

}

const countResult = await db.collection(dbName).where(filter).count()

const total = countResult.total

if (total) {

return {

has: true

}

} else {

return db.collection(dbName).add({

data: {

user: event.user,

_openid: wxContext.OPENID,

time: db.serverDate()

}

})

}

}

toMessage (e) {

const that = this

if (e.target.errMsg === 'getUserInfo:ok') {

wx.getUserInfo({

success: function (res) {

that.userInfo = res.userInfo

wx.navigateTo({

url: `/pages/writeMessage/main?avatar=${that.userInfo.avatarUrl}&name=${that.userInfo.nickName}`

})

that.addUser(that.userInfo)

}

})

}

},

addUser (obj) {

wx.cloud.callFunction({

name: 'addUser',

data: {

user: obj

}

})

}

总结

大概的功能就是这么多,希望可以帮助到大家,觉得写得不错的记得给作者点个赞,你们的支持是我不断更新的最大动力!

源码地址

后续优化

留言审核

一些动画效果

分页处理

云函数优化

回到顶部

最后

一开始不清楚随笔和文章的区别,还是希望这篇随笔能展示在博客园首页(所以改成随笔),让更多的朋友看到

希望对那些有想法又不会后台开发的朋友一些启示作用,祝你早日做出只属于自己的小程序。

前端请柬框架_mpvue+小程序云开发,纯前端实现婚礼邀请函(相册小程序)相关推荐

  1. 小程序云开发从前端到后台实战记录,开发一个完整功能的小程序

    小程序云开发从前端到后台,开发一个完整功能的小程序 一.编程基本知识要求 有vue 或wpf经验者很好理解 有mongodb经验者很好理解云后台 二.理解微信体系 微信有三个平台 1.开放平台 2.公 ...

  2. 用小程序·云开发打造功能全面的博客小程序丨实战

    用小程序·云开发将博客小程序常用功能"一网打尽" 本文介绍mini博客小程序的详情页的功能按钮如何实现,具体包括评论.点赞.收藏和海报功能,这里记录下整个实现过程和实际编码中的一些 ...

  3. 从小程序到小程序云开发

    点击观看大咖分享 小程序云开发为微信小程序开发者提供云到端的一站式解决方案,帮助开发者统一构建和管理资源,能有效降低技术门槛.减少研发成本.提升开发效率,协助开发者快速搭建稳定高质量的微信小程序应用. ...

  4. ❤️微信小程序 云开发 教程合集(视频+图文)免费❤️

    一.视频版 微信小程序云开发视频教程上线啦 二.图文版 (1)预备知识 1. 怎么注册开通个人微信小程序 2. 微信小程序云开发教程-互联网软件的运作模式 3.微信小程序云开发教程-云开发对微信小程序 ...

  5. 腾讯云发布“小程序·云开发十大优秀实践”,猫眼、唯品会、香格里拉等入选

    10月19日,腾讯云联合微信小程序团队举办"小程序·云开发"技术峰会.会上,腾讯云首次对外公布了"小程序·云开发十大优秀实践", 包括白鹭引擎.千墨科技.腾讯新 ...

  6. 怎么实现微信小程序云开发拼团功能?

    [微信云开发教程]怎么实现微信小程序云开发拼团功能? 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 [微 ...

  7. 视频教程-5小时零基础入门小程序云开发-微信开发

    5小时零基础入门小程序云开发 码农一枚,非著名全栈开发人员.分享自己的一些经验,学习心得,希望后来人少走弯路,少填坑. 多年全栈开发经验,擅长小程序,java,安卓,web前端开发. 邱石 ¥29.0 ...

  8. 大小端交换的程序_数据库在小程序云开发中的应用

    " 高效率数据库为小程序·云开发赋能,共同为客户提供更多优质解决方案. " 目录 小程序·云开发介绍小程序·云开发的数据库服务解决方案及客户成功案例 小程序云开发介绍 " ...

  9. 微信小程序云开发-树洞小程序Treehole(介绍)

    记录一款基于云开发的微信小程序. 树洞,顾名思义是作为匿名吐槽的平台,主要功能可以参照微信朋友圈的形式,不过采取的是匿名的方式. 主要基于微信小程序云开发,前端个人主页页面的界面使用的是ColorUi ...

最新文章

  1. mybatis 报错最终解决 :argument type mismatch
  2. 机器人“快递小哥”上岗了!京东配送机器人编队长沙亮相
  3. C# 泛型编程学习总结
  4. python自定义函数实例计算1-n的偶偶数和_python用户输入一个整数N,计算并输出1到N相加的和,请问这个程序错在哪里了?...
  5. vue实现多行数据提交_2020年大厂面试指南 Vue篇
  6. linux挂载VMFS硬盘,ESX4.1挂载NFS共享存储(VMkernel)
  7. 设置和清除LD_LIBRARY_PATH
  8. obj[]与obj._Ruby中带有示例的Array.include?(obj)方法
  9. arm linux 进程页表,arm-linux内存页表创建
  10. 快递实名制正式实施 市民最担心个人信息泄露
  11. 机器学习三个部分:输入、算法、输出 资料收集
  12. Linux环境下安装MySQL 5.6.45
  13. mysql 银行卡卡号长度_卡bin查询sql分享
  14. 学子商城实训项目总结
  15. 纤亿通教你如何选择合适的 Cat6 网线
  16. Flash游戏开发技术分析(下)
  17. 【学习笔记】统计学入门(5/7)——二项分布
  18. svn里 is already locked
  19. 实验报告 三密码破解技术
  20. 微信小程序:音乐项目

热门文章

  1. MATLAB水母搜索算法求解超市物流配送选址问题代码实例
  2. 高级软件工程师证书有用吗_bim工程师证书有用吗?bim好在哪里?
  3. python实现删除空行
  4. 后期强美颜大师完整破解版
  5. 玩转华为ENSP模拟器系列 | 配置OSPF的NSSA区域
  6. 二维码读取之ecc200_contrast_tolerance.hdev
  7. 【23考研】计算机择校信息库-辽宁高校计算机相关专业22专业目录分类汇总(按专业课分类汇总)
  8. 安装记录:安装3dmax2015时出现的问题以及解决方法
  9. Kubernetes(K8S)一
  10. 秒表计时器开始计时代码_如何在Windows 10中设置计时器,警报和秒表