Commit 6387f8dcbee227d3fb5bccb4dd4189ab19dc5f0f

Authored by wdn127929
1 parent 8d0143c2

vvv

  1 +import 'package:flutter/material.dart';
  2 +import 'package:flutter_screenutil/flutter_screenutil.dart';
  3 +import 'package:one_key/provider/user_model.dart';
  4 +import 'package:one_key/res/colors.dart';
  5 +import 'package:one_key/utils/dialog_util.dart';
  6 +import 'package:one_key/utils/utils.dart';
  7 +import 'package:one_key/widgets/h_empty_view.dart';
  8 +import 'package:one_key/widgets/system_app_bar.dart';
  9 +import 'package:one_key/widgets/v_empty_view.dart';
  10 +import 'package:provider/provider.dart';
  11 +
  12 +import '../../../models/sai_you_yun_change_model.dart';
  13 +import '../../../models/sai_you_yun_model.dart';
  14 +import '../../../provider/marketing_center_provider.dart';
  15 +import '../../../utils/event_bus.dart';
  16 +import '../../../utils/screen_util.dart';
  17 +import '../../../widgets/round_button.dart';
  18 +
  19 +/**
  20 + * 短信模版配置页面
  21 + */
  22 +
  23 +class SmsTemplateSettingPage extends StatefulWidget {
  24 + final String smsTemplate;
  25 + final String bookinginfo;
  26 + SmsTemplateSettingPage({Key key, this.smsTemplate, this.bookinginfo})
  27 + : super(key: key);
  28 +
  29 + @override
  30 + _SmsTemplateSettingPageState createState() => _SmsTemplateSettingPageState();
  31 +}
  32 +
  33 +class _SmsTemplateSettingPageState extends State<SmsTemplateSettingPage> {
  34 + String contentChangeStart = '';
  35 +
  36 + TextEditingController controller = TextEditingController();
  37 +
  38 + bool canChange = false; //选择了可变模版
  39 +
  40 + List<String> btnData = ['店名', '仅包间名字', '小程序预订卡片'];
  41 +
  42 + MarketingCenterProvider marketingCenterProvider;
  43 +
  44 + String smsSelectValue = '';
  45 +
  46 + int _cursorPosition = 0;
  47 +
  48 + @override
  49 + void initState() {
  50 + // TODO: implement initState
  51 + super.initState();
  52 +
  53 + smsSelectValue = widget.smsTemplate;
  54 +
  55 + WidgetsBinding.instance.addPostFrameCallback((_) {
  56 + contentChangeStart =
  57 + '尊敬的贵宾,您好!诚挚感谢您选择${Provider.of<UserModel>(context, listen: false).user.sellername}用餐!\n';
  58 + _getSaiYouYunList();
  59 + // _getSaiYouYunChangeList();
  60 + // _getSmsChangeField();
  61 + _getData();
  62 + });
  63 + }
  64 +
  65 + ///固定模版
  66 + _getSaiYouYunList() async {
  67 + await marketingCenterProvider.getSaiYouYunList(context);
  68 + if (widget.smsTemplate != null && widget.smsTemplate.length > 0) {
  69 + //默认选中
  70 +
  71 + marketingCenterProvider.saiYouYunFixedData.forEach((element) {
  72 + if (element.temp == widget.smsTemplate) {
  73 + element.select = true;
  74 + controller.text = element.content;
  75 + }
  76 + });
  77 + }
  78 + marketingCenterProvider.notifyListeners();
  79 + }
  80 +
  81 + Future<bool> _getSaiYouYunChangeList() async {
  82 + bool sucess = await marketingCenterProvider.getSaiYouYunChangeList(context);
  83 + if (sucess) {
  84 + if (widget.smsTemplate != null && widget.smsTemplate.length > 0) {
  85 + //默认选中
  86 +
  87 + marketingCenterProvider.saiYouYunchangedData.forEach((element) {
  88 + if (element.temp == widget.smsTemplate) {
  89 + canChange = true;
  90 + element.select = true;
  91 + contentChangeStart = element.content.templateParam;
  92 + // controller.text = element.content;
  93 + }
  94 + });
  95 + }
  96 + setState(() {});
  97 + return true;
  98 + } else {
  99 + return false;
  100 + }
  101 +
  102 + // marketingCenterProvider.notifyListeners();
  103 + }
  104 +
  105 + _getData() async {
  106 + Future.wait<bool>([_getSmsChangeField(), _getSaiYouYunChangeList()])
  107 + .then((results) async {
  108 + print(results[0]);
  109 + if (results[0] && results[1]) {
  110 + //成功
  111 + String temp = widget.bookinginfo;
  112 + marketingCenterProvider.smsChangeFieldData.forEach((element) {
  113 + int count = widget.bookinginfo.split(element.code).length - 1;
  114 +
  115 + element.times = count;
  116 + if (temp.contains(element.code)) {
  117 + temp = temp.replaceAll(element.code, element.show);
  118 + }
  119 + });
  120 + // if (contentChangeStart.endsWith('\n')) {
  121 + // print(temp);
  122 + // String result = temp.substring(1, temp.length);
  123 + // print(result);
  124 +
  125 + // controller.text =
  126 + // temp.startsWith('\n') ? temp.substring(1, temp.length) : temp;
  127 + // }
  128 + controller.text = temp;
  129 + print(controller.text);
  130 + setState(() {});
  131 + } else {
  132 + // if (!results[0]) {
  133 + // //没有优惠券
  134 + // } else {}
  135 + // if (!results[1]) {
  136 + // //没有营销中心数据
  137 + // } else {}
  138 + setState(() {});
  139 + }
  140 + }).catchError((e) {
  141 + print(e);
  142 + });
  143 + }
  144 +
  145 + /// 获取可变模板允许配置字段
  146 + Future<bool> _getSmsChangeField() async {
  147 + bool suceess = await marketingCenterProvider.getSmsChangeField(context);
  148 + if (suceess) {
  149 + return true;
  150 + } else {
  151 + return false;
  152 + }
  153 + }
  154 +
  155 + ///设置店铺短信可变模版
  156 + Future<bool> _saveSaiYouYunChange(String text,
  157 + {bool isShowLoading = true}) async {
  158 + Map<String, String> params = {
  159 + 'sid': Provider.of<UserModel>(context, listen: false).user.sellerid,
  160 + 'sms_code': smsSelectValue,
  161 + // 'bookinginfo': smsSelectValue == 'tqNH3' || smsSelectValue == 'EZXsN2'
  162 + // ? text.startsWith('\n')
  163 + // ? text
  164 + // : '\n' + text
  165 + // : text,
  166 + 'bookinginfo': text,
  167 + // 'bookinginfo': text.startsWith('\n') ? text : '\n' + text,
  168 + };
  169 +
  170 + bool success = await marketingCenterProvider
  171 + .saveSaiYouYunChange(context, params, showLoading: isShowLoading);
  172 + // await _setTempleteSMS();
  173 +
  174 + if (success) {
  175 + await _setTempleteSMS();
  176 + } else {
  177 + return false;
  178 + }
  179 + }
  180 +
  181 + ///设置店铺短信固定模版
  182 + Future<bool> _setTempleteSMS({bool isShowLoading = true}) async {
  183 + Map<String, String> params = {
  184 + 'sid': Provider.of<UserModel>(context, listen: false).user.sellerid,
  185 + 'key': 'saiyouyun',
  186 + 'value': smsSelectValue,
  187 + };
  188 +
  189 + bool success = await marketingCenterProvider.setConfing(context, params,
  190 + needToast: false);
  191 +
  192 + if (success) {
  193 + bus.emit('getDzSellerConf');
  194 + Utils.showToast('保存成功');
  195 + Navigator.pop(context);
  196 + return true;
  197 + } else {
  198 + return false;
  199 + }
  200 + }
  201 +
  202 + List<Widget> templateWidgets(String title, List<SaiYouYunSunModel> temData) {
  203 + List<Widget> data = [];
  204 + for (var i = 0; i < temData.length; i++) {
  205 + data.add(btnItem(temData[i].temp, temData[i].select, onTap: () {
  206 + // if (title == '固定模版') {
  207 + canChange = false;
  208 + marketingCenterProvider.saiYouYunFixedData.forEach((element) {
  209 + element.select = false;
  210 + });
  211 + // i 的select = true;
  212 + temData[i].select = true;
  213 + smsSelectValue = temData[i].temp;
  214 + // controller.text = temData[i].content;
  215 + // controller.text = temData[i].content;
  216 + String temp = temData[i].content;
  217 + controller.text = temp;
  218 + // controller.text =
  219 + // temp.startsWith('\n') ? temp.substring(1, temp.length) : temp;
  220 + marketingCenterProvider.saiYouYunchangedData.forEach((element) {
  221 + element.select = false;
  222 + });
  223 + // } else {
  224 + // canChange = true;
  225 + // marketingCenterProvider.saiYouYunchangedData.forEach((element) {
  226 + // element.select = false;
  227 + // });
  228 + // // i 的select = true;
  229 + // temData[i].select = true;
  230 + // smsSelectValue = temData[i].temp;
  231 + // controller.text = temData[i].content;
  232 + // marketingCenterProvider.saiYouYunFixedData.forEach((element) {
  233 + // element.select = false;
  234 + // });
  235 + // }
  236 + setState(() {});
  237 + }));
  238 + }
  239 + return data;
  240 + }
  241 +
  242 + List<Widget> templateChangesWidgets(
  243 + String title, List<SaiYouYunChangeSunModel> temData) {
  244 + List<Widget> data = [];
  245 + for (var i = 0; i < temData.length; i++) {
  246 + data.add(btnItem(temData[i].temp, temData[i].select, onTap: () {
  247 + // if (!canChange) {
  248 + //从固定模版变更过来的,清空文本框内容
  249 + controller.text = temData[i].now == null
  250 + ? ''
  251 + : temData[i].now.content == null
  252 + ? ''
  253 + : temData[i].now.content;
  254 +
  255 + // controller.text = temData[i].now == null
  256 + // ? ''
  257 + // : temData[i].now.content == null
  258 + // ? ''
  259 + // : temData[i].now.content.startsWith('\n')
  260 + // ? temData[i]
  261 + // .now
  262 + // .content
  263 + // .substring(1, temData[i].now.content.length)
  264 + // : temData[i].now.content;
  265 + // }
  266 + canChange = true;
  267 + marketingCenterProvider.saiYouYunchangedData.forEach((element) {
  268 + element.select = false;
  269 + });
  270 + // i 的select = true;
  271 + contentChangeStart = temData[i].content.templateParam;
  272 + temData[i].select = true;
  273 + smsSelectValue = temData[i].temp;
  274 +
  275 + // controller.text = temData[i].content.templateCode;
  276 + marketingCenterProvider.saiYouYunFixedData.forEach((element) {
  277 + element.select = false;
  278 + });
  279 +
  280 + setState(() {});
  281 + }));
  282 + }
  283 + return data;
  284 + }
  285 +
  286 + List<Widget> btnWidgets() {
  287 + List<Widget> data = [];
  288 + for (var i = 0;
  289 + i < marketingCenterProvider.smsChangeFieldData.length;
  290 + i++) {
  291 + data.add(btnItem(marketingCenterProvider.smsChangeFieldData[i].name, true,
  292 + onTap: () {
  293 + marketingCenterProvider.smsChangeFieldData[i].times++;
  294 + print(controller.text);
  295 +
  296 + // 获取当前光标位置
  297 + // int cursorPosition = _cursorPosition;
  298 + int cursorPosition = controller.selection.baseOffset;
  299 + if (cursorPosition != -1) {
  300 + // 插入文本
  301 + controller.text = controller.text.substring(0, cursorPosition) +
  302 + marketingCenterProvider.smsChangeFieldData[i].show +
  303 + controller.text.substring(cursorPosition);
  304 +
  305 + print(controller.text);
  306 +
  307 + // 设置新的光标位置
  308 + controller.selection = TextSelection.fromPosition(TextPosition(
  309 + offset: cursorPosition +
  310 + marketingCenterProvider.smsChangeFieldData[i].show.length,
  311 + affinity: TextAffinity.downstream));
  312 + setState(() {
  313 + _cursorPosition = controller.selection.baseOffset;
  314 + // cursorPosition = _cursorPosition;
  315 + });
  316 + } else {
  317 + controller.text += marketingCenterProvider.smsChangeFieldData[i].show;
  318 + }
  319 + }));
  320 + }
  321 + return data;
  322 + }
  323 +
  324 + Widget btnItem(String title, bool select, {GestureTapCallback onTap}) {
  325 + return InkWell(
  326 + onTap: onTap,
  327 + child: Container(
  328 + padding: EdgeInsets.all(w(3)),
  329 + decoration: BoxDecoration(
  330 + color: select ? MyColours.color_438E93 : MyColours.color_ffffff,
  331 + borderRadius: BorderRadius.circular(r(5)),
  332 + border: Border.all(width: w(1), color: MyColours.color_438E93)),
  333 + child: Text(title,
  334 + style: TextStyle(
  335 + color: select ? MyColours.color_ffffff : MyColours.color_438E93,
  336 + fontSize: font(12),
  337 + fontWeight: FontWeight.bold)),
  338 + ),
  339 + );
  340 + }
  341 +
  342 + Widget _templeteWidget(
  343 + String title,
  344 + ) {
  345 + return Container(
  346 + margin: EdgeInsets.symmetric(horizontal: w(16), vertical: w(5)),
  347 + child: Row(
  348 + crossAxisAlignment: CrossAxisAlignment.start,
  349 + children: [
  350 + Text(
  351 + title,
  352 + style: TextStyle(fontSize: font(16), color: MyColours.color_515151),
  353 + ),
  354 + HEmptyView(34),
  355 + Expanded(
  356 + child: Wrap(
  357 + spacing: w(5),
  358 + runSpacing: w(5),
  359 + children: title == '固定模版'
  360 + ? templateWidgets(
  361 + title, marketingCenterProvider.saiYouYunFixedData)
  362 + : templateChangesWidgets(
  363 + title, marketingCenterProvider.saiYouYunchangedData),
  364 + ))
  365 + ],
  366 + ),
  367 + );
  368 + }
  369 +
  370 + @override
  371 + Widget build(BuildContext context) {
  372 + marketingCenterProvider =
  373 + Provider.of<MarketingCenterProvider>(context, listen: true);
  374 + return Scaffold(
  375 + backgroundColor: MyColours.color_ffffff,
  376 + appBar: SystemAppBar('预订通知短信模版选择'),
  377 + body: Column(
  378 + children: [
  379 + Expanded(
  380 + child: SingleChildScrollView(
  381 + child: Column(
  382 + crossAxisAlignment: CrossAxisAlignment.start,
  383 + children: [
  384 + _templeteWidget('固定模版'),
  385 + _templeteWidget('可变模版'),
  386 + VEmptyView(10),
  387 + Container(
  388 + width: double.infinity,
  389 + padding: EdgeInsets.all(w(10)),
  390 + margin: EdgeInsets.symmetric(horizontal: w(16)),
  391 + decoration: BoxDecoration(
  392 + border: Border.all(
  393 + color: MyColours.color_DBDBDB, width: w(1)),
  394 + borderRadius: BorderRadius.circular(r(8))),
  395 + child: Column(
  396 + crossAxisAlignment: CrossAxisAlignment.start,
  397 + children: [
  398 + if (canChange)
  399 + Text(
  400 + contentChangeStart,
  401 + style: TextStyle(
  402 + height: 1.5,
  403 + color: MyColours.color_66000000,
  404 + fontSize: font(14)),
  405 + ),
  406 + TextField(
  407 + enabled: canChange,
  408 + controller: controller,
  409 + // textAlign: TextAlign.right,
  410 + // textAlignVertical: TextAlignVertical.bottom,
  411 + maxLines: 10,
  412 +
  413 + // inputFormatters: title == '金额'
  414 + // ? [
  415 + // FilteringTextInputFormatter(RegExp("[0-9.]"),
  416 + // allow: true),
  417 + // MyNumberTextInputFormatter(digit: 2),
  418 + // ]
  419 + // : [],
  420 +
  421 + // //光标颜色
  422 + // cursorColor:
  423 + // widget.isSelect ? Colors.white : MyColours.color_199095,
  424 + //输入文本的样式
  425 + style: TextStyle(
  426 + fontSize: ScreenUtil().setSp(28),
  427 + color: MyColours.color_515151),
  428 + // focusNode: focusNode,
  429 + decoration: InputDecoration(
  430 + border:
  431 + OutlineInputBorder(borderSide: BorderSide.none),
  432 + hintText:
  433 + canChange ? '以上内容为固定不可编辑,点击这里开始编辑短信内容' : '请选择模版',
  434 + hintStyle: TextStyle(
  435 + color: MyColours.color_999999,
  436 + fontSize: ScreenUtil().setSp(28),
  437 + ),
  438 + contentPadding: EdgeInsets.zero,
  439 + ),
  440 + onChanged: (value) {
  441 + //添加的代码:光标一直在文本框后面
  442 + // controller.value = TextEditingValue(
  443 + // text: controller.text,
  444 + // selection:
  445 + // TextSelection.fromPosition(TextPosition(
  446 + // affinity: TextAffinity.downstream,
  447 + // offset: controller.text.length,
  448 + // )));
  449 + // setState(() {
  450 + // _cursorPosition = controller.selection.baseOffset;
  451 + // });
  452 + },
  453 + onEditingComplete: () {
  454 + FocusScope.of(context)
  455 + .requestFocus(FocusNode()); //点击空白处隐藏键盘
  456 + },
  457 + // keyboardType: inputType,
  458 + )
  459 + ],
  460 + ),
  461 + ),
  462 + VEmptyView(10),
  463 + if (canChange)
  464 + Container(
  465 + padding: EdgeInsets.symmetric(horizontal: w(21)),
  466 + child: Wrap(
  467 + crossAxisAlignment: WrapCrossAlignment.start,
  468 + spacing: w(10),
  469 + runSpacing: w(10),
  470 + children: btnWidgets(),
  471 + ))
  472 + ],
  473 + ),
  474 + )),
  475 + Container(
  476 + height: ScreenUtil().setWidth(91 * 2),
  477 + width: double.infinity,
  478 + padding: EdgeInsets.only(
  479 + top: ScreenUtil().setWidth(16 * 2),
  480 + bottom: ScreenUtil().setWidth(35 * 2),
  481 + left: ScreenUtil().setWidth(23 * 2),
  482 + right: ScreenUtil().setWidth(23 * 2),
  483 + ),
  484 + decoration: BoxDecoration(
  485 + color: MyColours.color_ffffff,
  486 + boxShadow: [
  487 + BoxShadow(
  488 + spreadRadius: ScreenUtil().setWidth(2),
  489 + color: Color.fromRGBO(0, 0, 0, 0.1),
  490 + offset: Offset(0, ScreenUtil().setWidth(-2)),
  491 + blurRadius: ScreenUtil().setWidth(8))
  492 + ],
  493 + borderRadius: BorderRadius.all(
  494 + Radius.circular(ScreenUtil().setWidth(16)),
  495 + ),
  496 + ),
  497 + child: Row(
  498 + mainAxisAlignment: MainAxisAlignment.spaceBetween,
  499 + children: [
  500 + // RoundButton(
  501 + // text: '预览效果',
  502 + // width: w(100),
  503 + // height: ScreenUtil().setWidth(80),
  504 + // radius: ScreenUtil().setWidth(14),
  505 + // gradientStart: MyColours.color_ffffff,
  506 + // gradientEnd: MyColours.color_ffffff,
  507 + // borderColor: MyColours.color_438E93,
  508 + // textColor: MyColours.color_438E93,
  509 + // isBold: true,
  510 + // fontSize: 32,
  511 + // onPressed: () {
  512 + // DialogUtil.cupertinoAlertDialog(context,
  513 + // textAlign: TextAlign.left,
  514 + // showlTitle: false,
  515 + // content: canChange
  516 + // ? contentChangeStart + controller.text
  517 + // : controller.text);
  518 + // },
  519 + // ),
  520 + // HEmptyView(30),
  521 + Expanded(
  522 + child: RoundButton(
  523 + text: '保存',
  524 + height: ScreenUtil().setWidth(80),
  525 + radius: ScreenUtil().setWidth(14),
  526 + isBold: true,
  527 + fontSize: 32,
  528 + onPressed: () async {
  529 + if (smsSelectValue.length == 0) {
  530 + Utils.showToast('请选择模版类型');
  531 + return;
  532 + }
  533 + if (canChange) {
  534 + if (controller.text.trim().length == 0) {
  535 + Utils.showToast('请输入内容后保存');
  536 + return;
  537 + }
  538 +
  539 + // bool canRequest = marketingCenterProvider
  540 + // .smsChangeFieldData
  541 + // .every((element) {
  542 + // if (element.times > 0) {
  543 + // int count =
  544 + // controller.text.split(element.show).length -
  545 + // 1;
  546 +
  547 + // if (count != element.times) {
  548 + // Utils.showToast('${element.show}不符合规范,请检查');
  549 +
  550 + // return false;
  551 + // }
  552 + // }
  553 + // return true;
  554 + // });
  555 +
  556 + // if (canRequest) {
  557 + String tempStr = controller.text;
  558 + marketingCenterProvider.smsChangeFieldData
  559 + .forEach((element) {
  560 + if (tempStr.contains(element.show)) {
  561 + tempStr = tempStr.replaceAll(
  562 + element.show, element.code);
  563 + }
  564 + });
  565 +
  566 + _saveSaiYouYunChange(tempStr);
  567 + // }
  568 + // print('fffff');
  569 + } else {
  570 + _setTempleteSMS();
  571 + }
  572 + },
  573 + ),
  574 + )
  575 + ],
  576 + )),
  577 + ],
  578 + ),
  579 + );
  580 + }
  581 +}