Skip to content

Commit 01a03d6

Browse files
committed
feat(maimai2): improve ApiResponse and print message if api failed
1 parent c3c59be commit 01a03d6

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

src/app/model/ApiResponse.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
export interface ApiResponse<T> {
22
data: T;
33
time: string;
4-
status: number;
4+
status: {
5+
code: number,
6+
message: string
7+
};
58
}
9+
10+
export function isOk<T>(resp: ApiResponse<T>) {
11+
return resp?.status?.code == 9_200_1;
12+
}

src/app/sega/maimai2/maimai2-circle/maimai2-circle.component.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { UserService } from 'src/app/user.service';
1111
import { Maimai2Circle } from '../model/Maimai2Circle';
1212
import { Page } from 'src/app/model/Page';
1313
import { error } from 'console';
14-
import { ApiResponse } from 'src/app/model/ApiResponse';
14+
import { ApiResponse, isOk } from 'src/app/model/ApiResponse';
1515
import { Maimai2UserCircleInfo } from '../model/Maimai2UserCircleInfo';
1616
import { Maimai2RequestJoinCircleUser } from '../model/Maimai2RequestJoinCircleUser';
1717
import { Maimai2UserCircleData } from '../model/Maimai2UserCircleData';
@@ -126,9 +126,13 @@ export class Maimai2CircleComponent implements OnInit {
126126
const param = new HttpParams().set('aimeId', this.aimeId);
127127
this.api.get('api/game/maimai2/userCircleInfo', param).pipe().subscribe(
128128
(data: ApiResponse<Maimai2UserCircleInfo>) => {
129-
this.userCircleInfo = data.data;
130-
this.updateCommentStr = data.data?.joinedCircle?.comment;
131-
console.log(`loadUserCircleInfo() loaded successfully`);
129+
if (isOk(data)) {
130+
this.userCircleInfo = data.data;
131+
this.updateCommentStr = data.data?.joinedCircle?.comment;
132+
console.log(`loadUserCircleInfo() loaded successfully`);
133+
} else {
134+
this.toastShowFailedMessage(data, "加载用户Circle信息失败");
135+
}
132136
}
133137
,
134138
(error: string) => {
@@ -191,10 +195,10 @@ export class Maimai2CircleComponent implements OnInit {
191195
const param = new HttpParams().set('aimeId', this.aimeId).set('circleId', circle.circleId);
192196
this.api.post('api/game/maimai2/requestJoinCircle', param).pipe().subscribe(
193197
(data: ApiResponse<boolean>) => {
194-
if (data.data)
198+
if (isOk(data))
195199
this.messageService.toastService.show("申请加入Circle成功, 等待对方同意");
196200
else
197-
this.messageService.toastService.show("申请加入Circle失败");
201+
this.toastShowFailedMessage(data, "申请加入Circle失败");
198202
}
199203
,
200204
(error: string) => {
@@ -212,13 +216,13 @@ export class Maimai2CircleComponent implements OnInit {
212216
const param = new HttpParams().set('aimeId', this.aimeId).set('userCode', memberInfo?.userCode);
213217
await this.api.post('api/game/maimai2/deleteUserToCircle', param).pipe().subscribe(
214218
(data: ApiResponse<boolean>) => {
215-
if (data.data) {
219+
if (isOk(data)) {
216220
this.messageService.toastService.show("踢出玩家成功");
217221
//refresh
218222
this.loadCircleMemberUserList(this.circleMemberUserListPage);
219223
}
220224
else
221-
this.messageService.toastService.show("踢出玩家失败");
225+
this.toastShowFailedMessage(data, "踢出玩家失败");
222226
}
223227
,
224228
(error: string) => {
@@ -233,14 +237,14 @@ export class Maimai2CircleComponent implements OnInit {
233237
const param = new HttpParams().set('aimeId', this.aimeId).set('userCode', userCode);
234238
this.api.post('api/game/maimai2/approveUserJoinCircle', param).pipe().subscribe(
235239
(data: ApiResponse<boolean>) => {
236-
if (data.data) {
240+
if (isOk(data)) {
237241
this.messageService.toastService.show("同意玩家加入Circle成功");
238242
//refresh
239243
this.loadRequestJoinCircleList(this.requestJoinUserListPage);
240244
this.loadCircleMemberUserList(this.circleMemberUserListPage);
241245
}
242246
else
243-
this.messageService.toastService.show("同意玩家加入Circle失败");
247+
this.toastShowFailedMessage(data, "同意玩家加入Circle失败");
244248
}
245249
,
246250
(error: string) => {
@@ -255,13 +259,13 @@ export class Maimai2CircleComponent implements OnInit {
255259
const param = new HttpParams().set('aimeId', this.aimeId).set('userCode', userCode);
256260
this.api.post('api/game/maimai2/rejectUserJoinCircle', param).pipe().subscribe(
257261
(data: ApiResponse<boolean>) => {
258-
if (data.data) {
262+
if (isOk(data)) {
259263
this.messageService.toastService.show("已拒绝玩家加入Circle");
260264
//refresh
261265
this.loadRequestJoinCircleList(this.requestJoinUserListPage);
262266
}
263267
else
264-
this.messageService.toastService.show("拒绝玩家加入Circle失败");
268+
this.toastShowFailedMessage(data, "拒绝玩家加入Circle失败");
265269
}
266270
,
267271
(error: string) => {
@@ -271,17 +275,17 @@ export class Maimai2CircleComponent implements OnInit {
271275
}
272276
);
273277
}
274-
278+
275279
updateCircle() {
276280
const param = new HttpParams().set('aimeId', this.aimeId);
277281
this.api.post('api/game/maimai2/updateCircle', this.tmpUserCircle, param).pipe().subscribe(
278282
(data: ApiResponse<boolean>) => {
279-
if (data.data) {
283+
if (isOk(data)) {
280284
this.messageService.toastService.show("更新Circle信息成功");
281285
this.loadUserCircleInfo(); // refresh
282286
}
283287
else
284-
this.messageService.toastService.show("更新Circle信息失败");
288+
this.toastShowFailedMessage(data, "更新Circle信息失败");
285289
}
286290
,
287291
(error: string) => {
@@ -315,12 +319,12 @@ export class Maimai2CircleComponent implements OnInit {
315319
const param = new HttpParams().set('aimeId', this.aimeId);
316320
this.api.post('api/game/maimai2/createCircle', this.tmpUserCircle, param).pipe().subscribe(
317321
(data: ApiResponse<boolean>) => {
318-
if (data.data) {
322+
if (isOk(data)) {
319323
this.messageService.toastService.show("新建Circle成功");
320324
this.loadUserCircleInfo(); // refresh
321325
}
322326
else
323-
this.messageService.toastService.show("新建Circle失败");
327+
this.toastShowFailedMessage(data, "新建Circle失败");
324328
}
325329
,
326330
(error: string) => {
@@ -337,12 +341,12 @@ export class Maimai2CircleComponent implements OnInit {
337341
const param = new HttpParams().set('aimeId', this.aimeId);
338342
await this.api.post('api/game/maimai2/exitCircle', param).pipe().subscribe(
339343
(data: ApiResponse<boolean>) => {
340-
if (data.data) {
344+
if (isOk(data)) {
341345
this.messageService.toastService.show("退出Circle成功");
342346
this.loadUserCircleInfo(); // refresh
343347
}
344348
else
345-
this.messageService.toastService.show("退出Circle失败");
349+
this.toastShowFailedMessage(data, "退出Circle失败");
346350
}
347351
,
348352
(error: string) => {
@@ -359,12 +363,12 @@ export class Maimai2CircleComponent implements OnInit {
359363
const param = new HttpParams().set('aimeId', this.aimeId);
360364
this.api.post('api/game/maimai2/dissolveCircle', param).pipe().subscribe(
361365
(data: ApiResponse<boolean>) => {
362-
if (data.data) {
366+
if (isOk(data)) {
363367
this.messageService.toastService.show("解散Circle成功");
364368
this.loadUserCircleInfo(); // refresh
365369
}
366370
else
367-
this.messageService.toastService.show("解散Circle失败");
371+
this.toastShowFailedMessage(data, "解散Circle失败");
368372
}
369373
,
370374
(error: string) => {
@@ -375,5 +379,10 @@ export class Maimai2CircleComponent implements OnInit {
375379
);
376380
}
377381

382+
toastShowFailedMessage(apiResp: ApiResponse<any>, contentPrefix: string) {
383+
var msg = `${contentPrefix}: [${apiResp?.status?.code}] ${apiResp?.status?.message}`;
384+
this.messageService.toastService.show(msg);
385+
}
386+
378387
protected readonly length = length;
379388
}

0 commit comments

Comments
 (0)