6.4 本地数据保存及缓存API
微信小程序提供了丰富的API用于本地数据保存和缓存。以下是微信小程序中常用的本地数据保存和缓存的API:
本地数据保存
-
wx.setStorageSync(key, data): 同步地将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。
-
wx.getStorageSync(key): 同步获取本地缓存中指定 key 的内容。
-
wx.removeStorageSync(key): 同步移除本地缓存中指定的 key。
-
wx.clearStorageSync(): 同步清理本地缓存中的所有数据。
缓存API(主要用于网络请求的缓存)
-
wx.request(OBJECT): 发起网络请求。可以设置
cache-control
来控制缓存行为,例如使用wx.request({ cacheControl: 'no-cache' })
来避免使用缓存。 -
wx.offlineManager: 提供了与离线管理相关的API,可以监听网络状态变化、设置网络超时时间等。
6.4.1 保存数据
1. wx.setStrong(Object)
示例代码:
wx.setStorage({
key:'name',
data:'sdy',
success:function(res){
console.log(res)
}
})
2. wx.setStorageSync('key', data)
示例代码:
wx.setStorageSync('age', 25)
6.4.2 获取数据
1. wx.getStorage(Object)
示例代码:
wx.getStorage({
key:'name',
success:function(res){
console.log(res.data)
},
})
2. wx.getStorageInfoSync(key)
示例代码:
try{
var value = wx.getStorageInfoSync('age')
if(value){
console.log("获取成功"+value)
}
}catch(e){
console.log("获取失败")
}
6.4.3 删除数据
1. wx.removeStorage(Object)
示例代码:
wx.removeStorage({
key: 'name',
success:function(res){
console.log("删除成功")
},
fail:function(){
console.log("删除失败")
}
})
2. wx.removeStorageSync(key)
示例代码:
try{
wx.removeStorageSync('name')
}catch(e){
//Do something when catch error
}
6.4.4 清空数据
1. wx.wx.clearStorage()
示例代码:
wx.clearStorage({
key:'name',
success:function(res){
wx.clearStorage()
},
})
2. wx.clearStorageSync()
示例代码:
try{
wx.clearStorageSync()
}catch(e){
}
6.5 位置信息 API
6.5.1 获取位置信息
示例代码:
wx:getLocation({
type:'wgs84',
success:function(res){
console.log("经度:"+res.longitude);
console.log("纬度:"+res.longitude);
console.log("速度:"+res.longitude);
console.log("位置的精准度:"+res.accuracy);
console.log("水平精准度:"+res.horizontalAccuracy);
console.log("垂直精准度:"+res.verticalAccuracy);
},
})
6.5.2 选择位置信息
代码:
wx.chooseLocation({
success:function(res){
console.log("位置的名称:" +res.name)
console.log("位置的地址:" +res.address)
console.log("位置的经度:" +res.longitude)
console.log("位置的纬度:" +res.latitude)
}
})
6.5.3 显示位置信息
示例代码:
wx:getLocation({
type: 'gcj02',
success:function(res){
var latitude = res.latitude
var longtitude = res.longitude
wx.openLocation({
latitude: latitude,
longitude: longitude,
scale:10,
name:'智慧国际酒店',
address:'西安市长安区西长安区300号'
})
}
})
6.6 设备相关 API
6.6.1 获取系统信息
示例代码:
wx.getSystemInfo({
success:function(res){
console.log("手机型号:"+res.model)
console.log("设备像素比:"+res.pixelRatio)
console.log("窗口的宽度:"+res.windowWidth)
console.log("窗口的高度:"+res.windowHeight)
console.log("微信的版本号:"+res.version)
console.log("操作系统版本:"+res.system)
console.log("客户端平台:"+res.platform)
}
})
6.6.2 网络状态
1. 获取网络状态
示例代码:
wx.getNetworkType({
success:function(res){
console.log(res.networkType)
},
})
2. 监听网络状态变化
示例代码:
wx.onNetworkStatusChange(function(res){
console.log("网络是否连接:"+res.isConnected)
console.log("变化后的网络类型:"+res.networkType)
})
6.6.3 拨打电话
示例代码:
wx.makePhoneCall({
phoneNumber: '18092585093',//需要拨打的电话号码
})
6.6.4 扫描二维码
示例代码:
//允许从相机和相册扫码
wx.scanCode({
success:(res)=>{
console.log(res.result)
console.log(res.scanType)
console.log(res.charSet)
console.log(res.path)
}
})
//只允许从相机扫码
wx.scanCode({
onlyFromCamera:true,
success:(res)=>{
console.log(res)
}
})
6.7 本章小结
本章主要介绍了小程序的各类核心API,包括网络API,多媒体API,文件API、本地数据及缓存API、位置信息API及设备相关API等。