feat(app): 接入金币充值与会员商品下单
This commit is contained in:
1 parent
ec2db6842e
commit
730e38622f
21 files changed
+1325
-1
No files matched your search
@@ -0,0 +1,75 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../models/order.dart';
|
||||
import '../models/product.dart';
|
||||
import '../repositories/store_repository.dart';
|
||||
import '../remote/api_exception.dart';
|
||||
|
||||
/// 商品货架状态:按类型拉取商品并创建订单。
|
||||
class StoreController extends ChangeNotifier {
|
||||
StoreController(this._repository, {required this.type});
|
||||
|
||||
static const String coinPackType = AppProduct.coinPackType;
|
||||
static const String vipType = AppProduct.vipType;
|
||||
|
||||
final StoreRepository _repository;
|
||||
final String type;
|
||||
|
||||
List<AppProduct> products = const [];
|
||||
bool loading = false;
|
||||
String? error;
|
||||
String? purchasingProductId;
|
||||
String? purchaseError;
|
||||
AppOrder? order;
|
||||
bool _disposed = false;
|
||||
|
||||
Future<void> load() async {
|
||||
loading = true;
|
||||
error = null;
|
||||
_notify();
|
||||
try {
|
||||
products = await _repository.fetchProducts(type: type);
|
||||
} on ApiException catch (exception) {
|
||||
error = exception.message;
|
||||
} catch (_) {
|
||||
error = 'productsLoadFailed';
|
||||
} finally {
|
||||
loading = false;
|
||||
_notify();
|
||||
}
|
||||
}
|
||||
|
||||
/// 创建订单;成功后保留 [order] 供页面展示订单号。
|
||||
Future<AppOrder?> buy(AppProduct product) async {
|
||||
if (purchasingProductId != null) return null;
|
||||
purchasingProductId = product.id;
|
||||
purchaseError = null;
|
||||
_notify();
|
||||
try {
|
||||
final created = await _repository.createOrder(product.id);
|
||||
order = created;
|
||||
return created;
|
||||
} on ApiException catch (exception) {
|
||||
purchaseError = exception.message;
|
||||
return null;
|
||||
} catch (_) {
|
||||
purchaseError = 'orderCreateFailed';
|
||||
return null;
|
||||
} finally {
|
||||
purchasingProductId = null;
|
||||
_notify();
|
||||
}
|
||||
}
|
||||
|
||||
void _notify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user