Browse Source

1、商品详情的预加载实现

2、我的人脉的头部推荐人bug修复
tags/0.0.1
PH2 4 years ago
parent
commit
c75396ce8d
11 changed files with 207 additions and 117 deletions
  1. +8
    -5
      lib/pages/goods_details_page/bloc/goods_details_page_bloc.dart
  2. +137
    -83
      lib/pages/goods_details_page/bloc/goods_details_page_repository.dart
  3. +3
    -4
      lib/pages/goods_details_page/goods_details_page.dart
  4. +0
    -1
      lib/pages/team_page/bloc/team_bloc.dart
  5. +10
    -1
      lib/pages/team_page/model/team_data_model.dart
  6. +2
    -2
      lib/pages/team_page/team_page.dart
  7. +17
    -13
      lib/widgets/home/home_goods/models/home_goods_model.dart
  8. +6
    -1
      lib/widgets/team/recommend/bloc/team_recommend_bloc.dart
  9. +15
    -0
      lib/widgets/team/recommend/bloc/team_recommend_repository.dart
  10. +2
    -2
      lib/widgets/team/recommend/bloc/team_recommend_state.dart
  11. +7
    -5
      lib/widgets/team/recommend/team_recommend_widget.dart

+ 8
- 5
lib/pages/goods_details_page/bloc/goods_details_page_bloc.dart View File

@@ -30,13 +30,16 @@ class GoodsDetailsPageBloc extends Bloc<GoodsDetailsPageEvent, GoodsDetailsPageS
} }


Stream<GoodsDetailsPageState> _mapInitEventToState(GoodsDetailsPageInitEvent event) async* { Stream<GoodsDetailsPageState> _mapInitEventToState(GoodsDetailsPageInitEvent event) async* {
var result = await repository.fetchInitModData(event.model);

// 获取上面传回来的数据
var parendData = await repository.fetchParentPageData(event?.model);
if(!EmptyUtil.isEmpty(parendData)){
yield GoodsDetailsPageLoadedState(model: parendData);
}
// 获取服务器的数据
var result = await repository.fetchInitModData(event?.model);
if (!EmptyUtil.isEmpty(result)) { if (!EmptyUtil.isEmpty(result)) {
yield GoodsDetailsPageLoadedState(model: result); yield GoodsDetailsPageLoadedState(model: result);
// var itemModelData = await repository.fetchModData(event.model);
// if(!EmptyUtil.isEmpty(itemModelData)){
// yield GoodsDetailsPageLoadedState(model: itemModelData);
// }
} else { } else {
yield GoodsDetailsPageErrorState(); yield GoodsDetailsPageErrorState();
} }


+ 137
- 83
lib/pages/goods_details_page/bloc/goods_details_page_repository.dart View File

@@ -2,107 +2,161 @@ import 'dart:convert';
import 'package:zhiying_comm/zhiying_comm.dart'; import 'package:zhiying_comm/zhiying_comm.dart';


class GoodsDetailsPageRepository { class GoodsDetailsPageRepository {



/// 获取上个页面传进来的数据
Future<List<Map<String, dynamic>>> fetchParentPageData(final Map<String, dynamic> model) async {
try {
if (!EmptyUtil.isEmpty(model) && model.containsKey('detail_data') && !EmptyUtil.isEmpty(model['detail_data'])) {
String provider = model['provider']?.toString();
String goodId = model['good_id']?.toString();
if (!EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(goodId)) {
Map<String, dynamic> detailData = model['detail_data'];
return _baseDataProcess(goodId, provider, detailData);
// Logger.log('商品类型 = $provider, 商品ID = $goodId');
// List<Map<String, dynamic>> _pageData = [];
// List<dynamic> modLists = detailData['mod_list'];
// for (int i = 0; i < modLists.length; i++) {
// Map<String, dynamic> item = modLists[i];
// Map<String, dynamic> data = item['data'];
// Map<String, dynamic> baseData = {'provider': provider, 'good_id': goodId};
// data.addAll(baseData);
// item['data'] = data;
// _pageData.add(item);
// }
// return _pageData;
}
}
} catch (e, s) {
Logger.error(e, s);
}
return null;
}


/// 获取数据 /// 获取数据
Future<List<Map<String, dynamic>>> fetchInitModData(final Map<String, dynamic> model) async{

String provider = model['provider'];
String goodId = model['good_id'];
Future<List<Map<String, dynamic>>> fetchInitModData(final Map<String, dynamic> model) async {
try { try {
String provider = model['provider'];
String goodId = model['good_id'];
if (!EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(goodId)) { if (!EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(goodId)) {
Logger.log('商品类型 = $provider, 商品ID = $goodId'); Logger.log('商品类型 = $provider, 商品ID = $goodId');
var result = await NetUtil.post('/api/v1/detail/$provider/$goodId', method: NetMethod.GET); var result = await NetUtil.post('/api/v1/detail/$provider/$goodId', method: NetMethod.GET);
if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) { if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
return _baseDataProcess(goodId, provider, result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
// List<Map<String, dynamic>> _pageData = [];


List<Map<String, dynamic>> _pageData = [];
/// 合并数据 /// 合并数据
List<dynamic> modLists = result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list'];
for(int i = 0; i < modLists.length; i++){
Map<String, dynamic> item = modLists[i];
Map<String, dynamic> data = item['data'];
Map<String, dynamic> style = jsonDecode(item['style']);
// List<dynamic> modLists = result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list'];
// for (int i = 0; i < modLists.length; i++) {
// Map<String, dynamic> item = modLists[i];
// Map<String, dynamic> data = item['data'];
// Map<String, dynamic> style = jsonDecode(item['style']);
//
// Map<String, dynamic> baseData = {'provider': provider, 'good_id': goodId};
// style.addAll(baseData);
//
// if (!EmptyUtil.isEmpty(data)) {
// style.addAll(data);
// item['data'] = jsonEncode(style);
// _pageData.add(item);
// } else {
// item['data'] = jsonEncode(style);
// _pageData.add(item);
// }
// }
//
// return _pageData;
}
}
} catch (e, s) {
Logger.error(e, s);
}
return null;
}


Map<String, dynamic> baseData = {'provider': provider, 'good_id': goodId};
style.addAll(baseData);
/// 数据处理
List<Map<String, dynamic>> _baseDataProcess(String goodId, String provider, final Map<String, dynamic> model) {
if (!EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(goodId) && !EmptyUtil.isEmpty(model)) {
try {
List<Map<String, dynamic>> _pageData = [];
Map<String, dynamic> baseData = {'provider': provider, 'good_id': goodId};
List<dynamic> modLists = model['mod_list'];
for (int i = 0; i < modLists.length; i++) {
Map<String, dynamic> item = modLists[i];
Map<String, dynamic> data = item['data'] is String ? jsonDecode(item['data']) : item['data'];
Map<String, dynamic> style = jsonDecode(item['style']);
style.addAll(baseData);


if(!EmptyUtil.isEmpty(data)){
style.addAll(data);
item['data'] = jsonEncode(style);
_pageData.add(item);
}else{
item['data'] = jsonEncode(style);
_pageData.add(item);
}
if (!EmptyUtil.isEmpty(data)) {
style.addAll(data);
item['data'] = jsonEncode(style);
_pageData.add(item);
} else {
item['data'] = jsonEncode(style);
_pageData.add(item);
} }

return _pageData;
} }
return _pageData;
} catch (e, s) {
Logger.error(e, s);
} }
}catch(e){
Logger.log(e);
} }
return null; return null;
} }



///【弃用】获取mod数据 把子mod数据塞在data中 ///【弃用】获取mod数据 把子mod数据塞在data中
// Future<List<Map<String, dynamic>>> fetchModData(final Map<String, dynamic> model) async{
// String provider = model['provider'];
// String goodId = model['good_id'];
// try {
// if (!EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(goodId)) {
// Logger.log('商品类型 = $provider, 商品ID = $goodId');
// var result = await NetUtil.post('/api/v1/detail/$provider/$goodId', method: NetMethod.GET);
// if(NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
// for (int i = 0; i < _pageData.length; i++) {
// //result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list'][i];
// Map<String, dynamic> resultData = result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list'][i];
// Map<String, dynamic> data = jsonDecode(_pageData[i]['data']);
// if(!EmptyUtil.isEmpty(data)){
// data.addAll(resultData);
// _pageData[i]['data'] = jsonEncode(data);
// }else{
// _pageData[i]['data'] = (null != resultData && resultData.length > 0) ? jsonEncode(resultData) : '';
// }
// }
// return _pageData;
// }
// }
// }catch(e){
// Logger.log(e);
// }
// return null;
// }
//
// ///【弃用】初始化
// Future<List<Map<String, dynamic>>> fetchInitData(Map<String, dynamic> model) async {
// int id = 13;
// var result = await NetUtil.post('/api/v1/mod', method: NetMethod.POST, params: {'ids': [id]});
// try {
// if(NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
// return _loadData(id, result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
// }
// } catch (e) {
// Logger.log(e);
// }
// return null;
// }
//
// ///【弃用】处理数据
// List<Map<String, dynamic>> _loadData(int id, dynamic data) {
// String key = id.toString();
// Map<String, dynamic> json = Map<String, dynamic>.from(data);
// if (json.containsKey(key)) {
// List<dynamic> list = json[key];
// _pageData = list.map((item) {
// return Map<String, dynamic>.from(item);
// }).toList();
// return _pageData;
// }
// return null;
// }
// Future<List<Map<String, dynamic>>> fetchModData(final Map<String, dynamic> model) async{
// String provider = model['provider'];
// String goodId = model['good_id'];
// try {
// if (!EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(goodId)) {
// Logger.log('商品类型 = $provider, 商品ID = $goodId');
// var result = await NetUtil.post('/api/v1/detail/$provider/$goodId', method: NetMethod.GET);
// if(NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
// for (int i = 0; i < _pageData.length; i++) {
// //result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list'][i];
// Map<String, dynamic> resultData = result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]['mod_list'][i];
// Map<String, dynamic> data = jsonDecode(_pageData[i]['data']);
// if(!EmptyUtil.isEmpty(data)){
// data.addAll(resultData);
// _pageData[i]['data'] = jsonEncode(data);
// }else{
// _pageData[i]['data'] = (null != resultData && resultData.length > 0) ? jsonEncode(resultData) : '';
// }
// }
// return _pageData;
// }
// }
// }catch(e){
// Logger.log(e);
// }
// return null;
// }
//
// ///【弃用】初始化
// Future<List<Map<String, dynamic>>> fetchInitData(Map<String, dynamic> model) async {
// int id = 13;
// var result = await NetUtil.post('/api/v1/mod', method: NetMethod.POST, params: {'ids': [id]});
// try {
// if(NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
// return _loadData(id, result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
// }
// } catch (e) {
// Logger.log(e);
// }
// return null;
// }
//
// ///【弃用】处理数据
// List<Map<String, dynamic>> _loadData(int id, dynamic data) {
// String key = id.toString();
// Map<String, dynamic> json = Map<String, dynamic>.from(data);
// if (json.containsKey(key)) {
// List<dynamic> list = json[key];
// _pageData = list.map((item) {
// return Map<String, dynamic>.from(item);
// }).toList();
// return _pageData;
// }
// return null;
// }


} }

+ 3
- 4
lib/pages/goods_details_page/goods_details_page.dart View File

@@ -23,7 +23,7 @@ class GoodsDetailsPage extends StatefulWidget {
class _GoodsDetailsPageState extends State<GoodsDetailsPage> { class _GoodsDetailsPageState extends State<GoodsDetailsPage> {
@override @override
void initState() { void initState() {
print("parent data = ${widget.data}");
print("parent data = ${widget?.data}");
super.initState(); super.initState();
} }


@@ -36,8 +36,7 @@ class _GoodsDetailsPageState extends State<GoodsDetailsPage> {
], ],
child: BlocProvider<GoodsDetailsPageBloc>( child: BlocProvider<GoodsDetailsPageBloc>(
create: (_) => create: (_) =>
GoodsDetailsPageBloc(repository: GoodsDetailsPageRepository())
..add(GoodsDetailsPageInitEvent(model: widget?.data)),
GoodsDetailsPageBloc(repository: GoodsDetailsPageRepository())..add(GoodsDetailsPageInitEvent(model: widget?.data)),
child: GoodsDetailsContainer(widget?.data), child: GoodsDetailsContainer(widget?.data),
), ),
); );
@@ -118,7 +117,7 @@ class _GoodsDetailsContainerState extends State<GoodsDetailsContainer> {
return true; return true;
}, },
builder: (context, state) { builder: (context, state) {
print('currente state = $state');
print('GoodsDetailsPage currente state = $state');
if (state is GoodsDetailsPageLoadedState) { if (state is GoodsDetailsPageLoadedState) {
return _getMainWidget(state?.model); return _getMainWidget(state?.model);
} }


+ 0
- 1
lib/pages/team_page/bloc/team_bloc.dart View File

@@ -46,7 +46,6 @@ class TeamBloc extends Bloc<TeamEvent, TeamState> {
} }
var resultData = await repository.fetchNetData(); var resultData = await repository.fetchNetData();
if (!EmptyUtil.isEmpty(resultData) && (!EmptyUtil.isEmpty(resultStyle) || !EmptyUtil.isEmpty(cache))) { if (!EmptyUtil.isEmpty(resultData) && (!EmptyUtil.isEmpty(resultStyle) || !EmptyUtil.isEmpty(cache))) {
Logger.log('================================================= load =================');
yield TeamLoadedState(dataModel: resultData, styleModel: !EmptyUtil.isEmpty(resultStyle) ? resultStyle : cache); yield TeamLoadedState(dataModel: resultData, styleModel: !EmptyUtil.isEmpty(resultStyle) ? resultStyle : cache);
} else { } else {
yield TeamErrorState(); yield TeamErrorState();


+ 10
- 1
lib/pages/team_page/model/team_data_model.dart View File

@@ -1,4 +1,6 @@
class TeamDataModel {
import 'package:equatable/equatable.dart';

class TeamDataModel extends Equatable {
String all_fans; String all_fans;
String direct_fans_count; String direct_fans_count;
String indirect_fans_count; String indirect_fans_count;
@@ -52,4 +54,11 @@ class TeamDataModel {
data['referrer_avatar'] = this.referrerAvatar; data['referrer_avatar'] = this.referrerAvatar;
return data; return data;
} }




@override
List<Object> get props => [this.all_fans, this.direct_fans_count, this.indirect_fans_count, this.referrer_invite_code, this.referrer_phone, this.referrer_username, this.referrer_wechat,
this.today_add, this.yesterday_add, this.referrerAvatar];
} }

+ 2
- 2
lib/pages/team_page/team_page.dart View File

@@ -52,10 +52,10 @@ class _TeamPageContainerState extends State<_TeamPageContainer> {
return true; return true;
}, },
builder: (context, state) { builder: (context, state) {
print('TeamPage state === $state');
print('TeamPage state === $state, ');
if (state is TeamLoadedState) { if (state is TeamLoadedState) {
_initTabController(state?.styleModel); _initTabController(state?.styleModel);
return _getMainWidget(state.styleModel, state.dataModel);
return _getMainWidget(state?.styleModel, state?.dataModel);
} }


/// 骨架屏幕 /// 骨架屏幕


+ 17
- 13
lib/widgets/home/home_goods/models/home_goods_model.dart View File

@@ -12,19 +12,22 @@ class HomeGoodsModel extends Equatable {
String marketPrice; String marketPrice;
String currentPrice; String currentPrice;
String inorderCount; String inorderCount;
Map<String, dynamic> detailData;


HomeGoodsModel(
{this.provider,
this.providerName,
this.goodId,
this.goodImage,
this.goodTitle,
this.shopName,
this.coupon,
this.commission,
this.marketPrice,
this.currentPrice,
this.inorderCount});
HomeGoodsModel({
this.provider,
this.providerName,
this.goodId,
this.goodImage,
this.goodTitle,
this.shopName,
this.coupon,
this.commission,
this.marketPrice,
this.currentPrice,
this.inorderCount,
this.detailData,
});


HomeGoodsModel.fromJson(Map<String, dynamic> json) { HomeGoodsModel.fromJson(Map<String, dynamic> json) {
provider = json['provider']; provider = json['provider'];
@@ -38,6 +41,7 @@ class HomeGoodsModel extends Equatable {
marketPrice = json['market_price']; marketPrice = json['market_price'];
currentPrice = json['current_price']; currentPrice = json['current_price'];
inorderCount = json['inorder_count']; inorderCount = json['inorder_count'];
detailData = json['detail_data'];
} }


Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
@@ -53,10 +57,10 @@ class HomeGoodsModel extends Equatable {
data['market_price'] = this.marketPrice; data['market_price'] = this.marketPrice;
data['current_price'] = this.currentPrice; data['current_price'] = this.currentPrice;
data['inorder_count'] = this.inorderCount; data['inorder_count'] = this.inorderCount;
data['detail_data'] = this.detailData;
return data; return data;
} }


@override @override
List<Object> get props => [this.goodId, this.provider, this.providerName]; List<Object> get props => [this.goodId, this.provider, this.providerName];
} }


+ 6
- 1
lib/widgets/team/recommend/bloc/team_recommend_bloc.dart View File

@@ -37,7 +37,12 @@ class TeamRecommendBloc extends Bloc<TeamRecommendEvent, TeamRecommendState> {


/// 初始化数据 /// 初始化数据
Stream<TeamRecommendState> _mapInitEventToState(TeamRecommendInitEvent event) async* { Stream<TeamRecommendState> _mapInitEventToState(TeamRecommendInitEvent event) async* {
yield TeamRecommendLoadedState(event?.dataModel);
var dataModel = await repository.fetchNetData();
if (!EmptyUtil.isEmpty(dataModel) && !EmptyUtil.isEmpty(dataModel.referrer_username) && !EmptyUtil.isEmpty(dataModel.referrer_invite_code)) {
yield TeamRecommendLoadedState(dataModel);
} else {
yield TeamRecommendErrorState();
}
} }


/// 关联推荐人 /// 关联推荐人


+ 15
- 0
lib/widgets/team/recommend/bloc/team_recommend_repository.dart View File

@@ -14,4 +14,19 @@ class TeamRecommendRepository {
} }
return null; return null;
} }

/// 获取网络数据
Future<TeamDataModel> fetchNetData() async {
var data = await NetUtil.post('/api/v1/user/myteam', method: NetMethod.GET);
try {
if (NetUtil.isSuccess(data) && !EmptyUtil.isEmpty(data[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
TeamDataModel _dataModel = TeamDataModel.fromJson(data[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
return _dataModel;
}
} catch (e, s) {
Logger.log('e = $e, s = $s');
}
return null;
}

} }

+ 2
- 2
lib/widgets/team/recommend/bloc/team_recommend_state.dart View File

@@ -10,9 +10,9 @@ class TeamRecommendInitial extends TeamRecommendState {
} }


class TeamRecommendLoadedState extends TeamRecommendState { class TeamRecommendLoadedState extends TeamRecommendState {
TeamDataModel model;
final TeamDataModel model;


TeamRecommendLoadedState(this.model);
const TeamRecommendLoadedState(this.model);


@override @override
List<Object> get props => [this.model]; List<Object> get props => [this.model];


+ 7
- 5
lib/widgets/team/recommend/team_recommend_widget.dart View File

@@ -21,7 +21,7 @@ class TeamRecommendWidget extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider<TeamRecommendBloc>( return BlocProvider<TeamRecommendBloc>(
create: (_) => TeamRecommendBloc(TeamRecommendRepository())..add(TeamRecommendInitEvent(dataModel: dataModel)), create: (_) => TeamRecommendBloc(TeamRecommendRepository())..add(TeamRecommendInitEvent(dataModel: dataModel)),
child: _TeamRecommendWidgetContainer(styleModel),
child: _TeamRecommendWidgetContainer(styleModel, dataModel),
); );
} }
} }
@@ -29,17 +29,19 @@ class TeamRecommendWidget extends StatelessWidget {
class _TeamRecommendWidgetContainer extends StatefulWidget { class _TeamRecommendWidgetContainer extends StatefulWidget {
TeamStyleModel styleModel; TeamStyleModel styleModel;


// TeamDataModel dataModel;
TeamDataModel dataModel;


_TeamRecommendWidgetContainer(this.styleModel);
_TeamRecommendWidgetContainer(this.styleModel, dataModel);


@override @override
_TeamRecommendWidgetState createState() => _TeamRecommendWidgetState();
__TeamRecommendWidgetState createState() => __TeamRecommendWidgetState();
} }


class _TeamRecommendWidgetState extends State<_TeamRecommendWidgetContainer> {
class __TeamRecommendWidgetState extends State<_TeamRecommendWidgetContainer> {
TextEditingController _textEditingController; TextEditingController _textEditingController;
FocusNode _focusNode; FocusNode _focusNode;
TeamStyleModel styleModel;
TeamDataModel dataModel;


/// 按钮点击添加事件 /// 按钮点击添加事件
void _onClickListener() { void _onClickListener() {


Loading…
Cancel
Save