55 < title > BuggyShop - 有Bug的商城</ title >
66 < style >
77 * { margin : 0 ; padding : 0 ; box-sizing : border-box; }
8- body { font-family : sans-serif; background : # f5f5f5 ; padding : 20 px ; }
9- h1 { text-align : center; margin-bottom : 20 px ; color : # 333 ; }
10- .product { background : white; border-radius : 8 px ; padding : 16 px ; margin-bottom : 12 px ; box-shadow : 0 1px 3px rgba (0 , 0 , 0 , 0.1 ); }
11- .product h3 { margin-bottom : 8 px ; }
8+ body { font-family : sans-serif; background : # f5f5f5 ; padding : 8 px ; }
9+ h1 { text-align : center; margin-bottom : 8 px ; color : # 333; font-size : 18 px ; }
10+ .product { background : white; border-radius : 6 px ; padding : 8 px 12 px ; margin-bottom : 6 px ; box-shadow : 0 1px 3px rgba (0 , 0 , 0 , 0.1 ); display : flex; align-items : center; gap : 12 px ; }
11+ .product h3 { margin-bottom : 0 ; font-size : 14 px ; }
1212 .price { color : # e44 ; font-size : 18px ; font-weight : bold; }
1313 .btn { padding : 8px 16px ; border : none; border-radius : 4px ; cursor : pointer; margin : 4px ; font-size : 14px ; }
1414 .btn-buy { background : # 4CAF50 ; color : white; }
1515 .btn-cart { background : # 2196F3 ; color : white; }
1616 .btn-checkout { background : # FF9800 ; color : white; font-size : 16px ; padding : 12px 24px ; }
17- # cart { background : white; border-radius : 8 px ; padding : 16 px ; margin-top : 20 px ; box-shadow : 0 1px 3px rgba (0 , 0 , 0 , 0.1 ); }
18- # cart h2 { margin-bottom : 12 px ; }
19- .cart-item { display : flex; justify-content : space-between; padding : 8 px 0 ; border-bottom : 1px solid # eee ; }
20- # total { font-size : 20 px ; font-weight : bold; margin : 12 px 0 ; color : # 333 ; }
21- # message { padding : 12 px ; margin-top : 12 px ; border-radius : 4px ; display : none; }
17+ # cart { background : white; border-radius : 6 px ; padding : 10 px ; margin-top : 8 px ; box-shadow : 0 1px 3px rgba (0 , 0 , 0 , 0.1 ); }
18+ # cart h2 { margin-bottom : 6 px ; font-size : 16 px ; }
19+ .cart-item { display : flex; justify-content : space-between; padding : 4 px 0 ; border-bottom : 1px solid # eee ; }
20+ # total { font-size : 18 px ; font-weight : bold; margin : 8 px 0 ; color : # 333 ; }
21+ # message { padding : 10 px ; margin-top : 8 px ; border-radius : 4px ; display : none; }
2222 .success { background : # e8f5e9 ; color : # 2e7d32 ; display : block !important ; }
2323 .error { background : # fce4ec ; color : # c62828 ; display : block !important ; }
24- # login-form { background : white; border-radius : 8px ; padding : 16px ; margin-bottom : 20px ; box-shadow : 0 1px 3px rgba (0 , 0 , 0 , 0.1 ); }
25- # login-form input { padding : 8px ; margin : 4px ; border : 1px solid # ddd ; border-radius : 4px ; }
26- # user-info { color : # 666 ; margin-bottom : 10px ; }
24+ # login-form { background : white; border-radius : 6px ; padding : 8px 12px ; margin-bottom : 8px ; box-shadow : 0 1px 3px rgba (0 , 0 , 0 , 0.1 ); display : flex; align-items : center; gap : 8px ; flex-wrap : wrap; }
25+ # login-form h3 { font-size : 14px ; }
26+ # login-form input { padding : 6px ; border : 1px solid # ddd ; border-radius : 4px ; }
27+ # user-info { color : # 666 ; }
2728 </ style >
2829</ head >
2930< body >
@@ -48,7 +49,7 @@ <h3>无线蓝牙耳机</h3>
4849 < h3 > 机械键盘</ h3 >
4950 <!-- BUG 1: 显示价格和实际价格不一致 -->
5051 < span class ="price " id ="price-2 "> ¥199</ span >
51- < button class ="btn btn-cart " onclick ="addToCart(2, '机械键盘', 599 ) "> 加入购物车</ button >
52+ < button class ="btn btn-cart " onclick ="addToCart(2, '机械键盘', 199 ) "> 加入购物车</ button >
5253 </ div >
5354 < div class ="product " data-id ="3 ">
5455 < h3 > USB-C 扩展坞</ h3 >
@@ -75,12 +76,14 @@ <h2>购物车</h2>
7576 const user = document . getElementById ( 'username' ) . value ;
7677 const pass = document . getElementById ( 'password' ) . value ;
7778
78- // BUG 2: 空密码也能登录
79- if ( user ) {
79+ if ( user && pass ) {
8080 loggedIn = true ;
8181 username = user ;
8282 document . getElementById ( 'user-info' ) . textContent = '欢迎, ' + user ;
8383 document . getElementById ( 'user-info' ) . className = 'success' ;
84+ } else {
85+ document . getElementById ( 'user-info' ) . textContent = '请输入用户名和密码' ;
86+ document . getElementById ( 'user-info' ) . className = 'error' ;
8487 }
8588 }
8689
@@ -101,8 +104,7 @@ <h2>购物车</h2>
101104 container . appendChild ( div ) ;
102105 total += item . price ;
103106 } ) ;
104- // BUG 4: 总价计算有精度问题 (故意不用toFixed)
105- document . getElementById ( 'total' ) . textContent = '总计: ¥' + ( total * 1.0000001 ) ;
107+ document . getElementById ( 'total' ) . textContent = '总计: ¥' + Math . round ( total ) ;
106108 }
107109
108110 function checkout ( ) {
@@ -114,17 +116,16 @@ <h2>购物车</h2>
114116 return ;
115117 }
116118
117- // BUG 5: 未登录也能结算(没检查 loggedIn)
118- let total = 0 ;
119- cart . forEach ( item => total += item . price ) ;
120-
121- // BUG 6: 总价超过1000时结算报错(模拟后端错误)
122- if ( total > 1000 ) {
123- msg . textContent = '系统错误: 订单金额异常,请联系客服 (Error 500)' ;
119+ if ( ! loggedIn ) {
120+ msg . textContent = '请先登录后再结算' ;
124121 msg . className = 'error' ;
125122 return ;
126123 }
127124
125+ let total = 0 ;
126+ cart . forEach ( item => total += item . price ) ;
127+
128+
128129 msg . textContent = '订单提交成功!总计 ¥' + total + ',感谢 ' + ( username || '游客' ) + ' 的购买!' ;
129130 msg . className = 'success' ;
130131 cart = [ ] ;
0 commit comments