/// <summary> /// 读取当前魔蓝成员信息 /// </summary> /// <param name="row"></param> /// <returns></returns> private string GetMoredianMember(DataRow row) { try { #region 获取参数并简单校验 string Mobile = string.Empty; if (row.Table.Columns.Contains("Mobile")) { Mobile = row["Mobile"].ToString(); } if (string.IsNullOrEmpty(Mobile)) { return(new WxResponse(0, "用户不存在", null).toJson()); } if (Mobile.Length != 11) { return(new WxResponse(0, "用户不存在", null).toJson()); } DateTime DateNow = DateTime.Now; #endregion using (IDbConnection conn = new SqlConnection(PubConstant.WChat2020ConnectionString), erpConn = new SqlConnection(erpConnStr)) { #region 查询用户信息 Tb_User tb_User = conn.QueryFirstOrDefault <Tb_User>("SELECT * FROM Tb_User WITH(NOLOCK) WHERE Mobile = @Mobile", new { Mobile }); if (null == tb_User) { return(new WxResponse(0, "用户不存在", null).toJson()); } #endregion #region 获取AppToken TianChengEntranceSyncService.Config.EntranceConnectionStr = erpConnStr; TianChengEntranceSyncService.Config.WChat2020ConnectionStr = PubConstant.WChat2020ConnectionString; string app_token = AppTokenRedis.GetAppToken(); if (string.IsNullOrEmpty(app_token)) { return(new WxResponse(0, "获取AppToken失败,请联系管理员", null).toJson()); } #endregion // 成员信息不存在,进行创建 #region 先查询/创建组织机构 MoredianOrg moredianOrg = OrgRedis.GetMoredianOrg(); if (null == moredianOrg) { return(new WxResponse(0, "组织机构配置错误,请联系管理员", null).toJson()); } #endregion #region 查询人员信息 MoredianMember moredianMember = MemberRedis.GetMoredianMember(Mobile); #endregion return(new WxResponse(200, "获取成功", moredianMember).toJson()); } } catch (Exception ex) { GetLog().Error(ex.Message + Environment.CommandLine + ex.StackTrace); return(new WxResponse(0, "响应异常", null).toJson()); } }
private string GetVisitorInfo(DataRow row) { try { #region 获取参数并简单校验 string RecordID = string.Empty; if (row.Table.Columns.Contains("RecordID")) { RecordID = row["RecordID"].ToString(); } if (string.IsNullOrEmpty(RecordID)) { return(new WxResponse(0, "该记录不存在或已过期", null).toJson()); } #endregion using (IDbConnection erpConn = new SqlConnection(erpConnStr)) { dynamic info = erpConn.QueryFirstOrDefault("SELECT b.CommName, b.RoomSign, b.RoomName, a.MemberName, a.MemberMobile, a.MemberType, a.BeginDate, a.EndDate FROM Tb_HSPR_Entrance_Group_Visitor_Record a WITH(NOLOCK) JOIN view_HSPR_Room_Filter b WITH(NOLOCK) ON a.CommID = b.CommID AND a.RoomID = b.RoomID WHERE a.RecordID = @RecordID", new { RecordID }); if (null == info) { return(new WxResponse(0, "该记录不存在或已过期", null).toJson()); } string Mobile = Convert.ToString(info.MemberMobile); #region 先查询/创建组织机构 TianChengEntranceSyncService.Config.EntranceConnectionStr = erpConnStr; TianChengEntranceSyncService.Config.WChat2020ConnectionStr = PubConstant.WChat2020ConnectionString; MoredianOrg moredianOrg = OrgRedis.GetMoredianOrg(); if (null == moredianOrg) { return(new WxResponse(0, "组织机构配置错误,请联系管理员", null).toJson()); } #endregion #region 获取组织机构的AccessToken string access_token = AccessTokenRedis.GetAccessToken(Convert.ToString(moredianOrg.orgId), moredianOrg.orgAuthKey); if (string.IsNullOrEmpty(access_token)) { return(new WxResponse(0, "获取AccessToken失败,请联系管理员", null).toJson()); } #endregion MoredianMember moredianMember = MemberRedis.GetMoredianMember(Mobile); if (null != moredianMember) { info.showFace = moredianMember.showFace; } else { info.showFace = ""; } return(new WxResponse(200, "查询成功", info).toJson()); } } catch (Exception ex) { GetLog().Error(ex.Message + Environment.CommandLine + ex.StackTrace); return(new WxResponse(0, "响应异常", null).toJson()); } }
/// <summary> /// 外部串行控制,不需要再开线程 /// </summary> public static MoredianOrg Run() { try { /* 1.初始化组织机构*/ // 从Redis获取组织机构,如果不存在就去数据库查并缓存,如果数据库也不存在,就进行创建然后存储到数据库并缓存 MoredianOrg moredianOrg = OrgRedis.GetMoredianOrg(); return(moredianOrg); } catch (Exception) { return(null); } }
/// <summary> /// 发起远程开门 /// </summary> /// <param name="row"></param> /// <returns></returns> private string RemoteOpenDoor(DataRow row) { try { #region 获取参数并简单校验 string Mobile = string.Empty; if (row.Table.Columns.Contains("Mobile")) { Mobile = row["Mobile"].ToString(); } if (string.IsNullOrEmpty(Mobile)) { return(new WxResponse(0, "用户不存在", null).toJson()); } if (Mobile.Length != 11) { return(new WxResponse(0, "用户不存在", null).toJson()); } if (!row.Table.Columns.Contains("UserID") || !int.TryParse(row["UserID"].ToString(), out int UserID)) { return(new WxResponse(0, "用户不存在", null).toJson()); } string DeviceId = string.Empty; if (row.Table.Columns.Contains("DeviceId")) { DeviceId = row["DeviceId"].ToString(); } if (string.IsNullOrEmpty(DeviceId)) { return(new WxResponse(0, "请选择要开的门", null).toJson()); } DateTime DateNow = DateTime.Now; #endregion using (IDbConnection conn = new SqlConnection(PubConstant.WChat2020ConnectionString), erpConn = new SqlConnection(erpConnStr)) { #region 查询用户信息 Tb_User tb_User = conn.QueryFirstOrDefault <Tb_User>("SELECT * FROM Tb_User WITH(NOLOCK) WHERE Id = @Id AND Mobile = @Mobile", new { Id = UserID, Mobile }); if (null == tb_User) { return(new WxResponse(0, "用户不存在", null).toJson()); } #endregion #region 获取AppToken TianChengEntranceSyncService.Config.EntranceConnectionStr = erpConnStr; TianChengEntranceSyncService.Config.WChat2020ConnectionStr = PubConstant.WChat2020ConnectionString; string app_token = AppTokenRedis.GetAppToken(); if (string.IsNullOrEmpty(app_token)) { return(new WxResponse(0, "获取AppToken失败,请联系管理员", null).toJson()); } #endregion // 成员信息不存在,进行创建 #region 先查询/创建组织机构 MoredianOrg moredianOrg = OrgRedis.GetMoredianOrg(); if (null == moredianOrg) { return(new WxResponse(0, "组织机构配置错误,请联系管理员", null).toJson()); } #endregion #region 获取AccessToken string access_token = AccessTokenRedis.GetAccessToken(Convert.ToString(moredianOrg.orgId), moredianOrg.orgAuthKey); if (string.IsNullOrEmpty(access_token)) { return(new WxResponse(0, "获取AccessToken失败,请联系管理员", null).toJson()); } #endregion #region 查询人员信息 MoredianMember moredianMember = MemberRedis.GetMoredianMember(Mobile); if (null == moredianMember) { return(new WxResponse(0, "请先使用“人脸采集”功能录入人脸信息后再使用远程开门", null).toJson()); } long memberId = moredianMember.memberId; string memberName = moredianMember.memberName; if (string.IsNullOrEmpty(memberName)) { memberName = moredianMember.mobile; } #endregion #region 查询设备是否存在 DeviceId = erpConn.QueryFirstOrDefault <string>("SELECT DeviceId FROM Tb_HSPR_Entrance_Device WITH(NOLOCK) WHERE DeviceId = @DeviceId", new { DeviceId }); if (string.IsNullOrEmpty(DeviceId)) { return(new WxResponse(0, "该设备不存在", null).toJson()); } #endregion #region 发起远程开门 { IMoredianApiClient client = new DefaultMoredianApiClient(TianChengEntranceSyncService.Config.MoreDian.APIURL); MoredianRemoteOpenDoorRequest request = new MoredianRemoteOpenDoorRequest { deviceId = Convert.ToInt64(DeviceId), memberId = memberId, memberName = memberName }; MoredianRemoteOpenDoorResponse response = client.Execute(request, app_token, access_token); if (!response.IsSucc()) { return(new WxResponse(0, response.Message, response).toJson()); } return(new WxResponse(200, "操作成功", response).toJson()); } #endregion } } catch (Exception ex) { GetLog().Error(ex.Message + Environment.CommandLine + ex.StackTrace); return(new WxResponse(0, "响应异常", null).toJson()); } }
/// <summary> /// 提交魔蓝成员信息(创建或者修改成员信息) /// </summary> /// <param name="row"></param> /// <returns></returns> private string SubmitMoredianMember(DataRow row) { try { #region 获取参数并简单校验 string Mobile = string.Empty; if (row.Table.Columns.Contains("Mobile")) { Mobile = row["Mobile"].ToString(); } if (string.IsNullOrEmpty(Mobile)) { return(new WxResponse(0, "用户不存在", null).toJson()); } if (Mobile.Length != 11) { return(new WxResponse(0, "用户不存在", null).toJson()); } string Face = string.Empty; if (row.Table.Columns.Contains("Face")) { Face = row["Face"].ToString(); } DateTime DateNow = DateTime.Now; #endregion using (IDbConnection conn = new SqlConnection(PubConstant.WChat2020ConnectionString), erpConn = new SqlConnection(erpConnStr)) { #region 获取AppToken TianChengEntranceSyncService.Config.EntranceConnectionStr = erpConnStr; TianChengEntranceSyncService.Config.WChat2020ConnectionStr = PubConstant.WChat2020ConnectionString; string app_token = AppTokenRedis.GetAppToken(); if (string.IsNullOrEmpty(app_token)) { return(new WxResponse(0, "获取AppToken失败,请联系管理员", null).toJson()); } #endregion #region 先查询/创建组织机构 MoredianOrg moredianOrg = OrgRedis.GetMoredianOrg(); if (null == moredianOrg) { return(new WxResponse(0, "组织机构配置错误,请联系管理员", null).toJson()); } #endregion #region 获取组织机构的AccessToken string access_token = AccessTokenRedis.GetAccessToken(Convert.ToString(moredianOrg.orgId), moredianOrg.orgAuthKey); if (string.IsNullOrEmpty(access_token)) { return(new WxResponse(0, "获取AccessToken失败,请联系管理员", null).toJson()); } #endregion MoredianMember moredianMember = MemberRedis.GetMoredianMember(Mobile); #region 读取人脸图片 byte[] showFace = null; string IncidentAcceptImageUrl = Global_Fun.AppWebSettings("IncidentAcceptImageUrl"); if (Face.Contains(IncidentAcceptImageUrl)) { string IncidentAcceptImageSavePath = Global_Fun.AppWebSettings("IncidentAcceptImageSavePath"); #region 从本地服务器读取图片 // 替换文件路径为本地路径 string localFacePath = Face.Replace(IncidentAcceptImageUrl, IncidentAcceptImageSavePath); if (!File.Exists(localFacePath)) { return(new WxResponse(0, "人脸图片不存在,请重试", null).toJson()); } FileStream fileStream = File.OpenRead(localFacePath); int filelength = (int)fileStream.Length; showFace = new byte[filelength]; fileStream.Read(showFace, 0, filelength); #endregion } else { #region 从网络获取图片内容 { try { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = Face, //URL 必需项 Method = "GET", //URL 可选项 默认为Get Timeout = 5000, //连接超时时间 可选项默认为100000 ReadWriteTimeout = 5000, //写入Post数据超时时间 可选项默认为30000 ResultType = ResultType.Byte, //返回数据类型,是Byte还是String ProtocolVersion = HttpVersion.Version11, //获取或设置用于请求的 HTTP 版本。默认为 System.Net.HttpVersion.Version11 }; HttpResult result = http.GetHtml(item); showFace = result.ResultByte; } catch (Exception ex) { return(new WxResponse(0, "读取人脸图片失败,请重试", ex.Message).toJson()); } } #endregion } #endregion #region 成员不存在,创建成员 if (null == moredianMember) { // 进行创建人员信息 IMoredianApiClient client = new DefaultMoredianApiClient(TianChengEntranceSyncService.Config.MoreDian.APIURL); MoredianCreateMemberRequest request = new MoredianCreateMemberRequest { moredianMember = new MoredianMember { mobile = Mobile, memberName = Mobile, }, showFace = showFace, verifyFace = showFace, }; MoredianCreateMemberResponse response = client.Execute(request, app_token, access_token); if (!response.IsSucc()) { return(new WxResponse(0, "上传人脸信息失败,请重试", response.Message).toJson()); } moredianMember = new MoredianMember { memberId = response.memberId, mobile = Mobile, showFace = Face }; erpConn.Execute("INSERT INTO [dbo].[Tb_HSPR_Entrance_Member](Mobile, MemberId, Face) VALUES (@Mobile, @MemberId, @Face)", new { Mobile, Face, MemberId = moredianMember.memberId }); moredianMember = MemberRedis.UpdateMoredianMember(Mobile); return(new WxResponse(200, "上传人脸信息成功", moredianMember).toJson()); } #endregion if (string.IsNullOrEmpty(moredianMember.showFace) && string.IsNullOrEmpty(Face)) { return(new WxResponse(200, "操作成功", null).toJson()); } #region 除人脸 if (string.IsNullOrEmpty(Face)) { // 更新显示图片 IMoredianApiClient client = new DefaultMoredianApiClient(TianChengEntranceSyncService.Config.MoreDian.APIURL); MoredianDeleteMemberVerifyFaceRequest request = new MoredianDeleteMemberVerifyFaceRequest { memberId = moredianMember.memberId, }; MoredianDeleteMemberVerifyFaceResponse response = client.Execute(request, app_token, access_token); if (!response.IsSucc()) { return(new WxResponse(0, "删除人脸信息失败", response.Message).toJson()); } erpConn.Execute("UPDATE Tb_HSPR_Entrance_Member SET Face = @Face WHERE Mobile = @Mobile AND MemberId = @MemberId", new { Mobile, Face, MemberId = moredianMember.memberId }); moredianMember = MemberRedis.UpdateMoredianMember(Mobile); return(new WxResponse(200, "删除人脸信息成功", moredianMember).toJson()); } #endregion #region 更新成员人脸 { // 更新显示图片 IMoredianApiClient client = new DefaultMoredianApiClient(TianChengEntranceSyncService.Config.MoreDian.APIURL); MoredianUpdateMemberVerifyFaceRequest request = new MoredianUpdateMemberVerifyFaceRequest { memberId = moredianMember.memberId, face = showFace }; MoredianUpdateMemberVerifyFaceResponse response = client.Execute(request, app_token, access_token); if (!response.IsSucc()) { return(new WxResponse(0, "更新人脸信息失败,请重试", response.Message).toJson()); } } { // 更新显示图片 IMoredianApiClient client = new DefaultMoredianApiClient(TianChengEntranceSyncService.Config.MoreDian.APIURL); MoredianUpdateMemberShowFaceRequest request = new MoredianUpdateMemberShowFaceRequest { memberId = moredianMember.memberId, face = showFace }; MoredianUpdateMemberShowFaceResponse response = client.Execute(request, app_token, access_token); // 显示图片不处理失败情况 } erpConn.Execute("UPDATE Tb_HSPR_Entrance_Member SET Face = @Face WHERE Mobile = @Mobile AND MemberId = @MemberId", new { Mobile, Face, MemberId = moredianMember.memberId }); moredianMember = MemberRedis.UpdateMoredianMember(Mobile); return(new WxResponse(200, "更新人脸信息成功", moredianMember).toJson()); #endregion } } catch (Exception ex) { GetLog().Error(ex.Message + Environment.CommandLine + ex.StackTrace); return(new WxResponse(0, "响应异常", null).toJson()); } }