Skip to content

Commit

Permalink
perf: 合并http请求,大量用户体验优化、性能优化、可维护性优化
Browse files Browse the repository at this point in the history
  • Loading branch information
fuqihao.foliet committed Oct 17, 2023
1 parent 8817c06 commit 5cb39ce
Show file tree
Hide file tree
Showing 58 changed files with 915 additions and 1,315 deletions.
2 changes: 1 addition & 1 deletion .env/.env.development
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VITE_NAME = "开发环境"
VITE_BASIC_URL = "https://meowchat.xhpolaris.com"
VITE_XH_ENV = "test"
VITE_ENABLE_DEBUG = true
1 change: 0 additions & 1 deletion .env/.env.production
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
VITE_NAME = "生产环境"
VITE_BASIC_URL = "https://meowchat.xhpolaris.com"
VITE_XH_ENV = "pro"
3 changes: 2 additions & 1 deletion src/apis/auth/auth-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export interface SignInReq {
authId: string;
password?: string;
verifyCode?: string;
appId: number;
}

export interface SignInResp {
export interface GignInResp {
userId: string;
accessToken: string;
accessExpire: number;
Expand Down
36 changes: 23 additions & 13 deletions src/apis/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@ import {
SetPasswordReq,
SetPasswordResp,
SignInReq,
SignInResp
GignInResp
} from "./auth-interfaces";
import { getPrefetchData } from "@/apis/prefetch";

/**
* @description
* @param req
*/
export function signIn(req: SignInReq) {
return new Promise<SignInResp>((resolve, reject) => {
uni.request({
url: "/auth/sign_in",
data: req,
method: "POST",
success(res: UniNamespace.RequestSuccessCallbackResult) {
if (res.statusCode !== 200) {
reject(res);
return new Promise<GignInResp>((resolve, reject) => {
getPrefetchData()
.then((res) => {
if (!res.signInResp) {
return Promise.reject();
}
const data = res.data as SignInResp;
resolve(data);
}
});
resolve(res.signInResp);
})
.catch(() => {
uni.request({
url: "/auth/sign_in",
data: req,
method: "POST",
success(res: UniNamespace.RequestSuccessCallbackResult) {
if (res.statusCode !== 200) {
reject(res);
}
const data = res.data as GignInResp;
resolve(data);
}
});
});
});
}

Expand Down
13 changes: 1 addition & 12 deletions src/apis/collection/collection-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@ import { Cat, CatPreview } from "../schemas";
export interface GetCatPreviewsReq {
page: number;
communityId: string;
}

export interface SearchCatPreviewsReq {
communityId: string;
page: number;
keyword: string;
}

export interface SearchCatPreviewsResp {
cats: CatPreview[];
code: number;
msg: string;
keyword?: string;
}

export interface GetCatPreviewsResp {
Expand Down
28 changes: 1 addition & 27 deletions src/apis/collection/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import {
GetImageByCatReq,
GetImageByCatResp,
NewCatReq,
NewCatResp,
SearchCatPreviewsReq,
SearchCatPreviewsResp
NewCatResp
} from "./collection-interfaces";
import { PictureStyle } from "@/apis/cos/cos-interface";

Expand Down Expand Up @@ -80,30 +78,6 @@ export async function getCatPreviews(req: GetCatPreviewsReq) {
});
}

/**
* @description
* @param req
*/
export async function searchCatPreviews(req: SearchCatPreviewsReq) {
return await new Promise<SearchCatPreviewsResp>((resolve, reject) => {
uni.request({
url: "/collection/search_cat",
data: req,
method: "GET",
success(res: UniNamespace.RequestSuccessCallbackResult) {
if (res.statusCode !== 200) {
reject(res);
}
const data = res.data as SearchCatPreviewsResp;
data.cats.forEach((cat) => {
cat.avatarUrl += PictureStyle.thumbnail;
});
resolve(data);
}
});
});
}

/**
* @description
* @param req
Expand Down
20 changes: 18 additions & 2 deletions src/apis/comment/comment-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Comment } from "@/apis/schemas";

export interface GetCommentsReq {
// 作用域,如动态、帖子、评论
scope: string;
type: CommentType;
page: number;
id: string;
}
Expand All @@ -17,10 +17,26 @@ export interface GetCommentsResp {
export interface NewCommentReq {
text: string;
id: string;
scope: string;
firstLevelId?: string;
type: CommentType;
}

export interface NewCommentResp {
code: number;
msg: string;
}

export interface DeleteCommentReq {
commentId: string;
}

export interface DeleteCommentResp {
code: number;
msg: string;
}

export enum CommentType {
Comment = 1,
Post = 2,
Moment = 3
}
6 changes: 4 additions & 2 deletions src/apis/comment/comment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
DeleteCommentReq,
DeleteCommentResp,
GetCommentsReq,
GetCommentsResp,
NewCommentReq,
Expand Down Expand Up @@ -51,7 +53,7 @@ export async function getComments(req: GetCommentsReq) {
* @description
* @param req
*/
export async function deleteCommment(req: object) {
export async function deleteComment(req: DeleteCommentReq) {
return await new Promise<object>((resolve, reject) => {
uni.request({
url: "/comment/delete_comment",
Expand All @@ -61,7 +63,7 @@ export async function deleteCommment(req: object) {
if (res.statusCode !== 200) {
reject(res);
}
const data = res.data as GetCommentsResp;
const data = res.data as DeleteCommentResp;
resolve(data);
}
});
Expand Down
36 changes: 24 additions & 12 deletions src/apis/community/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ListCommunityReq,
ListCommunityResp
} from "@/apis/community/community-interfaces";
import { getPrefetchData } from "@/apis/prefetch";

let cache: ListCommunityResp | null;

Expand All @@ -25,19 +26,30 @@ export async function listCommunity(req: ListCommunityReq) {
return;
}

uni.request({
url: "/community/list_community",
data: req,
method: "GET",
success(res: UniNamespace.RequestSuccessCallbackResult) {
if (res.statusCode !== 200) {
reject(res);
getPrefetchData()
.then((res) => {
if (!res.listCommunityResp) {
return Promise.reject();
}
const data = res.data as ListCommunityResp;
cache = data;
resolve(data);
}
});
cache = res.listCommunityResp;
resolve(res.listCommunityResp);
})
.catch((reason) => {
console.log(reason);
uni.request({
url: "/community/list_community",
data: req,
method: "GET",
success(res: UniNamespace.RequestSuccessCallbackResult) {
if (res.statusCode !== 200) {
reject(res);
}
const data = res.data as ListCommunityResp;
cache = data;
resolve(data);
}
});
});
});
}

Expand Down
24 changes: 7 additions & 17 deletions src/apis/moment/moment-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ export interface DeleteMomentResp {
msg: string;
}

export interface SearchMomentPreviewsReq {
page: number;
keyword: string;
communityId: string;
Authorization?: string;
}

export interface SearchMomentPreviewsResp {
moments: Moment[];
code: number;
msg: string;
}

export interface NewMomentReq {
id?: string; // 留空表示创建
title: string;
Expand All @@ -38,13 +25,16 @@ export interface NewMomentResp {
}

export interface GetMomentPreviewsReq {
backward?: number;
communityId: string;
communityId?: string;
isParent?: number;
onlyUserId?: string;
keyword?: string;

backward?: number;
lastToken?: string;
limit?: number;
onlyUserId?: string;
page: number;
page?: number;
offset?: number;
}

export interface GetMomentPreviewsResp {
Expand Down
21 changes: 1 addition & 20 deletions src/apis/moment/moment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import {
GetMomentPreviewsReq,
GetMomentPreviewsResp,
NewMomentReq,
NewMomentResp,
SearchMomentPreviewsReq,
SearchMomentPreviewsResp
NewMomentResp
} from "./moment-components";
import { PictureStyle } from "@/apis/cos/cos-interface";

Expand Down Expand Up @@ -82,23 +80,6 @@ export async function getMomentPreviews(req: GetMomentPreviewsReq) {
});
}

export async function searchMomentPreviews(req: SearchMomentPreviewsReq) {
return await new Promise<SearchMomentPreviewsResp>((resolve, reject) => {
uni.request({
url: "/moment/search_moment",
data: req,
method: "GET",
success(res: UniNamespace.RequestSuccessCallbackResult) {
if (res.statusCode !== 200) {
reject(res);
}
const data = res.data as SearchMomentPreviewsResp;
resolve(data);
}
});
});
}

/**
* @description
* @param req
Expand Down
22 changes: 7 additions & 15 deletions src/apis/post/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import { PictureStyle } from "@/apis/cos/cos-interface";

export async function getPostPreviews(req: GetPostPreviewsReq) {
return await new Promise<GetPostPreviewsResp>((resolve, reject) => {
req.page = 2;
if (!req.paginationOption) {
req.paginationOption = { limit: 10 };
} else if (!req.paginationOption.limit) {
req.paginationOption.limit = 10;
}
uni.request({
url: "/post/get_post_previews",
method: "POST",
Expand All @@ -27,19 +21,15 @@ export async function getPostPreviews(req: GetPostPreviewsReq) {
reject(res);
}
const data = res.data as GetPostPreviewsResp;
const newData: GetPostPreviewsResp = {
...data,
posts: []
};
data.posts?.forEach((post) => {
if (!post.user) return;
if (post.coverUrl) {
post.coverUrl += PictureStyle.thumbnail;
}
post.user.avatarUrl += PictureStyle.thumbnail;
newData.posts.push(post);
if (post.user?.avatarUrl) {
post.user.avatarUrl += PictureStyle.thumbnail;
}
});
resolve(newData);
resolve(data);
}
});
});
Expand Down Expand Up @@ -90,7 +80,9 @@ export async function getPostDetail(req: GetPostDetailReq) {
reject(res);
}
const data = res.data as GetPostDetailResp;
data.post.user.avatarUrl += PictureStyle.thumbnail;
if (data.post.user?.avatarUrl) {
data.post.user.avatarUrl += PictureStyle.thumbnail;
}
resolve(data);
}
});
Expand Down
Loading

0 comments on commit 5cb39ce

Please sign in to comment.