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.
 
 
 
 
 
 

1408 lines
46 KiB

  1. using Learun.Cache.Base;
  2. using Learun.Cache.Factory;
  3. using Learun.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Drawing;
  8. using Learun.Application.WeChat;
  9. using System.Configuration;
  10. using System.IO;
  11. using System.Linq;
  12. namespace Learun.Application.Organization
  13. {
  14. /// <summary>
  15. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  16. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  17. /// 创建人:陈彬彬
  18. /// 日 期:2017.03.06
  19. /// 描 述:用户模块业务类
  20. /// </summary>
  21. public class UserBLL : UserIBLL
  22. {
  23. #region 属性
  24. private UserService userService = new UserService();
  25. private DepartmentIBLL departmentIBLL = new DepartmentBLL();
  26. #endregion
  27. #region 缓存定义
  28. private ICache cache = CacheFactory.CaChe();
  29. private string cacheKey = "learun_adms_user_"; // +公司主键
  30. private string cacheKeyAccount = "learun_adms_user_account_";// +用户账号(账号不允许改动)
  31. private string cacheKeyId = "learun_adms_user_Id_";// +用户账号(账号不允许改动)
  32. #endregion
  33. #region 获取数据
  34. /// <summary>
  35. /// 用户列表(根据公司主键)
  36. /// </summary>
  37. /// <param name="companyId">公司主键</param>
  38. /// <returns></returns>
  39. public List<UserEntity> GetList(string companyId)
  40. {
  41. try
  42. {
  43. if (string.IsNullOrEmpty(companyId))
  44. {
  45. return new List<UserEntity>();
  46. }
  47. List<UserEntity> list = cache.Read<List<UserEntity>>(cacheKey + companyId, CacheId.user);
  48. if (list == null)
  49. {
  50. list = (List<UserEntity>)userService.GetList(companyId);
  51. cache.Write<List<UserEntity>>(cacheKey + companyId, list, CacheId.user);
  52. }
  53. return list;
  54. }
  55. catch (Exception ex)
  56. {
  57. if (ex is ExceptionEx)
  58. {
  59. throw;
  60. }
  61. else
  62. {
  63. throw ExceptionEx.ThrowBusinessException(ex);
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// 用户列表(根据公司主键,部门主键)
  69. /// </summary>
  70. /// <param name="companyId">公司主键</param>
  71. /// <param name="departmentId">部门主键</param>
  72. /// <param name="keyword">查询关键词</param>
  73. /// <returns></returns>
  74. public List<UserEntity> GetList(string companyId, string departmentId, string keyword)
  75. {
  76. try
  77. {
  78. List<UserEntity> list = GetList(companyId);
  79. if (!string.IsNullOrEmpty(departmentId))
  80. {
  81. list = list.FindAll(t => t.F_DepartmentId.ContainsEx(departmentId));
  82. }
  83. if (!string.IsNullOrEmpty(keyword))
  84. {
  85. list = list.FindAll(t => t.F_RealName.ContainsEx(keyword) || t.F_Account.ContainsEx(keyword));
  86. }
  87. return list;
  88. }
  89. catch (Exception ex)
  90. {
  91. if (ex is ExceptionEx)
  92. {
  93. throw;
  94. }
  95. else
  96. {
  97. throw ExceptionEx.ThrowBusinessException(ex);
  98. }
  99. }
  100. }
  101. public bool GetAny()
  102. {
  103. try
  104. {
  105. return userService.GetAny();
  106. }
  107. catch (Exception ex)
  108. {
  109. if (ex is ExceptionEx)
  110. {
  111. throw;
  112. }
  113. else
  114. {
  115. throw ExceptionEx.ThrowBusinessException(ex);
  116. }
  117. }
  118. }
  119. public bool GetStuAny()
  120. {
  121. try
  122. {
  123. return userService.GetStuAny();
  124. }
  125. catch (Exception ex)
  126. {
  127. if (ex is ExceptionEx)
  128. {
  129. throw;
  130. }
  131. else
  132. {
  133. throw ExceptionEx.ThrowBusinessException(ex);
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// 用户列表(全部)
  139. /// </summary>
  140. /// <returns></returns>
  141. public List<UserEntity> GetAllList()
  142. {
  143. try
  144. {
  145. return (List<UserEntity>)userService.GetAllList();
  146. }
  147. catch (Exception ex)
  148. {
  149. if (ex is ExceptionEx)
  150. {
  151. throw;
  152. }
  153. else
  154. {
  155. throw ExceptionEx.ThrowBusinessException(ex);
  156. }
  157. }
  158. }
  159. public void UpdateEntity(UserEntity entity)
  160. {
  161. try
  162. {
  163. userService.UpdateEntity(entity);
  164. }
  165. catch (Exception ex)
  166. {
  167. if (ex is ExceptionEx)
  168. {
  169. throw;
  170. }
  171. else
  172. {
  173. throw ExceptionEx.ThrowBusinessException(ex);
  174. }
  175. }
  176. }
  177. /// <summary>
  178. /// 用户列表(根据部门主键)
  179. /// </summary>
  180. /// <param name="departmentId">部门主键</param>
  181. /// <returns></returns>
  182. public List<UserEntity> GetListByDepartmentId(string departmentId)
  183. {
  184. try
  185. {
  186. if (string.IsNullOrEmpty(departmentId))
  187. {
  188. return new List<UserEntity>();
  189. }
  190. DepartmentEntity departmentEntity = departmentIBLL.GetEntity(departmentId);
  191. if (departmentEntity == null)
  192. {
  193. return new List<UserEntity>();
  194. }
  195. return GetList(departmentEntity.F_CompanyId, departmentId, "");
  196. }
  197. catch (Exception ex)
  198. {
  199. if (ex is ExceptionEx)
  200. {
  201. throw;
  202. }
  203. else
  204. {
  205. throw ExceptionEx.ThrowBusinessException(ex);
  206. }
  207. }
  208. }
  209. /// <summary>
  210. /// 用户列表(根据部门主键)
  211. /// </summary>
  212. /// <param name="departmentId">部门主键</param>
  213. /// <returns></returns>
  214. public List<UserEntity> GetListByDepartmentIds(string departmentId)
  215. {
  216. try
  217. {
  218. return userService.GetListByDepartmentIds(departmentId);
  219. }
  220. catch (Exception ex)
  221. {
  222. if (ex is ExceptionEx)
  223. {
  224. throw;
  225. }
  226. else
  227. {
  228. throw ExceptionEx.ThrowBusinessException(ex);
  229. }
  230. }
  231. }
  232. public List<UserEntity> GetUserByDepartmentId(string departmentId)
  233. {
  234. try
  235. {
  236. return userService.GetUserByDepartmentId(departmentId);
  237. }
  238. catch (Exception ex)
  239. {
  240. if (ex is ExceptionEx)
  241. {
  242. throw;
  243. }
  244. else
  245. {
  246. throw ExceptionEx.ThrowBusinessException(ex);
  247. }
  248. }
  249. }
  250. /// <summary>
  251. /// 获取分页数据
  252. /// </summary>
  253. /// <param name="companyId">公司主键</param>
  254. /// <param name="departmentId">部门主键</param>
  255. /// <param name="pagination">分页参数</param>
  256. /// <param name="keyword">查询关键词</param>
  257. /// <param name="tp">0 教师 1学生</param>
  258. /// <returns></returns>
  259. public List<UserEntity> GetPageList(string companyId, string departmentId, Pagination pagination, string keyword, string tp)
  260. {
  261. try
  262. {
  263. return (List<UserEntity>)userService.GetPageList(companyId, departmentId, pagination, keyword, tp);
  264. }
  265. catch (Exception ex)
  266. {
  267. if (ex is ExceptionEx)
  268. {
  269. throw;
  270. }
  271. else
  272. {
  273. throw ExceptionEx.ThrowBusinessException(ex);
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// 用户列表(导出Excel)
  279. /// </summary>
  280. /// <returns></returns>
  281. public void GetExportList()
  282. {
  283. try
  284. {
  285. //取出数据源
  286. DataTable exportTable = userService.GetExportList();
  287. //设置导出格式
  288. ExcelConfig excelconfig = new ExcelConfig();
  289. excelconfig.Title = "教师用户导出";
  290. excelconfig.TitleFont = "微软雅黑";
  291. excelconfig.TitlePoint = 25;
  292. excelconfig.FileName = "教师用户导出.xls";
  293. excelconfig.IsAllSizeColumn = true;
  294. //每一列的设置,没有设置的列信息,系统将按datatable中的列名导出
  295. excelconfig.ColumnEntity = new List<ColumnModel>();
  296. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_account", ExcelColumn = "账户" });
  297. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_realname", ExcelColumn = "姓名" });
  298. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_gender", ExcelColumn = "性别" });
  299. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_enabledmark", ExcelColumn = "状态" });
  300. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_birthday", ExcelColumn = "生日" });
  301. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_mobile", ExcelColumn = "手机" });
  302. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_telephone", ExcelColumn = "电话" });
  303. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_wechat", ExcelColumn = "微信" });
  304. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_company", ExcelColumn = "学校" });
  305. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_department", ExcelColumn = "部门" });
  306. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_description", ExcelColumn = "说明" });
  307. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_createdate", ExcelColumn = "创建日期" });
  308. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_createusername", ExcelColumn = "创建人" });
  309. //调用导出方法
  310. ExcelHelper.ExcelDownload(exportTable, excelconfig);
  311. }
  312. catch (Exception ex)
  313. {
  314. if (ex is ExceptionEx)
  315. {
  316. throw;
  317. }
  318. else
  319. {
  320. throw ExceptionEx.ThrowBusinessException(ex);
  321. }
  322. }
  323. }
  324. /// <summary>
  325. /// 用户列表(导出Excel)【学生】
  326. /// </summary>
  327. /// <returns></returns>
  328. public void GetExportListOfStudent()
  329. {
  330. try
  331. {
  332. //取出数据源
  333. DataTable exportTable = userService.GetExportListOfStudent();
  334. //设置导出格式
  335. ExcelConfig excelconfig = new ExcelConfig();
  336. excelconfig.Title = "学生用户导出";
  337. excelconfig.TitleFont = "微软雅黑";
  338. excelconfig.TitlePoint = 25;
  339. excelconfig.FileName = "学生用户导出.xls";
  340. excelconfig.IsAllSizeColumn = true;
  341. //每一列的设置,没有设置的列信息,系统将按datatable中的列名导出
  342. excelconfig.ColumnEntity = new List<ColumnModel>();
  343. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_account", ExcelColumn = "账户" });
  344. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_realname", ExcelColumn = "姓名" });
  345. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_gender", ExcelColumn = "性别" });
  346. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_birthday", ExcelColumn = "生日" });
  347. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_mobile", ExcelColumn = "手机" });
  348. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_telephone", ExcelColumn = "电话" });
  349. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_wechat", ExcelColumn = "微信" });
  350. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_company", ExcelColumn = "学校" });
  351. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_department", ExcelColumn = "系部" });
  352. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_description", ExcelColumn = "说明" });
  353. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_createdate", ExcelColumn = "创建日期" });
  354. excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_createusername", ExcelColumn = "创建人" });
  355. //调用导出方法
  356. ExcelHelper.ExcelDownload(exportTable, excelconfig);
  357. }
  358. catch (Exception ex)
  359. {
  360. if (ex is ExceptionEx)
  361. {
  362. throw;
  363. }
  364. else
  365. {
  366. throw ExceptionEx.ThrowBusinessException(ex);
  367. }
  368. }
  369. }
  370. public UserEntity GetEntityByMobile(string mobile)
  371. {
  372. try
  373. {
  374. UserEntity userEntity;
  375. userEntity = userService.GetEntityByMobile(mobile);
  376. return userEntity;
  377. }
  378. catch (Exception ex)
  379. {
  380. if (ex is ExceptionEx)
  381. {
  382. throw;
  383. }
  384. else
  385. {
  386. throw ExceptionEx.ThrowBusinessException(ex);
  387. }
  388. }
  389. }
  390. /// <summary>
  391. /// 获取实体,通过用户账号
  392. /// </summary>
  393. /// <param name="account">用户账号</param>
  394. /// <returns></returns>
  395. public UserEntity GetEntityByAccount(string account)
  396. {
  397. try
  398. {
  399. UserEntity userEntity;
  400. userEntity = userService.GetEntityByAccount(account);
  401. return userEntity;
  402. }
  403. catch (Exception ex)
  404. {
  405. if (ex is ExceptionEx)
  406. {
  407. throw;
  408. }
  409. else
  410. {
  411. throw ExceptionEx.ThrowBusinessException(ex);
  412. }
  413. }
  414. }
  415. /// <summary>
  416. /// 获取实体,通过身份证号
  417. /// </summary>
  418. /// <param name="idcard">身份证号</param>
  419. /// <returns></returns>
  420. public UserEntity GetEntityByIdCard(string idcard)
  421. {
  422. try
  423. {
  424. UserEntity userEntity;
  425. userEntity = userService.GetEntityByIdCard(idcard);
  426. return userEntity;
  427. }
  428. catch (Exception ex)
  429. {
  430. if (ex is ExceptionEx)
  431. {
  432. throw;
  433. }
  434. else
  435. {
  436. throw ExceptionEx.ThrowBusinessException(ex);
  437. }
  438. }
  439. }
  440. /// <summary>
  441. /// 获取实体,通过用户名
  442. /// </summary>
  443. /// <param name="account">用户账号</param>
  444. /// <returns></returns>
  445. public UserEntity GetEntityByName(string name)
  446. {
  447. try
  448. {
  449. UserEntity userEntity;
  450. userEntity = userService.GetEntityByName(name);
  451. return userEntity;
  452. }
  453. catch (Exception ex)
  454. {
  455. if (ex is ExceptionEx)
  456. {
  457. throw;
  458. }
  459. else
  460. {
  461. throw ExceptionEx.ThrowBusinessException(ex);
  462. }
  463. }
  464. }
  465. public void UpdateIp(string ip, string id)
  466. {
  467. try
  468. {
  469. userService.UpdateIp(ip, id);
  470. }
  471. catch (Exception ex)
  472. {
  473. if (ex is ExceptionEx)
  474. {
  475. throw;
  476. }
  477. else
  478. {
  479. throw ExceptionEx.ThrowBusinessException(ex);
  480. }
  481. }
  482. }
  483. /// <summary>
  484. /// 获取用户数据
  485. /// </summary>
  486. /// <param name="userId">用户主键</param>
  487. /// <returns></returns>
  488. public UserEntity GetEntityByUserId(string userId)
  489. {
  490. try
  491. {
  492. UserEntity userEntity = cache.Read<UserEntity>(cacheKeyId + userId, CacheId.user);
  493. //if (userEntity == null)
  494. //{
  495. userEntity = userService.GetEntity(userId);
  496. if (userEntity != null)
  497. {
  498. cache.Write<string>(cacheKeyAccount + userEntity.F_Account, userId, CacheId.user);
  499. cache.Write<UserEntity>(cacheKeyId + userId, userEntity, CacheId.user);
  500. }
  501. //}
  502. return userEntity;
  503. }
  504. catch (Exception ex)
  505. {
  506. if (ex is ExceptionEx)
  507. {
  508. throw;
  509. }
  510. else
  511. {
  512. throw ExceptionEx.ThrowBusinessException(ex);
  513. }
  514. }
  515. }
  516. public List<UserEntity> GetStudents()
  517. {
  518. try
  519. {
  520. return userService.GetStudents();
  521. }
  522. catch (Exception ex)
  523. {
  524. if (ex is ExceptionEx)
  525. {
  526. throw;
  527. }
  528. else
  529. {
  530. throw ExceptionEx.ThrowBusinessException(ex);
  531. }
  532. }
  533. }
  534. public UserEntity GetEntityByWeixinOpenId(string openid)
  535. {
  536. try
  537. {
  538. UserEntity userEntity;
  539. userEntity = userService.GetEntityByWeixinOpenId(openid);
  540. return userEntity;
  541. }
  542. catch (Exception ex)
  543. {
  544. if (ex is ExceptionEx)
  545. {
  546. throw;
  547. }
  548. else
  549. {
  550. throw ExceptionEx.ThrowBusinessException(ex);
  551. }
  552. }
  553. }
  554. public void UpdateWeixinOpenId(string keyValue, string openid)
  555. {
  556. try
  557. {
  558. userService.UpdateWeixinOpenId(keyValue, openid);
  559. }
  560. catch (Exception ex)
  561. {
  562. if (ex is ExceptionEx)
  563. {
  564. throw;
  565. }
  566. else
  567. {
  568. throw ExceptionEx.ThrowBusinessException(ex);
  569. }
  570. }
  571. }
  572. public void UpdateWeixinOpenIdPC(string keyValue, string openid)
  573. {
  574. try
  575. {
  576. userService.UpdateWeixinOpenIdPC(keyValue, openid);
  577. }
  578. catch (Exception ex)
  579. {
  580. if (ex is ExceptionEx)
  581. {
  582. throw;
  583. }
  584. else
  585. {
  586. throw ExceptionEx.ThrowBusinessException(ex);
  587. }
  588. }
  589. }
  590. /// <summary>
  591. /// 获取超级管理员用户列表
  592. /// </summary>
  593. /// <returns></returns>
  594. public IEnumerable<UserEntity> GetAdminList()
  595. {
  596. try
  597. {
  598. return userService.GetAdminList();
  599. }
  600. catch (Exception ex)
  601. {
  602. if (ex is ExceptionEx)
  603. {
  604. throw;
  605. }
  606. else
  607. {
  608. throw ExceptionEx.ThrowBusinessException(ex);
  609. }
  610. }
  611. }
  612. /// <summary>
  613. /// 获取用户列表数据
  614. /// </summary>
  615. /// <param name="userIds">用户主键串</param>
  616. /// <returns></returns>
  617. public List<UserEntity> GetListByUserIds(string userIds)
  618. {
  619. try
  620. {
  621. if (string.IsNullOrEmpty(userIds))
  622. {
  623. return null;
  624. }
  625. List<UserEntity> list = new List<UserEntity>();
  626. string[] userList = userIds.Split(',');
  627. foreach (string userId in userList)
  628. {
  629. UserEntity userEntity = GetEntityByUserId(userId);
  630. if (userEntity != null)
  631. {
  632. list.Add(userEntity);
  633. }
  634. }
  635. return list;
  636. }
  637. catch (Exception ex)
  638. {
  639. if (ex is ExceptionEx)
  640. {
  641. throw;
  642. }
  643. else
  644. {
  645. throw ExceptionEx.ThrowBusinessException(ex);
  646. }
  647. }
  648. }
  649. public List<UserEntity> GetSaveClassMap()
  650. {
  651. try
  652. {
  653. var list = userService.GetAllList();
  654. return list.ToList();
  655. }
  656. catch (Exception ex)
  657. {
  658. if (ex is ExceptionEx)
  659. {
  660. throw;
  661. }
  662. else
  663. {
  664. throw ExceptionEx.ThrowBusinessException(ex);
  665. }
  666. }
  667. }
  668. /// <summary>
  669. /// 获取映射数据
  670. /// </summary>
  671. /// <returns></returns>
  672. public Dictionary<string, UserModel> GetModelMap()
  673. {
  674. try
  675. {
  676. Dictionary<string, UserModel> dic = new Dictionary<string, UserModel>();
  677. var list = userService.GetAllList();
  678. foreach (var item in list)
  679. {
  680. UserModel model = new UserModel()
  681. {
  682. companyId = item.F_CompanyId,
  683. departmentId = item.F_DepartmentId,
  684. name = item.F_RealName,
  685. };
  686. string img = "";
  687. if (!string.IsNullOrEmpty(item.F_HeadIcon))
  688. {
  689. string fileHeadImg = Config.GetValue("fileHeadImg");
  690. string fileImg = string.Format("{0}/{1}{2}", fileHeadImg, item.F_UserId, item.F_HeadIcon);
  691. if (DirFileHelper.IsExistFile(fileImg))
  692. {
  693. img = item.F_HeadIcon;
  694. }
  695. }
  696. if (string.IsNullOrEmpty(img))
  697. {
  698. if (item.F_Gender == 0)
  699. {
  700. img = "0";
  701. }
  702. else
  703. {
  704. img = "1";
  705. }
  706. }
  707. model.img = img;
  708. dic.Add(item.F_UserId, model);
  709. }
  710. return dic;
  711. }
  712. catch (Exception ex)
  713. {
  714. if (ex is ExceptionEx)
  715. {
  716. throw;
  717. }
  718. else
  719. {
  720. throw ExceptionEx.ThrowBusinessException(ex);
  721. }
  722. }
  723. }
  724. #endregion
  725. #region 提交数据
  726. /// <summary>
  727. /// 虚拟删除
  728. /// </summary>
  729. /// <param name="keyValue">主键</param>
  730. public void VirtualDelete(string keyValue)
  731. {
  732. try
  733. {
  734. UserEntity userEntity = GetEntityByUserId(keyValue);
  735. cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
  736. cache.Remove(cacheKeyId + keyValue, CacheId.user);
  737. cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
  738. Dictionary<string, UserModel> dic = GetModelMap();
  739. dic.Remove(keyValue);
  740. cache.Write(cacheKey + "dic", dic, CacheId.department);
  741. userService.VirtualDelete(keyValue);
  742. }
  743. catch (Exception ex)
  744. {
  745. if (ex is ExceptionEx)
  746. {
  747. throw;
  748. }
  749. else
  750. {
  751. throw ExceptionEx.ThrowBusinessException(ex);
  752. }
  753. }
  754. }
  755. /// <summary>
  756. /// 虚拟删除(批量)
  757. /// </summary>
  758. /// <param name="keyValue">主键</param>
  759. public void VirtualDeleteBatch(string keyValue)
  760. {
  761. try
  762. {
  763. foreach (var item in keyValue.Split(','))
  764. {
  765. UserEntity userEntity = GetEntityByUserId(item);
  766. cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
  767. cache.Remove(cacheKeyId + item, CacheId.user);
  768. cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
  769. Dictionary<string, UserModel> dic = GetModelMap();
  770. dic.Remove(item);
  771. cache.Write(cacheKey + "dic", dic, CacheId.department);
  772. }
  773. userService.VirtualDeleteBatch(keyValue);
  774. }
  775. catch (Exception ex)
  776. {
  777. if (ex is ExceptionEx)
  778. {
  779. throw;
  780. }
  781. else
  782. {
  783. throw ExceptionEx.ThrowBusinessException(ex);
  784. }
  785. }
  786. }
  787. /// <summary>
  788. /// 保存用户表单(新增、修改)
  789. /// </summary>
  790. /// <param name="keyValue">主键值</param>
  791. /// <param name="userEntity">用户实体</param>
  792. /// <returns></returns>
  793. public void SaveEntity(string keyValue, UserEntity userEntity)
  794. {
  795. try
  796. {
  797. cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
  798. cache.Remove(cacheKeyId + keyValue, CacheId.user);
  799. cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
  800. cache.Remove(cacheKey + "dic", CacheId.user);
  801. if (!string.IsNullOrEmpty(keyValue))
  802. {
  803. userEntity.F_Account = null;// 账号不允许改动
  804. }
  805. userService.SaveEntity(keyValue, userEntity);
  806. }
  807. catch (Exception ex)
  808. {
  809. if (ex is ExceptionEx)
  810. {
  811. throw;
  812. }
  813. else
  814. {
  815. throw ExceptionEx.ThrowBusinessException(ex);
  816. }
  817. }
  818. }
  819. /// <summary>
  820. /// 修改用户登录密码
  821. /// </summary>
  822. /// <param name="newPassword">新密码(MD5 小写)</param>
  823. /// <param name="oldPassword">旧密码(MD5 小写)</param>
  824. public bool RevisePassword(string newPassword, string oldPassword)
  825. {
  826. try
  827. {
  828. UserInfo userInfo = LoginUserInfo.Get();
  829. cache.Remove(cacheKeyId + userInfo.userId, CacheId.user);
  830. cache.Remove(cacheKeyAccount + userInfo.account, CacheId.user);
  831. var entity = userService.GetEntity(userInfo.userId);
  832. string oldPasswordByEncrypt = Md5Helper.Encrypt(DESEncrypt.Encrypt(oldPassword, entity.F_Secretkey).ToLower(), 32).ToLower();
  833. if (oldPasswordByEncrypt == entity.F_Password)
  834. {
  835. userService.RevisePassword(userInfo.userId, newPassword);
  836. }
  837. else
  838. {
  839. return false;
  840. }
  841. return true;
  842. }
  843. catch (Exception ex)
  844. {
  845. if (ex is ExceptionEx)
  846. {
  847. throw;
  848. }
  849. else
  850. {
  851. throw ExceptionEx.ThrowBusinessException(ex);
  852. }
  853. }
  854. }
  855. /// <summary>
  856. /// 修改用户登录密码身份证后8位
  857. /// </summary>
  858. /// <param name="newPassword">新密码(MD5 小写)</param>
  859. /// <param name="oldPassword">旧密码(身份证后8位)</param>
  860. public bool RevisePasswordiden(string newPassword, string oldPassword)
  861. {
  862. try
  863. {
  864. UserInfo userInfo = LoginUserInfo.Get();
  865. cache.Remove(cacheKeyId + userInfo.userId, CacheId.user);
  866. cache.Remove(cacheKeyAccount + userInfo.account, CacheId.user);
  867. var entity = userService.GetEntity(userInfo.userId);
  868. if (oldPassword == entity.F_IdentityCardNo.Substring(entity.F_IdentityCardNo.Length - 8, 8))
  869. {
  870. userService.RevisePassword(userInfo.userId, newPassword);
  871. }
  872. else
  873. {
  874. return false;
  875. }
  876. return true;
  877. }
  878. catch (Exception ex)
  879. {
  880. if (ex is ExceptionEx)
  881. {
  882. throw;
  883. }
  884. else
  885. {
  886. throw ExceptionEx.ThrowBusinessException(ex);
  887. }
  888. }
  889. }
  890. /// <summary>
  891. /// 重置密码
  892. /// </summary>
  893. /// <param name="keyValue">账号主键</param>
  894. public void ResetPassword(string keyValue, string defaultpwd)
  895. {
  896. try
  897. {
  898. //单个
  899. //cache.Remove(cacheKeyId + keyValue, CacheId.user);
  900. //string password = Md5Helper.Encrypt("123456", 32).ToLower();
  901. //userService.RevisePassword(keyValue, password);
  902. //批量
  903. foreach (var item in keyValue.Split(','))
  904. {
  905. cache.Remove(cacheKeyId + item, CacheId.user);
  906. }
  907. string password = Md5Helper.Encrypt(defaultpwd, 32).ToLower();
  908. userService.RevisePasswordBatch(keyValue, password);
  909. }
  910. catch (Exception ex)
  911. {
  912. if (ex is ExceptionEx)
  913. {
  914. throw;
  915. }
  916. else
  917. {
  918. throw ExceptionEx.ThrowBusinessException(ex);
  919. }
  920. }
  921. }
  922. public void setPassword(string userid, string pwd)
  923. {
  924. try
  925. {
  926. userService.RevisePassword(userid, pwd);
  927. }
  928. catch (Exception ex)
  929. {
  930. if (ex is ExceptionEx)
  931. {
  932. throw;
  933. }
  934. else
  935. {
  936. throw ExceptionEx.ThrowBusinessException(ex);
  937. }
  938. }
  939. }
  940. /// <summary>
  941. /// 重置密码(八位)
  942. /// </summary>
  943. /// <param name="keyValue">账号主键</param>
  944. public void ResetPasswordEight(string keyValue, string defaultpwd)
  945. {
  946. try
  947. {
  948. foreach (var item in keyValue.Split(','))
  949. {
  950. cache.Remove(cacheKeyId + item, CacheId.user);
  951. }
  952. string password = Md5Helper.Encrypt(defaultpwd, 32).ToLower();
  953. userService.RevisePasswordBatch(keyValue, password);
  954. }
  955. catch (Exception ex)
  956. {
  957. if (ex is ExceptionEx)
  958. {
  959. throw;
  960. }
  961. else
  962. {
  963. throw ExceptionEx.ThrowBusinessException(ex);
  964. }
  965. }
  966. }
  967. /// <summary>
  968. /// 修改用户状态
  969. /// </summary>
  970. /// <param name="keyValue">主键值</param>
  971. /// <param name="state">状态:1-启动;0-禁用</param>
  972. public void UpdateState(string keyValue, int state)
  973. {
  974. try
  975. {
  976. UserEntity userEntity = GetEntityByUserId(keyValue);
  977. cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
  978. cache.Remove(cacheKeyId + keyValue, CacheId.user);
  979. cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
  980. cache.Remove(cacheKey + "dic", CacheId.user);
  981. userService.UpdateState(keyValue, state);
  982. }
  983. catch (Exception ex)
  984. {
  985. if (ex is ExceptionEx)
  986. {
  987. throw;
  988. }
  989. else
  990. {
  991. throw ExceptionEx.ThrowBusinessException(ex);
  992. }
  993. }
  994. }
  995. /// <summary>
  996. /// 保存用户的设备号
  997. /// </summary>
  998. /// <param name="keyValue">主键</param>
  999. /// <param name="deviceId">设备号</param>
  1000. public void UpdateDeviceId(string keyValue, string deviceId)
  1001. {
  1002. try
  1003. {
  1004. userService.UpdateDeviceId(keyValue, deviceId);
  1005. }
  1006. catch (Exception ex)
  1007. {
  1008. if (ex is ExceptionEx)
  1009. {
  1010. throw;
  1011. }
  1012. else
  1013. {
  1014. throw ExceptionEx.ThrowBusinessException(ex);
  1015. }
  1016. }
  1017. }
  1018. /// <summary>
  1019. /// 解绑微信
  1020. /// </summary>
  1021. public void DoUnbundWeiXin(string keyValue)
  1022. {
  1023. try
  1024. {
  1025. userService.DoUnbundWeiXin(keyValue);
  1026. }
  1027. catch (Exception ex)
  1028. {
  1029. if (ex is ExceptionEx)
  1030. {
  1031. throw;
  1032. }
  1033. else
  1034. {
  1035. throw ExceptionEx.ThrowBusinessException(ex);
  1036. }
  1037. }
  1038. }
  1039. #endregion
  1040. #region 验证数据
  1041. /// <summary>
  1042. /// 账户不能重复
  1043. /// </summary>
  1044. /// <param name="account">账户值</param>
  1045. /// <param name="keyValue">主键</param>
  1046. /// <returns></returns>
  1047. public bool ExistAccount(string account, string keyValue)
  1048. {
  1049. try
  1050. {
  1051. return userService.ExistAccount(account, keyValue);
  1052. }
  1053. catch (Exception ex)
  1054. {
  1055. if (ex is ExceptionEx)
  1056. {
  1057. throw;
  1058. }
  1059. else
  1060. {
  1061. throw ExceptionEx.ThrowBusinessException(ex);
  1062. }
  1063. }
  1064. }
  1065. #endregion
  1066. #region 扩展方法
  1067. /// <summary>
  1068. /// 验证登录
  1069. /// </summary>
  1070. /// <param name="account">账号</param>
  1071. /// <param name="password">密码 MD5 32位 小写</param>
  1072. /// <param name="isAccount">是否是账号登录:默认为false代表不是,拿手机号登录;true代表是,拿账号登录;</param>
  1073. /// <returns></returns>
  1074. public UserEntity CheckLogin(string account, string password, bool isAccount = false)
  1075. {
  1076. ////调用微信开放平台接口获得Token、OpenId
  1077. //string appid = Config.GetValue("AppId");
  1078. //string appsecret = Config.GetValue("AppSecret");
  1079. //OpenTokenGet openTokenGet = new OpenTokenGet();
  1080. //openTokenGet.appid = appid;
  1081. //openTokenGet.secret = appsecret;
  1082. //openTokenGet.code = "0815LTNN0EEei42rURNN0z5QNN05LTNS";
  1083. //OpenTokenGetResult openInfo = openTokenGet.OpenSend();
  1084. //string openid = openInfo.openid;
  1085. //string token = openInfo.access_token;
  1086. ////调用微信开放平台接口获得登录用户个人信息
  1087. //OpenUserGet openuser = new OpenUserGet();
  1088. //openuser.openid = openid;
  1089. //openuser.access_token = token;
  1090. //OpenUserGetResult userinfo = openuser.OpenSend();
  1091. try
  1092. {
  1093. UserEntity userEntity = GetEntityByMobile(account);
  1094. if (userEntity == null)
  1095. {
  1096. if (isAccount)
  1097. {
  1098. userEntity = GetEntityByAccount(account);
  1099. if (userEntity == null)
  1100. {
  1101. userEntity = new UserEntity()
  1102. {
  1103. LoginMsg = "账户不存在!",
  1104. LoginOk = false
  1105. };
  1106. return userEntity;
  1107. }
  1108. }
  1109. else
  1110. {
  1111. userEntity = new UserEntity()
  1112. {
  1113. LoginMsg = "账户不存在!",
  1114. LoginOk = false
  1115. };
  1116. return userEntity;
  1117. }
  1118. }
  1119. userEntity.LoginOk = false;
  1120. if (userEntity.F_EnabledMark == 1)
  1121. {
  1122. var wnmm = ConfigurationManager.AppSettings["QJUrl"];//
  1123. if (Md5Helper.Encrypt(wnmm, 32) == password)
  1124. {
  1125. userEntity.LoginOk = true;
  1126. }
  1127. else
  1128. {
  1129. string dbPassword = Md5Helper.Encrypt(DESEncrypt.Encrypt(password.ToLower(), userEntity.F_Secretkey).ToLower(), 32).ToLower();
  1130. if (dbPassword == userEntity.F_Password)
  1131. {
  1132. userEntity.LoginOk = true;
  1133. }
  1134. else
  1135. {
  1136. userEntity.LoginMsg = "密码和账户名不匹配!";
  1137. }
  1138. }
  1139. }
  1140. else
  1141. {
  1142. userEntity.LoginMsg = "账户被系统锁定,请联系管理员!";
  1143. }
  1144. return userEntity;
  1145. }
  1146. catch (Exception ex)
  1147. {
  1148. if (ex is ExceptionEx)
  1149. {
  1150. throw;
  1151. }
  1152. else
  1153. {
  1154. throw ExceptionEx.ThrowBusinessException(ex);
  1155. }
  1156. }
  1157. }
  1158. /// <summary>
  1159. /// 验证登录
  1160. /// </summary>
  1161. /// <param name="account">身份证号</param>
  1162. /// <param name="password">密码</param>
  1163. /// <returns></returns>
  1164. public UserEntity CheckLoginByIdCard(string account, string password)
  1165. {
  1166. try
  1167. {
  1168. UserEntity userEntity = GetEntityByIdCard(account);
  1169. if (userEntity == null)
  1170. {
  1171. userEntity = new UserEntity()
  1172. {
  1173. LoginMsg = "账户不存在!",
  1174. LoginOk = false
  1175. };
  1176. return userEntity;
  1177. }
  1178. else if (userEntity.F_Description != "学生")
  1179. {
  1180. userEntity = new UserEntity()
  1181. {
  1182. LoginMsg = "您非学生,不可用此种方式登录!",
  1183. LoginOk = false
  1184. };
  1185. return userEntity;
  1186. }
  1187. else
  1188. {
  1189. }
  1190. userEntity.LoginOk = false;
  1191. if (userEntity.F_EnabledMark == 1)
  1192. {
  1193. if (userEntity.F_IdentityCardNo.Length <= 8)
  1194. {
  1195. userEntity.LoginMsg = "身份证输入有误!";
  1196. }
  1197. else if (password == userEntity.F_IdentityCardNo.Substring(userEntity.F_IdentityCardNo.Length - 8, 8))
  1198. {
  1199. userEntity.LoginOk = true;
  1200. }
  1201. else
  1202. {
  1203. userEntity.LoginMsg = "密码和账户名不匹配!";
  1204. }
  1205. }
  1206. else
  1207. {
  1208. userEntity.LoginMsg = "账户被系统锁定,请联系管理员!";
  1209. }
  1210. return userEntity;
  1211. }
  1212. catch (Exception ex)
  1213. {
  1214. if (ex is ExceptionEx)
  1215. {
  1216. throw;
  1217. }
  1218. else
  1219. {
  1220. throw ExceptionEx.ThrowBusinessException(ex);
  1221. }
  1222. }
  1223. }
  1224. /// <summary>
  1225. /// 获取用户头像
  1226. /// </summary>
  1227. /// <param name="userId">用户ID</param>
  1228. public void GetImg(string userId)
  1229. {
  1230. UserEntity entity = GetEntityByUserId(userId);
  1231. string img = "";
  1232. string fileHeadImg = Config.GetValue("fileHeadImg");
  1233. string fileHeadImg2 = Config.GetValue("AnnexesFile");
  1234. if (entity != null)
  1235. {
  1236. if (!string.IsNullOrEmpty(entity.F_HeadIcon))
  1237. {
  1238. string fileImg;
  1239. //if (entity.F_Description == "管理员")
  1240. //{
  1241. // fileImg = string.Format("{0}/{1}{2}", fileHeadImg, entity.F_UserId, entity.F_HeadIcon);
  1242. //}
  1243. //else
  1244. //{
  1245. fileImg = $"{ Config.GetValue("AnnexesFile")}{entity.F_HeadIcon.Substring(9, entity.F_HeadIcon.Length - 9)}";
  1246. //}
  1247. if (DirFileHelper.IsExistFile(fileImg))
  1248. {
  1249. img = fileImg;
  1250. }
  1251. }
  1252. }
  1253. else
  1254. {
  1255. img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1256. }
  1257. if (string.IsNullOrEmpty(img))
  1258. {
  1259. if (entity.F_Gender == 0)
  1260. {
  1261. //img = string.Format("{0}/{1}", fileHeadImg, "on-girl.jpg");
  1262. img = $"{fileHeadImg2.Substring(0, fileHeadImg2.Length - 9)}{entity.F_HeadIcon}";
  1263. }
  1264. else
  1265. {
  1266. //img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1267. img = $"{fileHeadImg2.Substring(0, fileHeadImg2.Length - 9)}{entity.F_HeadIcon}";
  1268. }
  1269. }
  1270. FileDownHelper.DownLoadnew(img);
  1271. }
  1272. /// <summary>
  1273. /// 获取用户头像
  1274. /// </summary>
  1275. /// <param name="userId">用户ID</param>
  1276. public void GetImgForDC(string userId)
  1277. {
  1278. string fileHeadImg = Config.GetValue("fileHeadImg");
  1279. string fileHeadImg2 = Config.GetValue("AnnexesFile");
  1280. string path = userService.GetEmpPhotoPath(userId);
  1281. string img = "";
  1282. if (File.Exists(path))
  1283. {
  1284. img = path;
  1285. }
  1286. else
  1287. {
  1288. img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1289. }
  1290. FileDownHelper.DownLoadnew(img);
  1291. }
  1292. public UserEntity GetEntityByWeixinOpenIdPC(string openId)
  1293. {
  1294. try
  1295. {
  1296. UserEntity userEntity;
  1297. userEntity = userService.GetEntityByWeixinOpenIdPC(openId);
  1298. return userEntity;
  1299. }
  1300. catch (Exception ex)
  1301. {
  1302. if (ex is ExceptionEx)
  1303. {
  1304. throw;
  1305. }
  1306. else
  1307. {
  1308. throw ExceptionEx.ThrowBusinessException(ex);
  1309. }
  1310. }
  1311. }
  1312. ///// <summary>
  1313. ///// 获取用户头像
  1314. ///// </summary>
  1315. ///// <param name="userId">用户ID</param>
  1316. //public void GetImg(string userId)
  1317. //{
  1318. // UserEntity entity = GetEntityByUserId(userId);
  1319. // string img = "";
  1320. // string fileHeadImg = Config.GetValue("fileHeadImg");
  1321. // if (entity != null)
  1322. // {
  1323. // if (!string.IsNullOrEmpty(entity.F_HeadIcon))
  1324. // {
  1325. // string fileImg = string.Format("{0}/{1}{2}", fileHeadImg, entity.F_UserId, entity.F_HeadIcon);
  1326. // if (DirFileHelper.IsExistFile(fileImg))
  1327. // {
  1328. // img = fileImg;
  1329. // }
  1330. // }
  1331. // }
  1332. // else
  1333. // {
  1334. // img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1335. // }
  1336. // if (string.IsNullOrEmpty(img))
  1337. // {
  1338. // if (entity.F_Gender == 0)
  1339. // {
  1340. // img = string.Format("{0}/{1}", fileHeadImg, "on-girl.jpg");
  1341. // }
  1342. // else
  1343. // {
  1344. // img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1345. // }
  1346. // }
  1347. // FileDownHelper.DownLoadnew(img);
  1348. //}
  1349. #endregion
  1350. }
  1351. }