wei
2021-08-27 1f5fdd892e8efc9f1babe1ace9d810803edef396
HDL_ON/UI/UI2/4-PersonalCenter/FaceManagement/FacePassagePage.cs
@@ -1,4 +1,7 @@
using System;
using System.Threading;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using HDL_ON.UI.CSS;
using Shared;
@@ -7,10 +10,44 @@
    public class FacePassagePage : FrameLayout
    {
        FrameLayout bodyView;
        FrameLayout contentView;
        Button btnCommunityAccessControlIcon;
        Button btnSetFaceId;
        Button btnEraseData;
        #region 图标选择部分图标
        /// <summary>
        /// 面容id
        /// 背景图选项区域
        /// </summary>
        string faceid = "";
        FrameLayout pictureOptionView;
        /// <summary>
        /// 背景图选项选择区域
        /// </summary>
        VerticalScrolViewLayout optionView;
        /// <summary>
        /// 拍照按钮
        /// </summary>
        Button btnTakePicture;
        /// <summary>
        /// 相册按钮
        /// </summary>
        Button btnAlbum;
        /// <summary>
        /// 取消按钮
        /// </summary>
        Button btnCancel;
        #endregion
        /// <summary>
        /// 住户详情
        /// </summary>
        CustomerObj customerObj = new CustomerObj();
        public FacePassagePage()
        {
@@ -23,7 +60,7 @@
            bodyView.BackgroundColor = CSS_Color.BackgroundColor;
            var contentView = new FrameLayout()
            contentView = new FrameLayout()
            {
                Y = Application.GetRealHeight(64),
                Height = Application.GetRealHeight(667-64),
@@ -82,7 +119,7 @@
            communityRow.AddChidren(btnCommunityTitle);
            var btnCommunityAccessControlIcon = new Button()
            btnCommunityAccessControlIcon = new Button()
            {
                X = Application.GetRealWidth(314),
                Gravity = Gravity.CenterVertical,
@@ -93,7 +130,20 @@
            };
            communityRow.AddChidren(btnCommunityAccessControlIcon);
            btnCommunityAccessControlIcon.MouseUpEventHandler = (sender, e) => {
                if(customerObj.faceClose == 3)//没有人脸数据不能开启人脸通行功能
                {
                    return;
                }
                btnCommunityAccessControlIcon.IsSelected = !btnCommunityAccessControlIcon.IsSelected;
                int switchStatus = btnCommunityAccessControlIcon.IsSelected == true ? 1 :2;
                //失败回调
                Action action = () => {
                    btnCommunityAccessControlIcon.IsSelected = !btnCommunityAccessControlIcon.IsSelected;
                };
                editFaceInfo(switchStatus,action);
            };
            #endregion
@@ -110,7 +160,7 @@
            contentView.AddChidren(btnTip);
            var btnSetFaceId = new Button()
            btnSetFaceId = new Button()
            {
                Y = Application.GetRealHeight(539),
                Gravity = Gravity.CenterHorizontal,
@@ -125,32 +175,378 @@
                IsBold = true,
            };
            contentView.AddChidren(btnSetFaceId);
            btnSetFaceId.MouseUpEventHandler = (sender, e) => {
                LoadPictureOptionView();
            };
            if (string.IsNullOrEmpty(faceid))
            btnEraseData = new Button()
            {
            }
            else
            {
                btnSetFaceId.TextID = StringId.ResetFace;
                var btnEraseData = new Button()
                {
                    Y = Application.GetRealHeight(481),
                    Gravity = Gravity.CenterHorizontal,
                    Width = Application.GetRealWidth(220),
                    Height = Application.GetRealWidth(44),
                    Radius = (uint)Application.GetRealWidth(22),
                    BackgroundColor = CSS_Color.WarningColor,
                    TextID = StringId.EraseData,
                    TextSize = CSS_FontSize.SubheadingFontSize,
                    TextColor = CSS_Color.MainBackgroundColor,
                    TextAlignment = TextAlignment.Center,
                    IsBold = true,
                Y = Application.GetRealHeight(481),
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth(220),
                Height = Application.GetRealWidth(44),
                Radius = (uint)Application.GetRealWidth(22),
                BackgroundColor = CSS_Color.WarningColor,
                TextID = StringId.EraseData,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextColor = CSS_Color.MainBackgroundColor,
                TextAlignment = TextAlignment.Center,
                IsBold = true,
            };
            btnEraseData.MouseUpEventHandler = (sender,e) => {
                Action action = () => {
                    initButtonStatus();
                };
                contentView.AddChidren(btnEraseData);
            }
                editFaceInfo(3,action);
            };
            ReadFaceInfo();
        }
        /// <summary>
        /// 获取人脸信息
        /// </summary>
        void ReadFaceInfo()
        {
            var waitPage = new Loading();
            waitPage.Start();
            new Thread(() =>
            {
                try
                {
                    var hsr = new HttpServerRequest();
                    var pack = hsr.GetCustomerInfo();
                    if (pack != null)
                    {
                        if (pack.Code == StateCode.SUCCESS)
                        {
                            var cus = Newtonsoft.Json.JsonConvert.DeserializeObject<CustomerObj>(pack.Data.ToString());
                            if (cus != null)
                            {
                                customerObj = cus;
                                Application.RunOnMainThread(() =>
                                {
                                    initButtonStatus();
                                });
                            }
                        }
                    }
                }catch(Exception ex)
                {
                    MainPage.Log($"读取人脸信息异常:{ex.Message}");
                }
                finally
                {
                    Application.RunOnMainThread(() => {
                        if (waitPage != null)
                        {
                            waitPage.RemoveFromParent();
                            waitPage = null;
                        }
                    });
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 清除人脸数据
        /// </summary>
        void editFaceInfo(int status,Action action)
        {
            var waitPage = new Loading();
            waitPage.Start();
            new Thread(() =>
            {
                try
                {
                    var hsr = new HttpServerRequest();
                    var pack = hsr.EditFaceFunction(status);
                    if (pack != null)
                    {
                        if (pack.Code == StateCode.SUCCESS)
                        {
                            customerObj.faceClose = status;
                        }
                        else
                        {
                            Application.RunOnMainThread(() =>
                            {
                                IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    MainPage.Log($"清除人脸信息异常:{ex.Message}");
                }
                finally
                {
                    Application.RunOnMainThread(() => {
                        if (waitPage != null)
                        {
                            waitPage.RemoveFromParent();
                            waitPage = null;
                        }
                        action?.Invoke();
                    });
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 初始化底部按钮
        /// </summary>
        void initButtonStatus()
        {
            if (customerObj.faceClose == 1 || customerObj.faceClose == 2)//人脸数据已经存在1:人脸通行开启;2:关闭
            {
                if (customerObj.faceStatus == 2)//人脸数据下发门口机成功
                {
                    btnCommunityAccessControlIcon.IsSelected = customerObj.faceClose == 1;//人脸通行功能是否开启
                    contentView.AddChidren(btnEraseData);
                    btnSetFaceId.TextID = StringId.ResetFace;
                    return;
                }
            }
                btnCommunityAccessControlIcon.IsSelected = false;
                btnEraseData.RemoveFromParent();
                btnSetFaceId.TextID = StringId.SetFace;
        }
        /// <summary>
        /// 加载图标选择选项
        /// </summary>
        void LoadPictureOptionView()
        {
            var pView = new FrameLayout()
            {
                BackgroundColor = CSS_Color.DialogTransparentColor1,
            };
            bodyView.AddChidren(pView);
            pictureOptionView = new FrameLayout()
            {
                Y = Application.GetRealHeight(445+50),
                Height = Application.GetRealHeight(250),
                AnimateSpeed = 0.3f,
                Animate = Animate.DownToUp,
            };
            pView.AddChidren(pictureOptionView);
            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);
            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 });
            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 });
            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);
            LoadEvent_PictureOptionViewEventList(pView);
        }
        /// <summary>
        /// 加载背景图选择区域事件列表
        /// </summary>
        void LoadEvent_PictureOptionViewEventList(FrameLayout pView)
        {
            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, 4, 6, 1000);
                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, 4, 6,1000);
                pictureOptionView.Parent.RemoveFromParent();
            };
        }
        /// <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.SelectPicture((imagePath) =>
                            {
                                CropImageCallBack(imagePath, 2, imageName);
                            }, imageName, 4, 6);
                        }
                        else
                        {
                            CropImage.TakePicture((imagePath) =>
                            {
                                CropImageCallBack(imagePath, 1,imageName);
                            }, imageName, 4, 6);
                        }
                    }else
                    {
                        ReadFaceInfo();
                    }
                };
                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().FaceSetting(base64string);
                        if (pack != null)
                        {
                            uploadResultAction?.Invoke(pack.Code == StateCode.SUCCESS);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        Application.RunOnMainThread(() =>
                        {
                            if (waitPage != null)
                            {
                                waitPage.RemoveFromParent();
                                waitPage = null;
                            }
                        });
                    }
                })
                { IsBackground = true }.Start();
            }
            catch { }
        }
    }
}