在這里,我主要整理了一些小程序開發過程中常用的功能點,主題和tabBar的設置、授權檢測、模板消息推送等。
1、主題和tabBar的設置,這倆會經常配置在app.json里面。
-
"window":{
-
"navigationBarBackgroundColor": "#1AAD19",//導航條背景色
-
"navigationBarTitleText": "某某某",
-
"navigationBarTextStyle":"white"//導航條文字以及圖標顏色
-
},
-
"tabBar": { //list選項最少2個,最多5個
-
"color": "#515151",
-
"selectedColor": "#ff5777",
-
"borderStyle": "black",
-
"backgroundColor": "#fff",
-
"list": [
-
{
-
"text": "Tab1",
-
"pagePath": "pages/index/index",
-
"iconPath": "pages/image/tidan_unfocus.png",
-
"selectedIconPath": "pages/image/tidan_focus.png"
-
},
-
{
-
"text": "Tab2",
-
"pagePath": "pages/activity/activity",
-
"iconPath": "pages/image/activity_unfocus.png",
-
"selectedIconPath": "pages/image/activity_focus.png"
-
},
-
{
-
"text": "Tab3",
-
"pagePath": "pages/my/my",
-
"iconPath": "pages/image/my_unfocus.png",
-
"selectedIconPath": "pages/image/my_focus.png"
-
}
-
]
-
}
2、是否已經授權的檢測
-
/**
-
* 判斷是否已經授權
-
*/
-
judgeAuthorize: function() {
-
wx.getSetting({
-
success: function(res) {
-
if (!res.authSetting['scope.userInfo']) { //未授權
-
//進行彈窗提示去授權或者設置一個授權頁面覆蓋
-
}
-
}
-
})
-
},
需要注意的一點是:隨著版本的升級,可能以后wx.getUserInfo都就無法獲取用戶信息了,雖然在正式版中現在還好用。官方提倡用button組件去實現彈窗授權。說實話,我真心受不了這種實現方式。按照文檔設置一下:
-
<button class='text btn' open-type="getUserInfo" bindgetuserinfo='getInfo'>登錄授權</button>
這樣就監聽getInfo這個回調方法就行了!
-
/**
-
* 點擊獲取用戶授權
-
*/
-
getInfo: function(e) {
-
if (e.detail.userInfo) { //同意授權
-
app.nickName = e.detail.userInfo.nickName;
-
app.avatarUrl = e.detail.userInfo.avatarUrl;
-
self.getOpenId();
-
} else { //拒絕授權
-
wx.showModal({
-
title: '提示',
-
content: '未授權,無法正常使用小程序功能,請重新點擊授權',
-
showCancel: false
-
})
-
}
-
},
后面流程就不貼代碼了,就是用wx.login去獲取code,再用code去獲取openid,最后再把openid、昵稱、頭像一塊提交到后臺接口就ok了!
3、模板消息的推送
表單提交,指定一些屬性即可。如下
-
<form report-submit="true" bindsubmit="commitInfo">
-
<button formType="submit">提交</button>
-
</form>
-
-
-
-
commitInfo:function(e){
-
console.log(e.detail.formId);
-
}
后臺要實現模板推送,需要前臺提供一個formId,需要注意兩點:1、這個formId只能使用一次,用完即失效。2、模板消息只能推送給當前提交這個formId的微信賬號,說白了,就是只能推送給自己,不能推送給別人。
以上就是一些簡單的微信小程序開發知識總結,希望對大家有一定幫助。