Skip to content

Commit

Permalink
feat: offline remark
Browse files Browse the repository at this point in the history
  • Loading branch information
yigger committed Jun 6, 2019
1 parent 195488a commit 8227e08
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 10 deletions.
38 changes: 36 additions & 2 deletions src/app.wpy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { setStore, getStore } from 'wepy-redux'
import configStore from './store'
import { asyncList } from '@/store/actions'
import wxRequest from '@/utils/wxRequest'
import Session from '@/utils/session'
setStore(configStore())

export default class extends wepy.app {
Expand Down Expand Up @@ -124,15 +125,26 @@ export default class extends wepy.app {
}

globalData = {
user: {}
user: {},
timer: null
}

async onLaunch () {
onLaunch () {
this.getUser()
this.checkVersionUpdate()
this.runPoll()
}

// 预加载用户
async getUser() {
const data = await wxRequest.Get('users')
if (typeof data !== 'undefined') {
this.globalData.user = data
}
}

// 热更新
checkVersionUpdate() {
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
Expand Down Expand Up @@ -161,6 +173,28 @@ export default class extends wepy.app {
}
}

runPoll() {
// this.runPoll = setInterval(() => {
// console.log('test')
// }, 2000);
this.uploadData()
}

uploadData() {
const errs = Session.getErrors()
if (!!errs && errs.length > 0) {

}

const statements = Session.getStatements()
if (!!statements && statements.length > 0) {
for(let statement of statements) {
wxRequest.PostBasic(`statements`, statement)
// 请求完成后需要清空 session
}
}
}

constructor () {
super()
this.use('requestfix')
Expand Down
11 changes: 10 additions & 1 deletion src/pages/index.wpy
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import StatementItem from '@/components/index/statement'
import { getStore } from 'wepy-redux'
import { asyncList } from '@/store/actions'
import Tip from '@/utils/tip'
export default class Index extends wepy.page {
config = {
navigationBarTitleText: '首页',
Expand Down Expand Up @@ -81,6 +82,10 @@
}

const data = await wxRequest.Get('header')
if (data === undefined) {
return false
}

this.header = data
Session.set('header_load_cache', this.header)
this.$apply()
Expand All @@ -93,7 +98,11 @@
}

const store = getStore()
await store.dispatch(asyncList())
const result = await store.dispatch(asyncList())
if (typeof result.payload === 'undefined') {
Tip.error("可能由于网络问题,获取列表失败", 2300)
return false
}
const res = store.getState().statement.statements
this.list = res
Session.set('index_load_cache', res)
Expand Down
5 changes: 4 additions & 1 deletion src/pages/statements/chose_asset.wpy
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@
}

const data = await wxRequest.Get('statements/assets')
if (data === undefined) {
return false
}
this.frequent = data.frequent
this.last = data.last
this.list = data.categories
this.$apply()
Session.set(sessionKey, data)
this.$apply()
}

computed = {
Expand Down
4 changes: 4 additions & 0 deletions src/pages/statements/chose_category.wpy
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
}

const data = await wxRequest.Get('statements/categories', { type: this.type })
if (data === undefined) {
console.log(data)
return false
}
this.frequent = data.frequent
this.last = data.last
this.list = data.categories
Expand Down
14 changes: 13 additions & 1 deletion src/pages/statements/create.wpy
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import { addStatement, modifyStatement } from '@/store/actions'
import tip from '@/utils/tip'
import Util from '@/utils/util.js'
import Session from '@/utils/session'

export default class StatementCreate extends wepy.page {
config = {
Expand Down Expand Up @@ -87,7 +88,18 @@
},
async submit (statement, detail) {
this.submiting = true
const result = await wxRequest.Post(`statements`, {statement: statement, formId: detail.formId})
const params = {statement: statement, formId: detail.formId}
const result = await wxRequest.Post(`statements`, params)
if (result === undefined) {
this.submiting = false
await tip.confirm('由于网络原因,无法同步账单到服务器,现已临时保存在本地,下次网络正常后系统将自动同步到服务端', {}, '保存失败')
Session.pushFailStatement(params)
wepy.navigateBack({
delta: 1
})
return false
}

try {
if (result.status == 200) {
const store = getStore()
Expand Down
36 changes: 34 additions & 2 deletions src/utils/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const KEY = {
},
alreadyLogin: "@alreadyLogin@",
login: 'weapp_login_session',
bgImageKey: '@user_index_bg@'
bgImageKey: '@user_index_bg@',
errorKey: '@request_error@',
localStatementKey: '@local_statement_cache@'
}

module.exports = {
Expand All @@ -27,8 +29,38 @@ module.exports = {

clear: function (key) {
wx.removeStorageSync(key);
},
},

// 排查为何频繁拉取失败
pushError: function(err) {
let kV = wx.getStorageSync(KEY['errorKey']) || null
if (kV === null) {
kV = [err]
} else {
kV.push(err)
}
wx.setStorageSync(KEY['errorKey'], kV)
},

getErrors: function() {
return wx.getStorageSync(KEY['errorKey']) || null;
},

// 由于网络原因导致账单提交失败的一律先存储到缓存,下次再重新提交
pushFailStatement: function(statement) {
let kV = wx.getStorageSync(KEY['localStatementKey']) || null
if (kV === null) {
kV = [statement]
} else {
kV.push(statement)
}
wx.setStorageSync(KEY['localStatementKey'], kV)
},

getStatements: function() {
return wx.getStorageSync(KEY['localStatementKey']) || null;
},

clearByKey (item) {
for(let obj in KEY) {
if (obj == item) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/tip.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export default class Tips {
* 错误框
*/

static error(title) {
static error(title, duration = 1500) {
wx.showToast({
title: title,
icon: 'none',
mask: false,
duration: 1500
duration: duration
});
}

Expand Down
7 changes: 6 additions & 1 deletion src/utils/wxRequest/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ const doRequest = async (url, method, params, options = {}) => {
if(cacheKey != '') setByCache(cacheKey, result)
return result
} catch (e) {
Session.pushError({ url: url, method: method, params: params, err: e, time: new Date().toLocaleString()})
let message = e.errMsg
if (message.trim() === "request:fail") {
message = '网络请求失败...'
}
wx.showToast({
title: e.errMsg,
title: message,
icon: 'none',
duration: 3000
})
Expand Down
2 changes: 2 additions & 0 deletions src/utils/wxRequest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import Host from '@/utils/host'
const Get = async (url, params, options = {}) => await Base.doRequest(`${Host.url}/${url}`, 'GET', params, options)
const Put = async (url, params, options = {}) => await Base.doRequest(`${Host.url}/${url}`, 'PUT', params, options)
const Post = async (url, params, options = {}) => await Base.doRequest(`${Host.url}/${url}`, 'Post', params, options)
const PostBasic = (url, params, options = {}) => Base.doRequest(`${Host.url}/${url}`, 'Post', params, options)
const Destroy = async (url, params, options = {}) => await Base.doRequest(`${Host.url}/${url}`, 'DELETE', params, options)
const Upload = async (filePath, params = {}) => await Base.wxUpload(`${Host.url}/upload`, filePath, params)
export default {
Get,
Put,
Post,
Destroy,
PostBasic,
Upload
}

0 comments on commit 8227e08

Please sign in to comment.