feat: storage

master
limin 2020-12-09 08:31:18 +08:00 committed by XuPeng
parent 08417c092d
commit b8f7746481
5 changed files with 276 additions and 1 deletions

View File

@ -49,7 +49,8 @@
"pages/API/image/image",
"pages/API/load-font-face/load-font-face",
"pages/API/video/video",
"pages/API/audio/audio"
"pages/API/audio/audio",
"pages/API/storage/storage"
],
"window": {
"backgroundTextStyle": "light",

View File

@ -0,0 +1,224 @@
// pages/API/storage/storage.js
Page({
data: {
key: '',
data: '',
dialog: {
title: '',
content: '',
hidden: true
}
},
keyChange(e) {
this.data.key = e.detail.value
},
dataChange(e) {
this.data.data = e.detail.value
},
getStorageSync() {
const {key, data} = this.data
let storageData
if (key.length === 0) {
this.setData({
key,
data,
})
wx.showModal({
title: '读取数据失败',
content: 'key 不能为空'
})
} else {
storageData = wx.getStorageSync(key)
console.log(storageData)
if (storageData === '') {
this.setData({
key,
data: storageData
})
wx.showModal({
title: '读取数据失败',
content: '找不到 key 对应的数据'
})
} else {
this.setData({
key,
data:storageData
})
wx.showModal({
title: '读取数据成功',
content: storageData,
})
}
}
},
setStorageSync() {
const {key, data} = this.data
if (key.length === 0) {
this.setData({
key,
data,
})
wx.showModal({
title: '保存数据失败',
content: 'key 不能为空'
})
} else {
wx.setStorageSync(key, data)
this.setData({
key,
data,
})
wx.showModal({
title: '存储数据成功'
})
}
},
clearStorageSync() {
wx.clearStorageSync()
this.setData({
key: '',
data: '',
})
wx.showModal({
title: '清除数据成功'
})
},
removeStorageSync() {
const key = this.data.key;
if (!key) {
wx.showModal({
title: '移除数据失败',
content: 'key 不能为空'
})
return;
}
wx.removeStorageSync(key)
wx.showModal({
title: '移除数据成功',
})
},
getStorageInfoSync() {
const res = wx.getStorageInfoSync();
wx.showModal({
title: '请查看console',
})
console.log(res)
},
clearStorage() {
wx.clearStorage({
success: (res) =>{
this.setData({
key: '',
data: '',
})
wx.showModal({
title: '清除数据成功'
})
},
fail: (res) => {
console.log(res);
}
})
},
getStorage() {
const key = this.data.key;
if (!key) {
wx.showModal({
title: '获取数据失败',
content: 'key 不能为空'
})
return;
}
wx.getStorage({
key,
success: (res) =>{
console.log(res);
wx.showModal({
title: '获取数据成功',
})
this.setData({
data: res.data
})
},
fail: (res) => {
console.log(res);
}
})
},
getStorageInfo() {
wx.getStorageInfo({
success: (res) =>{
console.log(res);
wx.showModal({
title: '获取数据成功',
content: '请查看console'
})
},
fail: (res) => {
console.log(res);
}
})
},
removeStorage() {
const key = this.data.key;
if (!key) {
wx.showModal({
title: '移除数据失败',
content: 'key 不能为空'
})
return;
}
wx.removeStorage({
key,
success: (res) =>{
console.log(res);
wx.showModal({
title: '移除数据成功',
})
},
fail: (res) => {
console.log(res);
}
})
},
setStorage() {
const key = this.data.key;
const data = this.data.data;
if (!key) {
wx.showModal({
title: '保存数据失败',
content: 'key 不能为空'
})
return;
}
wx.setStorage({
key,
data,
success: (res) =>{
console.log(res);
wx.showModal({
title: '保存数据成功',
})
},
fail: (res) => {
console.log(res);
}
})
},
clearData() {
this.setData({
key: '',
data: '',
})
}
})

View File

@ -0,0 +1,3 @@
{
"navigationBarTitleText": "数据存储"
}

View File

@ -0,0 +1,45 @@
<!--pages/API/storage/storage.wxml-->
<import src="../../common/head.wxml" />
<import src="../../common/foot.wxml" />
<view class="container">
<template is="head" data="{{title: 'get/set/clearStorage'}}"/>
<view class="page-body">
<view class="page-section">
<view class="weui-cells weui-cells_after-title">
<view class="weui-cell weui-cell_input">
<view class="weui-cell__hd">
<view class="weui-label">key</view>
</view>
<view class="weui-cell__bd">
<input class="weui-input" type="text" placeholder="请输入key" name="key" value="{{key}}" bindinput="keyChange"></input>
</view>
</view>
<view class="weui-cell weui-cell_input">
<view class="weui-cell__hd">
<view class="weui-label">value</view>
</view>
<view class="weui-cell__bd">
<input class="weui-input" type="text" placeholder="请输入value" name="data" value="{{data}}" bindinput="dataChange"></input>
</view>
</view>
</view>
<view class="btn-area">
<button type="primary" bindtap="setStorageSync">同步存储数据</button>
<button type="primary" bindtap="getStorageInfoSync">同步读取全部数据</button>
<button type="primary" bindtap="getStorageSync">同步读取数据</button>
<button bindtap="clearStorageSync">同步清理全部数据</button>
<button bindtap="removeStorageSync">同步清理指定key数据</button>
<button type="primary" bindtap="setStorage">异步存储数据</button>
<button type="primary" bindtap="getStorageInfo">异步读取全部数据</button>
<button type="primary" bindtap="getStorage">异步读取数据</button>
<button bindtap="clearStorage">异步清理全部数据</button>
<button bindtap="removeStorage">异步清理指定key数据</button>
<button bindtap="clearData">清空界面数据</button>
</view>
</view>
</view>
<template is="foot" />
</view>

View File

@ -0,0 +1,2 @@
/* pages/API/storage/storage.wxss */
@import "../../common/lib/weui.wxss";