From 7243dd699a26da3cb879c0bb5297d4d9eee521af Mon Sep 17 00:00:00 2001 From: MaysWind Date: Thu, 17 Dec 2020 23:32:37 +0800 Subject: [PATCH] get account list api supports only requesting visible accounts --- pkg/api/accounts.go | 16 ++++++++++++++-- pkg/models/account.go | 12 ++++++++---- src/lib/services.js | 4 ++-- src/views/mobile/accounts/List.vue | 4 ++-- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/pkg/api/accounts.go b/pkg/api/accounts.go index 03909e5e..ca00acf3 100644 --- a/pkg/api/accounts.go +++ b/pkg/api/accounts.go @@ -22,6 +22,14 @@ var ( ) func (a *AccountsApi) AccountListHandler(c *core.Context) (interface{}, *errs.Error) { + var accountListReq models.AccountListRequest + err := c.ShouldBindQuery(&accountListReq) + + if err != nil { + log.WarnfWithRequestId(c, "[accounts.AccountListHandler] parse request failed, because %s", err.Error()) + return nil, errs.NewIncompleteOrIncorrectSubmissionError(err) + } + uid := c.GetCurrentUid() accounts, err := a.accounts.GetAllAccountsByUid(uid) @@ -41,6 +49,10 @@ func (a *AccountsApi) AccountListHandler(c *core.Context) (interface{}, *errs.Er for i := 0; i < len(userAllAccountResps); i++ { userAccountResp := userAllAccountResps[i] + if accountListReq.VisibleOnly && userAccountResp.Hidden { + continue + } + if userAccountResp.ParentId <= models.ACCOUNT_PARENT_ID_LEVEL_ONE { continue } @@ -54,10 +66,10 @@ func (a *AccountsApi) AccountListHandler(c *core.Context) (interface{}, *errs.Er parentAccount.SubAccounts = append(parentAccount.SubAccounts, userAccountResp) } - userFinalAccountResps := make(models.AccountInfoResponseSlice, 0) + userFinalAccountResps := make(models.AccountInfoResponseSlice, 0, len(userAllAccountResps)) for i := 0; i < len(userAllAccountResps); i++ { - if userAllAccountResps[i].ParentId == models.ACCOUNT_PARENT_ID_LEVEL_ONE { + if userAllAccountResps[i].ParentId == models.ACCOUNT_PARENT_ID_LEVEL_ONE && (!accountListReq.VisibleOnly || !userAllAccountResps[i].Hidden) { sort.Sort(userAllAccountResps[i].SubAccounts) userFinalAccountResps = append(userFinalAccountResps, userAllAccountResps[i]) } diff --git a/pkg/models/account.go b/pkg/models/account.go index 6df58ddf..bc58a9d4 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -62,10 +62,6 @@ type Account struct { DeletedUnixTime int64 } -type AccountGetRequest struct { - Id int64 `form:"id,string" binding:"required,min=1"` -} - type AccountCreateRequest struct { Name string `json:"name" binding:"required,notBlank,max=32"` Category AccountCategory `json:"category" binding:"required"` @@ -89,6 +85,14 @@ type AccountModifyRequest struct { SubAccounts []*AccountModifyRequest `json:"subAccounts" binding:"omitempty"` } +type AccountListRequest struct { + VisibleOnly bool `form:"visible_only"` +} + +type AccountGetRequest struct { + Id int64 `form:"id,string" binding:"required,min=1"` +} + type AccountHideRequest struct { Id int64 `json:"id,string" binding:"required,min=1"` Hidden bool `json:"hidden"` diff --git a/src/lib/services.js b/src/lib/services.js index 60b8db3a..c084526a 100644 --- a/src/lib/services.js +++ b/src/lib/services.js @@ -166,8 +166,8 @@ export default { password }); }, - getAllAccounts: () => { - return axios.get('v1/accounts/list.json'); + getAllAccounts: ({ visibleOnly }) => { + return axios.get('v1/accounts/list.json?visible_only=' + !!visibleOnly); }, getAccount: ({ id }) => { return axios.get('v1/accounts/get.json?id=' + id); diff --git a/src/views/mobile/accounts/List.vue b/src/views/mobile/accounts/List.vue index 2abbb4cc..b067bbfd 100644 --- a/src/views/mobile/accounts/List.vue +++ b/src/views/mobile/accounts/List.vue @@ -318,7 +318,7 @@ export default { self.loading = true; - self.$services.getAllAccounts().then(response => { + self.$services.getAllAccounts({ visibleOnly: false }).then(response => { const data = response.data; if (!data || !data.success || !data.result) { @@ -358,7 +358,7 @@ export default { const self = this; - self.$services.getAllAccounts().then(response => { + self.$services.getAllAccounts({ visibleOnly: false }).then(response => { if (done) { done(); }