微信小程序開發文檔php
https://mp.weixin.qq.com/debug/wxadoc/dev/?t=20161107git
項目效果圖:github
{ "pages": [ "pages/index/index", "pages/logs/index" ], "window": { "navigationBarTitleText": "Demo" }, "tabBar": { "list": [{ "pagePath": "pages/index/index", "text": "首頁" }, { "pagePath": "pages/logs/logs", "text": "日誌" }] }, "networkTimeout": { "request": 10000, "downloadFile": 10000 }, "debug": true }
App({ onLaunch: function() { // Do something initial when launch. }, onShow: function() { // Do something when show. }, onHide: function() { // Do something when hide. }, globalData: 'I am global data' })
經過全局的getApp()函數,獲取小程序實例json
// other.js var appInstance = getApp() console.log(appInstance.globalData) // I am global data
注意:小程序
App() 必須在 app.js 中註冊,且不能註冊多個。微信小程序
不要在定義於 App() 內的函數中調用 getApp() ,使用 this 就能夠拿到 app 實例。緩存
不要在 onLaunch 的時候調用 getCurrentPage(),此時 page 尚未生成。服務器
經過 getApp() 獲取實例以後,不要私自調用生命週期函數。微信
//index.js Page({ data: { text: "This is page data." }, onLoad: function(options) { // Do some initialize when page load. }, onReady: function() { // Do something when page ready. }, onShow: function() { // Do something when page show. }, onHide: function() { // Do something when page hide. }, onUnload: function() { // Do something when page close. }, onPullDownRefresh: function() { // Do something when pull down. }, onReachBottom: function() { // Do something when page reach bottom. }, // Event handler. viewTap: function() { this.setData({ text: 'Set some data for updating view.' }) }, customData: { hi: 'MINA' } })
wx.request({ url: 'test.php', //僅爲示例,並不是真實的接口地址 method:"GET", data: { x: '' , y: '' }, header: { 'content-type': 'application/json' }, success: function(res) { console.log(res.data) } })
經過key的形式添加緩存setStorage (異步接口)網絡
wx.setStorage({ key:"key" data:"value" })
經過key的形式獲取緩存getStorage (異步接口)
wx.getStorage({ key: 'key', success: function(res) { console.log(res.data) } })
從本地緩存中異步移除指定 key
wx.removeStorage({ key: 'key', success: function(res) { console.log(res.data) } })
清理本地數據緩存
wx.clearStorage()
wx.showToast({ title: '加載中', icon: 'loading', duration: 10000 }) setTimeout(function(){ wx.hideToast() },2000)
wx.setNavigationBarTitle({ title: '當前頁面' })
保留當前頁面,跳轉到應用內的某個頁面
wx.navigateTo({ url: 'test?id=1' })
關閉當前頁面,跳轉到應用內的某個頁面
wx.redirectTo({ url: 'test?id=1' })
wx.getUserInfo({ success: function(res) { var userInfo = res.userInfo var nickName = userInfo.nickName var avatarUrl = userInfo.avatarUrl var gender = userInfo.gender //性別 0:未知、1:男、2:女 var province = userInfo.province var city = userInfo.city var country = userInfo.country } })
獲取網絡類型
wx.getNetworkType({ success: function(res) { var networkType = res.networkType // 返回網絡類型2g,3g,4g,wifi } })
獲取系統信息(異步接口)
wx.getSystemInfo({ success: function(res) { console.log(res.model) console.log(res.pixelRatio) console.log(res.windowWidth) console.log(res.windowHeight) console.log(res.language) console.log(res.version) } })
撥打電話
wx.makePhoneCall({ phoneNumber: '1340000' //僅爲示例,並不是真實的電話號碼 })
wx.getLocation({ type: 'wgs84', success: function(res) { var latitude = res.latitude var longitude = res.longitude var speed = res.speed var accuracy = res.accuracy } })