You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

348 line
14 KiB

  1. using Learun.Util;
  2. using Nancy;
  3. using System.Linq;
  4. using System.Configuration;
  5. using Learun.Application.TwoDevelopment.Permission;
  6. using System;
  7. using Learun.Cache.Factory;
  8. using Learun.Cache.Base;
  9. namespace Learun.Application.WebApi.Modules
  10. {
  11. public class SSOApi : BaseApi
  12. {
  13. private Perm_FunctionIBLL perm_FunctionIBLL = new Perm_FunctionBLL();
  14. private Perm_FunctionTypeIBLL perm_FunctionTypeIBLL = new Perm_FunctionTypeBLL();
  15. Perm_UserPermissionIBLL permUserPermissionIbll = new Perm_UserPermissionBLL();
  16. private Perm_FunctionVisitIBLL functionVisitIbll = new Perm_FunctionVisitBLL();
  17. private ICache cache = CacheFactory.CaChe();
  18. public SSOApi()
  19. : base("/quanjiang/sso")
  20. {
  21. Get["/list"] = GetList;
  22. Get["/list20"] = GetList20;
  23. Get["/goto"] = GoTo;
  24. Get["/goto20"] = GoToApplication;
  25. Get["/first"] = First;
  26. Post["/first"] = FirstPost;
  27. Get["/authorize"] = Authorize;
  28. }
  29. /// <summary>
  30. /// 统一身份认证2.0
  31. /// </summary>
  32. /// <param name="_"></param>
  33. /// <returns></returns>
  34. public Response Authorize(dynamic _)
  35. {
  36. string appid = Request.Query["appid"];
  37. string secret = Request.Query["secret"];
  38. string appkey = Request.Query["appkey"];
  39. if (string.IsNullOrEmpty(appid))
  40. {
  41. return Fail("参数:appid不能为空");
  42. }
  43. if (string.IsNullOrEmpty(secret))
  44. {
  45. return Fail("参数:secret不能为空");
  46. }
  47. if (string.IsNullOrEmpty(appkey))
  48. {
  49. return Fail("参数:appkey不能为空");
  50. }
  51. var application = perm_FunctionIBLL.GetPerm_FunctionEntity(appid);
  52. if (application != null)
  53. {
  54. if (Util.DESEncrypt.Decrypt(application.FSecret,
  55. ConfigurationManager.AppSettings["SSOPublicSecret"]).Equals(secret))
  56. {
  57. try
  58. {
  59. var code = DESEncrypt.Decrypt(appkey, "bjqjsso");
  60. if (!string.IsNullOrEmpty(cache.Read<string>(code)))
  61. {
  62. return Success(new { useraccount = cache.Read<string>(code) });
  63. }
  64. else
  65. {
  66. return Fail("appkey已过期");
  67. }
  68. }
  69. catch (Exception e)
  70. {
  71. return Fail("appkey错误");
  72. }
  73. }
  74. else
  75. {
  76. return Fail("secret错误");
  77. }
  78. }
  79. else
  80. return Fail("未授权的appid");
  81. }
  82. private Response FirstPost(dynamic _)
  83. {
  84. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  85. var ssoparam = this.GetReqData<SSOParam>();
  86. if (!string.IsNullOrEmpty(ssoparam.UPId))
  87. {
  88. Perm_FunctionEntity up = new Perm_FunctionEntity();
  89. up.UPUserName = ssoparam.UPUserName;
  90. up.UPPass = ssoparam.UPPass;
  91. up.FId = ssoparam.FId;
  92. up.UserId = ssoparam.UserId;
  93. perm_FunctionIBLL.SaveEntityByUPId(ssoparam.UPId, up);
  94. return Success(new { FInterfaceUrl = "/SSO/GoTo?sysid=" + DESEncrypt.Encrypt(up.FId, publickey) + "&openid=" + DESEncrypt.Encrypt(up.UserId, publickey) });
  95. }
  96. else
  97. {
  98. return Fail("参数错误");
  99. }
  100. }
  101. private Response First(dynamic _)
  102. {
  103. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  104. var ssoparam = this.GetReqData<SSOParam>();
  105. string sysid = ssoparam.sysid;
  106. string strsysid = DESEncrypt.Decrypt(sysid, publickey);
  107. string openid = ssoparam.openid;
  108. string userid = DESEncrypt.Decrypt(openid, publickey);
  109. var uplist = permUserPermissionIbll.GetPerm_UserPermissionEntityByFIdAndUid(strsysid, userid);
  110. if (uplist == null)
  111. {
  112. return Fail("用户未授权。");
  113. }
  114. var perfun = perm_FunctionIBLL.GetPerm_FunctionEntityByUPId(uplist.UPId);
  115. return Success(perfun);
  116. }
  117. public class SSOParam
  118. {
  119. public string sysid { get; set; }
  120. public string openid { get; set; }
  121. public string UPId { get; set; }
  122. public string FId { get; set; }
  123. public string UserId { get; set; }
  124. public string UPUserName { get; set; }
  125. public string UPPass { get; set; }
  126. public string appid { get; set; }
  127. }
  128. public Response GoToApplication(dynamic _)
  129. {
  130. try
  131. {
  132. var userinfo = userInfo;
  133. var ssoparam = this.GetReqData<SSOParam>();
  134. if (userinfo != null)
  135. {
  136. var Stype = userinfo.Description;
  137. if (Stype == "学生")
  138. {
  139. Stype = "1";
  140. }
  141. else
  142. {
  143. Stype = "2";
  144. }
  145. var perm_application = perm_FunctionIBLL.GetPerm_FunctionEntity(ssoparam.appid);
  146. if (perm_application != null)
  147. {
  148. //写入当前请求所登录的用户
  149. var code = Util.CommonHelper.RndNum(9);
  150. cache.Write(code, userinfo.account, TimeSpan.FromMinutes(10));
  151. var url = perm_application.FInterfaceUrl;
  152. if (url.Contains("?"))
  153. {
  154. url += "&appkey=" + DESEncrypt.Encrypt(code, "bjqjsso") + "&stype=" + Stype;
  155. }
  156. else
  157. {
  158. url += "?appkey=" + DESEncrypt.Encrypt(code, "bjqjsso") + "&stype=" + Stype;
  159. }
  160. return Success(new { FInterfaceUrl = url });
  161. }
  162. else
  163. return Fail("appid解析失败,请确认。");
  164. }
  165. else
  166. {
  167. return Fail("用户信息解析失败,请确认。");
  168. }
  169. }
  170. catch (Exception)
  171. {
  172. return Fail("参数错误。。。");
  173. }
  174. }
  175. public Response GoTo(dynamic _)
  176. {
  177. try
  178. {
  179. var ssoparam = this.GetReqData<SSOParam>();
  180. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  181. string sysid = ssoparam.sysid;
  182. string strsysid = DESEncrypt.Decrypt(sysid, publickey);
  183. string openid = ssoparam.openid;
  184. string userid = DESEncrypt.Decrypt(openid, publickey);
  185. var uplist = permUserPermissionIbll.GetPerm_UserPermissionEntityByFIdAndUid(strsysid, userid);
  186. Perm_FunctionVisitEntity functionVisitEntity = new Perm_FunctionVisitEntity();
  187. functionVisitEntity.Create();
  188. functionVisitEntity.Fid = strsysid;
  189. functionVisitEntity.PDate = DateTime.Now;
  190. functionVisitEntity.PUId = userid;
  191. var userinfo = userInfo;
  192. functionVisitEntity.PUName = userinfo.realName;
  193. if (uplist == null)
  194. {
  195. functionVisitEntity.PIsLoginSuccess = false;
  196. functionVisitEntity.PContent = "用户未授权";
  197. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  198. return Fail("用户未授权。");
  199. }
  200. var perfun = perm_FunctionIBLL.GetPerm_FunctionEntityByUPId(uplist.UPId);
  201. string secretkey = DESEncrypt.Decrypt(perfun.FSecret, publickey);
  202. if (perfun.FIsManagePage == true)
  203. {
  204. if (!string.IsNullOrEmpty(perfun.FInterfaceUrl))
  205. {
  206. if (!string.IsNullOrEmpty(perfun.UPUserName) && !string.IsNullOrEmpty(perfun.UPPass))
  207. {
  208. functionVisitEntity.PIsLoginSuccess = true;
  209. functionVisitEntity.PContent = "成功转到统一认证网站:" + perfun.FUrl;
  210. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  211. var Stype = LoginUserInfo.Get().Description;
  212. if (Stype == "学生")
  213. {
  214. Stype = "1";
  215. }
  216. else
  217. {
  218. Stype = "2";
  219. }
  220. return Success(new
  221. {
  222. FInterfaceUrl = perfun.FInterfaceUrl + "?u=" +
  223. DESEncrypt.Encrypt(DESEncrypt.Encrypt(perfun.UPUserName, secretkey),
  224. publickey) + "&p=" +
  225. DESEncrypt.Encrypt(DESEncrypt.Encrypt(perfun.UPPass, secretkey),
  226. publickey) + "&t=" +
  227. DESEncrypt.Encrypt(
  228. DESEncrypt.Encrypt(DateTime.Now.ToString("yyyyMMddHHmmss"), secretkey),
  229. publickey) + "&ip=" +
  230. DESEncrypt.Encrypt(DESEncrypt.Encrypt(GetIP(), secretkey),
  231. publickey) + "&stype=" + Stype
  232. });
  233. }
  234. else
  235. {
  236. functionVisitEntity.PIsLoginSuccess = false;
  237. functionVisitEntity.PContent = "用户未配置转到用户名密码配置页面";
  238. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  239. //用户未配置转到用户名密码配置页面
  240. return Success(new { FInterfaceUrl = "/SSO/FirstLogin?sysid=" + sysid + "&openid=" + openid });
  241. }
  242. }
  243. else
  244. {
  245. functionVisitEntity.PIsLoginSuccess = false;
  246. functionVisitEntity.PContent = "未配置登录接口地址";
  247. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  248. return Fail("未配置登录接口地址。");
  249. }
  250. }
  251. else
  252. {
  253. if (!string.IsNullOrEmpty(perfun.FUrl))
  254. {
  255. functionVisitEntity.PIsLoginSuccess = true;
  256. functionVisitEntity.PContent = "成功转到统一认证网站:" + perfun.FUrl;
  257. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  258. return Success(new { FInterfaceUrl = perfun.FUrl });
  259. }
  260. else
  261. {
  262. functionVisitEntity.PIsLoginSuccess = false;
  263. functionVisitEntity.PContent = "未配置地址";
  264. functionVisitIbll.SaveEntity(null, functionVisitEntity);
  265. return Fail("未配置地址。");
  266. }
  267. }
  268. }
  269. catch (Exception e)
  270. {
  271. return Fail("参数错误。");
  272. }
  273. }
  274. public Response GetList20(dynamic _)
  275. {
  276. var userinfo = userInfo;
  277. var functionlist = perm_FunctionIBLL.GetListByUserId(userinfo.userId).Where(m => m.FIsH5 == true).Select(m =>
  278. new
  279. {
  280. m.FName,
  281. m.FId,
  282. });
  283. return Success(functionlist);
  284. }
  285. /// <summary>
  286. /// 获取页面显示列表数据
  287. /// <summary>
  288. /// <param name="_"></param>
  289. /// <returns></returns>
  290. public Response GetList(dynamic _)
  291. {
  292. string publickey = ConfigurationManager.AppSettings["SSOPublicSecret"];
  293. var logininfo = userInfo;
  294. var datatype = perm_FunctionTypeIBLL.GetListByUserId(userInfo.userId);
  295. foreach (var typeEntity in datatype)
  296. {
  297. var datafunction = perm_FunctionIBLL.GetListByFTId(typeEntity.FTId, userInfo.userId).Where(m => m.FIsH5 == true);
  298. foreach (var item in datafunction)
  299. {
  300. if (item.FIsManagePage == true)
  301. {
  302. item.FInterfaceUrl = "/SSO/GoTo?sysid=" + DESEncrypt.Encrypt(item.FId, publickey) + "&openid=" + DESEncrypt.Encrypt(logininfo.userId, publickey);
  303. }
  304. else
  305. {
  306. item.FInterfaceUrl = item.FUrl;
  307. }
  308. item.FUrl = null;
  309. }
  310. typeEntity.PermFunction = datafunction.ToList();
  311. }
  312. return Success(datatype);
  313. }
  314. /// <summary>
  315. /// 获取IP
  316. /// </summary>
  317. /// <returns></returns>
  318. private string GetIP()
  319. {
  320. string ip = string.Empty;
  321. if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
  322. ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
  323. if (string.IsNullOrEmpty(ip))
  324. ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
  325. return ip;
  326. }
  327. }
  328. }