feat(user): 用户系统重做——人机验证、30 天签到、主题色购买与退款、人品值
存储 - 目录改为 .GC/user/<用户名>/pw.yaml(用户名、积分、sha512 口令 + 盐) - .GC/user/user.yaml 记住当前登录(用户名 + session token),启动自动登录 - 自动迁移旧的 .GC/user/<用户名>.yaml 注册 / 登录 - 新增算术人机验证:GET /api/user/captcha(5 分钟有效、一次性、用后即焚) - 用户名不可重复、不能含空格、1–20 字符 - 口令只存 sha512(密码 + ":" + 随机盐),不存明文 - 新增 /api/user/logout 签到 - 30 天一循环:1–6 天 +10,第 7 天 +30,8–13 +15,第 14 天 +40, 15–20 +20,第 21 天 +50,22–27 +25,第 28 天 +60,29 天 +40,第 30 天 +100 - 断签重置连续天数;接口返回 cycle_day / next_reward / today_signed 主题色 - 默认蓝免费,预置色 50,自定义色 150(服务端算价,不信客户端) - 购买后 1 小时内换色退 80%,record 里存 theme_paid / theme_paid_at - 禁止充值:积分只能签到获得 前端用户页 - 账号卡片:积分、连续天数、周期进度条、一键签到、退出登录 - 注册表单内嵌验证码(换一题、失败自动刷新) - 人品值:0–100 随机 + 评语,每天不限次数 - 二选一:两个输入框随机选一个 - 主题色:8 个预置色 + 自定义色取色(十六进制 + 预览),积分不足上锁 - 应用主题色时同步覆盖全局强调色
This commit is contained in:
1 parent
e7c1429889
commit
ecb71ad347
7 files changed
+1419
-463
No files matched your search
@@ -514,46 +514,11 @@ class GcApi {
|
||||
/// 是否拿到了会话令牌(诊断用)。
|
||||
bool get hasSessionToken => _sessionToken != null;
|
||||
|
||||
/// 用户 session token(登录时后端返回,持久化到 ~/.GC/user/user.yaml)
|
||||
/// 用户 session token:登录时后端返回,只放内存。
|
||||
/// 持久化由后端负责(写 .GC/user/user.yaml),下次启动后端会用它自动登录,
|
||||
/// 前端不需要自己去猜 .GC 在哪。
|
||||
static String? _userToken;
|
||||
static String? _username;
|
||||
|
||||
static String? get userToken => _userToken;
|
||||
static String? get username => _username;
|
||||
|
||||
static Future<void> _loadUserToken() async {
|
||||
try {
|
||||
final dir = Directory('${Platform.environment['HOME'] ?? '.'}/.GC/user');
|
||||
if (!dir.existsSync()) return;
|
||||
final file = File('${dir.path}/user.yaml');
|
||||
if (!file.existsSync()) return;
|
||||
final lines = file.readAsLinesSync();
|
||||
for (var line in lines) {
|
||||
if (line.startsWith('username:')) _username = line.split(':').skip(1).join(':').trim();
|
||||
if (line.startsWith('session_token:')) _userToken = line.split(':').skip(1).join(':').trim();
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
static Future<void> _saveUserToken(String username, String token) async {
|
||||
try {
|
||||
final dir = Directory('${Platform.environment['HOME'] ?? '.'}/.GC/user');
|
||||
dir.createSync(recursive: true);
|
||||
final file = File('${dir.path}/user.yaml');
|
||||
file.writeAsStringSync('username: $username\nsession_token: $token\n');
|
||||
_username = username;
|
||||
_userToken = token;
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
static Future<void> clearUserToken() async {
|
||||
_username = null;
|
||||
_userToken = null;
|
||||
try {
|
||||
final file = File('${Platform.environment['HOME'] ?? '.'}/.GC/user/user.yaml');
|
||||
if (file.existsSync()) file.deleteSync();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 带会话令牌的请求头;没有令牌时返回 const 空 map(调试/独立后端模式)。
|
||||
Map<String, String> get _authHeaders => {
|
||||
@@ -705,27 +670,34 @@ class GcApi {
|
||||
|
||||
// ==================== 用户系统 ====================
|
||||
|
||||
/// 初始化:加载本地保存的用户token
|
||||
Future<void> initUserSession() async {
|
||||
await _loadUserToken();
|
||||
/// 取一个人机验证题(算术题,5 分钟内有效、一次性)。
|
||||
Future<CaptchaInfo> captcha() async {
|
||||
final r = await _send('GET', '/api/user/captcha');
|
||||
return CaptchaInfo.fromJson(r as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
/// 当前登录用户;未登录返回 null(后端会用 .GC/user/user.yaml 自动登录)。
|
||||
Future<Map<String, dynamic>?> me() async {
|
||||
try {
|
||||
final r = await _send('GET', '/api/user/me');
|
||||
return r as Map<String, dynamic>?;
|
||||
} on ApiException catch (e) {
|
||||
if (e.statusCode == 401 || e.statusCode == 403) {
|
||||
await clearUserToken();
|
||||
_userToken = null;
|
||||
return null;
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> register(String username, String password) async {
|
||||
final r = await _send('POST', '/api/user/register',
|
||||
body: {'username': username, 'password': password});
|
||||
Future<Map<String, dynamic>> register(String username, String password,
|
||||
{required String captchaId, required String captchaAnswer}) async {
|
||||
final r = await _send('POST', '/api/user/register', body: {
|
||||
'username': username,
|
||||
'password': password,
|
||||
'captchaId': captchaId,
|
||||
'captcha': captchaAnswer,
|
||||
});
|
||||
return r as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
@@ -733,15 +705,18 @@ class GcApi {
|
||||
final r = await _send('POST', '/api/user/login',
|
||||
body: {'username': username, 'password': password});
|
||||
final result = r as Map<String, dynamic>;
|
||||
// 保存 session token
|
||||
if (result.containsKey('session_token')) {
|
||||
await _saveUserToken(username, result['session_token'] as String);
|
||||
}
|
||||
_userToken = result['session_token'] as String?;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// 退出登录:清掉后端的 user.yaml 与内存 token。
|
||||
Future<void> logout() async {
|
||||
await clearUserToken();
|
||||
try {
|
||||
await _send('POST', '/api/user/logout');
|
||||
} catch (_) {
|
||||
// 后端连不上也要让界面退出
|
||||
}
|
||||
_userToken = null;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> signin() async {
|
||||
@@ -749,9 +724,22 @@ class GcApi {
|
||||
return r as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> changeTheme(String color, int cost) async {
|
||||
final r = await _send('POST', '/api/user/theme',
|
||||
body: {'color': color, 'cost': cost});
|
||||
/// 换主题色:只传颜色,价格由后端判定(预置 50 / 自定义 150)。
|
||||
Future<Map<String, dynamic>> changeTheme(String color) async {
|
||||
final r = await _send('POST', '/api/user/theme', body: {'color': color});
|
||||
return r as Map<String, dynamic>;
|
||||
}
|
||||
}
|
||||
|
||||
/// 人机验证题
|
||||
class CaptchaInfo {
|
||||
const CaptchaInfo({required this.id, required this.question});
|
||||
|
||||
final String id;
|
||||
final String question;
|
||||
|
||||
factory CaptchaInfo.fromJson(Map<String, dynamic> json) => CaptchaInfo(
|
||||
id: json['id'] as String? ?? '',
|
||||
question: json['question'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
@@ -160,7 +160,7 @@ class I18n {
|
||||
'user.login': '登录',
|
||||
'user.register': '注册',
|
||||
'user.pick': '二选一',
|
||||
'user.theme': '主题颜色',
|
||||
'user.themeColor': '主题颜色',
|
||||
'user.username': '用户名',
|
||||
'user.password': '密码',
|
||||
'user.option1': '选项一',
|
||||
@@ -175,6 +175,40 @@ class I18n {
|
||||
'user.fillAll': '请填写用户名和密码',
|
||||
'user.fillOptions': '请填写两个选项',
|
||||
'user.loginFirst': '请先登录',
|
||||
'user.notLoggedIn': '未登录',
|
||||
'user.loginHint': '登录后可签到攒积分、解锁主题色',
|
||||
'user.logout': '退出登录',
|
||||
'user.logoutDone': '已退出登录',
|
||||
'user.cycleDay': '第 {0} / {1} 天',
|
||||
'user.signedToday': '今天已签到,明天再来',
|
||||
'user.signedTodayShort': '已签到',
|
||||
'user.nextReward': '现在签到可得 {0} 积分',
|
||||
'user.refunded': '已退回',
|
||||
'user.usernameRule': '1–20 个字符,不能有空格,不能与他人重复',
|
||||
'user.captcha': '人机验证',
|
||||
'user.captchaMissing': '请先获取人机验证题',
|
||||
'user.captchaAnswer': '验证码答案',
|
||||
'user.fillCaptcha': '请填写验证码答案',
|
||||
'user.refreshCaptcha': '换一题',
|
||||
'user.noRecharge': '积分只能通过每日签到获得,不提供任何充值渠道。',
|
||||
'user.fortune': '人品值',
|
||||
'user.fortuneDesc': '每天可以无限次计算,纯属娱乐,不影响积分。',
|
||||
'user.fortuneIdle': '点一下,看看今天手气',
|
||||
'user.fortuneRoll': '开始计算',
|
||||
'user.fortuneAgain': '再算一次',
|
||||
'user.pickDesc': '填两个选项,随机帮你选一个。',
|
||||
'user.pickResult': '结果:',
|
||||
'user.themeDesc': '默认蓝色免费;预置色 50 积分一个,自定义色 150 积分一个。',
|
||||
'user.custom': '自定义',
|
||||
'user.customColor': '自定义颜色',
|
||||
'user.customColorCost': '自定义颜色需要 {0} 积分。',
|
||||
'user.confirmBuy': '购买并应用',
|
||||
'user.hexInvalid': '格式应为 #RRGGBB',
|
||||
'user.free': '免费',
|
||||
'user.themeUpdated': '主题色已更新',
|
||||
'user.refundHint': '1 小时内换色可退 80%,当前可退(剩余 {0})',
|
||||
'user.minutes': '分钟',
|
||||
'user.seconds': '秒',
|
||||
},
|
||||
'en': <String, String>{
|
||||
'app.name': 'Morris GC',
|
||||
@@ -327,7 +361,7 @@ class I18n {
|
||||
'user.login': 'Login',
|
||||
'user.register': 'Register',
|
||||
'user.pick': 'Pick One',
|
||||
'user.theme': 'Theme Color',
|
||||
'user.themeColor': 'Theme Color',
|
||||
'user.username': 'Username',
|
||||
'user.password': 'Password',
|
||||
'user.option1': 'Option A',
|
||||
@@ -342,6 +376,40 @@ class I18n {
|
||||
'user.fillAll': 'Please fill in username and password',
|
||||
'user.fillOptions': 'Please fill in both options',
|
||||
'user.loginFirst': 'Please login first',
|
||||
'user.notLoggedIn': 'Not signed in',
|
||||
'user.loginHint': 'Sign in to earn points and unlock theme colors',
|
||||
'user.logout': 'Sign out',
|
||||
'user.logoutDone': 'Signed out',
|
||||
'user.cycleDay': 'Day {0} / {1}',
|
||||
'user.signedToday': 'Signed in today, come back tomorrow',
|
||||
'user.signedTodayShort': 'Signed',
|
||||
'user.nextReward': 'Sign in now for {0} points',
|
||||
'user.refunded': 'refunded',
|
||||
'user.usernameRule': '1–20 chars, no spaces, must be unique',
|
||||
'user.captcha': 'Verification',
|
||||
'user.captchaMissing': 'Please load the verification question first',
|
||||
'user.captchaAnswer': 'Answer',
|
||||
'user.fillCaptcha': 'Please answer the verification question',
|
||||
'user.refreshCaptcha': 'New question',
|
||||
'user.noRecharge': 'Points come from daily check-ins only. There is no way to buy points.',
|
||||
'user.fortune': 'Luck Meter',
|
||||
'user.fortuneDesc': 'Unlimited rolls per day. Just for fun — no points involved.',
|
||||
'user.fortuneIdle': 'Tap to see how lucky you are today',
|
||||
'user.fortuneRoll': 'Roll',
|
||||
'user.fortuneAgain': 'Roll again',
|
||||
'user.pickDesc': 'Two options, one random pick.',
|
||||
'user.pickResult': 'Result:',
|
||||
'user.themeDesc': 'Default blue is free; preset colors cost 50, custom colors 150.',
|
||||
'user.custom': 'Custom',
|
||||
'user.customColor': 'Custom color',
|
||||
'user.customColorCost': 'A custom color costs {0} points.',
|
||||
'user.confirmBuy': 'Buy & apply',
|
||||
'user.hexInvalid': 'Format must be #RRGGBB',
|
||||
'user.free': 'Free',
|
||||
'user.themeUpdated': 'Theme color updated',
|
||||
'user.refundHint': 'Change within 1 hour for a 80% refund (left {0})',
|
||||
'user.minutes': 'min',
|
||||
'user.seconds': 's',
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,47 +1,95 @@
|
||||
/// 用户记录
|
||||
/// 用户记录(对应后端 UserPayload)
|
||||
class UserRecord {
|
||||
const UserRecord({
|
||||
required this.username,
|
||||
required this.points,
|
||||
required this.consecutiveDays,
|
||||
required this.cycleDay,
|
||||
required this.cycleDays,
|
||||
required this.todaySigned,
|
||||
required this.nextReward,
|
||||
this.lastSignIn,
|
||||
this.themeColor,
|
||||
this.themePaid = 0,
|
||||
this.refundSeconds = 0,
|
||||
});
|
||||
|
||||
final String username;
|
||||
final int points;
|
||||
|
||||
/// 连续签到天数(断签会重置)
|
||||
final int consecutiveDays;
|
||||
|
||||
/// 当前处在 30 天周期的第几天(1–30)
|
||||
final int cycleDay;
|
||||
|
||||
/// 周期长度,固定 30
|
||||
final int cycleDays;
|
||||
|
||||
/// 今天是否已签到
|
||||
final bool todaySigned;
|
||||
|
||||
/// 下一次签到能拿多少分
|
||||
final int nextReward;
|
||||
|
||||
final String? lastSignIn;
|
||||
|
||||
/// 形如 `4A90D9`(不带 #)
|
||||
final String? themeColor;
|
||||
|
||||
/// 当前这个颜色实际花了多少分
|
||||
final int themePaid;
|
||||
|
||||
/// 退款窗口剩余秒数(0 表示没有可退的)
|
||||
final int refundSeconds;
|
||||
|
||||
factory UserRecord.fromJson(Map<String, dynamic> json) {
|
||||
return UserRecord(
|
||||
username: json['username'] as String? ?? '',
|
||||
points: json['points'] as int? ?? 0,
|
||||
consecutiveDays: json['consecutive_days'] as int? ?? 0,
|
||||
points: (json['points'] as num?)?.toInt() ?? 0,
|
||||
consecutiveDays: (json['consecutive_days'] as num?)?.toInt() ?? 0,
|
||||
cycleDay: (json['cycle_day'] as num?)?.toInt() ?? 1,
|
||||
cycleDays: (json['cycle_days'] as num?)?.toInt() ?? 30,
|
||||
todaySigned: json['today_signed'] as bool? ?? false,
|
||||
nextReward: (json['next_reward'] as num?)?.toInt() ?? 10,
|
||||
lastSignIn: json['last_sign_in'] as String?,
|
||||
themeColor: json['theme_color'] as String?,
|
||||
themePaid: (json['theme_paid'] as num?)?.toInt() ?? 0,
|
||||
refundSeconds: (json['refund_seconds'] as num?)?.toInt() ?? 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 签到奖励配置(30天周期)
|
||||
/// 签到奖励:30 天一循环,第 7/14/21/28 天给周奖励,第 30 天给满月奖励。
|
||||
/// 与后端 `UserService.GetSignInReward` 保持一致。
|
||||
class SignInRewards {
|
||||
static const List<int> rewards = [
|
||||
10, 10, 10, 10, 10, 10, 10, // 第1-7天:每天+10
|
||||
20, 20, 20, 20, 20, 20, 20, // 第8-15天:每天+20
|
||||
30, 30, 30, 30, 30, 30, 30, // 第16-23天:每天+30
|
||||
30, 30, 30, 30, 30, 30, 30, // 第24-30天:每天+30
|
||||
];
|
||||
static const int cycleDays = 30;
|
||||
|
||||
static int getReward(int day) {
|
||||
if (day < 1) day = 1;
|
||||
if (day > 30) day = ((day - 1) % 30) + 1;
|
||||
return rewards[day - 1];
|
||||
var d = day;
|
||||
if (d < 1) d = 1;
|
||||
if (d > cycleDays) d = ((d - 1) % cycleDays) + 1;
|
||||
switch (d) {
|
||||
case 7:
|
||||
return 30;
|
||||
case 14:
|
||||
return 40;
|
||||
case 21:
|
||||
return 50;
|
||||
case 28:
|
||||
return 60;
|
||||
case 30:
|
||||
return 100;
|
||||
}
|
||||
if (d <= 6) return 10;
|
||||
if (d <= 13) return 15;
|
||||
if (d <= 20) return 20;
|
||||
if (d <= 27) return 25;
|
||||
return 40;
|
||||
}
|
||||
}
|
||||
|
||||
/// 主题颜色
|
||||
/// 主题颜色:预置色 50 分一个,自定义色 150 分一个,默认蓝免费。
|
||||
class ThemeColor {
|
||||
const ThemeColor({required this.name, required this.hex, required this.cost});
|
||||
|
||||
@@ -49,14 +97,58 @@ class ThemeColor {
|
||||
final String hex;
|
||||
final int cost;
|
||||
|
||||
static const List<ThemeColor> predefined = [
|
||||
ThemeColor(name: '蓝色', hex: '4A90D9', cost: 0),
|
||||
ThemeColor(name: '红色', hex: 'E74C3C', cost: 50),
|
||||
ThemeColor(name: '绿色', hex: '27AE60', cost: 50),
|
||||
ThemeColor(name: '紫色', hex: '9B59B6', cost: 50),
|
||||
ThemeColor(name: '橙色', hex: 'F39C12', cost: 50),
|
||||
ThemeColor(name: '青色', hex: '1ABC9C', cost: 50),
|
||||
ThemeColor(name: '粉色', hex: 'E91E63', cost: 50),
|
||||
ThemeColor(name: '深蓝', hex: '2C3E50', cost: 50),
|
||||
/// 默认色(免费)
|
||||
static const String defaultHex = '4A90D9';
|
||||
|
||||
/// 预置色单价
|
||||
static const int presetCost = 50;
|
||||
|
||||
/// 自定义色单价
|
||||
static const int customCost = 150;
|
||||
|
||||
/// 购买后 1 小时内换色可退 80%
|
||||
static const Duration refundWindow = Duration(hours: 1);
|
||||
static const double refundRate = 0.8;
|
||||
|
||||
static const List<ThemeColor> predefined = <ThemeColor>[
|
||||
ThemeColor(name: '蓝色', hex: defaultHex, cost: 0),
|
||||
ThemeColor(name: '红色', hex: 'E74C3C', cost: presetCost),
|
||||
ThemeColor(name: '绿色', hex: '27AE60', cost: presetCost),
|
||||
ThemeColor(name: '紫色', hex: '9B59B6', cost: presetCost),
|
||||
ThemeColor(name: '橙色', hex: 'F39C12', cost: presetCost),
|
||||
ThemeColor(name: '青色', hex: '1ABC9C', cost: presetCost),
|
||||
ThemeColor(name: '粉色', hex: 'E91E63', cost: presetCost),
|
||||
ThemeColor(name: '深蓝', hex: '2C3E50', cost: presetCost),
|
||||
];
|
||||
|
||||
/// 某个十六进制颜色的价格;默认蓝 0,预置色 50,其它 150。
|
||||
static int costOf(String hex) {
|
||||
final h = hex.replaceAll('#', '').toUpperCase();
|
||||
if (h == defaultHex) return 0;
|
||||
for (final c in predefined) {
|
||||
if (c.hex.toUpperCase() == h) return presetCost;
|
||||
}
|
||||
return customCost;
|
||||
}
|
||||
}
|
||||
|
||||
/// 人品值结果
|
||||
class FortuneResult {
|
||||
const FortuneResult(this.value, this.comment);
|
||||
|
||||
final int value;
|
||||
final String comment;
|
||||
|
||||
/// 0–100,越高越「人品爆发」
|
||||
static String commentFor(int value) {
|
||||
if (value >= 96) return '欧皇附体,今天买彩票吧';
|
||||
if (value >= 90) return '运气爆棚,适合干大事';
|
||||
if (value >= 80) return '手气不错,稳';
|
||||
if (value >= 65) return '中上水平,稍微努一下';
|
||||
if (value >= 50) return '普普通通,够用';
|
||||
if (value >= 35) return '有点背,低调点';
|
||||
if (value >= 20) return '今天别赌任何东西';
|
||||
if (value >= 8) return '极差,建议躺平';
|
||||
return '非酋之王,明天再来';
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user