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,后续接口都需要用到
///
/// 视频监控接口Post请求
///
/// 接口名称
/// 参数
///
public static string requestPost(string actionName,
IDictionary parameters, IDictionary 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 方法 -> 取时间戳
///
/// 取时间戳,高并发情况下会有重复。想要解决这问题请使用sleep线程睡眠1毫秒。
///
/// 精确到毫秒
/// 返回一个长整数时间戳
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
///
/// 登录接口
///
///
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}×tamp={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;
}
///
/// 同步用户到视频系统
///
/// 命令(1:添加;2:修改)
/// 用户姓名
/// 用户账号
/// 用户密码
/// 手机号码
/// 用户状态
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 headParam = new Dictionary();
headParam.Add("token", token);
//参数
IDictionary parameters = new Dictionary();
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;
}
}
}