xm
2021-12-01 6d73bf6e816570291865674bef8bce8972e4de3f
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,54 @@
    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>
        /// imageHeight
        /// OutputYSize 400代表Y分辨率高的值,所以最终得到图片的分辨率为:266*400
        /// 0代表不指定不压缩,直接保存裁剪后的图片,目前只对iOS有效 Android设置小于0的话默认800
        /// </summary>
#if __IOS__
        const int imageHeight = 0;
#else
        const int imageHeight = 400;
#endif
        /// <summary>
        /// 住户详情
        /// </summary>
        CustomerObj customerObj = new CustomerObj();
        public FacePassagePage()
        {
@@ -23,10 +70,10 @@
            bodyView.BackgroundColor = CSS_Color.BackgroundColor;
            var contentView = new FrameLayout()
            contentView = new FrameLayout()
            {
                Y = Application.GetRealHeight(64),
                Height = Application.GetRealHeight(667-64),
                Height = Application.GetRealHeight(667 - 64),
            };
            bodyView.AddChidren(contentView);
@@ -59,9 +106,9 @@
            };
            topView.AddChidren(btnText1);
            topView.AddChidren(new Button() { Height = 1,BackgroundColor = CSS_Color.DividingLineColor,Y = Application.GetRealHeight(188) ,X = Application.GetRealWidth(16),Width = Application.GetRealWidth(359)});
            topView.AddChidren(new Button() { Height = 1, BackgroundColor = CSS_Color.DividingLineColor, Y = Application.GetRealHeight(188), X = Application.GetRealWidth(16), Width = Application.GetRealWidth(359) });
            #region 社区门禁通行
#region 社区门禁通行
            var communityRow = new FrameLayout()
            {
                Y = topView.Bottom,
@@ -82,7 +129,7 @@
            communityRow.AddChidren(btnCommunityTitle);
            var btnCommunityAccessControlIcon = new Button()
            btnCommunityAccessControlIcon = new Button()
            {
                X = Application.GetRealWidth(314),
                Gravity = Gravity.CenterVertical,
@@ -93,9 +140,24 @@
            };
            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 = () =>
                {
                    initButtonStatus();
                };
                editFaceInfo(switchStatus, action);
            };
            #endregion
#endregion
            var btnTip = new Button()
@@ -110,7 +172,7 @@
            contentView.AddChidren(btnTip);
            var btnSetFaceId = new Button()
            btnSetFaceId = new Button()
            {
                Y = Application.GetRealHeight(539),
                Gravity = Gravity.CenterHorizontal,
@@ -125,32 +187,396 @@
                IsBold = true,
            };
            contentView.AddChidren(btnSetFaceId);
            if (string.IsNullOrEmpty(faceid))
            btnSetFaceId.MouseUpEventHandler = (sender, e) =>
            {
            }
            else
                LoadPictureOptionView();
            };
            //抹掉数据
            btnEraseData = new Button()
            {
                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,
            };
            btnEraseData.MouseUpEventHandler = (sender, e) =>
            {
                Action action = () =>
                {
                    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,
                    initButtonStatus();
                };
                contentView.AddChidren(btnEraseData);
                editFaceInfo(3, action);
            };
            ReadFaceInfo();
        }
        /// <summary>
        /// 获取人脸信息
        /// </summary>
        void ReadFaceInfo(bool isLoading = true)
        {
            Loading waitPage = null;
            if (isLoading)
            {
                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, 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, 4, 6, imageHeight);
                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.TakePicture((imagePath) =>
                            {
                                CropImageCallBack(imagePath, 2, imageName);
                            }, imageName, 4, 6, imageHeight);
                        }
                        else
                        {
                            CropImage.SelectPicture((imagePath) =>
                            {
                                CropImageCallBack(imagePath, 1, imageName);
                            }, imageName, 4, 6, imageHeight);
                        }
                    }
                };
                if (isSuccess)
                {
                    ReadFaceInfo(false);
                }
                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);
                        //Utlis.WriteLine("上传图片Length:" + imageBytes.Length + " base64:" + base64string);
                        var pack = new HttpServerRequest().FaceSetting(base64string);
                        if (pack != null)
                        {
                            Utlis.WriteLine("上传结果:" + pack.message);
                            uploadResultAction?.Invoke(pack.Code == StateCode.SUCCESS);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        Application.RunOnMainThread(() =>
                        {
                            if (waitPage != null)
                            {
                                waitPage.RemoveFromParent();
                                waitPage = null;
                            }
                        });
                    }
                })
                { IsBackground = true }.Start();
            }
            catch { }
        }
    }
}