Example #1
0
        public Result <User> LoginByFace(string path)
        {
            Result <User> rlt = new Result <User>();

            try
            {
                string    result    = FaceInit.test_identify(path);
                FaceResut faceResut = JsonConvert.DeserializeObject <FaceResut>(result);
                if (faceResut.errno == 0)
                {
                    if (faceResut.data.result_num > 0)
                    {
                        int     user_id = Convert.ToInt32(faceResut.data.result[0].user_id);
                        decimal score   = Convert.ToDecimal(faceResut.data.result[0].score);
                        if (score > 70)
                        {
                            var user = Db.Users.FirstOrDefault(p => p.Id == user_id);
                            if (user == null)
                            {
                                rlt.HasError = true;
                                rlt.Msg      = "用户不存在!";
                            }
                            else
                            {
                                rlt.Entities = user;
                            }
                        }
                    }
                    else
                    {
                        rlt.HasError = true;
                        rlt.Msg      = "用户不存在!";
                    }
                }
                else
                {
                    rlt.HasError = true;
                    rlt.Msg      = "识别失败!" + FaceErrors.GetFaceError(faceResut.errno);
                }
            }
            catch (Exception ex)
            {
                rlt.HasError = true;
                rlt.Msg      = ex.Message;
            }
            return(rlt);
        }
Example #2
0
        public Result <bool> EditUser(User u)
        {
            Result <bool> rlt = new Result <bool>();

            try
            {
                var rptrlt = IsRepeat(u);
                if (rptrlt.Entities)
                {
                    rlt.Entities = false;
                    rlt.Msg      = rptrlt.Msg;
                }
                else
                { //判断图片发生变化
                    bool imgChange = false;
                    var  old       = Db.Users.AsNoTracking().First(p => p.Id == u.Id);
                    if (old.ImagePath != u.ImagePath)
                    {
                        imgChange = true;
                    }
                    var entity = Db.Entry(u);

                    entity.State = System.Data.Entity.EntityState.Modified;
                    Db.SaveChanges();
                    if (!string.IsNullOrEmpty(u.ImagePath) && imgChange)
                    {
                        try
                        {
                            if (System.IO.File.Exists(u.ImagePath))
                            {
                                string imgPath = System.IO.Path.Combine(ConfigurationUtil.GetConfiguration("ImageDir"), Guid.NewGuid() + ".jpg");
                                System.IO.File.Copy(u.ImagePath, imgPath);

                                //查询用户是否存在
                                string result  = FaceInit.test_get_user_info(u.Id);
                                var    faceRlt = JsonConvert.DeserializeObject <FaceResut>(result);
                                if (faceRlt.errno == 0)
                                {
                                    if (faceRlt.data.result == null || faceRlt.data.result.Count == 0)
                                    {
                                        result = FaceInit.test_user_add(u.Id.ToString(), imgPath);
                                    }
                                    else
                                    {
                                        result = FaceInit.test_user_update(u.Id.ToString(), imgPath);
                                    }
                                    faceRlt = JsonConvert.DeserializeObject <FaceResut>(result);
                                    if (faceRlt.errno == 0)//成功
                                    {
                                        try
                                        {
                                            System.IO.File.Delete(old.ImagePath);
                                        }
                                        catch (Exception ex)
                                        {
                                        }
                                        u.ImagePath = imgPath;
                                        u.FaceToken = faceRlt.data.face_token;
                                    }
                                    else//失败
                                    {
                                        u.ImagePath  = "";
                                        rlt.HasError = true;
                                        rlt.Msg      = "人脸识别信息更新失败,请使用【用户编辑】功能重新录入人脸信息!\r\n" + FaceErrors.GetFaceError(faceRlt.errno);
                                    }
                                }
                                else
                                {
                                    u.ImagePath  = "";
                                    rlt.HasError = true;
                                    rlt.Msg      = "人脸识别信息更新失败,请使用【用户编辑】功能重新录入人脸信息!\r\n" + FaceErrors.GetFaceError(faceRlt.errno);
                                }

                                Db.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            LogUtil.WriteLog(ex);
                            rlt.HasError = true;
                            rlt.Msg      = "人脸识别信息更新失败,请使用【用户编辑】功能重新录入人脸信息!\r\n" + ex.Message;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.WriteLog(ex);
                rlt.HasError = true;
                rlt.Msg      = ex.Message;
            }
            return(rlt);
        }
Example #3
0
        public Result <bool> AddUser(User user)
        {
            Result <bool> rlt = new Result <bool>();

            try
            {
                var rptrlt = IsRepeat(user);
                if (rptrlt.Entities)
                {
                    rlt.Entities = false;
                    rlt.Msg      = rptrlt.Msg;
                }
                else
                {
                    Db.Users.Add(user);
                    Db.SaveChanges();
                    if (!string.IsNullOrEmpty(user.ImagePath))
                    {
                        try
                        {
                            if (System.IO.File.Exists(user.ImagePath))
                            {
                                string imgPath = System.IO.Path.Combine(ConfigurationUtil.GetConfiguration("ImageDir"), Guid.NewGuid() + ".jpg");
                                System.IO.File.Copy(user.ImagePath, imgPath);
                                string resut   = FaceInit.test_user_add(user.Id.ToString(), imgPath);
                                var    faceRlt = JsonConvert.DeserializeObject <FaceResut>(resut);
                                if (faceRlt.errno == 0)//成功
                                {
                                    user.ImagePath = imgPath;
                                    user.FaceToken = faceRlt.data.face_token;
                                }
                                else//失败
                                {
                                    user.ImagePath = "";
                                    rlt.HasError   = true;
                                    rlt.Msg        = "人脸识别信息注册失败,请使用【用户编辑】功能重新录入人脸信息!\r\n" + FaceErrors.GetFaceError(faceRlt.errno);
                                }
                                Db.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            LogUtil.WriteLog(ex);
                            rlt.HasError = true;
                            rlt.Msg      = "人脸识别信息注册失败,请使用【用户编辑】功能重新录入人脸信息!\r\n" + ex.Message;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.WriteLog(ex);
                rlt.HasError = true;
                rlt.Msg      = ex.Message;
            }
            return(rlt);
        }