You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
317 lines
10 KiB
317 lines
10 KiB
import 'dart:convert'; |
|
import 'dart:io'; |
|
|
|
import 'package:dio/dio.dart'; |
|
import 'package:flutter/cupertino.dart'; |
|
import 'package:flutter/material.dart'; |
|
import 'package:flutter/rendering.dart'; |
|
import 'package:flutter_easyloading/flutter_easyloading.dart'; |
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; |
|
import 'package:huixiang/generated/l10n.dart'; |
|
import 'package:huixiang/retrofit/data/activity.dart'; |
|
import 'package:huixiang/retrofit/data/base_data.dart'; |
|
import 'package:huixiang/retrofit/data/store_info.dart'; |
|
import 'package:huixiang/retrofit/data/user_entity.dart'; |
|
import 'package:huixiang/retrofit/data/user_info.dart'; |
|
import 'package:huixiang/retrofit/retrofit_api.dart'; |
|
import 'package:huixiang/union/union_view/store_activity.dart'; |
|
import 'package:huixiang/union/union_view/store_info.dart'; |
|
import 'package:huixiang/union/union_view/union_coupon.dart'; |
|
import 'package:huixiang/union/union_view/vip.dart'; |
|
import 'package:huixiang/utils/font_weight.dart'; |
|
import 'package:huixiang/utils/min.dart'; |
|
import 'package:huixiang/view_widget/classic_header.dart'; |
|
import 'package:huixiang/view_widget/my_appbar.dart'; |
|
import 'package:huixiang/view_widget/receive_success.dart'; |
|
import 'package:path_provider/path_provider.dart'; |
|
import 'package:shared_preferences/shared_preferences.dart'; |
|
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
|
import 'package:pull_to_refresh/pull_to_refresh.dart'; |
|
|
|
class UnionDetailsPage extends StatefulWidget { |
|
final Map<String, dynamic> arguments; |
|
|
|
UnionDetailsPage({this.arguments}); |
|
|
|
@override |
|
State<StatefulWidget> createState() { |
|
return _UnionDetailsPage(); |
|
} |
|
} |
|
|
|
class _UnionDetailsPage extends State<UnionDetailsPage> { |
|
ApiService apiService; |
|
|
|
@override |
|
void dispose() { |
|
super.dispose(); |
|
refreshController.dispose(); |
|
} |
|
|
|
StoreInfo storeInfo; |
|
List<Activity> activitys; |
|
|
|
queryStoreInfo() async { |
|
final SharedPreferences value = await SharedPreferences.getInstance(); |
|
apiService = |
|
ApiService(Dio(), context: context, token: value.getString('token')); |
|
|
|
BaseData baseData = await apiService |
|
.queryStoreInfo(widget.arguments["id"]) |
|
.catchError((error) { |
|
refreshController.refreshFailed(); |
|
}); |
|
if (baseData != null && baseData.isSuccess) { |
|
refreshController.refreshCompleted(); |
|
storeInfo = StoreInfo.fromJson(baseData.data); |
|
activitys = storeInfo.informationVOPageVO.list |
|
.map((e) => Activity.fromJson(e)) |
|
.toList(); |
|
} else { |
|
refreshController.refreshFailed(); |
|
} |
|
} |
|
|
|
final RefreshController refreshController = RefreshController(); |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return FutureBuilder( |
|
future: queryStoreInfo(), |
|
builder: (context, snapshot) { |
|
return Scaffold( |
|
appBar: MyAppBar( |
|
background: Color(0xFFF7F7F7), |
|
title: storeInfo == null |
|
? (widget.arguments["storeName"] ?? "") |
|
: storeInfo.storeName, |
|
titleColor: Colors.black87, |
|
titleSize: 18.sp, |
|
leadingColor: Colors.black, |
|
), |
|
body: Column( |
|
children: [ |
|
Expanded( |
|
child: SmartRefresher( |
|
controller: refreshController, |
|
enablePullDown: true, |
|
enablePullUp: false, |
|
header: MyHeader(), |
|
physics: BouncingScrollPhysics(), |
|
onRefresh: () { |
|
setState(() {}); |
|
}, |
|
child: SingleChildScrollView( |
|
physics: NeverScrollableScrollPhysics(), |
|
child: Column( |
|
children: [ |
|
///门店信息 |
|
StoreInfos(storeInfo), |
|
|
|
///门店对应VIP信息 |
|
Vip(storeInfo, _receiveVip, isReceive), |
|
|
|
///门店对应优惠券 |
|
UnionCoupon(storeInfo, _receiveCoupon), |
|
|
|
/// 门店对应的活动 |
|
StoreActivity(widget.arguments, activitys), |
|
], |
|
), |
|
), |
|
), |
|
flex: 1, |
|
), |
|
InkWell( |
|
onTap: _loginMin, |
|
child: Container( |
|
padding: EdgeInsets.only(top: 16.h, bottom: 16.h), |
|
decoration: BoxDecoration( |
|
color: isEnable() ? Color(0xFF32A060) : Color(0xFFD8D8D8), |
|
borderRadius: BorderRadius.vertical( |
|
top: Radius.circular(4), |
|
), |
|
), |
|
alignment: Alignment.center, |
|
child: Text( |
|
isEnable() |
|
? S.of(context).jinrushangdian |
|
: S.of(context).zanwuxianshangjindian, |
|
style: TextStyle( |
|
fontSize: 16.sp, |
|
color: isEnable() ? Colors.white : Color(0xFFA0A0A0), |
|
fontWeight: MyFontWeight.semi_bold, |
|
), |
|
), |
|
), |
|
), |
|
], |
|
), |
|
); |
|
}, |
|
); |
|
} |
|
|
|
bool isEnable() { |
|
if (storeInfo == null) return false; |
|
if (storeInfo.mini == null) return false; |
|
String miniAppId = storeInfo.mini.miniAppId; |
|
if (miniAppId == null || |
|
miniAppId == "" || |
|
storeInfo.mini.miniVersion == null || |
|
storeInfo.mini.miniVersion == "" || |
|
storeInfo.mini.miniDownloadUrl == null || |
|
storeInfo.mini.miniDownloadUrl == "") { |
|
return false; |
|
} else { |
|
return true; |
|
} |
|
} |
|
|
|
///领取优惠券 |
|
_receiveCoupon(couponId) async { |
|
BaseData baseData = await apiService.receiveCoupon(couponId).catchError((onError) {}); |
|
if (baseData != null && baseData.isSuccess) { |
|
queryStoreInfo(); |
|
showAlertDialog(); |
|
} |
|
} |
|
|
|
/// 领取成功弹窗 |
|
showAlertDialog() { |
|
//显示对话框 |
|
showDialog( |
|
context: context, |
|
builder: (BuildContext context) { |
|
return ReceiveSuccess(); |
|
}, |
|
); |
|
} |
|
|
|
bool isReceive = true; |
|
|
|
///领取VIP |
|
_receiveVip() async { |
|
BaseData baseData = await apiService.minLogin(storeInfo.id).catchError((onError) {}); |
|
if (baseData != null && baseData.isSuccess) { |
|
SmartDialog.showToast(S.of(context).lingquchenggong, |
|
alignment: Alignment.center); |
|
setState(() { |
|
isReceive = false; |
|
}); |
|
} |
|
} |
|
|
|
printMin() async { |
|
String miniAppId = storeInfo.mini.miniAppId; |
|
debugPrint("print isExistsApp: ${await Min.isExistsApp(miniAppId)}"); |
|
debugPrint("print getAppBasePath: ${await Min.getAppBasePath(miniAppId)}"); |
|
debugPrint("print currentPageUrl: ${await Min.currentPageUrl()}"); |
|
debugPrint("print runingAppid: ${await Min.runingAppid()}"); |
|
} |
|
|
|
_loginMin() async { |
|
if (storeInfo == null) return; |
|
if (!isEnable()) { |
|
SmartDialog.showToast(S.of(context).zanbuzhichixianshangdiancan, |
|
alignment: Alignment.center); |
|
return; |
|
} |
|
BaseData baseData = await apiService.minLogin(storeInfo.id).catchError((onError) {}); |
|
if (baseData != null && baseData.isSuccess) { |
|
UserEntity userEntity = UserEntity.fromJson(baseData.data); |
|
startMin(userEntity.token, userEntity.userId); |
|
} |
|
} |
|
|
|
startMin(token, userId) async { |
|
if (storeInfo == null) return; |
|
if (storeInfo.mini == null) return; |
|
if(Platform.isAndroid){ |
|
if (!(await Min.isInitialize())) { |
|
// 小程序的微信支付和app的充值支付使用同一个WXPayEntryActivity回调, |
|
// 然而充值时小程序未初始化会导致回调内部代码调用getPackage空指针, |
|
// 故而在此初始化一下 |
|
await Min.initialize(); |
|
} |
|
} |
|
printMin(); |
|
String miniAppId = storeInfo.mini.miniAppId; |
|
String filePath = ""; |
|
if (Platform.isAndroid) { |
|
filePath = (await getExternalStorageDirectory()).path; |
|
} else { |
|
filePath = (await getApplicationDocumentsDirectory()).path; |
|
} |
|
filePath = "$filePath/$miniAppId.wgt"; |
|
|
|
if (!(await Min.isExistsApp(miniAppId))) { |
|
await downloadWgt(miniAppId, filePath); |
|
await Min.reloadWgt(miniAppId, filePath); |
|
} else { |
|
String version = await Min.getAppVersionInfo(storeInfo.mini.miniAppId); |
|
if (version != storeInfo.mini.miniVersion) { |
|
await downloadWgt(miniAppId, filePath); |
|
await Min.reloadWgt(miniAppId, filePath); |
|
} |
|
} |
|
|
|
SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); |
|
String nickname = sharedPreferences.getString("nick"); |
|
String mobile = sharedPreferences.getString("mobile"); |
|
String user = sharedPreferences.getString('user'); |
|
String latitude = sharedPreferences.getString("latitude"); |
|
String longitude = sharedPreferences.getString("longitude"); |
|
print(user); |
|
UserInfo userInfo = UserInfo.fromJson(jsonDecode(user)); |
|
Min.startMin(miniAppId, { |
|
"token": "Bearer $token", |
|
"shopId": widget.arguments["id"], |
|
"tenantCode": storeInfo.tenantCode, |
|
if (latitude != null && longitude != null) |
|
"position": "$latitude,$longitude", |
|
"baseURL": "https://pos.api.lotus-wallet.com/app/", |
|
// "baseURL": "http://192.168.10.236:8765/app/", |
|
"uid": userId, |
|
"userInfo": { |
|
"nickname": nickname, |
|
"headimg": userInfo.headimg, |
|
"balance": userInfo.balance, |
|
"money": userInfo.money, |
|
"phone": mobile, |
|
"isBind": userInfo.isBind, |
|
}, |
|
}); |
|
} |
|
|
|
double progressValue = 0; |
|
String downText = S.current.zhengzaixiazaizhong; |
|
|
|
downloadWgt(appId, savePath) async { |
|
downText = S.of(context).zhengzaixiazaizhong; |
|
Response response = await Dio() |
|
.download(storeInfo.mini.miniDownloadUrl, savePath, |
|
onReceiveProgress: (progress, max) { |
|
progressValue = progress.toDouble() / max.toDouble(); |
|
EasyLoading.showProgress(progressValue, status: downText); |
|
}); |
|
if (response.statusCode == 200) { |
|
downText = S.of(context).xiazaiwancheng; |
|
EasyLoading.showProgress(progressValue, status: downText); |
|
SharedPreferences.getInstance().then((value) { |
|
value.setStringList( |
|
"miniAppid", |
|
(value.getStringList("miniAppid") != null |
|
? value.getStringList("miniAppid") |
|
: []) |
|
..add(appId)); |
|
}); |
|
} else { |
|
print("print 下载失败"); |
|
} |
|
Future.delayed(Duration(seconds: 1), () { |
|
EasyLoading.dismiss(); |
|
}); |
|
} |
|
|
|
}
|
|
|