微信小程序开发文档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 } })