Hibok
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.
 
 
 
 
 
 

290 lines
9.7 KiB

  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:chat/data/UserData.dart';
  3. import 'package:chat/generated/i18n.dart';
  4. import 'package:chat/models/ChatMsg.dart';
  5. import 'package:chat/models/UserInfo.dart';
  6. import 'package:chat/models/ref_name_provider.dart';
  7. import 'package:chat/proto/all.pbserver.dart';
  8. import 'package:chat/r.dart';
  9. import 'package:chat/utils/CustomUI.dart';
  10. import 'package:chat/utils/HttpUtil.dart';
  11. import 'package:chat/utils/TokenMgr.dart';
  12. import 'package:chat/utils/screen.dart';
  13. import 'package:chat/utils/sql_util.dart';
  14. import 'package:dio/dio.dart';
  15. import 'package:flutter/material.dart';
  16. import 'package:oktoast/oktoast.dart';
  17. import 'package:provider/provider.dart';
  18. import '../utils/MessageMgr.dart';
  19. class CoinBagInfoPage extends StatefulWidget {
  20. final MsgModel msgModel;
  21. final String titleStr;
  22. CoinBagInfoPage(this.msgModel, {this.titleStr});
  23. @override
  24. _CoinBagInfoPageState createState() => _CoinBagInfoPageState();
  25. }
  26. class _CoinBagInfoPageState extends State<CoinBagInfoPage> {
  27. UserInfo friendInfo;
  28. RedWallet wallet;
  29. @override
  30. void initState() {
  31. super.initState();
  32. wallet = RedWallet.fromBuffer(widget.msgModel.msgContent);
  33. getInfo();
  34. }
  35. getInfo() async {
  36. int sendId = wallet.suId;
  37. if (sendId == UserData().basicInfo.userId) {
  38. friendInfo = UserData().basicInfo;
  39. } else {
  40. friendInfo = await HttpUtil().getFriendInfo(sendId);
  41. setState(() {});
  42. }
  43. updateRedWalletState();
  44. }
  45. @override
  46. Widget build(BuildContext context) {
  47. var foreColor = const Color(0xEEF9E3B2);
  48. return Scaffold(
  49. backgroundColor: Color(0xFFE86A52),
  50. appBar: AppBar(
  51. backgroundColor: Color(0xFFE86A52),
  52. leading: CustomUI.buildCustomLeading(context),
  53. ),
  54. body: SafeArea(
  55. child: Center(
  56. child: friendInfo == null
  57. ? CircularProgressIndicator(
  58. backgroundColor: Colors.orange,
  59. valueColor: AlwaysStoppedAnimation(Colors.orange))
  60. : Container(
  61. padding:
  62. EdgeInsets.only(left: 20,right: 20, bottom: 40),
  63. child: Column(
  64. mainAxisAlignment: MainAxisAlignment.center,
  65. children: <Widget>[
  66. Container(
  67. padding: EdgeInsets.symmetric(
  68. vertical: 40, horizontal: 20),
  69. child: Column(
  70. children: <Widget>[
  71. ClipRRect(
  72. borderRadius: BorderRadius.circular(8),
  73. child: CachedNetworkImage(
  74. imageUrl: friendInfo.headimgurl,
  75. placeholder: (context, url) {
  76. return Container(
  77. padding: EdgeInsets.all(10),
  78. child: CircularProgressIndicator(
  79. valueColor:
  80. AlwaysStoppedAnimation(
  81. foreColor)));
  82. },
  83. width: 100,
  84. height: 100,
  85. ),
  86. ),
  87. fixedText(
  88. I18n.of(context).ones_money.replaceFirst(
  89. '/s1',
  90. Provider.of<RefNameProvider>(context)
  91. .getRefName(friendInfo.userId,
  92. friendInfo.nickName)),
  93. fontSize: 12,
  94. color: foreColor),
  95. SizedBox(height: 40),
  96. Text(
  97. widget.titleStr,
  98. textScaleFactor: 1.0,
  99. textAlign: TextAlign.center,
  100. style: TextStyle(
  101. fontSize: 24,
  102. color: foreColor,
  103. fontWeight: FontWeight.w500),
  104. ),
  105. ],
  106. ),
  107. ),
  108. SizedBox(height: 20),
  109. _info(),
  110. Expanded(child: SizedBox()),
  111. fixedText(I18n.of(context).back_money,
  112. fontSize: 12, color: foreColor)
  113. ],
  114. ),
  115. ))));
  116. }
  117. _info() {
  118. var myId = UserData().basicInfo.userId;
  119. if (wallet.suId != myId) {
  120. //不是我发的
  121. if (wallet.state == RedWalletState.Uncollected) {
  122. return _openBtn();
  123. } else {
  124. return _getInfo();
  125. }
  126. } else {
  127. return _sendInfo();
  128. }
  129. }
  130. _receiveCoinBag() async {
  131. Map data = {
  132. "userId": UserData().basicInfo.userId,
  133. "redNo": wallet.orderId,
  134. };
  135. data['sign'] = TokenMgr().getSign(data);
  136. print(DateTime.now());
  137. Response res = await HttpUtil().post('red/packet/receive', data: data);
  138. if (res == null) {
  139. return null;
  140. }
  141. Map resData = res.data;
  142. if (resData['code'] == 0) {
  143. //领取成功
  144. print(DateTime.now());
  145. wallet.state = RedWalletState.Received;
  146. print('获得的金额${resData['data']['price']}');
  147. wallet.amount = resData['data']['price'];
  148. widget.msgModel.msgContent = wallet.writeToBuffer();
  149. MessageMgr().emit('Update RedWalletState', wallet);
  150. SqlUtil().updateWalletState(widget.msgModel);
  151. setState(() {});
  152. } else if (resData['code'] == -4) {
  153. //红包已过期
  154. showToast(I18n.of(context).money_over);
  155. } else if (resData['code'] == -5) {
  156. //红包已领过或者领取失败
  157. showToast(I18n.of(context).already_money);
  158. } else if (resData['code'] == -3) {
  159. //红包不存在
  160. showToast(I18n.of(context).no_money);
  161. } else {
  162. showToast(resData['msg']);
  163. }
  164. return null;
  165. }
  166. _openBtn() {
  167. return SizedBox(
  168. width: 100,
  169. height: 100,
  170. child: RaisedButton(
  171. padding: EdgeInsets.all(10.0),
  172. onPressed: _receiveCoinBag,
  173. shape: CircleBorder(),
  174. elevation: 2.0,
  175. child: Text(I18n.of(context).open,
  176. style: TextStyle(fontSize: 28),
  177. textAlign: TextAlign.center,
  178. textScaleFactor: 1.0),
  179. textColor: Color(0xFFD84F46),
  180. color: Color(0xFFFFE3AF),
  181. ));
  182. }
  183. _getInfo() {
  184. var infoStr = '';
  185. if (wallet.state == RedWalletState.Received) {
  186. infoStr = I18n.of(context).already_money;
  187. } else {
  188. infoStr = I18n.of(context).money_over;
  189. }
  190. return Container(
  191. child: Column(
  192. children: <Widget>[
  193. Row(
  194. mainAxisAlignment: MainAxisAlignment.center,
  195. crossAxisAlignment: CrossAxisAlignment.end,
  196. children: <Widget>[
  197. Text(wallet.amount.toString(),
  198. textScaleFactor: 1.0,
  199. style: TextStyle(fontSize: 50, color: Color(0xEEF9E3B2))),
  200. Padding(
  201. padding: EdgeInsets.only(left: 10, bottom: 15),
  202. child: Image.asset(R.assetsImagesCoin, scale: 3))
  203. ],
  204. ),
  205. fixedText(infoStr, color: Color(0xEEF9E3B2)),
  206. ],
  207. ),
  208. );
  209. }
  210. updateRedWalletState() async {
  211. Map data = {
  212. "userId": UserData().basicInfo.userId,
  213. "redNo": wallet.orderId,
  214. };
  215. data['sign'] = TokenMgr().getSign(data);
  216. Response res = await HttpUtil().post('red/packet/status', data: data);
  217. if (res == null) {
  218. return;
  219. }
  220. Map resData = res.data;
  221. if (resData['code'] == 0) {
  222. wallet.state = RedWalletState.valueOf(resData['data']['status']);
  223. if (wallet.state != RedWalletState.Uncollected) {
  224. if (mounted) {
  225. setState(() {});
  226. }
  227. }
  228. }
  229. }
  230. _sendInfo() {
  231. var msg;
  232. switch (wallet.state) {
  233. case RedWalletState.Expire:
  234. msg = I18n.of(context).back_user;
  235. break;
  236. case RedWalletState.Uncollected:
  237. msg = I18n.of(context).waiting_user;
  238. break;
  239. case RedWalletState.Received:
  240. msg = I18n.of(context).other_get;
  241. break;
  242. default:
  243. }
  244. return Container(
  245. child: Column(
  246. children: <Widget>[
  247. Row(
  248. mainAxisAlignment: MainAxisAlignment.center,
  249. crossAxisAlignment: CrossAxisAlignment.end,
  250. children: <Widget>[
  251. Text(wallet.amount.toString(),
  252. textScaleFactor: 1.0,
  253. style: TextStyle(fontSize: 50, color: Color(0xEEF9E3B2))),
  254. Padding(
  255. padding: EdgeInsets.only(left: 10, bottom: 15),
  256. child: Image.asset(R.assetsImagesCoin, scale: 3))
  257. ],
  258. ),
  259. fixedText(msg, color: Color(0xEEF9E3B2)),
  260. ],
  261. ),
  262. );
  263. }
  264. }