2025-03-27 15:05:14 +08:00

168 lines
7.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using HZQR.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Personnel.WebSite.Shanxi
{
public class videoHelper
{
protected static string videoUrl = ConfigurationManager.AppSettings["videoUrl"].ToString();
public static string url = videoUrl.Split('|')[0];//接口地址
public static string username = videoUrl.Split('|')[1]; //测试账号
public static string password = videoUrl.Split('|')[2]; //密码
public static string token = "";//token,后续接口都需要用到
/// <summary>
/// 视频监控接口Post请求
/// </summary>
/// <param name="actionName">接口名称</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public static string requestPost(string actionName,
IDictionary<string, string> parameters, IDictionary<string, string> headParam = null)
{
string getUrl = url + actionName;
//http请求
string contentType = "application/json";
string reString = HZQR.Common.HttpUtil.HttpUrlPost(getUrl, parameters, null, null, 3000, headParam, contentType);
//JObject info = new JObject();
//foreach (string key in parameters.Keys)
//{
// info[key] = parameters[key];
//}
//string postDataStr = info.ToString();
//string reString = HZQR.Common.Common.CommonHelper.HttpUrlPost(postDataStr, getUrl);
return reString;
}
#region ->
/// <summary>
/// 取时间戳高并发情况下会有重复。想要解决这问题请使用sleep线程睡眠1毫秒。
/// </summary>
/// <param name="AccurateToMilliseconds">精确到毫秒</param>
/// <returns>返回一个长整数时间戳</returns>
public static long GetTimeStamp(bool AccurateToMilliseconds = false)
{
if (AccurateToMilliseconds)
{
// 使用当前时间计时周期数636662920472315179减去1970年01月01日计时周期数621355968000000000除去删掉后面4位计数后四位计时单位小于毫秒快到不要不要再取整去小数点
//备注DateTime.Now.ToUniversalTime不能缩写成DateTime.Now.Ticks会有好几个小时的误差。
//621355968000000000计算方法 long ticks = (new DateTime(1970, 1, 1, 8, 0, 0)).ToUniversalTime().Ticks;
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;
}
else
{
//上面是精确到毫秒需要在最后除去10000这里只精确到秒只要在10000后面加三个0即可1秒等于1000毫米
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
}
#endregion
#region -> MD5转译
public static string GetMD5(string str)
{
MD5 _MD5 = new MD5CryptoServiceProvider();
byte[] t = _MD5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(str));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
#endregion
/// <summary>
/// 登录接口
/// </summary>
/// <returns></returns>
public static Models.VideoLogin LoginInfo(string Username = "", string Password = "")
{
Username = string.IsNullOrEmpty(Username) ? username : Username;
Password = string.IsNullOrEmpty(Password) ? password : Password;
Models.VideoLogin loginInfo = null;
string actionName = "/service/video.login?";
string timestamp = GetTimeStamp(true).ToString();//时间戳
StringBuilder sb = new StringBuilder();
sb.AppendFormat("ua={0}&pwd={1}&timestamp={2}", username, password, timestamp);
string singStr = sb.ToString() + "zsxzHi87";
string _Signstring = GetMD5(singStr);
sb.AppendFormat("&sign={0}", _Signstring);
string vedioLoginUrl = url + actionName + sb.ToString();
string json = HttpUtil.HttpUrlGet(vedioLoginUrl);
//接口请求失败
if (json == "error")
{
//山西交研接口请求出错
LogUtil.WriteLog(null, "post请求出错了,可能是由于您的网络环境差、不稳定或安全软件禁止访问网络," +
"您可在网络好时或关闭安全软件在重新访问网络。", DateTime.Now.ToString("yyyyMMdd_") + "sxjs");
return loginInfo;
}
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(Models.VideoLogin));
loginInfo = o as Models.VideoLogin;
//接口请求失败
if (loginInfo.code != 200)
{
LogUtil.WriteLog(null, json, DateTime.Now.ToString("yyyyMMdd_") + "sxVideo");
return loginInfo;
}
//登录成功
return loginInfo;
}
/// <summary>
/// 同步用户到视频系统
/// </summary>
/// <param name="Cmd">命令1添加2修改</param>
/// <param name="UName">用户姓名</param>
/// <param name="UAccount">用户账号</param>
/// <param name="Pwd">用户密码</param>
/// <param name="PhoneNum">手机号码</param>
/// <param name="UState">用户状态</param>
public static Models.VideoResult SyncUser(int cmd, string userName,
string userAccount, string password, string phoneNum, string state)
{
Models.VideoResult result = null;
string token = LoginInfo().data.token;
if (string.IsNullOrEmpty(token))
{
return result;
}
string actionName = "service/video.updateOrAddUser";
//请求头
IDictionary<string, string> headParam = new Dictionary<string, string>();
headParam.Add("token", token);
//参数
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("cmd", cmd.ToString());
parameters.Add("userName", userName);
parameters.Add("password", password);
parameters.Add("userAccount", userAccount);
parameters.Add("phoneNum", phoneNum);
parameters.Add("state", state);
string json = requestPost(actionName, parameters, headParam);
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(Models.VideoResult));
result = o as Models.VideoResult;
return result;
}
}
}