“我要點爆”微信小程序云開發(fā)之項目建立與我的頁面功能實現
開發(fā)環(huán)境搭建
使用自己的AppID新建小程序項目,后端服務選擇小程序·云開發(fā)假抄,點擊新建怎栽,完成項目新建。
新建成功后跳轉到開發(fā)者工具界面
新建后宿饱,微信端為我們提供了一個參考的模板程序熏瞄,這里我們自己來創(chuàng)建各個所需的文件與代碼,所以刪除所有不需要的文件谬以,刪除cloudfunctions强饮、miniprogram/images、miniprogram/pages文件下所有文件为黎,同時也刪除style文件和刪除app.json中原始的頁面配置胡陪。
此時編譯下方控制臺會報“VM8100:5 appJSON["pages"] 需至少存在一項”錯誤,因為app.json中未配置任何頁面路徑碍舍,下面我們來對app.json進行配置。
{
"cloud": true,
"pages": [
"pages/index/index",
"pages/detonation/detonation",
"pages/user/user"
],
“cloud”: true表示讓云能力可以在所有基礎庫中使用邑雅,在頁面路徑列表pages下加入三個Tab頁面路徑片橡,在window中設置全局的默認窗口樣式,通過tabBar設置底部tab欄的樣式淮野,配置完成后點擊編譯捧书,開發(fā)工具會自動生成三個頁面的文件夾以及相關文件吹泡。
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#FF3333",
"navigationBarTitleText": "我要點爆",
"navigationBarTextStyle": "white",
"backgroundColor": "#FF3333"
},
"tabBar": {
"backgroundColor": "#F2F2F2",
"color": "#6B6B6B",
"selectedColor": "#FF0000",
"list": [
{
"pagePath": "pages/index/index",
"text": "世界",
"iconPath": "/images/shi.png",
"selectedIconPath": "/images/shi1.png"
},
{
"pagePath": "pages/detonation/detonation",
"text": "點爆",
"iconPath": "/images/bao2.png",
"selectedIconPath": "/images/bao1.png"
},
{
"pagePath": "pages/user/user",
"text": "我的",
"iconPath": "/images/wo1.png",
"selectedIconPath": "/images/wo.png"
}
]
},
"sitemapLocation": "sitemap.json"
}
配置成功后頁面結構與效果
創(chuàng)建數據庫環(huán)境
設置環(huán)境名稱,環(huán)境名稱可以根據自己需求設置经瓷,這里設置與項目名相同dbx爆哑,下方的環(huán)境ID會自動生成,無需修改舆吮,點擊確定完成創(chuàng)建揭朝。
創(chuàng)建成功后跳轉云開發(fā)控制臺頁面
配置app.js文件,在調用云開發(fā)各 API 前色冀,需先調用初始化方法 init 一次(全局只需一次)潭袱,在wx.cloud.init中設置程序所讀環(huán)境的數據庫位置,剛才創(chuàng)建的數據庫環(huán)境的ID
實現我的頁面布局制作與用戶授權登錄功能
首先對頁面進行布局锋恬,頭部使用一個button按鈕來進行授權登錄獲取用戶信息的操作屯换,設置button的open-type為getUserInfo,使得按鈕可以從bindgetuserinfo回調中獲取到用戶信息,設置回調方法為getUserInfoHandler与学。為了讓用戶授權后實時更新用戶頭像與用戶名彤悔,這里使用數據綁定與判斷的方法。
<!-- pages/user/user.wxml -->
<view class="user_header">
<view class="header_box">
<image src="{{userTx || defaultUrl}}"></image>
<button class="{{username == '點擊登錄' ? 'usernameDe' : 'username'}}" open-type="getUserInfo" bindgetuserinfo="getUserInfoHandler">{{username}}</button>
<view class="qiandao">
<text>糖果</text>
</view>
</view>
</view>
<view class="user_main">
<view class="main_box">
<view class="box_item">
<image src="/images/jilu.png"></image>
<text>點爆記錄</text>
</view>
<view class="box_item">
<image src="/images/zhudian.png"></image>
<text>最近助點</text>
</view>
</view>
<view class="main_box">
<view class="box_item">
<image src="/images/fengcun.png"></image>
<text>我的封存</text>
</view>
<view class="box_item">
<image src="/images/usercang.png"></image>
<text>我的收藏</text>
</view>
</view>
</view>
頁面布局完成后進行user.js的編寫索守,data中設置頁面初始數據,username用于控制授權按鈕用戶名變換晕窑,defaultUrl設置默認頭像,userTx記錄用戶頭像蕾盯,userInfo記錄用戶授權后所獲取的信息幕屹,gender用與用戶性別判斷,province用于記錄地區(qū)信息级遭。
// pages/user/user.js
Page({
data: {
username: '點擊登錄',
defaultUrl: '/images/yuyin5.png',
userTx: '',
userInfo: {},
gender: 1,
province: '',
},
在onLoad中對頁面進行初始化設置和用戶是否登錄的初始化設置望拖,在用戶授權登錄后直接使用本地的用戶信息,如果本地信息不存在則通過wx.getSetting獲取用戶設置挫鸽,看用戶是否授權過说敏,如果授權過,則wx.getUserInfo直接獲取用戶信息丢郊。
onLoad: function () {
wx.setNavigationBarTitle({
title: '我的'
})
//當重新加載這個頁面時盔沫,查看是否有已經登錄的信息
let username = wx.getStorageSync('username'),
avater = wx.getStorageSync('avatar');
if (username) {
this.setData({
username: username,
userTx: avater
})
}
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: res => {
this.setData({
userTx: res.userInfo.avatarUrl,
userInfo: res.userInfo
})
}
})
}
}
})
},
getUserInfoHandler方法保存系統(tǒng)常用的用戶信息到本地和完成用戶信息數據庫注冊,button組件中bindgetuserinfo方法回調的detail數據與wx.getUserInfo返回的一致枫匾,通過detail將所需的用戶信息提取出來架诞,將性別gender替換為‘男’和‘女’,將頭像干茉、用戶名谴忧、性別、地區(qū)保存在本地。然后使用云數據庫API進行數據庫操作沾谓。
getUserInfoHandler: function (e) {
let d = e.detail.userInfo
var gen = d.gender == 1 ? '男' : '女'
this.setData({
userTx: d.avatarUrl,
username: d.nickName
})
wx.setStorageSync('avater', d.avatarUrl)
wx.setStorageSync('username', d.nickName)
wx.setStorageSync('gender', gen)
wx.setStorageSync('province', d.province)
//獲取數據庫引用
const db = wx.cloud.database()
const _ = db.command
//查看是否已有登錄委造,無,則獲取id
var userId = wx.getStorageSync('userId')
if (!userId) {
userId = this.getUserId()
}
//查找數據庫
db.collection('users').where({
_openid: d.openid
}).get({
success(res) {
// res.data 是包含以上定義的記錄的數組
//如果查詢到數據,將數據記錄均驶,否則去數據庫注冊
if (res.data && res.data.length > 0) {
wx.setStorageSync('openId', res.data[0]._openid)
} else {
//定時器
setTimeout(() => {
//寫入數據庫
db.collection('users').add({
data: {
userId: userId,
userSweet: 10,
voice: 0,
baovoice: 0,
iv: d.iv
},
success: function () {
console.log('用戶id新增成功')
db.collection('users').where({
userId: userId
}).get({
success: res => {
wx.setStorageSync('openId', res.data[0]._openid)
},
fail: err => {
console.log('用戶_openId設置失敗')
}
})
},
fail: function (e) {
console.log('用戶id新增失敗')
}
})
}, 100)
}
},
fail: err => {
}
})
},
getUserId: function () {
//生產唯一id昏兆,采用一個字母或數字+1970年到現在的毫秒數+10w的一個隨機數組成
var w = "abcdefghijklmnopqrstuvwxyz0123456789",
firstW = w[parseInt(Math.random() * (w.length))];
var userId = firstW + (Date.now()) + (Math.random() * 100000).toFixed(0)
wx.setStorageSync('userId', userId)
return userId;
},
})
在云開發(fā)控制臺中創(chuàng)建數據庫集合,我們新建一個users集合妇穴,我們只需新建集合爬虱,通過js中使用云開發(fā)API可自動創(chuàng)建集合中的屬性和數據。
該users集合為用戶信息表伟骨,記錄用戶信息饮潦,表users的結構如下:
字段名 | 數據類型 | 主鍵 | 非空 | 描述 |
---|---|---|---|---|
_id | String | 是 | 是 | ID |
_openid | String | 是 | 用戶唯一標識 | |
baoVoice | number | 爆炸之音數量 | ||
userId | String | 用戶注冊ID | ||
userSweet | number | 擁有糖果數量 | ||
voice | number | 點爆語音數量 |
集合創(chuàng)建成功后,點擊將出現進行編譯携狭,此時頁面效果如下:
我們點擊“點擊登錄”按鈕继蜡,然后對程序進行授權,授權后可以看到我們的頭像和用戶名都顯示出來了逛腿,同時稀并,打開云開發(fā)控制臺,查看users集合单默,可以看到我們信息已經成功保存在了集合中碘举。
至此,我們就完成了
1搁廓、云端控制臺數據庫的創(chuàng)建
2引颈、我的頁面的樣式制作
3、用戶授權登錄功能制作
4境蜕、云數據庫的用戶數據存儲的實現