Skip to content

Commit daa86a2

Browse files
chore: Koa.js is the best Node.js framework 🚀
1 parent 9b7b691 commit daa86a2

File tree

7 files changed

+166
-234
lines changed

7 files changed

+166
-234
lines changed

server/src/controller/auth.controller.ts

+19-32
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,14 @@ export class AuthController {
4848
return
4949
}
5050

51-
try {
52-
await AuthRepository.regist({
53-
phone,
54-
pwd: _pwd
55-
})
56-
57-
ctx.body = {
58-
apiCode: 0,
59-
message: '注册成功,请用手机号密码登录'
60-
}
61-
} catch (error) {
62-
ctx.body = {
63-
apiCode: 0,
64-
message: error.message
65-
}
51+
await AuthRepository.regist({
52+
phone,
53+
pwd: _pwd
54+
})
55+
56+
ctx.body = {
57+
apiCode: 0,
58+
message: '注册成功,请用手机号密码登录'
6659
}
6760
}
6861

@@ -101,23 +94,17 @@ export class AuthController {
10194
const { phone, pwd, phoneCode } = ctx.request.body as ResetPasswordDto
10295
const _pwd = md5.update(pwd).digest('hex')
10396

104-
try {
105-
// 需要校验 phoneCode
106-
// ......
107-
await AuthRepository.resetPassword({
108-
phone,
109-
pwd: _pwd,
110-
phoneCode
111-
})
112-
ctx.body = {
113-
apiCode: 0,
114-
message: '密码重置成功'
115-
}
116-
} catch (error) {
117-
ctx.body = {
118-
apiCode: -1,
119-
message: error.message
120-
}
97+
// 需要校验 phoneCode
98+
// ......
99+
await AuthRepository.resetPassword({
100+
phone,
101+
pwd: _pwd,
102+
phoneCode
103+
})
104+
105+
ctx.body = {
106+
apiCode: 0,
107+
message: '密码重置成功'
121108
}
122109
}
123110
}

server/src/controller/comment.controller.ts

+38-54
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,14 @@ export class CommentController {
99
public static async find (ctx: Context) {
1010
const { couponId } = ctx.query as Partial<GetCommentListDto>
1111

12-
try {
13-
const res = await CommentRepository.find({
14-
couponId
15-
})
16-
17-
ctx.body = {
18-
apiCode: 0,
19-
data: res,
20-
message: ''
21-
}
22-
} catch (error) {
23-
ctx.body = {
24-
apiCode: -1,
25-
data: null,
26-
message: error.message
27-
}
12+
const res = await CommentRepository.find({
13+
couponId
14+
})
15+
16+
ctx.body = {
17+
apiCode: 0,
18+
data: res,
19+
message: ''
2820
}
2921
}
3022

@@ -35,52 +27,44 @@ export class CommentController {
3527
// 这里的优惠券暂时没有判断什么时候就算使用了,所以测试执行以下逻辑:
3628
// 用户发表评论这个【动作】即是【使用】优惠券,优惠券使用完之后不能再次使用
3729
// 线上的项目,这块儿的逻辑可以修改下
38-
try {
39-
const couponUserItem = await CouponUserRepository.findById({
40-
couponId,
41-
userId
42-
})
43-
44-
if (!couponUserItem) {
45-
ctx.body = {
46-
apiCode: 0,
47-
data: null,
48-
message: '购买优惠券之后才能评论哦'
49-
}
50-
return
51-
}
52-
53-
if (couponUserItem.status && couponUserItem.status !== '0') {
54-
ctx.body = {
55-
apiCode: 0,
56-
data: null,
57-
message: '已经发布过评论了'
58-
}
59-
return
60-
}
61-
62-
const user = await UserRepository.getUserInfo({
63-
id: userId
64-
})
65-
66-
await CommentRepository.publish({
67-
commentUserPhone: user.userPhone,
68-
commentStar,
69-
commentContent,
70-
couponId
71-
})
30+
const couponUserItem = await CouponUserRepository.findById({
31+
couponId,
32+
userId
33+
})
7234

35+
if (!couponUserItem) {
7336
ctx.body = {
7437
apiCode: 0,
7538
data: null,
76-
message: '评论成功'
39+
message: '购买优惠券之后才能评论哦'
7740
}
78-
} catch (error) {
41+
return
42+
}
43+
44+
if (couponUserItem.status && couponUserItem.status !== '0') {
7945
ctx.body = {
80-
apiCode: -1,
46+
apiCode: 0,
8147
data: null,
82-
message: error.message
48+
message: '已经发布过评论了'
8349
}
50+
return
51+
}
52+
53+
const user = await UserRepository.getUserInfo({
54+
id: userId
55+
})
56+
57+
await CommentRepository.publish({
58+
commentUserPhone: user.userPhone,
59+
commentStar,
60+
commentContent,
61+
couponId
62+
})
63+
64+
ctx.body = {
65+
apiCode: 0,
66+
data: null,
67+
message: '评论成功'
8468
}
8569
}
8670
}

server/src/controller/coupon.controller.ts

+61-99
Original file line numberDiff line numberDiff line change
@@ -7,130 +7,92 @@ import { GetCouponDetailDto, GetCouponListDto, GetCouponRecordByUserDto, GetRece
77

88
export class CouponController {
99
public static async find (ctx: Context) {
10-
try {
11-
const { page, regionId, classifyId } = ctx.query as Partial<GetCouponListDto>
12-
13-
const res = await CouponRepository.list({
14-
page: +page,
15-
regionId: +regionId,
16-
classifyId: +classifyId
17-
})
18-
19-
ctx.body = {
20-
apiCode: 0,
21-
data: res,
22-
message: ''
23-
}
24-
} catch (error) {
25-
ctx.body = {
26-
apiCode: -1,
27-
data: null,
28-
message: error.message
29-
}
10+
const { page, regionId, classifyId } = ctx.query as Partial<GetCouponListDto>
11+
12+
const res = await CouponRepository.list({
13+
page: +page,
14+
regionId: +regionId,
15+
classifyId: +classifyId
16+
})
17+
18+
ctx.body = {
19+
apiCode: 0,
20+
data: res,
21+
message: ''
3022
}
3123
}
3224

3325
public static async detail (ctx: Context) {
34-
try {
35-
const { id } = ctx.query as Partial<GetCouponDetailDto>
36-
const res = await CouponRepository.detail({
37-
id
38-
})
39-
ctx.body = {
40-
apiCode: 0,
41-
data: res,
42-
message: ''
43-
}
44-
} catch (error) {
45-
ctx.body = {
46-
apiCode: -1,
47-
data: null,
48-
message: error.message
49-
}
26+
const { id } = ctx.query as Partial<GetCouponDetailDto>
27+
const res = await CouponRepository.detail({
28+
id
29+
})
30+
31+
ctx.body = {
32+
apiCode: 0,
33+
data: res,
34+
message: ''
5035
}
5136
}
5237

5338
public static async receive (ctx: Context) {
54-
try {
55-
const { userId, couponId } = ctx.request.body as ReceiveCouponDto
39+
const { userId, couponId } = ctx.request.body as ReceiveCouponDto
5640

57-
const hasReceived = await CouponUserRepository.findById({
58-
userId,
59-
couponId
60-
})
61-
62-
if (hasReceived) {
63-
ctx.body = {
64-
apiCode: 0,
65-
data: null,
66-
message: '已经领取过了'
67-
}
68-
return
69-
}
70-
71-
await CouponUserRepository.add({
72-
userId,
73-
couponId
74-
})
75-
76-
await CouponRepository.updateReceivedNum({
77-
couponId
78-
})
41+
const hasReceived = await CouponUserRepository.findById({
42+
userId,
43+
couponId
44+
})
7945

46+
if (hasReceived) {
8047
ctx.body = {
8148
apiCode: 0,
8249
data: null,
83-
message: '领取成功'
84-
}
85-
} catch (error) {
86-
ctx.body = {
87-
apiCode: -1,
88-
data: null,
89-
message: error.message
50+
message: '已经领取过了'
9051
}
52+
return
53+
}
54+
55+
await CouponUserRepository.add({
56+
userId,
57+
couponId
58+
})
59+
60+
await CouponRepository.updateReceivedNum({
61+
couponId
62+
})
63+
64+
ctx.body = {
65+
apiCode: 0,
66+
data: null,
67+
message: '领取成功'
9168
}
9269
}
9370

9471
public static async getCouponRecordByUser (ctx: Context) {
9572
const { userId } = ctx.query as Partial<GetCouponRecordByUserDto>
9673

97-
try {
98-
const res = await CouponRepository.getCouponRecordByUser({
99-
userId
100-
})
74+
const res = await CouponRepository.getCouponRecordByUser({
75+
userId
76+
})
10177

102-
ctx.body = {
103-
apiCode: 0,
104-
data: res,
105-
message: ''
106-
}
107-
} catch (error) {
108-
ctx.body = {
109-
apiCode: -1,
110-
data: null,
111-
message: error.message
112-
}
78+
ctx.body = {
79+
apiCode: 0,
80+
data: res,
81+
message: ''
11382
}
11483
}
11584

11685
public static async getReceivedCouponList (ctx: Context) {
117-
try {
118-
const { userId, type } = ctx.query as Partial<GetReceivedCouponListDto>
119-
const res = await CouponUserRepository.getReceivedCouponList({
120-
userId,
121-
type
122-
})
123-
ctx.body = {
124-
apiCode: 0,
125-
data: res,
126-
message: ''
127-
}
128-
} catch (error) {
129-
ctx.body = {
130-
apiCode: -1,
131-
data: null,
132-
message: error.message
133-
}
86+
const { userId, type } = ctx.query as Partial<GetReceivedCouponListDto>
87+
const res = await CouponUserRepository.getReceivedCouponList({
88+
userId,
89+
type
90+
})
91+
92+
ctx.body = {
93+
apiCode: 0,
94+
data: res,
95+
message: ''
13496
}
13597
}
13698
}

0 commit comments

Comments
 (0)