#region 上传身份证
/// <summary>
/// 上传身份证
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("WxUptIDCard")]
public ApiResultModel WxUptIDCard()
{
try
{
int result;
long uid = Convert.ToInt64(HttpContext.Current.Request["uid"]);//用户唯一标识
string side = HttpContext.Current.Request["side"];//身份证正反面
ApiResultModel apiResultModel = new ApiResultModel();
HttpFileCollection files = HttpContext.Current.Request.Files;
if (files != null)
{
HttpPostedFile file = files[0];
//临时保存,用于验证,验证完成后删除
string path = System.Web.Hosting.HostingEnvironment.MapPath("~/IDCardImages/");
if (!Directory.Exists(path))
{
//目标文件夹不存在时自动创建
Directory.CreateDirectory(path);
}
string newPath = path + DateTime.Now.Ticks + file.FileName;
file.SaveAs(newPath);
#region 过期代码
//var bytes = System.IO.File.ReadAllBytes(newPath);//读取图片的字节
//var base64String = Convert.ToBase64String(bytes);//把图片转为base64编码格式
// //传到阿里云身份识别当中,读取信息
//IDCardResponseModel iDCardResponseModel = new IDCardResponseModel();
//iDCardResponseModel = JsonConvert.DeserializeObject<IDCardResponseModel>(ReadIDCardMess.GetIDCardMess(base64String));
#endregion
if (side == "face")
{
var iDCardFaceMess = AliyunReadIDCardMess.AliyunGetIDCard(newPath, side);
var FaceMess = JsonConvert.DeserializeObject<AliyunIDCardFaceDTO>(iDCardFaceMess);
if (string.IsNullOrEmpty(FaceMess.name))
{
apiResultModel.Success = 0;
apiResultModel.Message = "未识别到姓名";
return apiResultModel;
}
if (string.IsNullOrEmpty(FaceMess.num))
{
apiResultModel.Success = 0;
apiResultModel.Message = "未识别到身份证号";
return apiResultModel;
}
#region 删除临时照片
try
{
File.Delete(newPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion
if (FaceMess.success)//正面识别成功
{
string savePath = ImageUpload(file,uid);
var mess = db.User_WorkerInfo.Where(r => r.UID == uid).FirstOrDefault();
mess.Name = FaceMess.name;
mess.Sex = FaceMess.sex;
mess.Nation = FaceMess.nationality;
mess.Address = FaceMess.address;
mess.IDCardNumber = FaceMess.num;
mess.PositiveIDCardImageUrl = savePath;
FaceMess.birth = FaceMess.birth.Insert(4, "-");
FaceMess.birth = FaceMess.birth.Insert(7, "-");
mess.Birthday = Convert.ToDateTime(FaceMess.birth);
mess.ModifyTime = DateTime.Now;
mess.ModifyBy = uid;
result = db.SaveChanges();
if (result > 0)//正面添加成功
{
apiResultModel.Success = 1;
apiResultModel.Message = "成功";
apiResultModel.Data = db.User_WorkerInfo.Where(r => r.UID == uid).FirstOrDefault();
return apiResultModel;
}
else//正面添加失败
{
apiResultModel.Success = 0;
apiResultModel.Message = "添加失败,请重新上传正面";
return apiResultModel;
}
}
else//正面识别失败
{
apiResultModel.Message = "正面信息识别错误,请重新拍摄";
apiResultModel.ErrorCode = 2;
apiResultModel.Success = 0;
return apiResultModel;
}
}//识别正面
else if (side == "back")
{
var iDCardBackMess = AliyunReadIDCardMess.AliyunGetIDCard(newPath, side);
#region 删除临时照片
try
{
File.Delete(newPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion
var BackMess = JsonConvert.DeserializeObject<AliyunIDCardBackDTO>(iDCardBackMess);
if (BackMess.success)//反面识别成功
{
var mess = db.User_WorkerInfo.Where(r => r.UID == uid).FirstOrDefault();
BackMess.start_date = BackMess.start_date.Insert(4, "-");
BackMess.start_date = BackMess.start_date.Insert(7, "-");
if (BackMess.end_date == "长期")
{
BackMess.end_date = BackMess.end_date;
mess.ExpiryDate = null;
}
else
{
BackMess.end_date = BackMess.end_date.Insert(4, "-");
BackMess.end_date = BackMess.end_date.Insert(7, "-");
mess.ExpiryDate = Convert.ToDateTime(BackMess.end_date);
}
mess.StartDate = Convert.ToDateTime(BackMess.start_date);
string savePath = ImageUpload(file, uid);
mess.NegativeIDCardImageUrl = savePath;
mess.GrantOrg = BackMess.issue;
result = db.SaveChanges();
if (result > 0)//反面添加成功
{
apiResultModel.Success = 1;
apiResultModel.Message = "成功";
apiResultModel.Data = db.User_WorkerInfo.Where(r => r.UID == uid).FirstOrDefault();
return apiResultModel;
}
else//反面添加失败
{
apiResultModel.Success = 0;
apiResultModel.Message = "添加失败,请重新上传正面";
return apiResultModel;
}
}
else//反面识别失败
{
apiResultModel.Message = "反面信息识别错误,请重新拍摄";
apiResultModel.ErrorCode = 2;
apiResultModel.Success = 0;
return apiResultModel;
}
}//识别反面
else
{
#region 数加接口验证人脸信息
FaceSDK faceSDK = new FaceSDK(aliyun_AccessKeyId, aliyun_AccessKeySecret);
using(var img = Image.FromFile(newPath))
{
//使用base64编码检测
var base64Str = Base64Helper.ImageToBase64(img);
var model = faceSDK.AttributeBase64(base64Str);
string err_msg = "";
bool isError = false;
if (model.errno != 0)
{
isError = true;
err_msg = "人脸检测失败:" + model.err_msg;
}
if (model.face_num < 1)
{
isError = true;
err_msg = "未检测到人脸";
}
if (model.face_num > 1)
{
isError = true;
err_msg = "检测到多个人脸";
}
if (isError)
{
#region 删除临时照片
try
{
File.Delete(newPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion
apiResultModel.Success = 0;
apiResultModel.Message = err_msg;
return apiResultModel;
}
}
#endregion
string savePath = ImageUpload(file, uid);
var mess = db.User_WorkerInfo.Where(r => r.UID == uid).FirstOrDefault();
mess.FrontViewOfEmployees = savePath;
result = db.SaveChanges();
#region 删除临时照片
try
{
File.Delete(newPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#endregion
if (result > 0)//员工正面照添加成功
{
apiResultModel.Success = 1;
apiResultModel.Message = "员工正面照上传成功";
apiResultModel.Data = db.User_WorkerInfo.Where(r => r.UID == uid).FirstOrDefault();
return apiResultModel;
}
else//员工正面照添加失败
{
apiResultModel.Success = 0;
apiResultModel.Message = "员工正面照上传失败,请重新上传正照";
return apiResultModel;
}
}//员工正面照
}//有图片
else
{
apiResultModel.Success = 0;
apiResultModel.Message = "失败,请上传图片";
return apiResultModel;
}//没有图片
}
catch (Exception ex)
{
ApiResultModel apiResultModel = new ApiResultModel()
{
Message = "图片拍摄不清晰,请重新拍摄",
DebugMessage = ex.Message,
ErrorCode = 3,
Success = 0
};
return apiResultModel;
}
}
#region 上传
/// <summary>
/// 个人自拍照片,身份证正反面照片上传
/// </summary>
/// <param name="file"></param>
/// <param name="uid"></param>
/// <returns></returns>
private string ImageUpload(HttpPostedFile file, long uid)
{
try
{
Stream MyStream;
int FileLen;
FileLen = file.ContentLength;
// 读取文件的 byte[]
byte[] bytes = new byte[FileLen];
MyStream = file.InputStream;
MyStream.Read(bytes, 0, FileLen);
var base64Str = Convert.ToBase64String(bytes);
var SubmitData = new Dictionary<string, string>();
SubmitData.Add("FileBase64", base64Str);
FileInfo fi = new FileInfo(file.FileName);
SubmitData.Add("FileExtName", fi.Extension);
var TokenDict = new Dictionary<string, string>();
TokenDict.Add("uid", uid.ToString());
TokenDict.Add("timestamp", DateTimeHelper.GetTimeStamp());
var dictJson = JsonConvert.SerializeObject(TokenDict);
TokenDict.Add("sign", RSACryption.RSAEncrypt(RSAConfig.PublicKey, dictJson));
var Result = JsonConvert.DeserializeObject<ApiResultModel<ImageUploadResult>>(ApiNetHelper.Post(ApiUrl_Upload + "WeChatUser", SubmitData, TokenDict));
string savePath = Result.Data.Url;
return savePath;
}
catch
{
return "";
}
}
#endregion
#endregion