21301050 | 01e07a1 | 2025-06-09 18:00:40 +0800 | [diff] [blame^] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import axios from 'axios'; |
| 3 | import { message, Button } from 'antd'; |
| 4 | import { ArrowLeftOutlined } from '@ant-design/icons'; |
| 5 | import { useNavigate } from 'react-router-dom'; |
| 6 | |
| 7 | const ShopPage = () => { |
| 8 | const navigate = useNavigate(); |
| 9 | const [user, setUser] = useState(null); |
| 10 | const [loading, setLoading] = useState(false); |
| 11 | const [shopItems, setShopItems] = useState([ |
| 12 | { |
| 13 | id: 1, |
| 14 | type: 'decoration', |
| 15 | name: '头像框', |
| 16 | description: '好看的头像框可以让别人更好地记住你', |
| 17 | price: 100, |
| 18 | image: 'https://img.icons8.com/?size=100&id=11730&format=png&color=000000' |
| 19 | }, |
| 20 | { |
| 21 | id: 2, |
| 22 | type: 'decoration', |
| 23 | name: '对话框', |
| 24 | description: '闪亮的对话框,让您的消息更醒目', |
| 25 | price: 150, |
| 26 | image: 'https://img.icons8.com/?size=100&id=143&format=png&color=000000' |
| 27 | }, |
| 28 | { |
| 29 | id: 3, |
| 30 | type: 'upload', |
| 31 | name: '上传量+100MB', |
| 32 | description: '增加100MB的上传量', |
| 33 | price: 50, |
| 34 | image: 'https://img.icons8.com/?size=100&id=368&format=png&color=000000' |
| 35 | }, |
| 36 | { |
| 37 | id: 4, |
| 38 | type: 'invite', |
| 39 | name: '邀请码', |
| 40 | description: '一个邀请码,用于邀请好友加入', |
| 41 | price: 200, |
| 42 | image: 'https://img.icons8.com/?size=100&id=5465&format=png&color=000000' |
| 43 | } |
| 44 | ]); |
| 45 | |
| 46 | useEffect(() => { |
| 47 | // 从localStorage获取用户信息 |
| 48 | const userData = localStorage.getItem('user'); |
| 49 | if (userData) { |
| 50 | setUser(JSON.parse(userData)); |
| 51 | } |
| 52 | }, []); |
| 53 | |
| 54 | const handlePurchase = async (item) => { |
| 55 | if (!user) { |
| 56 | message.error('请先登录'); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | setLoading(true); |
| 61 | try { |
| 62 | let response; |
| 63 | const username = user.username; |
| 64 | |
| 65 | switch (item.type) { |
| 66 | case 'decoration': |
| 67 | response = await axios.post('http://localhost:8080/shop/soldDecoration', null, { |
| 68 | params: { |
| 69 | buyername: username, |
| 70 | decoration: item.name, |
| 71 | price: item.price |
| 72 | } |
| 73 | }); |
| 74 | break; |
| 75 | case 'upload': |
| 76 | response = await axios.post('http://localhost:8080/shop/soldUpload', null, { |
| 77 | params: { |
| 78 | buyername: username, |
| 79 | price: item.price, |
| 80 | upload: 100 // 假设每次购买增加100MB |
| 81 | } |
| 82 | }); |
| 83 | break; |
| 84 | case 'invite': |
| 85 | response = await axios.post('http://localhost:8080/shop/soldInvite', null, { |
| 86 | params: { |
| 87 | buyername: username, |
| 88 | price: item.price |
| 89 | } |
| 90 | }); |
| 91 | break; |
| 92 | default: |
| 93 | throw new Error('未知的商品类型'); |
| 94 | } |
| 95 | |
| 96 | if (response.data && response.data.success) { |
| 97 | message.success('购买成功!'); |
| 98 | // 更新用户信息 |
| 99 | user.credit -= item.price; |
| 100 | if(item.type=='decoration'){ |
| 101 | user.decoration= user.decoration+" "+item.name; |
| 102 | } |
| 103 | localStorage.setItem('user', JSON.stringify(user)); |
| 104 | setUser(user); |
| 105 | } else { |
| 106 | message.error(response.data.message || '购买失败'); |
| 107 | } |
| 108 | } catch (error) { |
| 109 | console.error('购买出错:', error); |
| 110 | message.error(error.response?.data?.message || '购买过程中出错'); |
| 111 | } finally { |
| 112 | setLoading(false); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | if (!user) { |
| 117 | return ( |
| 118 | <div style={{ padding: '20px', textAlign: 'center' }}> |
| 119 | <h2>请先登录后再访问商城</h2> |
| 120 | </div> |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | return ( |
| 125 | <div className="shop-container" style={{ padding: '20px', maxWidth: '1200px', margin: '0 auto' }}> |
| 126 | {/* 添加返回按钮 */} |
| 127 | <Button |
| 128 | type="text" |
| 129 | icon={<ArrowLeftOutlined />} |
| 130 | onClick={() => navigate(-1)} |
| 131 | style={{ marginBottom: '20px', paddingLeft: 0 }} |
| 132 | > |
| 133 | 返回 |
| 134 | </Button> |
| 135 | |
| 136 | <h1 style={{ textAlign: 'center', marginBottom: '30px' }}>商城</h1> |
| 137 | <div style={{ marginBottom: '20px', display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}> |
| 138 | <div> |
| 139 | <h3>当前用户: {user.username}</h3> |
| 140 | <p>余额: {user.credit || 0} 积分</p> |
| 141 | </div> |
| 142 | </div> |
| 143 | |
| 144 | <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(280px, 1fr))', gap: '20px' }}> |
| 145 | {shopItems.map((item) => ( |
| 146 | <div key={item.id} style={{ |
| 147 | border: '1px solid #ddd', |
| 148 | borderRadius: '8px', |
| 149 | padding: '15px', |
| 150 | display: 'flex', |
| 151 | flexDirection: 'column', |
| 152 | boxShadow: '0 2px 8px rgba(0,0,0,0.1)' |
| 153 | }}> |
| 154 | <div style={{ height: '150px', backgroundColor: '#f5f5f5', display: 'flex', justifyContent: 'center', alignItems: 'center', marginBottom: '15px' }}> |
| 155 | {item.image ? ( |
| 156 | <img src={item.image} alt={item.name} style={{ maxWidth: '100%', maxHeight: '100%' }} /> |
| 157 | ) : ( |
| 158 | <span>商品图片</span> |
| 159 | )} |
| 160 | </div> |
| 161 | <h3 style={{ margin: '0 0 10px 0' }}>{item.name}</h3> |
| 162 | <p style={{ color: '#666', flexGrow: 1 }}>{item.description}</p> |
| 163 | <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: '15px' }}> |
| 164 | <span style={{ fontSize: '18px', fontWeight: 'bold', color: '#f56c6c' }}>{item.price} 积分</span> |
| 165 | <button |
| 166 | onClick={() => handlePurchase(item)} |
| 167 | disabled={loading || (user.credit < item.price)} |
| 168 | style={{ |
| 169 | padding: '8px 16px', |
| 170 | backgroundColor: user.credit >= item.price ? '#1890ff' : '#ccc', |
| 171 | color: 'white', |
| 172 | border: 'none', |
| 173 | borderRadius: '4px', |
| 174 | cursor: user.credit >= item.price ? 'pointer' : 'not-allowed' |
| 175 | }} |
| 176 | > |
| 177 | {loading ? '处理中...' : '购买'} |
| 178 | </button> |
| 179 | </div> |
| 180 | </div> |
| 181 | ))} |
| 182 | </div> |
| 183 | </div> |
| 184 | ); |
| 185 | }; |
| 186 | |
| 187 | export default ShopPage; |