Beispiel #1
0
        /// <summary>
        /// 调用阿里云接口
        /// </summary>
        /// <param name="annex"></param>
        /// <returns></returns>
        private async Task <IDCardPhotoRecord> PostALiYun(OwnerCertificationAnnex annex)
        {
            string aLiYunApiUrl = ALiYunApiUrl;
            string appcode      = ALiYunApiAppCode;

            var    url      = _ownerCertificationAnnexRepository.GetPath(annex.Id.ToString());
            string img_file = HttpRuntime.AppDomainAppPath.ToString() + url;

            bool is_old_format = false;

            string config = "{\\\"side\\\":\\\"face\\\"}";

            string method = "POST";

            string querys = "";

            using (FileStream fs = new FileStream(img_file, FileMode.Open))
            {
                BinaryReader br           = new BinaryReader(fs);
                byte[]       contentBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
                string       base64       = Convert.ToBase64String(contentBytes);
                string       bodys;
                if (is_old_format)
                {
                    bodys = "{\"inputs\" :" +
                            "[{\"image\" :" +
                            "{\"dataType\" : 50," +
                            "\"dataValue\" :\"" + base64 + "\"" +
                            "}";
                    if (config.Length > 0)
                    {
                        bodys += ",\"configure\" :" +
                                 "{\"dataType\" : 50," +
                                 "\"dataValue\" : \"" + config + "\"}" +
                                 "}";
                    }
                    bodys += "]}";
                }
                else
                {
                    bodys = "{\"image\":\"" + base64 + "\"";
                    if (config.Length > 0)
                    {
                        bodys += ",\"configure\" :\"" + config + "\"";
                    }
                    bodys += "}";
                }
                HttpWebRequest  httpRequest  = null;
                HttpWebResponse httpResponse = null;

                if (0 < querys.Length)
                {
                    aLiYunApiUrl = aLiYunApiUrl + "?" + querys;
                }

                if (aLiYunApiUrl.Contains("https://"))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(aLiYunApiUrl));
                }
                else
                {
                    httpRequest = (HttpWebRequest)WebRequest.Create(aLiYunApiUrl);
                }
                httpRequest.Method = method;
                httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
                //根据API的要求,定义相对应的Content-Type
                httpRequest.ContentType = "application/json; charset=UTF-8";
                if (0 < bodys.Length)
                {
                    byte[] data = Encoding.UTF8.GetBytes(bodys);
                    using (Stream stream = httpRequest.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                try
                {
                    httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    httpResponse = (HttpWebResponse)ex.Response;
                }
                string json;
                if (httpResponse.StatusCode != HttpStatusCode.OK)
                {
                    Console.WriteLine("http error code: " + httpResponse.StatusCode);
                    Console.WriteLine("error in header: " + httpResponse.GetResponseHeader("X-Ca-Error-Message"));
                    Console.WriteLine("error in body: ");
                    Stream       st     = httpResponse.GetResponseStream();
                    StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                    json = reader.ReadToEnd();
                    Console.WriteLine(reader.ReadToEnd());
                }
                else
                {
                    Stream       st     = httpResponse.GetResponseStream();
                    StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                    json = reader.ReadToEnd();
                }
                var entity = await _iDCardPhotoRecordRepository.AddAsync(new IDCardPhotoRecordDto
                {
                    ApplicationRecordId       = annex.ApplicationRecordId.ToString(),
                    OwnerCertificationAnnexId = annex.Id.ToString(),
                    Message         = json,
                    OperationTime   = DateTimeOffset.Now,
                    OperationUserId = "system",
                    PhotoBase64     = base64,
                });

                return(entity);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 后台执行校验 上传图片认证信息
        /// </summary>
        /// <param name="annex"></param>
        private async Task <ApiResult> Verification(OwnerCertificationAnnex annex)
        {
            var ownerCertificationRecordEntity = await _ownerCertificationRecordRepository.GetIncludeAsync(annex.ApplicationRecordId.ToString());

            try
            {
                OwnerCertificationRecordDto dto = new OwnerCertificationRecordDto
                {
                    OperationTime   = DateTimeOffset.Now,
                    OperationUserId = "system",
                    Id = ownerCertificationRecordEntity.Id.ToString()
                };

                try
                {
                    var entity = await PostALiYun(annex);

                    JsonModel json = new JsonModel();
                    try
                    {
                        json = JsonConvert.DeserializeObject <JsonModel>(entity.Message);
                        if (string.IsNullOrEmpty(json.Num))
                        {
                            throw new NotImplementedException("未识别到身份证信息,请提交正规清晰的身份证照片!");
                        }
                    }
                    catch (Exception)
                    {
                        throw new NotImplementedException("未识别到身份证信息,请提交正规清晰的身份证照片!");
                    }
                    var owner = (await _ownerRepository.GetListForLegalizeIncludeAsync(new OwnerDto {
                        IndustryId = ownerCertificationRecordEntity.IndustryId.ToString()
                    })).Where(x => x.IDCard == json.Num).FirstOrDefault();

                    if (owner != null)
                    {
                        dto.CertificationStatusValue = OwnerCertificationStatus.Success.Value;
                        dto.CertificationStatusName  = OwnerCertificationStatus.Success.Name;
                        dto.OwnerId             = owner.Id.ToString();
                        dto.OwnerName           = owner.Name.ToString();
                        dto.CertificationResult = "认证通过";
                    }
                    else
                    {
                        dto.CertificationStatusValue = OwnerCertificationStatus.Failure.Value;
                        dto.CertificationStatusName  = OwnerCertificationStatus.Failure.Name;
                        dto.CertificationResult      = "未查询到相关业主信息";
                    }
                }
                catch (Exception e)
                {
                    dto.CertificationStatusValue = OwnerCertificationStatus.Failure.Value;
                    dto.CertificationStatusName  = OwnerCertificationStatus.Failure.Name;
                    dto.CertificationResult      = e.Message;
                    throw new NotImplementedException(e.Message);
                }
                var recordEntity = await _ownerCertificationRecordRepository.UpdateStatusAsync(dto);

                if (string.IsNullOrWhiteSpace(dto.OwnerId))
                {
                    throw new NotImplementedException("未查询到相关业主信息");
                }

                await _ownerRepository.UpdateForLegalizeAsync(new OwnerDto
                {
                    OwnerCertificationRecordId = ownerCertificationRecordEntity.Id.ToString(),
                    Id   = recordEntity.OwnerId.ToString(),
                    Name = dto.OwnerName,
                });

                return(new ApiResult(APIResultCode.Success));
            }
            catch (Exception e)
            {
                await _ownerCertificationRecordRepository.UpdateInvalidAsync(new OwnerCertificationRecordDto
                {
                    Id              = ownerCertificationRecordEntity.Id.ToString(),
                    OperationTime   = DateTimeOffset.Now,
                    OperationUserId = "system",
                });

                await _ownerCertificationRecordRepository.DeleteAsync(new OwnerCertificationRecordDto
                {
                    Id              = ownerCertificationRecordEntity.Id.ToString(),
                    OperationTime   = DateTimeOffset.Now,
                    OperationUserId = "system",
                });

                throw new NotImplementedException(e.Message);
            }
        }