wjc
2023-03-30 cae4d4b5b508a666fbd0dff3c2a981fdff841bc8
HDL_ON/UI/UI2/4-PersonalCenter/MemberManagement/MemberFaceManagementPage.cs
New file
@@ -0,0 +1,628 @@
using System;
using System.Threading;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using HDL_ON.UI.CSS;
using Shared;
namespace HDL_ON.UI
{
    /// <summary>
    /// 增加子账号界面
    /// </summary>
    public partial class MemberFaceManagementPage : FrameLayout
    {
        FrameLayout bodyView;
        /// <summary>
        /// 人脸照片数据
        /// </summary>
        string base64string;
        ResidenceMemberInfo memberInfo;
        public MemberFaceManagementPage(ResidenceMemberInfo memberInfo)
        {
            bodyView = this;
            this.memberInfo = memberInfo;
        }
        public void LoadPage()
        {
            bodyView.BackgroundColor = CSS_Color.MainBackgroundColor;
            new TopViewDiv(bodyView, memberInfo.nickName).LoadTopView();
            #region 人脸管理
            var btnMemberFaceTitle = new Button()
            {
                Y = Application.GetRealHeight(80),
                X = Application.GetRealWidth(16),
                Height = Application.GetRealHeight(32),
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.TextFontSize,
                TextAlignment = TextAlignment.CenterLeft,
                TextID = StringId.MemberFace,
            };
            bodyView.AddChidren(btnMemberFaceTitle);
            var btnAddFace = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = btnMemberFaceTitle.Bottom,
                Width = Application.GetRealWidth(230),
                Height = Application.GetRealWidth(230),
                Radius = (uint)Application.GetRealWidth(22),
                BorderWidth = 0,
                UnSelectedImagePath = "PersonalCenter/FacePassage/FaceIcon.png",
                TextColor = CSS_Color.MainBackgroundColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                IsBold = true,
                TextAlignment = TextAlignment.Center,
            };
            bodyView.AddChidren(btnAddFace);
            EventHandler<MouseEventArgs> eFaceHandler = (sender, e) =>
            {
                if (OnAppConfig.Instance.FaceManagementTip)
                {
                    OnAppConfig.Instance.FaceManagementTip = true;
                    OnAppConfig.Instance.SaveConfig();
                    LoadPictureOptionView(btnAddFace);
                    return;
                }
                Dialog dialog = new Dialog()
                {
                    BackgroundColor = CSS_Color.DialogTransparentColor1,
                };
                FrameLayout contentView = new FrameLayout()
                {
                    Gravity = Gravity.Center,
                    Width = Application.GetRealWidth(270),
                    Height = Application.GetRealHeight(192),
                    BackgroundColor = CSS.CSS_Color.MainBackgroundColor,
                    BorderColor = 0x00000000,
                    BorderWidth = 0,
                    Radius = (uint)Application.GetMinRealAverage(10),
                };
                dialog.AddChidren(contentView);
                Button btnTitle = new Button()
                {
                    Y = Application.GetRealHeight(16),
                    Height = Application.GetRealHeight(30),
                    TextColor = CSS_Color.MainColor,
                    TextSize = CSS_FontSize.SubheadingFontSize,
                    TextAlignment = TextAlignment.Center,
                    IsBold = true,
                    TextID = StringId.FaceProtocolPrompt,
                };
                contentView.AddChidren(btnTitle);
                Button btnMsg = new Button()
                {
                    Gravity = Gravity.CenterHorizontal,
                    Height = Application.GetRealHeight(35),
                    Y = btnTitle.Bottom,
                    Width = Application.GetRealHeight(200),
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = CSS_Color.TextualColor,
                    TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                    Text = "    为保护您的个人隐私权益,特向您通知并申请明确同意授权。请你仔细阅读",
                    IsMoreLines = true,
                };
                if (Language.CurrentLanguage != "Chinese")
                {
                    btnMsg.Text = "   In order to protect your personal privacy rights, we hereby notify you and apply for explicit consent authorization. Please read it carefully";
                }
                contentView.AddChidren(btnMsg);
                Button btnMsg1 = new Button()
                {
                    Gravity = Gravity.CenterHorizontal,
                    Height = Application.GetRealHeight(15),
                    Y = btnMsg.Bottom,
                    Width = Application.GetRealHeight(200),
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = CSS_Color.MainColor,
                    TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                    Text = "    《On Pro人脸信息采集协议》",
                    IsMoreLines = true,
                };
                if (Language.CurrentLanguage != "Chinese")
                {
                    btnMsg1.Text = "    'On Pro Face Information Collection Protocol'";
                }
                contentView.AddChidren(btnMsg1);
                Button btnMsg2 = new Button()
                {
                    Gravity = Gravity.CenterHorizontal,
                    Height = Application.GetRealHeight(35),
                    Y = btnMsg1.Bottom,
                    Width = Application.GetRealHeight(200),
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = CSS_Color.TextualColor,
                    TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                    Text = "    若同意该协议,请点击【同意】按键,表示您已经理解并同意人脸信息采集协议。",
                    IsMoreLines = true,
                };
                if (Language.CurrentLanguage != "Chinese")
                {
                    btnMsg2.Text = "   If you agree to this agreement, please click the [Agree] button to indicate that you have understood and agreed to the face information collection agreement.";
                }
                contentView.AddChidren(btnMsg2);
                Button btnLine = new Button()
                {
                    Y = Application.GetRealHeight(149),
                    Height = Application.GetRealHeight(1),
                    Width = Application.GetRealWidth(270 / 2),
                    BackgroundColor = CSS.CSS_Color.DividingLineColor,
                };
                contentView.AddChidren(btnLine);
                Button btnCancel = new Button()
                {
                    Y = btnLine.Bottom,
                    Width = Application.GetRealWidth(135),
                    Height = Application.GetRealHeight(43),
                    TextAlignment = TextAlignment.Center,
                    TextColor = CSS_Color.TextualColor,
                    TextSize = CSS_FontSize.SubheadingFontSize,
                        //SelectedTextColor = CSS_Color.MainBackgroundColor,
                        //SelectedBackgroundColor = CSS_Color.MainColor,
                        TextID = StringId.Disagree,
                };
                contentView.AddChidren(btnCancel);
                Button btnConfirm = new Button()
                {
                    X = btnCancel.Right,
                    Y = btnLine.Y,
                    Width = Application.GetRealWidth(135),
                    Height = Application.GetRealHeight(45),
                    TextAlignment = TextAlignment.Center,
                        //TextColor = CSS_Color.TextualColor,
                        TextSize = CSS_FontSize.SubheadingFontSize,
                    TextColor = CSS_Color.MainBackgroundColor,
                    BackgroundColor = CSS_Color.MainColor,
                    TextID = StringId.Agree,
                };
                contentView.AddChidren(btnConfirm);
                    //例:右下圆角 大小为50
                    int mRectCornerID = HDLUtils.RectCornerBottomRight;
                btnConfirm.SetCornerWithSameRadius((uint)Application.GetMinRealAverage(10), mRectCornerID);
                dialog.Show();
                btnCancel.MouseUpEventHandler += (sender2, e2) =>
                {
                    dialog.Close();
                };
                btnMsg1.MouseUpEventHandler += (sender2, e2) =>
                {
                    string url = "https://developer.hdlcontrol.com/人脸信息采集协议.html";
                    if (Language.CurrentLanguage != "Chinese")
                    {
                        url = "https://developer.hdlcontrol.com/Face information collection protocol.html";
                    }
                    new WebViewDialog().LoadPage("人脸信息采集协议", url);
                    dialog.Close();
                };
                btnConfirm.MouseUpEventHandler = (sender2, e2) =>
                {
                    dialog.Close();
                    OnAppConfig.Instance.FaceManagementTip = true;
                    OnAppConfig.Instance.SaveConfig();
                    LoadPictureOptionView(btnAddFace);
                };
            };
            btnAddFace.MouseUpEventHandler = eFaceHandler;
            #endregion
            var btnDeleteFaceDate = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = btnAddFace.Bottom + Application.GetRealHeight(30),
                Width = Application.GetRealWidth(220),
                Height = Application.GetRealWidth(44),
                Radius = (uint)Application.GetRealWidth(22),
                BorderWidth = 0,
                BorderColor = 0x00000000,
                BackgroundColor = 0xFFFF7272,
                TextColor = CSS_Color.MainBackgroundColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                IsBold = true,
                TextAlignment = TextAlignment.Center,
                TextID = StringId.DeleteMemberFace,
            };
            bodyView.AddChidren(btnDeleteFaceDate);
            btnDeleteFaceDate.MouseUpEventHandler = (sender, e) => {
                var waitPage = new Loading();
                waitPage.Start("");
                new Thread(() =>
                {
                    try
                    {
                        var pack = new HttpServerRequest().DeleteMemberFace(memberInfo.id);
                        if (pack != null)
                        {
                            if(pack.Code == StateCode.SUCCESS)
                            {
                                Application.RunOnMainThread(() =>
                                {
                                    btnAddFace.ImageBytes = null;
                                    btnAddFace.UnSelectedImagePath = "PersonalCenter/FacePassage/FaceIcon.png";
                                    btnDeleteFaceDate.Enable = btnDeleteFaceDate.Visible = false;
                                });
                            }
                        }
                        else
                        {
                            Application.RunOnMainThread(() =>
                            {
                                TipDivMsg(Language.StringByID(StringId.delFail));
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        Application.RunOnMainThread(() =>
                        {
                            if (waitPage != null)
                            {
                                waitPage.RemoveFromParent();
                                waitPage = null;
                            }
                        });
                    }
                })
                { IsBackground = true }.Start();
            };
            if (!string.IsNullOrEmpty(memberInfo.userFaceUrl))
            {
                var waitPage = new Loading();
                waitPage.Start("");
                new Thread(() =>
                {
                    try
                    {
                        var bytes = downloadImageBytes(memberInfo.userFaceUrl);
                        if (bytes != null)
                        {
                            Application.RunOnMainThread(() =>
                            {
                                btnAddFace.ImageBytes = bytes;
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        Application.RunOnMainThread(() =>
                        {
                            if (waitPage != null)
                            {
                                waitPage.RemoveFromParent();
                                waitPage = null;
                            }
                        });
                    }
                })
                { IsBackground = true }.Start();
            }
            else
            {
                btnDeleteFaceDate.Enable = btnDeleteFaceDate.Visible = false;
            }
        }
        void TipDivMsg(string msg)
        {
            var tipBodyView = new FrameLayout();
            bodyView.AddChidren(tipBodyView);
            var msgView = new FrameLayout()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(167),
                Width = Application.GetRealWidth(210),
                Height = Application.GetRealHeight(62),
                Radius = (uint)Application.GetRealWidth(8),
                BorderWidth = 0,
                BorderColor = 0x00000000,
                BackgroundColor = 0xFF333333,
            };
            tipBodyView.AddChidren(msgView);
            var btnMsg = new Button()
            {
                Gravity = Gravity.Center,
                Width = Application.GetRealWidth(186),
                Height = Application.GetRealHeight(42),
                TextColor = CSS_Color.MainBackgroundColor,
                TextSize = CSS_FontSize.TextFontSize,
                TextAlignment = TextAlignment.Center,
                Text = msg,
                IsMoreLines = true,
            };
            msgView.AddChidren(btnMsg);
            new System.Threading.Thread(() => {
                System.Threading.Thread.Sleep(1200);
                Application.RunOnMainThread(() => {
                    tipBodyView.RemoveAll();
                    tipBodyView.RemoveFromParent();
                });
            })
            { IsBackground = true }.Start();
        }
        const int imageHeight = 400;
        /// <summary>
        /// 加载图标选择选项
        /// </summary>
        void LoadPictureOptionView(Button btnAddFace)
        {
            var pView = new FrameLayout()
            {
                BackgroundColor = CSS_Color.DialogTransparentColor1,
            };
            bodyView.AddChidren(pView);
            var pictureOptionView = new FrameLayout()
            {
                Y = Application.GetRealHeight(445 + 50),
                Height = Application.GetRealHeight(250),
                AnimateSpeed = 0.3f,
                Animate = Animate.DownToUp,
            };
            pView.AddChidren(pictureOptionView);
            var optionView = new VerticalScrolViewLayout()
            {
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealHeight(100),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(12),
            };
            pictureOptionView.AddChidren(optionView);
            var btnTakePicture = new Button()
            {
                Height = Application.GetRealHeight(50),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.TextualColor,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextID = StringId.TakePicture,
            };
            optionView.AddChidren(btnTakePicture);
            optionView.AddChidren(new Button() { Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
            var btnAlbum = new Button()
            {
                Height = Application.GetRealHeight(50),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.TextualColor,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextID = StringId.Album,
            };
            optionView.AddChidren(btnAlbum);
            optionView.AddChidren(new Button() { Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
            var btnCancel = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(8) + optionView.Bottom,
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealHeight(50),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(12),
                TextID = StringId.Cancel,
                TextColor = CSS_Color.WarningColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
            };
            pictureOptionView.AddChidren(btnCancel);
            pictureOptionView.MouseUpEventHandler = (sender, e) =>
            {
                pictureOptionView.Parent.RemoveFromParent();
            };
            pView.MouseUpEventHandler = (sender, e) =>
            {
                pictureOptionView.Parent.RemoveFromParent();
            };
            btnCancel.MouseUpEventHandler = (sender, e) =>
            {
                pictureOptionView.Parent.RemoveFromParent();
            };
            btnTakePicture.MouseDownEventHandler = (sender, e) =>
            {
                btnTakePicture.IsSelected = true;
            };
            btnTakePicture.MouseUpEventHandler = (sender, e) =>
            {
                btnTakePicture.IsSelected = false;
                var imageName = Guid.NewGuid().ToString();
                CropImage.TakePicture((imagePath) =>
                {
                    CropImageCallBack(imagePath, 1, imageName);
                }, imageName, 6, 6, imageHeight);
                pictureOptionView.Parent.RemoveFromParent();
            };
            btnAlbum.MouseDownEventHandler = (sender, e) =>
            {
                btnAlbum.IsSelected = true;
            };
            btnAlbum.MouseUpEventHandler = (sender, e) =>
            {
                btnAlbum.IsSelected = false;
                //从相册选择图片裁剪
                var imageName = Guid.NewGuid().ToString();
                //var imageName = scene.sid;
                CropImage.SelectPicture((imagePath) =>
                {
                    CropImageCallBack(imagePath, 2, imageName);
                }, imageName, 6, 6, imageHeight);
                pictureOptionView.Parent.RemoveFromParent();
            };
        }
        /// <summary>
        /// 下载云端人脸图片
        /// </summary>
        /// <param name="imageUrl"></param>
        /// <returns></returns>
        byte[] downloadImageBytes(string imageUrl)
        {
            var imageBytes = ImageUtlis.Current.GetImageDownloadUrl(imageUrl);
            return imageBytes;
        }
        /// <summary>
        /// 裁剪完照片回调,统一处理
        /// </summary>
        /// <param name="imagePath">裁剪后的真实路径</param>
        /// <param name="imageSource">照片来源;1:拍照;2:图库</param>
        void CropImageCallBack(string selectImagePath, int imageSource, string imageName)
        {
            if (string.IsNullOrEmpty(selectImagePath) == true)
            {
                return;
            }
            //上传成功到回调
            Action<bool> uploadSuccessAction = (isSuccess) =>
            {
                //点击重新录入事件
                Action action = () =>
                {
                    if (!isSuccess)
                    {
                        if (imageSource == 1)
                        {
                            CropImage.TakePicture((imagePath) =>
                            {
                                CropImageCallBack(imagePath, 1, imageName);
                            }, imageName, 6, 6, imageHeight);
                        }
                        else
                        {
                            CropImage.SelectPicture((imagePath) =>
                            {
                                CropImageCallBack(imagePath, 2, imageName);
                            }, imageName, 6, 6, imageHeight);
                        }
                    }
                };
                //if (isSuccess)
                //{
                //}
                Application.RunOnMainThread(() =>
                {
                    var page = new FaceSettingResultPage(action);
                    MainPage.BasePageView.AddChidren(page);
                    page.LoadPage(isSuccess);
                    MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
                });
            };
            //上传图片到云端
            UploadImage(selectImagePath, uploadSuccessAction);
        }
        /// <summary>
        /// 上传图片方法
        /// </summary>
        /// <param name="selectImagePath">裁剪后的图片路径</param>
        /// <param name="imageView"></param>
        /// <param name="uploadResultAction"></param>
        void UploadImage(string selectImagePath, Action<bool> uploadResultAction)
        {
            try
            {
                //1.读取裁剪后的图片,然后删除
                var imageBytes = Shared.IO.FileUtils.ReadFile(selectImagePath);
                System.IO.File.Delete(selectImagePath);
                var waitPage = new Loading();
                bodyView.AddChidren(waitPage);
                waitPage.Start(Language.StringByID(StringId.PleaseWait));
                //开始上传
                new Thread(() =>
                {
                    try
                    {
                        string base64string = Convert.ToBase64String(imageBytes);
                        var pack = new HttpServerRequest().UpdataMemberFace(memberInfo.id, base64string);
                        if (pack != null)
                        {
                            var data = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string,string>>(pack.Data.ToString());
                            if(data!= null)
                            {
                                data.TryGetValue("userFace",out memberInfo.userFaceUrl);
                            }
                            Utlis.WriteLine("上传结果:" + pack.message);
                            if(pack.Code == StateCode.SUCCESS)
                            {
                                Application.RunOnMainThread(() =>
                                {
                                    this.RemoveFromParent();
                                });
                            }
                            uploadResultAction?.Invoke(pack.Code == StateCode.SUCCESS);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        Application.RunOnMainThread(() =>
                        {
                            if (waitPage != null)
                            {
                                waitPage.RemoveFromParent();
                                waitPage = null;
                            }
                        });
                    }
                })
                { IsBackground = true }.Start();
            }
            catch (Exception ex)
            {
                MainPage.Log(ex.Message);
            }
        }
    }
}