微信小程序API学习:
API:
- 定时器setInterval
- 消息提示框wx.showtoaste
- 模拟对话框wx.showModal
- 操作菜单wx.showActionSheet
- 获取系统信息wx.getSystemInfoSync
- 获取网络类型wx.getNetworkType
- 获取电量信息 wx.getBatteryInfo
功能:
- 倒计时
- 获取设备电量、型号、网络
- 小程序更新
index.wxml
<button size="mini" class="btn"
bindtap='sendCode' disabled="{{disabled}}">{{getCode}}</button>
<button size="mini" class="btn"
bindtap='show'>操作菜单</button>
<button size="mini" class="btn"
bindtap='upda'>更新</button>
index.wxss
button {
color: #ff6f10;
border: #ff6f10 1px solid;
margin-left: 5%;
border-radius: 5%;
margin-top: 10%;
}
index.js
Page({
data: {
// color: "#ff6f10",
//按钮是否可以点击
disabled: false,
getCode: "开始倒计时",
},
//倒计时
sendCode: function(e) {
var that = this;
var times = 60
var i = setInterval(function() {
if(times == 60){
wx.showToast({
title: '倒计时开始',
icon:'none',
// duration:'1000',
})
// wx.hideToast()
}
times--
if (times <= 0) {
that.setData({
// color: "#ff6f10",
disabled: false,
getCode: "开始倒计时",
})
clearInterval(i)
wx.showToast({
title: '倒计时完成',
// duration:'1000',
})
} else {
that.setData({
getCode: "倒计时" + times + "s",
// color: "#999",
disabled: true
})
}
}, 1000)
},
//操作菜单
show(){
wx.showActionSheet({
itemList:['获取手机型号','获取网络状态','获取电量信息'],
success(res){
// console.log(res.tapIndex);
switch (res.tapIndex) {
//获取手机型号
case 0:
const a = wx.getSystemInfoSync()
// console.log(a);
wx.showToast({
title: '手机型号:'+a.model,
icon:'none',
// duration:'1000',
})
break;
//获取网络状态
case 1:
wx.getNetworkType({
success:function(b){
wx.showToast({
title: '当前网络类型:'+b.networkType,
icon:'none',
})
// console.log(b.networkType)
}
})
break;
case 2:
wx.getBatteryInfo({
success:function(c){
// console.log(c.level),
// console.log(c.isCharging);
if(c.isCharging){
wx.showToast({
title: '正在充电'+c.level+'%',
icon:'none'
})
}else{
wx.showToast({
title: c.level+'%',
icon:'none'
})
}
}
})
break;
default:
break;
}
}
})
},
//更新
upda(){
const update= wx.getUpdateManager();
update.onCheckForUpdate(function(res){
console.log(res.hasUpdate);
if(res.hasUpdate){
wx.showModal({
title:'更新提示',
content:'新版本已经准备完成,是否重启更新',
success(s){
if(s.confirm){
update.applyUpdate();
}
}
})
}
})
}
})