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.

318 lines
10 KiB

4 years ago
import 'dart:convert';
4 years ago
import 'dart:io';
4 years ago
4 years ago
import 'package:dio/dio.dart';
4 years ago
import 'package:flutter/cupertino.dart';
4 years ago
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
3 years ago
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
4 years ago
import 'package:huixiang/generated/l10n.dart';
4 years ago
import 'package:huixiang/retrofit/data/activity.dart';
4 years ago
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/store_info.dart';
4 years ago
import 'package:huixiang/retrofit/data/user_entity.dart';
4 years ago
import 'package:huixiang/retrofit/data/user_info.dart';
4 years ago
import 'package:huixiang/retrofit/retrofit_api.dart';
3 years ago
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';
3 years ago
import 'package:huixiang/utils/font_weight.dart';
4 years ago
import 'package:huixiang/utils/min.dart';
4 years ago
import 'package:huixiang/view_widget/classic_header.dart';
4 years ago
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:huixiang/view_widget/receive_success.dart';
4 years ago
import 'package:path_provider/path_provider.dart';
4 years ago
import 'package:shared_preferences/shared_preferences.dart';
4 years ago
import 'package:flutter_screenutil/flutter_screenutil.dart';
4 years ago
import 'package:pull_to_refresh/pull_to_refresh.dart';
4 years ago
class UnionDetailsPage extends StatefulWidget {
4 years ago
final Map<String, dynamic> arguments;
UnionDetailsPage({this.arguments});
4 years ago
@override
State<StatefulWidget> createState() {
return _UnionDetailsPage();
}
}
class _UnionDetailsPage extends State<UnionDetailsPage> {
4 years ago
ApiService apiService;
3 years ago
@override
void dispose() {
super.dispose();
refreshController.dispose();
}
4 years ago
StoreInfo storeInfo;
4 years ago
List<Activity> activitys;
4 years ago
queryStoreInfo() async {
3 years ago
final SharedPreferences value = await SharedPreferences.getInstance();
apiService =
ApiService(Dio(), context: context, token: value.getString('token'));
3 years ago
BaseData baseData = await apiService
.queryStoreInfo(widget.arguments["id"])
4 years ago
.catchError((error) {
refreshController.refreshFailed();
});
4 years ago
if (baseData != null && baseData.isSuccess) {
4 years ago
refreshController.refreshCompleted();
4 years ago
storeInfo = StoreInfo.fromJson(baseData.data);
activitys = storeInfo.informationVOPageVO.list
.map((e) => Activity.fromJson(e))
.toList();
4 years ago
} else {
refreshController.refreshFailed();
}
4 years ago
}
3 years ago
final RefreshController refreshController = RefreshController();
4 years ago
4 years ago
@override
Widget build(BuildContext context) {
3 years ago
return FutureBuilder(
future: queryStoreInfo(),
builder: (context, snapshot) {
return Scaffold(
3 years ago
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),
3 years ago
3 years ago
///门店对应VIP信息
Vip(storeInfo, _receiveVip, isReceive),
3 years ago
3 years ago
///门店对应优惠券
UnionCoupon(storeInfo, _receiveCoupon),
3 years ago
3 years ago
/// 门店对应的活动
StoreActivity(widget.arguments, activitys),
],
),
3 years ago
),
3 years ago
),
flex: 1,
4 years ago
),
3 years ago
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,
),
),
),
),
],
),
3 years ago
);
},
4 years ago
);
}
4 years ago
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;
}
}
4 years ago
///领取优惠券
3 years ago
_receiveCoupon(couponId) async {
3 years ago
BaseData baseData = await apiService.receiveCoupon(couponId).catchError((onError) {});
if (baseData != null && baseData.isSuccess) {
4 years ago
queryStoreInfo();
showAlertDialog();
}
}
/// 领取成功弹窗
showAlertDialog() {
//显示对话框
showDialog(
context: context,
builder: (BuildContext context) {
return ReceiveSuccess();
},
);
}
3 years ago
bool isReceive = true;
4 years ago
///领取VIP
3 years ago
_receiveVip() async {
3 years ago
BaseData baseData = await apiService.minLogin(storeInfo.id).catchError((onError) {});
4 years ago
if (baseData != null && baseData.isSuccess) {
3 years ago
SmartDialog.showToast(S.of(context).lingquchenggong,
alignment: Alignment.center);
4 years ago
setState(() {
4 years ago
isReceive = false;
4 years ago
});
}
}
4 years ago
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()}");
4 years ago
}
_loginMin() async {
if (storeInfo == null) return;
4 years ago
if (!isEnable()) {
3 years ago
SmartDialog.showToast(S.of(context).zanbuzhichixianshangdiancan,
alignment: Alignment.center);
4 years ago
return;
}
3 years ago
BaseData baseData = await apiService.minLogin(storeInfo.id).catchError((onError) {});
4 years ago
if (baseData != null && baseData.isSuccess) {
4 years ago
UserEntity userEntity = UserEntity.fromJson(baseData.data);
startMin(userEntity.token, userEntity.userId);
}
}
startMin(token, userId) async {
if (storeInfo == null) return;
if (storeInfo.mini == null) return;
3 years ago
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);
}
}
4 years ago
4 years ago
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String nickname = sharedPreferences.getString("nick");
String mobile = sharedPreferences.getString("mobile");
String user = sharedPreferences.getString('user');
4 years ago
String latitude = sharedPreferences.getString("latitude");
String longitude = sharedPreferences.getString("longitude");
4 years ago
print(user);
4 years ago
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,
},
});
4 years ago
}
4 years ago
4 years ago
double progressValue = 0;
3 years ago
String downText = S.current.zhengzaixiazaizhong;
4 years ago
3 years ago
downloadWgt(appId, savePath) async {
4 years ago
downText = S.of(context).zhengzaixiazaizhong;
4 years ago
Response response = await Dio()
.download(storeInfo.mini.miniDownloadUrl, savePath,
onReceiveProgress: (progress, max) {
4 years ago
progressValue = progress.toDouble() / max.toDouble();
3 years ago
EasyLoading.showProgress(progressValue, status: downText);
4 years ago
});
if (response.statusCode == 200) {
3 years ago
downText = S.of(context).xiazaiwancheng;
3 years ago
EasyLoading.showProgress(progressValue, status: downText);
4 years ago
SharedPreferences.getInstance().then((value) {
4 years ago
value.setStringList(
"miniAppid",
(value.getStringList("miniAppid") != null
? value.getStringList("miniAppid")
: [])
3 years ago
..add(appId));
4 years ago
});
4 years ago
} else {
print("print 下载失败");
4 years ago
}
3 years ago
Future.delayed(Duration(seconds: 1), () {
3 years ago
EasyLoading.dismiss();
});
4 years ago
}
4 years ago
4 years ago
}