Files
jiu/client/lib/repositories/license_repository.dart
T
wangjia f3cfdcadca feat: 购买下单透传终端类型,手机端收银台拉起支付宝 App(契约 v1.1.0)
根因:pay 判手机/PC 用下单请求 UA,而下单由 jiu 后端 Go client 发出,
恒被判 PC 扫码页——手机用户拿不到 wap 拉起页。

- backend:/license/purchase 请求体加可选 client_type(oneof=pc mobile),
  缺省按本请求 UA 兜底(官网浏览器购买自动受益);CreatePurchase 透传给 pay;
  单测断言透传 + 既有用例补参
- client:createPurchase 支持 clientType;PurchaseCard 按平台声明
  (iOS/Android=mobile,桌面=pc,Web 不传走 UA),kIsWeb 先于 dart:io;
  launchUrl 外部浏览器不变,wap 收银台自动拉起支付宝
- 配套:pay-contract v1.1.0(744725b)+ pay 侧 resolveIsMobile(dbdadd1)已各自提交

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JJ1g8XV1YhhmHRzhwWEW7o
2026-07-04 10:31:49 +08:00

102 lines
3.6 KiB
Dart

import 'package:dio/dio.dart';
import '../core/api/api_client.dart';
import '../core/device/device_id.dart';
import '../core/exceptions.dart';
import '../models/license.dart';
class LicenseRepository {
final ApiClient _client;
const LicenseRepository(this._client);
Future<LicenseInfo?> getInfo() async {
try {
final resp = await _client.get('/license/info');
final data = resp.data['data'];
if (data == null) return null;
return LicenseInfo.fromJson(data as Map<String, dynamic>);
} on DioException catch (e) {
throw AppException(
e.response?.data?['error'] as String? ?? '获取授权信息失败',
statusCode: e.response?.statusCode,
);
}
}
/// Activate a license key for this device.
Future<void> activate(String licenseKey, {String? deviceName}) async {
final deviceId = await DeviceId.get();
try {
await _client.post('/license/activate', data: {
'license_key': licenseKey,
'device_id': deviceId,
'device_name': deviceName ?? DeviceId.platformName,
'platform': DeviceId.platformName,
});
} on DioException catch (e) {
throw AppException(
e.response?.data?['error'] as String? ?? '激活失败',
statusCode: e.response?.statusCode,
);
}
}
/// 在线购买/续费下单(仅管理员),返回 pay 收银台跳转信息。
/// [clientType] 终端类型("pc"/"mobile",可选):显式声明收银台形态——
/// mobile 走支付宝手机网站支付(H5 拉起 App),pc 走电脑扫码页;
/// 不传时后端按请求 UA 判断(浏览器场景 UA 真实,App 场景须显式传)。
Future<PurchaseOrder> createPurchase(String bizCode,
{String? clientType}) async {
try {
final resp = await _client.post('/license/purchase', data: {
'biz_code': bizCode,
if (clientType != null) 'client_type': clientType,
});
return PurchaseOrder.fromJson(resp.data['data'] as Map<String, dynamic>);
} on DioException catch (e) {
throw AppException(
e.response?.data?['error'] as String? ?? '下单失败,请稍后重试',
statusCode: e.response?.statusCode,
);
}
}
/// 查询购买单状态(到账轮询)。到账(paid)后返回续期后的授权到期时间。
Future<PurchaseStatusInfo> purchaseStatus(String outTradeNo) async {
try {
final resp = await _client.get('/license/purchase/$outTradeNo');
return PurchaseStatusInfo.fromJson(
resp.data['data'] as Map<String, dynamic>);
} on DioException catch (e) {
throw AppException(
e.response?.data?['error'] as String? ?? '查询订单状态失败',
statusCode: e.response?.statusCode,
);
}
}
/// 本店首月特惠是否已享用(购买弹窗据此置灰特惠档)。
Future<bool> promoUsed() async {
try {
final resp = await _client.get('/license/promo-status');
final data = resp.data['data'] as Map<String, dynamic>?;
return data?['used'] as bool? ?? false;
} on DioException {
// 查询失败不阻塞弹窗:按未享用展示,下单时服务端仍会强校验
return false;
}
}
/// Deactivate (unbind) this device from its license.
Future<void> deactivate() async {
final deviceId = await DeviceId.get();
try {
await _client.post('/license/deactivate', data: {'device_id': deviceId});
} on DioException catch (e) {
throw AppException(
e.response?.data?['error'] as String? ?? '解绑失败',
statusCode: e.response?.statusCode,
);
}
}
}