/// <summary> /// 通过ServerID(mediaid)从微信服务器上下载图片,保存到本地,并返回文件名 /// </summary> /// <param name="mediaid"></param> /// <returns></returns> public async Task <string> Get(string mediaid) { var mongo = new MongoDBHelper <WeixinImgFileModels>("weixinImgFile"); //查询mongo中是否存储了mediaid对应的照片文件 var doc = await mongo.SelectOneAsync(x => x.MediaId == mediaid); if (doc != null) { return(doc.FileName); } //如果文件没有下载过,则下载 //http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID var queryString = HttpUtility.ParseQueryString(string.Empty); queryString["access_token"] = await Get(); queryString["media_id"] = mediaid; var uri = "http://file.api.weixin.qq.com/cgi-bin/media/get?" + queryString; HttpResponseMessage response; response = await client.GetAsync(uri); var msg = await response.Content.ReadAsStreamAsync(); var fileName = response.Content.Headers.ContentDisposition.FileName.Replace("\"", ""); var helper = new ProjecToxfordClientHelper(); var content = await FileHelper.ReadAsync(msg); FileHelper.SaveFile(content, fileName); await mongo.InsertAsync(new WeixinImgFileModels() { FileName = fileName, MediaId = mediaid }); return(fileName); }
public async Task <HttpResponseMessage> Verify(string faceId1, string faceId2) { var key = "verify"; var mongo = new MongoDBHelper <VerifyModels>("faceverify"); //先检查数据库中是否有上次比较的结果 var doc = await mongo.SelectOneAsync(x => (x.FaceID1 == faceId1 && x.FaceID2 == faceId2) ); if (doc != null) { var mongoResult = new { faceID1 = doc.FaceID1, faceID2 = doc.FaceID2, confidence = doc.Confidence, isIdentical = doc.IsIdentical }.ToJson(); //var apiResult = doc.ToJson<VerifyModels>(); return(client.CreateHttpResponseMessage( Request, new Models.ProjecToxfordResponseModels(mongoResult, HttpStatusCode.OK))); } //如果之前的结果没有查询到,则提交牛津查询 var result = await client.PostAsync( key, new { faceId1 = faceId1, faceId2 = faceId2 } ); if (result.StatusCode == HttpStatusCode.OK) { var tmp = Newtonsoft.Json.Linq.JObject.Parse(result.Message); //如果为了加速查询的话,我们采用两次写入 await mongo.InsertAsync(new VerifyModels() { FaceID1 = faceId1, FaceID2 = faceId2, Confidence = (double)tmp["confidence"], IsIdentical = (bool)tmp["isIdentical"] }); await mongo.InsertAsync(new VerifyModels() { FaceID1 = faceId2, FaceID2 = faceId1, Confidence = (double)tmp["confidence"], IsIdentical = (bool)tmp["isIdentical"] }); var resultJson = new { faceID1 = faceId1, faceID2 = faceId2, confidence = (double)tmp["confidence"], isIdentical = (bool)tmp["isIdentical"] }.ToJson(); return(client.CreateHttpResponseMessage( Request, new Models.ProjecToxfordResponseModels(resultJson, HttpStatusCode.OK))); } return(client.CreateHttpResponseMessage(Request, result)); }