wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.Text.Json;
using BoardApp.Config;
using BoardApp.Models;
using BoardApp.Services;
using BoardApp.Session;
using Microsoft.AspNetCore.Mvc;
 
namespace BoardApp.Controllers;
 
public sealed class BoardController : Controller
{
    private readonly BoardConfigLoader _cfg;
    private readonly ScenarioService _scenarios;
    private readonly UserScenarioService _userScenarios;
    private readonly WarehouseCalendarService _warehouse;
 
    public BoardController(
        BoardConfigLoader cfg,
        ScenarioService scenarios,
        UserScenarioService userScenarios,
        WarehouseCalendarService warehouse)
    {
        _cfg = cfg;
        _scenarios = scenarios;
        _userScenarios = userScenarios;
        _warehouse = warehouse;
    }
 
    private string BasePath => (Request.PathBase.HasValue ? Request.PathBase.Value! : "/board");
 
    private SessionUser CurrentUser()
        => HttpContext.Session.GetJson<SessionUser>("user") ?? new SessionUser();
 
    [HttpGet("/check/")]
    public IActionResult Check()
    {
        var u = HttpContext.Session.GetJson<SessionUser>("user");
        if (u is not null && !string.IsNullOrWhiteSpace(u.Username))
        {
            return Content("ok", "text/plain");
        }
        return StatusCode(401, "unauthorized");
    }
 
    [HttpGet("/login")]
    public IActionResult Login()
    {
        var u = HttpContext.Session.GetJson<SessionUser>("user");
        if (u is not null && !string.IsNullOrWhiteSpace(u.Username))
        {
            return Redirect(BasePath + "/");
        }
        return View("Login", new LoginViewModel { BasePath = BasePath });
    }
 
    [HttpGet("/")]
    public IActionResult Home()
    {
        var username = CurrentUser().Username;
        var categories = _scenarios.LoadHomeCatalogForUser(username);
        var imgMap = _scenarios.BuildScenarioImageMapForUser(username);
        var vm = new HomeViewModel
        {
            BasePath = BasePath,
            Username = username,
            Categories = categories,
            ScenarioImagesJson = JsonSerializer.Serialize(imgMap)
        };
        return View("Home", vm);
    }
 
    [HttpGet("/config/scenarios")]
    public IActionResult UserScenarios()
    {
        var username = CurrentUser().Username;
        var pages = _cfg.LoadPagesConfig().Pages;
        var userScenarios = _userScenarios.ListUserScenarios(username);
        return View("UserScenarios", new UserScenariosViewModel
        {
            BasePath = BasePath,
            Username = username,
            AllPages = pages,
            UserScenarios = userScenarios,
            StaytimeMin = UserScenarioService.StaytimeMin,
            StaytimeMax = UserScenarioService.StaytimeMax
        });
    }
 
    private static (int year, int month) ShiftMonth(int year, int month, int delta)
    {
        var d = new DateTime(year, month, 1).AddMonths(delta);
        return (d.Year, d.Month);
    }
 
    [HttpGet("/warehouse-calendar")]
    public async Task<IActionResult> WarehouseCalendar([FromQuery] int? year, [FromQuery] int? month)
    {
        var now = DateTime.Now;
        var y = year.HasValue && year.Value >= 2000 && year.Value <= 2100 ? year.Value : now.Year;
        var m = month.HasValue && month.Value >= 1 && month.Value <= 12 ? month.Value : now.Month;
 
        var monthTitle = new DateTime(y, m, 1).ToString("yyyy年M月", System.Globalization.CultureInfo.GetCultureInfo("zh-CN"));
        var prev = ShiftMonth(y, m, -1);
        var next = ShiftMonth(y, m, 1);
 
        var todayKey = WarehouseCalendarService.FormatDateKey(now.Year, now.Month, now.Day);
        var vm = new WarehouseCalendarViewModel
        {
            BasePath = BasePath,
            Username = CurrentUser().Username,
            Year = y,
            Month = m,
            MonthTitle = monthTitle,
            TodayKey = todayKey,
            PrevYear = prev.year,
            PrevMonth = prev.month,
            NextYear = next.year,
            NextMonth = next.month,
            WeekdayLabels = new List<string> { "一", "二", "三", "四", "五", "六", "日" }
        };
 
        try
        {
            var plans = await _warehouse.LoadWarehousePlansGroupedByDate(y, m);
            vm.Rows = WarehouseCalendarService.BuildMonthRows(y, m, plans);
            vm.DataError = null;
            return View("WarehouseCalendar", vm);
        }
        catch (Exception ex)
        {
            vm.Rows = WarehouseCalendarService.BuildMonthRows(y, m, new Dictionary<string, List<WarehousePlanEntry>>());
            vm.DataError = $"数据加载失败:{ex.Message}";
            Response.StatusCode = 503;
            return View("WarehouseCalendar", vm);
        }
    }
 
    private ScenarioViewModel? BuildScenarioViewSlides(string username, string scenarioId, string pageIdHint)
    {
        var appCfg = HttpContext.Items["BoardAppConfig"] as AppConfig ?? _cfg.LoadAppConfig();
        var resolved = _scenarios.ResolveScenarioForUser(username, scenarioId);
        if (resolved is null) return null;
 
        var sc = resolved.Scenario;
        var slides = sc.Pages.Select(p => new ViewSlide
        {
            Id = p.Id,
            Title = p.Title,
            Staytime = p.Staytime > 0 ? p.Staytime : 30,
            BoardUrl = BoardUrlResolver.ResolveBoardUrl(appCfg.DataEasePublicBase, p.BoardUrl)
        }).ToList();
 
        var startIndex = 0;
        if (!string.IsNullOrWhiteSpace(pageIdHint))
        {
            var idx = slides.FindIndex(p => string.Equals(p.Id, pageIdHint, StringComparison.Ordinal));
            if (idx >= 0) startIndex = idx;
        }
 
        return new ScenarioViewModel
        {
            BasePath = BasePath,
            DocumentTitle = sc.Name,
            ScenarioId = sc.Id,
            ScenarioName = sc.Name,
            ScenarioImage = sc.Image,
            Slides = slides,
            SlidesJson = JsonSerializer.Serialize(slides),
            StartIndex = startIndex
        };
    }
 
    [HttpGet("/view")]
    public IActionResult ViewScenario([FromQuery] string scenarioId, [FromQuery] string? pageId)
    {
        var username = CurrentUser().Username;
        var data = BuildScenarioViewSlides(username, scenarioId ?? "", pageId ?? "");
        if (data is null) return NotFound("场景不存在");
        return View("View", data);
    }
 
    [HttpGet("/view-i")]
    public IActionResult ViewScenarioI([FromQuery] string scenarioId, [FromQuery] string? pageId)
    {
        var username = CurrentUser().Username;
        var data = BuildScenarioViewSlides(username, scenarioId ?? "", pageId ?? "");
        if (data is null) return NotFound("场景不存在");
        return View("ViewI", data);
    }
 
    [HttpGet("/view-tabs")]
    public IActionResult ViewTabsCompat()
    {
        var q = Request.QueryString.HasValue ? Request.QueryString.Value : "";
        return Redirect(BasePath + "/view" + (q ?? ""));
    }
}