1. 程式人生 > >mpvue+小程式雲開發,純前端實現婚禮邀請函

mpvue+小程式雲開發,純前端實現婚禮邀請函

目錄

前言

準備工作

專案結構介紹

頁面介紹

雲開發介紹

總結

前言

感謝OnceLove提供的思路,藉助他的小程式的介面UI風格,自己重新用mpvue實現了屬於自己的婚禮邀請函,前前後後花了3天時間。在這之前本人是沒想過要自己實現這樣一個專案,原因是後臺那塊是個麻煩事,所以當媳婦讓我自己實現這個邀請函的時候,起初我是拒絕的。後面由於快要過年了,公司專案沒有重大更新,趁著這段空閒時間,自己研究了下小程式自帶的雲開發,無需後臺支援,前後端都可以自己來實現,下面我將一一介紹本專案的實現過程!

本小程式為婚禮正式使用的小程式,婚期將至,感興趣的可以掃碼體驗本專案,沾沾喜氣,歡迎大家體驗,有什麼問題可以在本文給我留言

準備工作

  1. mpvue框架 mpvue官方文件
  2. 小程式·雲開發 小程式·雲開發文件

注意:使用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檔案:本人主要是為了增加專案更新後的提醒,所以在這個檔案加了些相關內容,內容如下,

<script>
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: '當前微信版本過低,無法使用該功能,請升級到最新微信版本後重試。'
      })
    }
  }
}
</script>

<style lang="stylus">
page
    height 100%
image
    display block
</style>
複製程式碼

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()

首先,wx.createInnerAudioContext 介面獲取例項

接著,通過例項的相關方法來實現音樂的播放與暫停功能

關於小程式音訊相關文件

具體程式碼如下:

<script>
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'
    }
  }
}
</script>複製程式碼

以上程式碼中使用到了雲開發相關功能,文章後面會介紹,請大家稍安勿躁

  • 相簿頁——就一個輪播圖,這裡就不過多介紹
  • 地圖頁——這裡著重講一下地圖標籤map

map標籤 關於map元件的使用文件

這裡講一下標記點markers

data () {
    return {
      // qqSdk: '',
      markers: [{
        iconPath: '../../static/images/nav.png',
        id: 0,
        latitude: 30.08059,
        longitude: 115.93027,
        width: 50,
        height: 50
      }]
    }
  }複製程式碼
<template>
    <div class="map">
        <image mode="aspectFit" class="head-img" src="../../static/images/t1.png"/>
        <map class="content" id="map" longitude="115.93027" latitude="30.08059" :markers="markers" scale="18" @tap="toNav">
        </map>
        <div class="call">
            <div class="left" @tap="linkHe">
                <image src="../../static/images/he.png"/>
                <span>呼叫新郎</span>
            </div>
            <div class="right" @tap="linkShe">
                <image src="../../static/images/she.png"/>
                <span>呼叫新娘</span>
            </div>
        </div>
        <image class="footer" src="../../static/images/grren-flower-line.png"/>
    </div>
</template>複製程式碼
  • 祝福頁——也是雲開發相關內容,後面會介紹
  • 留言頁——也是雲開發相關內容,後面會介紹

雲開發介紹

小程式雲開發文件

  • 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 使用雲端儲存功能。

  • 雲函式

雲函式是一段執行在雲端的程式碼,無需管理伺服器,在開發工具內編寫、一鍵上傳部署即可執行後端程式碼。

下面開始講解使用雲開發的過程:

  1. 雲能力初始化,在小程式端開始使用雲能力前,需先呼叫 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()
複製程式碼

2. 資料庫的使用:

在開始使用資料庫 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
      })
}複製程式碼

例:祝福頁,使用者送上祝福儲存使用者

<script>
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()
      })
    }
  }
}
</script>複製程式碼
  • 進入祝福頁,首先我們需要獲取送祝福的好友列表
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
  }))
}
複製程式碼

使用雲函式前,在開發者工具上,找到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()
      })
}複製程式碼

存入到資料庫的資訊是這樣的:

總結

大概的功能就是這麼多,希望可以幫助到大家,覺得寫得不錯的記得給作者點個贊,你們的支援是我不斷更新的最大動力!