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.
 
 
 
 
 
 

333 regels
14 KiB

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