wht | 2bf8f80 | 2025-06-08 15:52:18 +0800 | [diff] [blame^] | 1 | import React, { useEffect, useState } from "react"; |
| 2 | import { API_BASE_URL } from "./config"; |
| 3 | |
| 4 | // 示例数据,实际应从后端获取 |
| 5 | const mockSeeds = [ |
| 6 | { |
| 7 | seed_id: "seed001", |
| 8 | title: "三体 1080P 蓝光", |
| 9 | tags: "科幻,电影", |
| 10 | popularity: 123, |
| 11 | promotion: { |
| 12 | start_time: "", |
| 13 | end_time: "", |
| 14 | discount: 1, |
| 15 | }, |
| 16 | }, |
| 17 | { |
| 18 | seed_id: "seed002", |
| 19 | title: "灌篮高手 国语配音", |
| 20 | tags: "动画,体育", |
| 21 | popularity: 88, |
| 22 | promotion: { |
| 23 | start_time: "", |
| 24 | end_time: "", |
| 25 | discount: 1, |
| 26 | }, |
| 27 | }, |
| 28 | ]; |
| 29 | |
| 30 | export default function SeedPromotionPage() { |
| 31 | const [seeds, setSeeds] = useState([]); |
| 32 | const [currentTime, setCurrentTime] = useState(""); |
| 33 | |
| 34 | useEffect(() => { |
| 35 | // 获取当前时间,格式为 yyyy-MM-ddTHH:mm |
| 36 | const now = new Date(); |
| 37 | const pad = (n) => n.toString().padStart(2, "0"); |
| 38 | const localISOTime = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}T${pad(now.getHours())}:${pad(now.getMinutes())}`; |
| 39 | setCurrentTime(localISOTime); |
| 40 | |
| 41 | // 实际应从后端获取种子及促销信息 |
| 42 | setSeeds(mockSeeds); |
| 43 | }, []); |
| 44 | |
| 45 | // 输入框变更处理 |
| 46 | const handlePromotionChange = (seedId, field, value) => { |
| 47 | setSeeds((prev) => |
| 48 | prev.map((s) => |
| 49 | s.seed_id === seedId |
| 50 | ? { |
| 51 | ...s, |
| 52 | promotion: { |
| 53 | ...s.promotion, |
| 54 | [field]: value, |
| 55 | }, |
| 56 | } |
| 57 | : s |
| 58 | ) |
| 59 | ); |
| 60 | }; |
| 61 | |
| 62 | // 结束时间校验 |
| 63 | const isEndTimeInvalid = (start, end) => { |
| 64 | return start && end && end < start; |
| 65 | }; |
| 66 | |
| 67 | return ( |
| 68 | <div style={{ padding: 40, maxWidth: 900, margin: "0 auto" }}> |
| 69 | <h1 style={{ textAlign: "center", marginBottom: 32 }}>种子促销管理</h1> |
| 70 | <table style={{ width: "100%", background: "#fff", borderRadius: 10, boxShadow: "0 2px 8px #e0e7ff" }}> |
| 71 | <thead> |
| 72 | <tr style={{ background: "#f5f5f5" }}> |
| 73 | <th>标题</th> |
| 74 | <th>标签</th> |
| 75 | <th>热度</th> |
| 76 | <th>促销开始时间</th> |
| 77 | <th>促销结束时间</th> |
| 78 | <th>促销倍率</th> |
| 79 | <th>操作</th> |
| 80 | </tr> |
| 81 | </thead> |
| 82 | <tbody> |
| 83 | {seeds.map((seed) => { |
| 84 | const { start_time, end_time, discount } = seed.promotion; |
| 85 | const endTimeInvalid = isEndTimeInvalid(start_time, end_time); |
| 86 | const canStartPromotion = start_time && end_time && !endTimeInvalid && discount >= 1; |
| 87 | return ( |
| 88 | <tr key={seed.seed_id}> |
| 89 | <td>{seed.title}</td> |
| 90 | <td>{seed.tags}</td> |
| 91 | <td>{seed.popularity}</td> |
| 92 | <td> |
| 93 | <input |
| 94 | type="datetime-local" |
| 95 | value={start_time} |
| 96 | min={currentTime} |
| 97 | onChange={(e) => |
| 98 | handlePromotionChange(seed.seed_id, "start_time", e.target.value) |
| 99 | } |
| 100 | /> |
| 101 | </td> |
| 102 | <td> |
| 103 | <input |
| 104 | type="datetime-local" |
| 105 | value={end_time} |
| 106 | min={start_time || currentTime} |
| 107 | onChange={(e) => |
| 108 | handlePromotionChange(seed.seed_id, "end_time", e.target.value) |
| 109 | } |
| 110 | style={endTimeInvalid ? { border: "1.5px solid #e53935" } : {}} |
| 111 | /> |
| 112 | {endTimeInvalid && ( |
| 113 | <div style={{ color: "#e53935", fontSize: 12 }}> |
| 114 | 结束时间不能早于开始时间 |
| 115 | </div> |
| 116 | )} |
| 117 | </td> |
| 118 | <td> |
| 119 | <div style={{ display: "flex", alignItems: "center", gap: 8 }}> |
| 120 | <button |
| 121 | style={{ width: 28, height: 28, fontSize: 18, borderRadius: 4, border: "1px solid #ccc", background: "#f5f5f5", cursor: discount > 1 ? "pointer" : "not-allowed" }} |
| 122 | onClick={() => |
| 123 | discount > 1 && |
| 124 | handlePromotionChange(seed.seed_id, "discount", discount - 1) |
| 125 | } |
| 126 | disabled={discount <= 1} |
| 127 | >-</button> |
| 128 | <span style={{ minWidth: 24, textAlign: "center" }}>{discount}</span> |
| 129 | <button |
| 130 | style={{ width: 28, height: 28, fontSize: 18, borderRadius: 4, border: "1px solid #ccc", background: "#f5f5f5", cursor: "pointer" }} |
| 131 | onClick={() => |
| 132 | handlePromotionChange(seed.seed_id, "discount", discount + 1) |
| 133 | } |
| 134 | >+</button> |
| 135 | </div> |
| 136 | </td> |
| 137 | <td> |
| 138 | <button |
| 139 | style={{ |
| 140 | background: canStartPromotion ? "#1976d2" : "#ccc", |
| 141 | color: "#fff", |
| 142 | border: "none", |
| 143 | borderRadius: 6, |
| 144 | padding: "4px 16px", |
| 145 | cursor: canStartPromotion ? "pointer" : "not-allowed", |
| 146 | fontWeight: 600, |
| 147 | }} |
| 148 | disabled={!canStartPromotion} |
| 149 | onClick={() => { |
| 150 | // 这里可调用后端API开启促销 |
| 151 | alert(`已为「${seed.title}」开启促销!`); |
| 152 | }} |
| 153 | > |
| 154 | 开启促销 |
| 155 | </button> |
| 156 | </td> |
| 157 | </tr> |
| 158 | ); |
| 159 | })} |
| 160 | </tbody> |
| 161 | </table> |
| 162 | </div> |
| 163 | ); |
| 164 | } |