開(kāi)發(fā)環(huán)境搭建
使用自己的AppID新建小程序項(xiàng)目,后端服務(wù)選擇小程序·云開(kāi)發(fā),點(diǎn)擊新建,完成項(xiàng)目新建。
新建成功后跳轉(zhuǎn)到開(kāi)發(fā)者工具界面
新建后,微信端為我們提供了一個(gè)參考的模板程序,這里我們自己來(lái)創(chuàng)建各個(gè)所需的文件與代碼,所以刪除所有不需要的文件,刪除cloudfunctions、miniprogram/images、miniprogram/pages文件下所有文件,同時(shí)也刪除style文件和刪除app.json中原始的頁(yè)面配置。
此時(shí)編譯下方控制臺(tái)會(huì)報(bào)“VM8100:5 appJSON["pages"] 需至少存在一項(xiàng)”錯(cuò)誤,因?yàn)閍pp.json中未配置任何頁(yè)面路徑,下面我們來(lái)對(duì)app.json進(jìn)行配置。
{ "cloud": true, "pages": [ "pages/index/index", "pages/detonation/detonation", "pages/user/user" ],
“cloud”: true表示讓云能力可以在所有基礎(chǔ)庫(kù)中使用,在頁(yè)面路徑列表pages下加入三個(gè)Tab頁(yè)面路徑,在window中設(shè)置全局的默認(rèn)窗口樣式,通過(guò)tabBar設(shè)置底部tab欄的樣式,配置完成后點(diǎn)擊編譯,開(kāi)發(fā)工具會(huì)自動(dòng)生成三個(gè)頁(yè)面的文件夾以及相關(guān)文件。
"window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#FF3333", "navigationBarTitleText": "我要點(diǎn)爆", "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": "點(diǎn)爆", "iconPath": "/images/bao2.png", "selectedIconPath": "/images/bao1.png" }, { "pagePath": "pages/user/user", "text": "我的", "iconPath": "/images/wo1.png", "selectedIconPath": "/images/wo.png" } ] }, "sitemapLocation": "sitemap.json" }
配置成功后頁(yè)面結(jié)構(gòu)與效果
創(chuàng)建數(shù)據(jù)庫(kù)環(huán)境
設(shè)置環(huán)境名稱,環(huán)境名稱可以根據(jù)自己需求設(shè)置,這里設(shè)置與項(xiàng)目名相同dbx,下方的環(huán)境ID會(huì)自動(dòng)生成,無(wú)需修改,點(diǎn)擊確定完成創(chuàng)建。
創(chuàng)建成功后跳轉(zhuǎn)云開(kāi)發(fā)控制臺(tái)頁(yè)面
配置app.js文件,在調(diào)用云開(kāi)發(fā)各 API 前,需先調(diào)用初始化方法 init 一次(全局只需一次),在wx.cloud.init中設(shè)置程序所讀環(huán)境的數(shù)據(jù)庫(kù)位置,剛才創(chuàng)建的數(shù)據(jù)庫(kù)環(huán)境的ID
實(shí)現(xiàn)我的頁(yè)面布局制作與用戶授權(quán)登錄功能
首先對(duì)頁(yè)面進(jìn)行布局,頭部使用一個(gè)button按鈕來(lái)進(jìn)行授權(quán)登錄獲取用戶信息的操作,設(shè)置button的open-type為getUserInfo,使得按鈕可以從bindgetuserinfo回調(diào)中獲取到用戶信息,設(shè)置回調(diào)方法為getUserInfoHandler。為了讓用戶授權(quán)后實(shí)時(shí)更新用戶頭像與用戶名,這里使用數(shù)據(jù)綁定與判斷的方法。
<!-- pages/user/user.wxml --> <view class="user_header"> <view class="header_box"> <image src="{{userTx || defaultUrl}}"></image> <button class="{{username == '點(diǎn)擊登錄' ? '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="https://www.gxlcms.com/images/jilu.png"></image> <text>點(diǎn)爆記錄</text> </view> <view class="box_item"> <image src="https://www.gxlcms.com/images/zhudian.png"></image> <text>最近助點(diǎn)</text> </view> </view> <view class="main_box"> <view class="box_item"> <image src="https://www.gxlcms.com/images/fengcun.png"></image> <text>我的封存</text> </view> <view class="box_item"> <image src="https://www.gxlcms.com/images/usercang.png"></image> <text>我的收藏</text> </view> </view> </view>
頁(yè)面布局完成后進(jìn)行user.js的編寫,data中設(shè)置頁(yè)面初始數(shù)據(jù),username用于控制授權(quán)按鈕用戶名變換,defaultUrl設(shè)置默認(rèn)頭像,userTx記錄用戶頭像,userInfo記錄用戶授權(quán)后所獲取的信息,gender用與用戶性別判斷,province用于記錄地區(qū)信息。
// pages/user/user.js Page({ data: { username: '點(diǎn)擊登錄', defaultUrl: '/images/yuyin5.png', userTx: '', userInfo: {}, gender: 1, province: '', },
在onLoad中對(duì)頁(yè)面進(jìn)行初始化設(shè)置和用戶是否登錄的初始化設(shè)置,在用戶授權(quán)登錄后直接使用本地的用戶信息,如果本地信息不存在則通過(guò)wx.getSetting獲取用戶設(shè)置,看用戶是否授權(quán)過(guò),如果授權(quán)過(guò),則wx.getUserInfo直接獲取用戶信息。
onLoad: function () { wx.setNavigationBarTitle({ title: '我的' }) //當(dāng)重新加載這個(gè)頁(yè)面時(shí),查看是否有已經(jīng)登錄的信息 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)常用的用戶信息到本地和完成用戶信息數(shù)據(jù)庫(kù)注冊(cè),**button組件中bindgetuserinfo方法回調(diào)的detail數(shù)據(jù)與wx.getUserInfo返回的一致**,通過(guò)detail將所需的用戶信息提取出來(lái),將性別gender替換為‘男'和‘女',將頭像、用戶名、性別、地區(qū)保存在本地。然后使用云數(shù)據(jù)庫(kù)API進(jìn)行數(shù)據(jù)庫(kù)操作。
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) //獲取數(shù)據(jù)庫(kù)引用 const db = wx.cloud.database() const _ = db.command //查看是否已有登錄,無(wú),則獲取id var userId = wx.getStorageSync('userId') if (!userId) { userId = this.getUserId() } //查找數(shù)據(jù)庫(kù) db.collection('users').where({ _openid: d.openid }).get({ success(res) { // res.data 是包含以上定義的記錄的數(shù)組 //如果查詢到數(shù)據(jù),將數(shù)據(jù)記錄,否則去數(shù)據(jù)庫(kù)注冊(cè) if (res.data && res.data.length > 0) { wx.setStorageSync('openId', res.data[0]._openid) } else { //定時(shí)器 setTimeout(() => { //寫入數(shù)據(jù)庫(kù) 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設(shè)置失敗') } }) }, fail: function (e) { console.log('用戶id新增失敗') } }) }, 100) } }, fail: err => { } }) }, getUserId: function () { //生產(chǎn)唯一id,采用一個(gè)字母或數(shù)字+1970年到現(xiàn)在的毫秒數(shù)+10w的一個(gè)隨機(jī)數(shù)組成 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; }, })
在云開(kāi)發(fā)控制臺(tái)中創(chuàng)建數(shù)據(jù)庫(kù)集合,我們新建一個(gè)users集合,我們只需新建集合,通過(guò)js中使用云開(kāi)發(fā)API可自動(dòng)創(chuàng)建集合中的屬性和數(shù)據(jù)。
該users集合為用戶信息表,記錄用戶信息,表users的結(jié)構(gòu)如下:
集合創(chuàng)建成功后,點(diǎn)擊將出現(xiàn)進(jìn)行編譯,此時(shí)頁(yè)面效果如下:
我們點(diǎn)擊“點(diǎn)擊登錄”按鈕,然后對(duì)程序進(jìn)行授權(quán),授權(quán)后可以看到我們的頭像和用戶名都顯示出來(lái)了,同時(shí),打開(kāi)云開(kāi)發(fā)控制臺(tái),查看users集合,可以看到我們信息已經(jīng)成功保存在了集合中。
至此,我們就完成了
1、云端控制臺(tái)數(shù)據(jù)庫(kù)的創(chuàng)建
2、我的頁(yè)面的樣式制作
3、用戶授權(quán)登錄功能制作
4、云數(shù)據(jù)庫(kù)的用戶數(shù)據(jù)存儲(chǔ)的實(shí)現(xiàn)
項(xiàng)目源碼:https://github.com/xiedong2016/dbx
總結(jié)
以上所述是小編給大家介紹的我要點(diǎn)爆”微信小程序云開(kāi)發(fā)之項(xiàng)目建立與我的頁(yè)面功能實(shí)現(xiàn),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com