lss
2022-05-26 a879e5dac4fc7cee747e25a63002c40462cdaef7
HDL_ON/UI/UI2/4-PersonalCenter/Visitor/VisitorQRCodePage.cs
@@ -1,19 +1,44 @@
using System;
using HDL_ON.UI.CSS;
using Shared;
using Com.Hdl.Hdllinphonesdk.Dialog;
using HDL_ON.DAL.Server;
using System.Collections.Generic;
using Com.Hdl.Hdllinphonesdk.Utils;
namespace HDL_ON.UI.UI2.PersonalCenter.Visitor
{
    public class VisitorQRCodePage : FrameLayout
    {
        /// <summary>
        /// 顶部title
        /// </summary>
        FrameLayout topView;
        FrameLayout bodyView;
        VerticalScrolViewLayout bodyScrolView;
        private EditText phoneNumberEditText;// 电话号码EditText
        private EditText pwdEditText;// 密码EditText
        private Button randomBtn;//随机密码Btn
        private FrameLayout codeLayout;
        private ImageView codeIV;// 二维码
        private Button codeTip;//二维码有效期提示
        private Button visitorVoucherBtn;
        private VisitorTempPassword tempPassword;
        public VisitorQRCodePage()
        {
            bodyView = this;
        }
        public VisitorQRCodePage(VisitorTempPassword pwd)
        {
            bodyView = this;
            this.tempPassword = pwd;
        }
        public void LoadPage()
        {
            #region 顶部导航栏
            if (tempPassword == null)
            {
                tempPassword = new VisitorTempPassword();
            Action click = () =>
            {
                VisitorHistoryPage page = new VisitorHistoryPage();
@@ -23,9 +48,490 @@
            };
            bodyView.BackgroundColor = CSS_Color.BackgroundColor;
            new TopViewDiv(bodyView, Language.StringByID(StringId.MemberPermissionManagement)).LoadTopView_SettingText(click, "");
                new TopViewDiv(bodyView, "访客邀请").LoadTopView_SettingText(click, "测试记录");
            }
            else
            {
                bodyView.BackgroundColor = CSS_Color.BackgroundColor;
                new TopViewDiv(bodyView, "访客邀请").LoadTopView();
            }
            #endregion
            bodyScrolView = new VerticalScrolViewLayout()
            {
                Y = Application.GetRealHeight(64),
                VerticalScrollBarEnabled = false,
                BackgroundColor = CSS_Color.MainBackgroundColor,
            };
            bodyView.AddChidren(bodyScrolView);
            // 提示文字
            bodyScrolView.AddChidren(new Button()
            {
                Text = "*您发起访客二维码后,该二维码将在您设置的有效期内,可自由刷码进行进出,请谨慎分享该二维码。",
                IsMoreLines = true,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                TextColor = CSS_Color.PromptingColor1,
                TextAlignment = TextAlignment.Center,
                Padding = new Padding(0, Application.GetRealWidth(20), 0, Application.GetRealWidth(20)),
                Height = Application.GetRealHeight(60),
            });
            #region 手机号
            bodyScrolView.AddChidren(new Button()
            {
                Text = "手机号",
                Y = Application.GetRealWidth(10),
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                Height = Application.GetRealHeight(20),
                Padding = new Padding(0, Application.GetRealWidth(20), 0, Application.GetRealWidth(20)),
                TextAlignment = TextAlignment.CenterLeft,
            });
            phoneNumberEditText = new EditText()
            {
                Height = Application.GetRealHeight(45),
                X = Application.GetRealWidth(20),
                Width = Application.GetRealWidth(335),
                TextSize = CSS_FontSize.TextFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                PlaceholderText = "输入访客手机号",
                Text = tempPassword.phone,
                IsNumberKeyboardType = true,
                PlaceholderTextColor = CSS_Color.PromptingColor1,
            };
            bodyScrolView.AddChidren(phoneNumberEditText);
            // 分割线
            bodyScrolView.AddChidren(new Button()
            {
                X = Application.GetRealWidth(20),
                Width = Application.GetRealWidth(335),
                Height = Application.GetRealHeight(1),
                BackgroundColor = CSS_Color.DividingLineColor,
            });
            #endregion
            #region 动态密码
            bodyScrolView.AddChidren(new Button() { Height = Application.GetRealWidth(10), });
            bodyScrolView.AddChidren(new Button()
            {
                Text = "动态密码",
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                Height = Application.GetRealHeight(20),
                Padding = new Padding(0, Application.GetRealWidth(20), 0, Application.GetRealWidth(20)),
                TextAlignment = TextAlignment.CenterLeft,
            });
            FrameLayout pwdLayout = new FrameLayout()
            {
                Height = Application.GetRealHeight(40),
            };
            // 密码输入框
            pwdEditText = new EditText()
            {
                X = Application.GetRealWidth(20),
                TextSize = CSS_FontSize.TextFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                Width = Application.GetRealWidth(335),
                PlaceholderText = "输入访6位密码",
                Text = tempPassword.tempPwd,
                PlaceholderTextColor = CSS_Color.PromptingColor1,
            };
            pwdLayout.AddChidren(pwdEditText);
            //随机密码Btn
            randomBtn = new Button()
            {
                X = Application.GetRealWidth(275),
                Width = Application.GetRealWidth(80),
                Text = "随机生成",
                TextAlignment = TextAlignment.CenterRight,
                TextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.TextFontSize,
            };
            pwdLayout.AddChidren(randomBtn);
            bodyScrolView.AddChidren(pwdLayout);
            randomBtn.MouseUpEventHandler += (sender, e) =>
            {
                string tPwd = getRandomPwd();
                pwdEditText.Text = tPwd;
            };
            // 分割线
            bodyScrolView.AddChidren(new Button()
            {
                X = Application.GetRealWidth(20),
                Width = Application.GetRealWidth(335),
                Height = Application.GetRealHeight(1),
                BackgroundColor = CSS_Color.DividingLineColor,
            });
            #endregion
            #region 二维码生效时间
            FrameLayout startLayout = new FrameLayout()
            {
                Height = Application.GetRealHeight(40),
                Y = Application.GetRealWidth(10),
                BackgroundColor = CSS_Color.BackgroundColor,
            };
            startLayout.AddChidren(new Button()
            {
                Text = "二维码生效时间",
                X = Application.GetRealWidth(20),
                Width = Application.GetRealWidth(130),
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextAlignment = TextAlignment.CenterLeft,
                Enable = false,
            });
            Button startTimeText = new Button()
            {
                X = Application.GetRealWidth(159),
                Width = Application.GetRealWidth(150),
                TextAlignment = TextAlignment.CenterRight,
                TextSize = CSS_FontSize.TextFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
            };
            if (tempPassword.tempPwd == null || tempPassword.tempPwd == "")
            {
                startTimeText.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                startTimeText.Text = tempPassword.validBeginTime;
            }
            startLayout.AddChidren(startTimeText);
            startLayout.AddChidren(new Button()
            {
                X = Application.GetRealWidth(339),
                Gravity = Gravity.CenterVertical,
                Width = Application.GetMinRealAverage(16),
                Height = Application.GetMinRealAverage(16),
                UnSelectedImagePath = "Public/Right.png",
                Enable = false,
            });
            Button startTimeBtn = new Button();
            startLayout.AddChidren(startTimeBtn);
            // dialog回调
            Action<string> startTimeAction = (str) =>
            {
                startTimeText.Text = str +":00";
            };
            startTimeBtn.MouseUpEventHandler += (sender, e) =>
            {
                try
                {
                    new LpTimeDialog(Application.Activity, new LpTimeDialogCallBak(startTimeAction)).Show();
                }
                catch (Exception ex)
                {
                    string error = ex.Message;
                }
            };
            bodyScrolView.AddChidren(startLayout);
            // 分割线
            bodyScrolView.AddChidren(new Button()
            {
                X = Application.GetRealWidth(20),
                Width = Application.GetRealWidth(335),
                Height = Application.GetRealHeight(1),
                BackgroundColor = CSS_Color.DividingLineColor,
            });
            #endregion
            #region 二维码失效时间
            FrameLayout endLayout = new FrameLayout()
            {
                Height = Application.GetRealHeight(40),
                Y = Application.GetRealWidth(10),
            };
            endLayout.AddChidren(new Button()
            {
                Text = "二维码失效时间",
                X = Application.GetRealWidth(20),
                Width = Application.GetRealWidth(130),
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextAlignment = TextAlignment.CenterLeft,
                Enable = false,
            });
            Button endTimeText = new Button()
            {
                X = Application.GetRealWidth(159),
                Width = Application.GetRealWidth(150),
                TextAlignment = TextAlignment.CenterRight,
                TextSize = CSS_FontSize.TextFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                Enable = false,
            };
            if (tempPassword.tempPwd == null || tempPassword.tempPwd == "")
            {
                endTimeText.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                endTimeText.Text = tempPassword.validEndTime;
            }
            endLayout.AddChidren(endTimeText);
            endLayout.AddChidren(new Button()
            {
                X = Application.GetRealWidth(339),
                Gravity = Gravity.CenterVertical,
                Width = Application.GetMinRealAverage(16),
                Height = Application.GetMinRealAverage(16),
                UnSelectedImagePath = "Public/Right.png",
                Enable = false,
            });
            Button endTimeBtn = new Button();
            endLayout.AddChidren(endTimeBtn);
            bodyScrolView.AddChidren(endLayout);
            // 分割线
            bodyScrolView.AddChidren(new Button()
            {
                X = Application.GetRealWidth(20),
                Width = Application.GetRealWidth(335),
                Height = Application.GetRealHeight(1),
                BackgroundColor = CSS_Color.DividingLineColor,
            });
            // dialog回调
            Action<string> endTimeAction = (str) =>
            {
                endTimeText.Text = str + ":00";
            };
            endTimeBtn.MouseUpEventHandler += (sender, e) =>
            {
                try
                {
                    new LpTimeDialog(Application.Activity, new LpTimeDialogCallBak(endTimeAction)).Show();
                }
                catch (Exception ex)
                {
                    string error = ex.Message;
                }
            };
            #endregion
            #region 二维码
            bodyScrolView.AddChidren(new Button() { Height = Application.GetRealWidth(33), });
            codeLayout = new FrameLayout()
            {
                X = Application.GetRealWidth(117),
                Width = Application.GetRealWidth(142),
                Height = Application.GetRealWidth(142),
            };
            bodyScrolView.AddChidren(codeLayout);
            // 二维码ImagView
            codeIV = new ImageView()
            {
                BackgroundColor = CSS_Color.BackgroundColor,
            };
            codeLayout.AddChidren(codeIV);
            bodyScrolView.AddChidren(new Button() { Height = Application.GetRealWidth(20), });
            bodyScrolView.AddChidren(new Button()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealWidth(20),
                Text = "长按可以把二维码保存到相册",
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.FirstLevelTitleColor,
            });
            #endregion
            bodyScrolView.AddChidren(new Button() { Height = Application.GetRealWidth(28), });
            visitorVoucherBtn = new Button()
            {
                X = Application.GetRealWidth(78),
                Width = Application.GetRealWidth(220),
                Height = Application.GetRealWidth(44),
                BackgroundColor = CSS_Color.MainColor,
                Radius = (uint)Application.GetRealWidth(44),
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.textWhiteColor,
            };
            bodyScrolView.AddChidren(visitorVoucherBtn);
            //
            Action voucher_action = () =>
            {
                if (codeIV.ImageBytes == null)
                {
                    // 请求服务端,生成临时密码
                    tempPassword.homeId = Entity.DB_ResidenceData.Instance.CurrentRegion.id;
                    tempPassword.phone = phoneNumberEditText.Text;
                    tempPassword.tempPwd = pwdEditText.Text;
                    tempPassword.validBeginTime = startTimeText.Text;
                    tempPassword.validEndTime = endTimeText.Text;
                    tempPassword.useCount = 1000;
                    CreateVisitorPassword(tempPassword);
                }
                else
                {
                    // 删除访客临时密码
                    DelVisitorPassword(tempPassword);
                }
            };
            visitorVoucherBtn.MouseUpEventHandler += (sender, e) =>
            {
                if (phoneNumberEditText.Text == "" || pwdEditText.Text == "")
                {
                    return;
                }
                string tip = "";
                string message = "";
                if (codeIV.ImageBytes == null)
                {
                    tip = "访客凭证";
                    message = "生成访客二维码并将临时密码发送至访客手机.";
                }
                else
                {
                    tip = "访客凭证";
                    message = "确定删除访客凭证?";
                }
                new LpTipDialog(Application.Activity, tip, message, new LpTipDialogCallBack(voucher_action)).Show();
            };
            //将二维码保存到本地相册
            codeIV.MouseLongEventHandler += (sender, e) =>
            {
#if __Android__
                bool bol = HDLImageUtils.SaveImageToGallery(Application.Activity,
                    Scan.BytesFromText(tempPassword.tempPwd, codeIV.Width, codeIV.Height));
                string msg = "";
                if (bol)
                {
                    msg = "保存成功!";
                }
                else
                {
                    msg = "保存失败!";
                }
                EmptyTipView tipView = new EmptyTipView(msg, 160);
                tipView.Gravity = Gravity.CenterHorizontal;
                bodyView.AddChidren(tipView);
#endif
            };
            RefreshVisitorVoucherBtn();
        }
        /// <summary>
        /// 刷新VisitorVoucherBtn状态
        /// </summary>
        private void RefreshVisitorVoucherBtn()
        {
            if (tempPassword.tempPwd != null && tempPassword.tempPwd != "")
            {
                codeIV.ImageBytes = Scan.BytesFromText(pwdEditText.Text, codeIV.Width, codeIV.Height);
                //判断二维码是否失效
                DateTime endTime = DateTime.Parse(tempPassword.validEndTime);
                DateTime currentTime = DateTime.Now;
                if (currentTime > endTime || !tempPassword.status)
                {
                    // 二维码有效期提示
                    codeTip = new Button()
                    {
                        TextAlignment = TextAlignment.Center,
                        TextColor = CSS_Color.textWhiteColor,
                        BackgroundColor = CSS_Color.DialogTransparentColor1,
                        Text = "二维码已失效",
                    };
                    codeLayout.AddChidren(codeTip);
                }
                visitorVoucherBtn.Text = "删除访客凭证";
            }
            else
            {
                visitorVoucherBtn.Text = "生成访客凭证";
            }
        }
        /// <summary>
        /// 添加临时密码(访客凭证)
        /// </summary>
        /// <param name="homeId">住宅id</param>
        /// <param name="phone">访客手机号</param>
        /// <param name="tempPwd">临时密码</param>
        /// <param name="validBeginTime">开始时间</param>
        /// <param name="validEndTime">结束时间</param>
        /// <param name="useCount">使用次数,默认一个数值大一些的</param>
        /// <returns></returns>
        private bool CreateVisitorPassword(VisitorTempPassword pwd)
        {
            var result = false;
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("homeId", pwd.homeId);
            dictionary.Add("phone", pwd.phone);
            dictionary.Add("tempPwd", pwd.tempPwd);
            dictionary.Add("validBeginTime", pwd.validBeginTime);
            dictionary.Add("validEndTime", pwd.validEndTime);
            dictionary.Add("useCount", pwd.useCount);
            var requestJson = HttpUtil.GetSignRequestJson(dictionary);
            var pack = HttpUtil.RequestHttpsPost(NewAPI.API_POST_ADD_TEMP_PASSWORD, requestJson);
            if (pack != null)
            {
                if (pack.Code == StateCode.SUCCESS)
                {
                    pwd.id = pack.Data.ToString();
                    codeIV.ImageBytes = Scan.BytesFromText(pwdEditText.Text, codeIV.Width, codeIV.Height);
                }
            }
            RefreshVisitorVoucherBtn();
            return result;
        }
        /// <summary>
        /// 删除临时密码(访客凭证)
        /// </summary>
        /// <param name="home_id">住宅id</param>
        /// <param name="pwd_id">临时密码id</param>
        private bool DelVisitorPassword(VisitorTempPassword pwd)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("homeId", pwd.homeId);
            dictionary.Add("id", pwd.id);
            var result = false;
            var requestJson = HttpUtil.GetSignRequestJson(dictionary);
            var pack = HttpUtil.RequestHttpsPost(NewAPI.API_POST_DEL_TEMP_PASSWORD, requestJson);
            if (pack != null)
            {
                if (pack.Code == StateCode.SUCCESS)
                {
                    // 关闭当前界面
                    RemoveFromParent();
                }
            }
            RefreshVisitorVoucherBtn();
            return result;
        }
        /// <summary>
        /// 生成6位随机密码
        /// </summary>
        /// <returns></returns>
        private string getRandomPwd()
        {
            string pwd = "";
            for (int i = 0; i < 6; i++)
            {
                int math = new Random().Next(0, 9);
                pwd += math;
            }
            return pwd;
        }
    }
}