feat: 对齐常驻商城与七鱼客服交互
- 同步商城 H5 登录态与原生 token - 完善商城/原生 tab 切换、动画及返回交互 - 修复七鱼客服键盘避让和状态栏恢复 - 调整开屏跳过按钮与登录入口交互
This commit is contained in:
1 parent
b4343ea528
commit
3b089e2205
37 files changed
+2970
-375
No files matched your search
@@ -6,6 +6,7 @@ import '../../../common/storage/storage_keys.dart';
|
||||
import '../../../common/storage/storage_service.dart';
|
||||
import '../../../common/network/dio_provider.dart';
|
||||
import '../data/auth_models.dart';
|
||||
import '../data/auth_api.dart';
|
||||
import '../data/one_key_login_api.dart';
|
||||
|
||||
final authProvider =
|
||||
@@ -40,21 +41,21 @@ class AuthController extends StateNotifier<AsyncValue<AuthState>> {
|
||||
}
|
||||
|
||||
Future<void> sendCode(String phone) async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 200));
|
||||
await AuthApi(_dio).sendSmsCode(phone);
|
||||
}
|
||||
|
||||
Future<void> verifyCode(String phone, String code) async {
|
||||
final token = 'mock-jinzhi-token-$phone-$code';
|
||||
await saveJwt(token);
|
||||
await StorageService.to.setString(storageJinzhiMockLoggedIn, phone);
|
||||
final reply = await AuthApi(_dio).verifySmsCode(phone, code);
|
||||
await saveJwt(reply.accessToken);
|
||||
await StorageService.to.setString(storageJinzhiMockLoggedIn, reply.phone);
|
||||
state = AsyncData(
|
||||
LoggedInAuthState(
|
||||
user: JinzhiUser(
|
||||
phone: phone,
|
||||
phone: reply.phone,
|
||||
nickname: '金值用户',
|
||||
createdAt: DateTime.now(),
|
||||
),
|
||||
token: token,
|
||||
token: reply.accessToken,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../common/network/dio_provider.dart';
|
||||
|
||||
/// 金值账号服务的 protobuf HTTP 契约。
|
||||
///
|
||||
/// 这个轻量编解码只覆盖短信登录需要的字段,避免把整套历史生成模型带入
|
||||
/// 当前重建中的应用;字段号与金值 `account.proto` 保持一致。
|
||||
class AuthApi {
|
||||
const AuthApi(this._dio);
|
||||
|
||||
final Dio _dio;
|
||||
|
||||
Future<int> sendSmsCode(String phone) async {
|
||||
final response = await _dio.post<List<int>>(
|
||||
'/api/v1/auth/sms/send',
|
||||
data: _ProtoWriter.stringField(1, phone),
|
||||
options: protobufOptions(),
|
||||
);
|
||||
return _ProtoMessage(response.data ?? const <int>[]).integer(1);
|
||||
}
|
||||
|
||||
Future<AuthLoginResult> verifySmsCode(String phone, String code) async {
|
||||
final response = await _dio.post<List<int>>(
|
||||
'/api/v1/auth/sms/verify',
|
||||
data: [
|
||||
..._ProtoWriter.stringField(1, phone),
|
||||
..._ProtoWriter.stringField(2, code),
|
||||
],
|
||||
options: protobufOptions(),
|
||||
);
|
||||
final reply = _ProtoMessage(response.data ?? const <int>[]);
|
||||
final accessToken = reply.string(1).trim();
|
||||
if (accessToken.isEmpty) {
|
||||
throw const FormatException('登录响应缺少 access_token');
|
||||
}
|
||||
final user = _ProtoMessage(reply.bytes(3));
|
||||
return AuthLoginResult(
|
||||
accessToken: accessToken,
|
||||
phone: user.string(2).trim().isEmpty ? phone : user.string(2).trim(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AuthLoginResult {
|
||||
const AuthLoginResult({required this.accessToken, required this.phone});
|
||||
|
||||
final String accessToken;
|
||||
final String phone;
|
||||
}
|
||||
|
||||
class _ProtoWriter {
|
||||
static List<int> stringField(int field, String value) => [
|
||||
..._varint(field << 3 | 2),
|
||||
..._varint(utf8.encode(value).length),
|
||||
...utf8.encode(value),
|
||||
];
|
||||
|
||||
static List<int> _varint(int value) {
|
||||
final bytes = <int>[];
|
||||
var remaining = value;
|
||||
while (remaining > 0x7f) {
|
||||
bytes.add(remaining & 0x7f | 0x80);
|
||||
remaining >>= 7;
|
||||
}
|
||||
bytes.add(remaining);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
||||
class _ProtoMessage {
|
||||
_ProtoMessage(List<int> bytes) : _fields = _parse(bytes);
|
||||
|
||||
final Map<int, Object> _fields;
|
||||
|
||||
int integer(int field) => _fields[field] is int ? _fields[field]! as int : 0;
|
||||
|
||||
String string(int field) => _fields[field] is List<int>
|
||||
? utf8.decode(_fields[field]! as List<int>, allowMalformed: true)
|
||||
: '';
|
||||
|
||||
List<int> bytes(int field) => _fields[field] is List<int>
|
||||
? _fields[field]! as List<int>
|
||||
: const <int>[];
|
||||
|
||||
static Map<int, Object> _parse(List<int> bytes) {
|
||||
final fields = <int, Object>{};
|
||||
var cursor = 0;
|
||||
while (cursor < bytes.length) {
|
||||
final tag = _readVarint(bytes, cursor);
|
||||
if (tag == null) break;
|
||||
cursor = tag.next;
|
||||
final field = tag.value >> 3;
|
||||
switch (tag.value & 7) {
|
||||
case 0:
|
||||
final value = _readVarint(bytes, cursor);
|
||||
if (value == null) return fields;
|
||||
fields[field] = value.value;
|
||||
cursor = value.next;
|
||||
case 2:
|
||||
final length = _readVarint(bytes, cursor);
|
||||
if (length == null || length.next + length.value > bytes.length) {
|
||||
return fields;
|
||||
}
|
||||
fields[field] = bytes.sublist(
|
||||
length.next,
|
||||
length.next + length.value,
|
||||
);
|
||||
cursor = length.next + length.value;
|
||||
default:
|
||||
return fields;
|
||||
}
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
static _ProtoValue? _readVarint(List<int> bytes, int cursor) {
|
||||
var value = 0;
|
||||
var shift = 0;
|
||||
while (cursor < bytes.length && shift <= 63) {
|
||||
final byte = bytes[cursor++];
|
||||
value |= (byte & 0x7f) << shift;
|
||||
if (byte & 0x80 == 0) return _ProtoValue(value, cursor);
|
||||
shift += 7;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class _ProtoValue {
|
||||
const _ProtoValue(this.value, this.next);
|
||||
|
||||
final int value;
|
||||
final int next;
|
||||
}
|
||||
@@ -16,7 +16,6 @@ import '../../../common/router/app_router.dart';
|
||||
import '../../../common/theme/spacing.dart';
|
||||
import '../../../common/widgets/circle_back_button.dart';
|
||||
import '../application/auth_controller.dart';
|
||||
import '../application/login_launcher.dart';
|
||||
|
||||
class LoginPage extends HookConsumerWidget {
|
||||
const LoginPage({super.key, this.next});
|
||||
@@ -32,7 +31,6 @@ class LoginPage extends HookConsumerWidget {
|
||||
final agreed = useState(false);
|
||||
final sending = useState(false);
|
||||
final verifying = useState(false);
|
||||
final oneKeyLoading = useState(false);
|
||||
final countdown = useState(0);
|
||||
final error = useState<String?>(null);
|
||||
final timer = useRef<Timer?>(null);
|
||||
@@ -165,9 +163,6 @@ class LoginPage extends HookConsumerWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> launchOneKey() =>
|
||||
LoginLauncher.start(context, ref, next: next);
|
||||
|
||||
void onSendTap() {
|
||||
if (sending.value || countdown.value > 0) return;
|
||||
if (phoneDigits().length != 11) {
|
||||
@@ -227,8 +222,6 @@ class LoginPage extends HookConsumerWidget {
|
||||
agreed: agreed.value,
|
||||
onToggleAgreed: () => agreed.value = !agreed.value,
|
||||
onSend: onSendTap,
|
||||
oneKeyLoading: oneKeyLoading.value,
|
||||
onOneKey: launchOneKey,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -247,8 +240,6 @@ class _PhoneStep extends StatelessWidget {
|
||||
required this.onToggleAgreed,
|
||||
required this.error,
|
||||
required this.onSend,
|
||||
required this.oneKeyLoading,
|
||||
required this.onOneKey,
|
||||
});
|
||||
|
||||
final ShadColorScheme cs;
|
||||
@@ -260,8 +251,6 @@ class _PhoneStep extends StatelessWidget {
|
||||
final VoidCallback onToggleAgreed;
|
||||
final String? error;
|
||||
final VoidCallback onSend;
|
||||
final bool oneKeyLoading;
|
||||
final VoidCallback onOneKey;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -306,13 +295,6 @@ class _PhoneStep extends StatelessWidget {
|
||||
dim: !phoneReady,
|
||||
onTap: onSend,
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
_PrimaryButton(
|
||||
label: '本机号码一键登录',
|
||||
enabled: !oneKeyLoading,
|
||||
loading: oneKeyLoading,
|
||||
onTap: onOneKey,
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
_AgreeRow(agreed: agreed, onToggle: onToggleAgreed, cs: cs),
|
||||
],
|
||||
|
||||
Reference in new issue
Block a user