[TOC]
## cache.js
> 存放路徑 /common/cache.js
~~~
/**
* 緩存數據優化
* var cache = require('utils/cache.js');
* import cache from '../cache'
* 使用方法 【
* 一、設置緩存
* string cache.put('k', 'string你好啊');
* json cache.put('k', { "b": "3" }, 2);
* array cache.put('k', [1, 2, 3]);
* boolean cache.put('k', true);
* 二、讀取緩存
* 默認值 cache.get('k')
* string cache.get('k', '你好')
* json cache.get('k', { "a": "1" })
* 三、移除/清理
* 移除: cache.remove('k');
* 清理:cache.clear();
* 】
* @type {String}
*/
var postfix = '_xjsU'; // 緩存前綴
/**
* 設置緩存
* @param {[type]} k [鍵名]
* @param {[type]} v [鍵值]
* @param {[type]} t [時間、單位秒]
*/
function set(k, v, t) {
uni.setStorageSync(k, v)
var seconds = parseInt(t);
if (seconds > 0) {
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000 + seconds;
uni.setStorageSync(k + postfix, timestamp + "")
} else {
uni.removeStorageSync(k + postfix)
}
}
/**
* 獲取緩存
* @param {[type]} k [鍵名]
* @param {[type]} def [獲取為空時默認]
*/
function get(k, def) {
var deadtime = parseInt(uni.getStorageSync(k + postfix))
if (deadtime) {
if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {
if (def) {
return def;
} else {
return false;
}
}
}
var res = uni.getStorageSync(k);
if (res) {
return res;
} else {
if (def == undefined || def == "") {
def = false;
}
return def;
}
}
function remove(k) {
uni.removeStorageSync(k);
uni.removeStorageSync(k + postfix);
}
/**
* 清理所有緩存
* @return {[type]} [description]
*/
function clear() {
uni.clearStorageSync();
}
module.exports = {
set: set,
get: get,
remove: remove,
clear: clear
}
~~~
## main.js 中注冊該方法
> 路徑 /main.js
~~~
import Vue from 'vue'
import App from './App'
import cache from './common/cache.js'
Vue.config.productionTip = false
Vue.prototype.$cache = cache
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
~~~
## 客戶端調用
~~~
<template>
<view>
<button type="default" @tap="setCache">設置緩存</button>
<button type="default" @tap="getCache">獲取緩存</button>
<button type="default" @tap="clearCache">刪除緩存</button>
</view>
</template>
<script>
export default {
methods: {
setCache: function(e) {
this.$cache.set('name', 'haha123');
},
getCache: function(e) {
let name = this.$cache.get('name');
console.log("name is : " + name)
},
clearCache: function(e) {
this.$cache.clear();
},
}
}
</script>
~~~
- 基礎知識
- UNI核心介紹
- flex布局
- 生命周期
- 全局方法
- 組件定義
- 自定義組件
- 全局組件
- 組件之間的數據傳輸
- 條件編譯
- 自定義頭部
- 節點信息 (SelectorQuery)
- vuejs基礎語法
- 頁面跳轉以及參數傳遞
- 事件的監聽注冊以及觸發
- css3動畫
- block的妙用
- mixin (混入)
- uniapp快捷鍵
- vuex狀態管理
- 實用功能
- 獲取服務提供商
- 啟動頁 / 啟動界面
- 引導頁
- tabbar配置
- 頭部導航欄基礎設置
- 上拉下拉(刷新/加載)
- 第三方登錄
- 第三方分享
- 推送通知 之 unipush
- scroll-view雙聯動
- 配置iOS通用鏈接(Universal Links)
- 本地緩存操作
- 升級/更新方案
- 熱更新
- 圖片上傳
- 搜索頁實現
- canvas繪圖助手
- 地圖定位
- 第三方支付————todo
- 分類輪播
- 清除應用緩存
- uniapp與webview的實時通訊
- 視頻-----todo
- 聊天----todo
- 長列表swiper左右切換
- 第三方插件
- uview
- mescroll
- uCharts (圖表)
- 無名 (更新插件)
- 第三方模版
- 自定義基座
- 打包發行
- 要封裝的方法
- 緩存 cache.js
- 請求接口 request.js
- 工具類 util.js
- 小程序登錄 xcxLogin.js
- 版本更新 update.js
- 優質插件
- 更新插件----todo
- 語音
- 語音識別 (含上傳)
- 百度語音合成播報接口
- 官方常用組建
- input 輸入框
- image 圖片
- audio 音頻
- picker 選擇器
- video 視頻
- scroll-view 滾動視圖