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

225 lines
8.7 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.Text;
using System.Web;
using HZQR.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Personnel.WebSite.Shanxi
{
/// <summary>
/// 山西交研接口对接类
/// </summary>
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
//正式地址
/// <summary>
/// 山西交研Get请求
/// </summary>
/// <param name="actionName">接口名称</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public static string requestSxjsGet(string actionName, string parameters)
{
string getUrl = url + actionName + parameters;
string reString = HZQR.Common.HttpUtil.HttpUrlGet(getUrl, 500);
return reString;
}
/// <summary>
/// 山西交研Post请求
/// </summary>
/// <param name="actionName">接口名称</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public static string requestSxjsPost(string actionName, IDictionary<string, string> parameters)
{
string getUrl = url + actionName;
//http请求
string reString = HZQR.Common.HttpUtil.HttpUrlPost(getUrl, parameters, null, null, 3000);
return reString;
}
/// <summary>
/// 登录接口
/// </summary>
/// <returns></returns>
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<string, string> parameters = new Dictionary<string, string>();
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;
}
/// <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.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;
}
/// <summary>
/// 退出登录
/// </summary>
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, "");
}
}
/// <summary>
/// 退出登录
/// </summary>
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);
}
}
/// <summary>
/// 获取会话ID
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 写入cookie
/// </summary>
/// <param name="cookieName"></param>
/// <param name="cookieValue"></param>
/// <param name="outTime">过期时间1440为24小时120为2小时</param>
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;
}
}
}