Skip to content

Commit

Permalink
日记存储在本地
Browse files Browse the repository at this point in the history
  • Loading branch information
小闹钟 committed Oct 19, 2016
1 parent 26eb6b9 commit 0a15eb1
Show file tree
Hide file tree
Showing 15 changed files with 369 additions and 408 deletions.
80 changes: 71 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,79 @@
// app.js

const config = require('config');
const diaries = require('demo/diaries');

App({

onLaunch: function () {
this.getDiaryList();
},

getUserInfo:function(cb){
// 获取用户信息
getUserInfo: function(cb) {
var that = this;

if (this.globalData.userInfo) {
typeof cb == 'function' && cb(this.globalData.userInfo)
} else {
// 先登录
wx.login({
success: function() {
wx.getUserInfo({
success: (res) => {
that.globalData.userInfo = res.userInfo;
typeof cb == 'function' && cb(that.globalData.userInfo)
}
})
}
})
}
},

getDiaryList() {
// 获取本地全部日记列表
getDiaryList(cb) {
var that = this;

wx.getStorage({
key: config.storage.diaryListKey,
success: (res) => {
this.globalData.diaryList = res.data;
}
})
if (this.globalData.diaryList) {
typeof cb == 'function' && cb(this.globalData.diaryList);
} else {
let list = [];

this.getLocalDiaries(storage => {
// 本地缓存数据
for (var k in storage) {
list.push(storage[k]);
}
});

// 本地假数据
list.push(...diaries.diaries);
that.globalData.diaryList = list;
typeof cb == 'function' && cb(that.globalData.diaryList)
}
},

// 获取本地日记缓存
getLocalDiaries(cb) {
var that = this;

if (this.globalData.localDiaries) {
typeof cb == 'function' && cb(this.globalData.localDiaries);
} else {
wx.getStorage({
key: config.storage.diaryListKey,
success: (res) => {
that.globalData.localDiaries = res.data;
typeof cb == 'function' && cb(that.globalData.localDiaries);
},
fail: (error) => {
that.globalData.localDiaries = {};
typeof cb == 'function' && cb(that.globalData.localDiaries);
}
});
}
},

// 获取当前设备信息
getDeviceInfo: function(callback) {
var that = this;

Expand All @@ -38,8 +90,18 @@ App({
},

globalData: {
// 设备信息,主要用于获取屏幕尺寸而做适配
deviceInfo: null,

// 本地日记缓存列表 + 假数据
// TODO 真实数据同步至服务端,本地只做部分缓存
diaryList: null,

// 本地日记缓存
localDiaries: null,

// 用户信息
userInfo: null,
}

})
3 changes: 1 addition & 2 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"pages":[
"pages/list/list",
"pages/mine/mine",
"pages/compose/compose",
"pages/new/new",
"pages/item/item"
"pages/entry/entry"
],
"window":{
"backgroundTextStyle": "light",
Expand Down
Loading

0 comments on commit 0a15eb1

Please sign in to comment.