using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using HZQR.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Personnel.WebSite.Shanxi
{
///
/// 山西交研接口对接类
///
public class sxjsHelper
{
protected static string sxjs = ConfigurationManager.AppSettings["sxjs"].ToString();
//public static string username = "superadm"; //测试账号
//public static string password = "superAdm123"; //密码
//public static string JSESSIONID = "";//会话ID
////public static string url = "http://154.8.169.153:8765/sxjs/a/";//测试地址
//public static string url = "http://124.165.206.9:11944/sxjs/a/";//正式地址
public static string url = sxjs.Split('|')[0];//接口地址
public static string username = sxjs.Split('|')[1]; //测试账号
public static string password = sxjs.Split('|')[2]; //密码
public static string JSESSIONID = "";//会话ID
//正式地址
///
/// 山西交研Get请求
///
/// 接口名称
/// 参数
///
public static string requestSxjsGet(string actionName, string parameters)
{
string getUrl = url + actionName + parameters;
string reString = HZQR.Common.HttpUtil.HttpUrlGet(getUrl, 500);
return reString;
}
///
/// 山西交研Post请求
///
/// 接口名称
/// 参数
///
public static string requestSxjsPost(string actionName, IDictionary parameters)
{
string getUrl = url + actionName;
//http请求
string reString = HZQR.Common.HttpUtil.HttpUrlPost(getUrl, parameters, null, null, 3000);
return reString;
}
///
/// 登录接口
///
///
public static Models.LoginInfo LoginInfo(string Username = "", string Password = "")
{
Username = string.IsNullOrEmpty(Username) ? username : Username;
Password = string.IsNullOrEmpty(Password) ? password : Password;
Models.LoginInfo loginInfo = null;
string actionName = "login;JSESSIONID=21a65e45a78f415c9c21f9376df9c1d3?__ajax=true&mobileLogin=true";
//参数
IDictionary parameters = new Dictionary();
parameters.Add("username", HttpUtility.UrlEncode(Username));
parameters.Add("password", HttpUtility.UrlEncode(Password));
parameters.Add("mobileLogin", HttpUtility.UrlEncode("false"));
string json = requestSxjsPost(actionName, parameters);
//接口请求失败
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.LoginResult));
Models.LoginResult loginResult = o as Models.LoginResult;
//接口请求失败
if (!loginResult.success)
{
LogUtil.WriteLog(null, json, DateTime.Now.ToString("yyyyMMdd_") + "sxjs");
return loginInfo;
}
//登录成功
loginInfo = loginResult.body;
return loginInfo;
}
///
/// 同步用户到安全管控平台
///
/// 命令(1:添加;2:修改)
/// 用户姓名
/// 用户账号
/// 用户密码
/// 手机号码
/// 用户状态
public static Models.Result SyncUser(string Cmd, string UName, string UAccount, string Pwd, string PhoneNum, string UState)
{
//Models.LoginInfo loginInfo = LoginInfo();
//string JSessionId = sxjs.sxjsHelper.GetJSessionId();
//if (loginInfo == null)
//{
// return;
//}
Models.Result result = null;
string JSessionId = GetJSESSIONID();
if (string.IsNullOrEmpty(JSessionId))
{
return result;
}
string actionName = string.Format(@"sys/register/sxjsSyncUser;JSESSIONID={0}?__ajax=true&mobileLogin=true", JSessionId);
StringBuilder sb = new StringBuilder();
sb.AppendFormat("&Cmd={0}", Cmd);
sb.AppendFormat("&UName={0}", UName);
sb.AppendFormat("&UAccount={0}", UAccount);
sb.AppendFormat("&Pwd={0}", Pwd);
//sb.AppendFormat("&Pwd={0}", HZQR.Common.Common.CommonHelper.SHA1_Encrypt(Pwd));
sb.AppendFormat("&PhoneNum={0}", PhoneNum);
sb.AppendFormat("&UState={0}", UState);
string param = sb.ToString();
LogUtil.WriteLog(null, param, "SyncUser");
string json = requestSxjsGet(actionName, param);
LogUtil.WriteLog(null, json, "SyncUser");
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(Models.Result));
result = o as Models.Result;
return result;
}
///
/// 退出登录
///
public static void Logout()
{
string JSessionId = GetJSESSIONID();
if (!string.IsNullOrEmpty(JSessionId))
{
string actionName = string.Format(@"logout?JSESSIONID={0}&__ajax=true&mobileLogin=true", JSessionId);
string reString = requestSxjsGet(actionName, "");
}
}
///
/// 退出登录
///
public static void GetUserList()
{
string JSessionId = GetJSESSIONID();
if (!string.IsNullOrEmpty(JSessionId))
{
//系统用户
string actionName = string.Format(@"/sys/user/list;JSESSIONID={0}?__ajax=true&mobileLogin=true", JSessionId);
//参数
string reString = requestSxjsPost(actionName, null);
}
}
///
/// 获取会话ID
///
///
public static string GetJSESSIONID()
{
string JSessionId = "";
//string JSessionId = GetCookie("JSESSIONID");
//if (JSessionId == "")
//{
Models.LoginInfo loginInfo = LoginInfo();
JSessionId = loginInfo.JSESSIONID;
if (string.IsNullOrEmpty(JSessionId))
{
return JSessionId;
}
//SetCookie("JSESSIONID", JSessionId, 120);
// }
return JSessionId;
}
///
/// 写入cookie
///
///
///
/// 过期时间,1440为24小时;120为2小时
public static void SetCookie(string cookieName, string cookieValue, int outTime = 1440)
{
//声明、写入cookies
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value = cookieValue;
cookie.Expires = DateTime.Now.AddMinutes(outTime);
//这句很重要,不能丢
HttpContext.Current.Response.Cookies.Add(cookie);
}
public static string GetCookie(string cookieName)
{
string cookieValue = "";
try
{
if (HttpContext.Current.Request.Cookies[cookieName] != null)
{
cookieValue = HttpContext.Current.Request.Cookies[cookieName].Value;
}
}
catch (Exception)
{
}
return cookieValue;
}
}
}