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.
 
 
 
 
 
 

1332 lines
43 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. /// <summary>
  371. /// 获取实体,通过用户账号
  372. /// </summary>
  373. /// <param name="account">用户账号</param>
  374. /// <returns></returns>
  375. public UserEntity GetEntityByAccount(string account)
  376. {
  377. try
  378. {
  379. UserEntity userEntity;
  380. userEntity = userService.GetEntityByAccount(account);
  381. return userEntity;
  382. }
  383. catch (Exception ex)
  384. {
  385. if (ex is ExceptionEx)
  386. {
  387. throw;
  388. }
  389. else
  390. {
  391. throw ExceptionEx.ThrowBusinessException(ex);
  392. }
  393. }
  394. }
  395. /// <summary>
  396. /// 获取实体,通过用户名
  397. /// </summary>
  398. /// <param name="account">用户账号</param>
  399. /// <returns></returns>
  400. public UserEntity GetEntityByName(string name)
  401. {
  402. try
  403. {
  404. UserEntity userEntity;
  405. userEntity = userService.GetEntityByName(name);
  406. return userEntity;
  407. }
  408. catch (Exception ex)
  409. {
  410. if (ex is ExceptionEx)
  411. {
  412. throw;
  413. }
  414. else
  415. {
  416. throw ExceptionEx.ThrowBusinessException(ex);
  417. }
  418. }
  419. }
  420. /// <summary>
  421. /// 获取实体,通过手机号
  422. /// </summary>
  423. /// <param name="mobile">手机号</param>
  424. /// <returns></returns>
  425. public UserEntity GetEntityByMobile(string mobile)
  426. {
  427. try
  428. {
  429. return userService.GetEntityByMobile(mobile);
  430. }
  431. catch (Exception ex)
  432. {
  433. if (ex is ExceptionEx)
  434. {
  435. throw;
  436. }
  437. else
  438. {
  439. throw ExceptionEx.ThrowBusinessException(ex);
  440. }
  441. }
  442. }
  443. public void UpdateIp(string ip, string id)
  444. {
  445. try
  446. {
  447. userService.UpdateIp(ip, id);
  448. }
  449. catch (Exception ex)
  450. {
  451. if (ex is ExceptionEx)
  452. {
  453. throw;
  454. }
  455. else
  456. {
  457. throw ExceptionEx.ThrowBusinessException(ex);
  458. }
  459. }
  460. }
  461. /// <summary>
  462. /// 获取用户数据
  463. /// </summary>
  464. /// <param name="userId">用户主键</param>
  465. /// <returns></returns>
  466. public UserEntity GetEntityByUserId(string userId)
  467. {
  468. try
  469. {
  470. UserEntity userEntity = cache.Read<UserEntity>(cacheKeyId + userId, CacheId.user);
  471. //if (userEntity == null)
  472. //{
  473. userEntity = userService.GetEntity(userId);
  474. if (userEntity != null)
  475. {
  476. cache.Write<string>(cacheKeyAccount + userEntity.F_Account, userId, CacheId.user);
  477. cache.Write<UserEntity>(cacheKeyId + userId, userEntity, CacheId.user);
  478. }
  479. //}
  480. return userEntity;
  481. }
  482. catch (Exception ex)
  483. {
  484. if (ex is ExceptionEx)
  485. {
  486. throw;
  487. }
  488. else
  489. {
  490. throw ExceptionEx.ThrowBusinessException(ex);
  491. }
  492. }
  493. }
  494. public List<UserEntity> GetStudents()
  495. {
  496. try
  497. {
  498. return userService.GetStudents();
  499. }
  500. catch (Exception ex)
  501. {
  502. if (ex is ExceptionEx)
  503. {
  504. throw;
  505. }
  506. else
  507. {
  508. throw ExceptionEx.ThrowBusinessException(ex);
  509. }
  510. }
  511. }
  512. public UserEntity GetEntityByWeixinOpenId(string openid)
  513. {
  514. try
  515. {
  516. UserEntity userEntity;
  517. userEntity = userService.GetEntityByWeixinOpenId(openid);
  518. return userEntity;
  519. }
  520. catch (Exception ex)
  521. {
  522. if (ex is ExceptionEx)
  523. {
  524. throw;
  525. }
  526. else
  527. {
  528. throw ExceptionEx.ThrowBusinessException(ex);
  529. }
  530. }
  531. }
  532. public void UpdateWeixinOpenId(string keyValue, string openid)
  533. {
  534. try
  535. {
  536. userService.UpdateWeixinOpenId(keyValue, openid);
  537. }
  538. catch (Exception ex)
  539. {
  540. if (ex is ExceptionEx)
  541. {
  542. throw;
  543. }
  544. else
  545. {
  546. throw ExceptionEx.ThrowBusinessException(ex);
  547. }
  548. }
  549. }
  550. public void UpdateWeixinOpenIdPC(string keyValue, string openid)
  551. {
  552. try
  553. {
  554. userService.UpdateWeixinOpenIdPC(keyValue, openid);
  555. }
  556. catch (Exception ex)
  557. {
  558. if (ex is ExceptionEx)
  559. {
  560. throw;
  561. }
  562. else
  563. {
  564. throw ExceptionEx.ThrowBusinessException(ex);
  565. }
  566. }
  567. }
  568. /// <summary>
  569. /// 获取超级管理员用户列表
  570. /// </summary>
  571. /// <returns></returns>
  572. public IEnumerable<UserEntity> GetAdminList()
  573. {
  574. try
  575. {
  576. return userService.GetAdminList();
  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. /// <param name="userIds">用户主键串</param>
  594. /// <returns></returns>
  595. public List<UserEntity> GetListByUserIds(string userIds)
  596. {
  597. try
  598. {
  599. if (string.IsNullOrEmpty(userIds))
  600. {
  601. return null;
  602. }
  603. List<UserEntity> list = new List<UserEntity>();
  604. string[] userList = userIds.Split(',');
  605. foreach (string userId in userList)
  606. {
  607. UserEntity userEntity = GetEntityByUserId(userId);
  608. if (userEntity != null)
  609. {
  610. list.Add(userEntity);
  611. }
  612. }
  613. return list;
  614. }
  615. catch (Exception ex)
  616. {
  617. if (ex is ExceptionEx)
  618. {
  619. throw;
  620. }
  621. else
  622. {
  623. throw ExceptionEx.ThrowBusinessException(ex);
  624. }
  625. }
  626. }
  627. public List<UserEntity> GetSaveClassMap()
  628. {
  629. try
  630. {
  631. var list = userService.GetAllList();
  632. return list.ToList();
  633. }
  634. catch (Exception ex)
  635. {
  636. if (ex is ExceptionEx)
  637. {
  638. throw;
  639. }
  640. else
  641. {
  642. throw ExceptionEx.ThrowBusinessException(ex);
  643. }
  644. }
  645. }
  646. /// <summary>
  647. /// 获取映射数据
  648. /// </summary>
  649. /// <returns></returns>
  650. public Dictionary<string, UserModel> GetModelMap()
  651. {
  652. try
  653. {
  654. Dictionary<string, UserModel> dic = cache.Read<Dictionary<string, UserModel>>(cacheKey + "dic", CacheId.user);
  655. if (dic == null)
  656. {
  657. dic = new Dictionary<string, UserModel>();
  658. var list = userService.GetAllListForMap();
  659. string fileHeadImg = Config.GetValue("fileHeadImg");
  660. foreach (var item in list)
  661. {
  662. UserModel model = new UserModel()
  663. {
  664. companyId = item.F_CompanyId,
  665. departmentId = item.F_DepartmentId,
  666. name = item.F_RealName,
  667. mobile = item.F_Mobile ?? ""
  668. };
  669. string img = "";
  670. if (!string.IsNullOrEmpty(item.F_HeadIcon))
  671. {
  672. string fileImg = string.Format("{0}/{1}{2}", fileHeadImg, item.F_UserId, item.F_HeadIcon);
  673. if (DirFileHelper.IsExistFile(fileImg))
  674. {
  675. img = item.F_HeadIcon;
  676. }
  677. }
  678. if (string.IsNullOrEmpty(img))
  679. {
  680. if (item.F_Gender == 0)
  681. {
  682. img = "0";
  683. }
  684. else
  685. {
  686. img = "1";
  687. }
  688. }
  689. model.img = img;
  690. dic.Add(item.F_UserId, model);
  691. cache.Write(cacheKey + "dic", dic, CacheId.user);
  692. }
  693. }
  694. return dic;
  695. }
  696. catch (Exception ex)
  697. {
  698. if (ex is ExceptionEx)
  699. {
  700. throw;
  701. }
  702. else
  703. {
  704. throw ExceptionEx.ThrowBusinessException(ex);
  705. }
  706. }
  707. }
  708. #endregion
  709. #region 提交数据
  710. /// <summary>
  711. /// 虚拟删除
  712. /// </summary>
  713. /// <param name="keyValue">主键</param>
  714. public void VirtualDelete(string keyValue)
  715. {
  716. try
  717. {
  718. UserEntity userEntity = GetEntityByUserId(keyValue);
  719. cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
  720. cache.Remove(cacheKeyId + keyValue, CacheId.user);
  721. cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
  722. Dictionary<string, UserModel> dic = GetModelMap();
  723. dic.Remove(keyValue);
  724. cache.Write(cacheKey + "dic", dic, CacheId.department);
  725. userService.VirtualDelete(keyValue);
  726. }
  727. catch (Exception ex)
  728. {
  729. if (ex is ExceptionEx)
  730. {
  731. throw;
  732. }
  733. else
  734. {
  735. throw ExceptionEx.ThrowBusinessException(ex);
  736. }
  737. }
  738. }
  739. /// <summary>
  740. /// 虚拟删除(批量)
  741. /// </summary>
  742. /// <param name="keyValue">主键</param>
  743. public void VirtualDeleteBatch(string keyValue)
  744. {
  745. try
  746. {
  747. foreach (var item in keyValue.Split(','))
  748. {
  749. UserEntity userEntity = GetEntityByUserId(item);
  750. cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
  751. cache.Remove(cacheKeyId + item, CacheId.user);
  752. cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
  753. Dictionary<string, UserModel> dic = GetModelMap();
  754. dic.Remove(item);
  755. cache.Write(cacheKey + "dic", dic, CacheId.department);
  756. }
  757. userService.VirtualDeleteBatch(keyValue);
  758. }
  759. catch (Exception ex)
  760. {
  761. if (ex is ExceptionEx)
  762. {
  763. throw;
  764. }
  765. else
  766. {
  767. throw ExceptionEx.ThrowBusinessException(ex);
  768. }
  769. }
  770. }
  771. /// <summary>
  772. /// 保存用户表单(新增、修改)
  773. /// </summary>
  774. /// <param name="keyValue">主键值</param>
  775. /// <param name="userEntity">用户实体</param>
  776. /// <returns></returns>
  777. public void SaveEntity(string keyValue, UserEntity userEntity)
  778. {
  779. try
  780. {
  781. cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
  782. cache.Remove(cacheKeyId + keyValue, CacheId.user);
  783. cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
  784. cache.Remove(cacheKey + "dic", CacheId.user);
  785. if (!string.IsNullOrEmpty(keyValue))
  786. {
  787. userEntity.F_Account = null;// 账号不允许改动
  788. }
  789. userService.SaveEntity(keyValue, userEntity);
  790. }
  791. catch (Exception ex)
  792. {
  793. if (ex is ExceptionEx)
  794. {
  795. throw;
  796. }
  797. else
  798. {
  799. throw ExceptionEx.ThrowBusinessException(ex);
  800. }
  801. }
  802. }
  803. /// <summary>
  804. /// 修改用户登录密码
  805. /// </summary>
  806. /// <param name="newPassword">新密码(MD5 小写)</param>
  807. /// <param name="oldPassword">旧密码(MD5 小写)</param>
  808. public bool RevisePassword(string newPassword, string oldPassword)
  809. {
  810. try
  811. {
  812. UserInfo userInfo = LoginUserInfo.Get();
  813. cache.Remove(cacheKeyId + userInfo.userId, CacheId.user);
  814. cache.Remove(cacheKeyAccount + userInfo.account, CacheId.user);
  815. var entity = userService.GetEntity(userInfo.userId);
  816. string oldPasswordByEncrypt = Md5Helper.Encrypt(DESEncrypt.Encrypt(oldPassword, entity.F_Secretkey).ToLower(), 32).ToLower();
  817. if (oldPasswordByEncrypt == entity.F_Password)
  818. {
  819. userService.RevisePassword(userInfo.userId, newPassword);
  820. }
  821. else
  822. {
  823. return false;
  824. }
  825. return true;
  826. }
  827. catch (Exception ex)
  828. {
  829. if (ex is ExceptionEx)
  830. {
  831. throw;
  832. }
  833. else
  834. {
  835. throw ExceptionEx.ThrowBusinessException(ex);
  836. }
  837. }
  838. }
  839. /// <summary>
  840. /// 重置密码
  841. /// </summary>
  842. /// <param name="keyValue">账号主键</param>
  843. public void ResetPassword(string keyValue, string defaultpwd)
  844. {
  845. try
  846. {
  847. //单个
  848. //cache.Remove(cacheKeyId + keyValue, CacheId.user);
  849. //string password = Md5Helper.Encrypt("123456", 32).ToLower();
  850. //userService.RevisePassword(keyValue, password);
  851. //批量
  852. foreach (var item in keyValue.Split(','))
  853. {
  854. cache.Remove(cacheKeyId + item, CacheId.user);
  855. }
  856. string password = Md5Helper.Encrypt(defaultpwd, 32).ToLower();
  857. userService.RevisePasswordBatch(keyValue, password);
  858. }
  859. catch (Exception ex)
  860. {
  861. if (ex is ExceptionEx)
  862. {
  863. throw;
  864. }
  865. else
  866. {
  867. throw ExceptionEx.ThrowBusinessException(ex);
  868. }
  869. }
  870. }
  871. public void setPassword(string userid, string pwd)
  872. {
  873. try
  874. {
  875. userService.RevisePassword(userid, pwd);
  876. }
  877. catch (Exception ex)
  878. {
  879. if (ex is ExceptionEx)
  880. {
  881. throw;
  882. }
  883. else
  884. {
  885. throw ExceptionEx.ThrowBusinessException(ex);
  886. }
  887. }
  888. }
  889. /// <summary>
  890. /// 重置密码(八位)
  891. /// </summary>
  892. /// <param name="keyValue">账号主键</param>
  893. public void ResetPasswordEight(string keyValue, string defaultpwd)
  894. {
  895. try
  896. {
  897. foreach (var item in keyValue.Split(','))
  898. {
  899. cache.Remove(cacheKeyId + item, CacheId.user);
  900. }
  901. string password = Md5Helper.Encrypt(defaultpwd, 32).ToLower();
  902. userService.RevisePasswordBatch(keyValue, password);
  903. }
  904. catch (Exception ex)
  905. {
  906. if (ex is ExceptionEx)
  907. {
  908. throw;
  909. }
  910. else
  911. {
  912. throw ExceptionEx.ThrowBusinessException(ex);
  913. }
  914. }
  915. }
  916. /// <summary>
  917. /// 修改用户状态
  918. /// </summary>
  919. /// <param name="keyValue">主键值</param>
  920. /// <param name="state">状态:1-启动;0-禁用</param>
  921. public void UpdateState(string keyValue, int state)
  922. {
  923. try
  924. {
  925. UserEntity userEntity = GetEntityByUserId(keyValue);
  926. cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
  927. cache.Remove(cacheKeyId + keyValue, CacheId.user);
  928. cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
  929. cache.Remove(cacheKey + "dic", CacheId.user);
  930. userService.UpdateState(keyValue, state);
  931. }
  932. catch (Exception ex)
  933. {
  934. if (ex is ExceptionEx)
  935. {
  936. throw;
  937. }
  938. else
  939. {
  940. throw ExceptionEx.ThrowBusinessException(ex);
  941. }
  942. }
  943. }
  944. /// <summary>
  945. /// 保存用户的设备号
  946. /// </summary>
  947. /// <param name="keyValue">主键</param>
  948. /// <param name="deviceId">设备号</param>
  949. public void UpdateDeviceId(string keyValue, string deviceId)
  950. {
  951. try
  952. {
  953. userService.UpdateDeviceId(keyValue, deviceId);
  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. public void DoUnbundWeiXin(string keyValue)
  971. {
  972. try
  973. {
  974. userService.DoUnbundWeiXin(keyValue);
  975. }
  976. catch (Exception ex)
  977. {
  978. if (ex is ExceptionEx)
  979. {
  980. throw;
  981. }
  982. else
  983. {
  984. throw ExceptionEx.ThrowBusinessException(ex);
  985. }
  986. }
  987. }
  988. #endregion
  989. #region 验证数据
  990. /// <summary>
  991. /// 账户不能重复
  992. /// </summary>
  993. /// <param name="account">账户值</param>
  994. /// <param name="keyValue">主键</param>
  995. /// <returns></returns>
  996. public bool ExistAccount(string account, string keyValue)
  997. {
  998. try
  999. {
  1000. return userService.ExistAccount(account, keyValue);
  1001. }
  1002. catch (Exception ex)
  1003. {
  1004. if (ex is ExceptionEx)
  1005. {
  1006. throw;
  1007. }
  1008. else
  1009. {
  1010. throw ExceptionEx.ThrowBusinessException(ex);
  1011. }
  1012. }
  1013. }
  1014. #endregion
  1015. #region 扩展方法
  1016. /// <summary>
  1017. /// 验证登录
  1018. /// </summary>
  1019. /// <param name="account">账号</param>
  1020. /// <param name="password">密码 MD5 32位 小写</param>
  1021. /// <returns></returns>
  1022. public UserEntity CheckLogin(string account, string password)
  1023. {
  1024. ////调用微信开放平台接口获得Token、OpenId
  1025. //string appid = Config.GetValue("AppId");
  1026. //string appsecret = Config.GetValue("AppSecret");
  1027. //OpenTokenGet openTokenGet = new OpenTokenGet();
  1028. //openTokenGet.appid = appid;
  1029. //openTokenGet.secret = appsecret;
  1030. //openTokenGet.code = "0815LTNN0EEei42rURNN0z5QNN05LTNS";
  1031. //OpenTokenGetResult openInfo = openTokenGet.OpenSend();
  1032. //string openid = openInfo.openid;
  1033. //string token = openInfo.access_token;
  1034. ////调用微信开放平台接口获得登录用户个人信息
  1035. //OpenUserGet openuser = new OpenUserGet();
  1036. //openuser.openid = openid;
  1037. //openuser.access_token = token;
  1038. //OpenUserGetResult userinfo = openuser.OpenSend();
  1039. try
  1040. {
  1041. UserEntity userEntity = userService.GetEntityByIdCard(account);
  1042. if (userEntity == null)
  1043. {
  1044. userEntity = userService.GetEntityByAccount(account);
  1045. }
  1046. if (userEntity == null)
  1047. {
  1048. userEntity = new UserEntity()
  1049. {
  1050. LoginMsg = "账户不存在!",
  1051. LoginOk = false
  1052. };
  1053. return userEntity;
  1054. }
  1055. userEntity.LoginOk = false;
  1056. if (userEntity.F_EnabledMark == 1)
  1057. {
  1058. var wnmm = ConfigurationManager.AppSettings["QJUrl"];//
  1059. if (Md5Helper.Encrypt(wnmm, 32) == password)
  1060. {
  1061. userEntity.LoginOk = true;
  1062. }
  1063. else
  1064. {
  1065. string dbPassword = Md5Helper.Encrypt(DESEncrypt.Encrypt(password.ToLower(), userEntity.F_Secretkey).ToLower(), 32).ToLower();
  1066. //string phone = Md5Helper.Encrypt(userEntity.F_Mobile, 32).ToLower();
  1067. if (dbPassword == userEntity.F_Password)
  1068. {
  1069. userEntity.LoginOk = true;
  1070. }
  1071. //else if (phone == password)
  1072. //{
  1073. // userEntity.LoginOk = true;
  1074. //}
  1075. else
  1076. {
  1077. userEntity.LoginMsg = "密码和账户名不匹配!";
  1078. }
  1079. }
  1080. }
  1081. else
  1082. {
  1083. userEntity.LoginMsg = "账户被系统锁定,请联系管理员!";
  1084. }
  1085. return userEntity;
  1086. }
  1087. catch (Exception ex)
  1088. {
  1089. if (ex is ExceptionEx)
  1090. {
  1091. throw;
  1092. }
  1093. else
  1094. {
  1095. throw ExceptionEx.ThrowBusinessException(ex);
  1096. }
  1097. }
  1098. }
  1099. /// <summary>
  1100. /// 获取用户头像
  1101. /// </summary>
  1102. /// <param name="userId">用户ID</param>
  1103. public void GetImg(string userId)
  1104. {
  1105. UserEntity entity = GetEntityByUserId(userId);
  1106. string img = "";
  1107. string fileHeadImg = Config.GetValue("fileHeadImg");
  1108. string fileHeadImg2 = Config.GetValue("AnnexesFile");
  1109. if (entity != null)
  1110. {
  1111. if (!string.IsNullOrEmpty(entity.F_HeadIcon))
  1112. {
  1113. string fileImg;
  1114. //if (entity.F_Description == "管理员")
  1115. //{
  1116. // fileImg = string.Format("{0}/{1}{2}", fileHeadImg, entity.F_UserId, entity.F_HeadIcon);
  1117. //}
  1118. //else
  1119. //{
  1120. fileImg = $"{ Config.GetValue("AnnexesFile")}{entity.F_HeadIcon.Substring(9, entity.F_HeadIcon.Length - 9)}";
  1121. //}
  1122. if (DirFileHelper.IsExistFile(fileImg))
  1123. {
  1124. img = fileImg;
  1125. }
  1126. }
  1127. }
  1128. else
  1129. {
  1130. img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1131. }
  1132. if (string.IsNullOrEmpty(img))
  1133. {
  1134. if (entity.F_Gender == 0)
  1135. {
  1136. //img = string.Format("{0}/{1}", fileHeadImg, "on-girl.jpg");
  1137. img = $"{fileHeadImg2.Substring(0, fileHeadImg2.Length - 9)}{entity.F_HeadIcon}";
  1138. }
  1139. else
  1140. {
  1141. //img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1142. img = $"{fileHeadImg2.Substring(0, fileHeadImg2.Length - 9)}{entity.F_HeadIcon}";
  1143. }
  1144. }
  1145. FileDownHelper.DownLoadnew(img);
  1146. }
  1147. /// <summary>
  1148. /// 获取用户头像
  1149. /// </summary>
  1150. /// <param name="userId">用户ID</param>
  1151. public void GetImgForDC(string userId)
  1152. {
  1153. string fileHeadImg = Config.GetValue("fileHeadImg");
  1154. string fileHeadImg2 = Config.GetValue("AnnexesFile");
  1155. string path = userService.GetEmpPhotoPath(userId);
  1156. string img = "";
  1157. if (File.Exists(path))
  1158. {
  1159. img = path;
  1160. }
  1161. else
  1162. {
  1163. img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1164. }
  1165. FileDownHelper.DownLoadnew(img);
  1166. }
  1167. public UserEntity GetEntityByWeixinOpenIdPC(string openId)
  1168. {
  1169. try
  1170. {
  1171. UserEntity userEntity;
  1172. userEntity = userService.GetEntityByWeixinOpenIdPC(openId);
  1173. return userEntity;
  1174. }
  1175. catch (Exception ex)
  1176. {
  1177. if (ex is ExceptionEx)
  1178. {
  1179. throw;
  1180. }
  1181. else
  1182. {
  1183. throw ExceptionEx.ThrowBusinessException(ex);
  1184. }
  1185. }
  1186. }
  1187. /// <summary>
  1188. /// 修改用户的允许登录结束时间
  1189. /// </summary>
  1190. /// <param name="keyValue">主键值</param>
  1191. /// <param name="state">状态:1-赋值;0-重置</param>
  1192. public void UpdateAllowEndTime(string keyValue, int state)
  1193. {
  1194. try
  1195. {
  1196. userService.UpdateAllowEndTime(keyValue, state);
  1197. }
  1198. catch (Exception ex)
  1199. {
  1200. if (ex is ExceptionEx)
  1201. {
  1202. throw;
  1203. }
  1204. else
  1205. {
  1206. throw ExceptionEx.ThrowBusinessException(ex);
  1207. }
  1208. }
  1209. }
  1210. ///// <summary>
  1211. ///// 获取用户头像
  1212. ///// </summary>
  1213. ///// <param name="userId">用户ID</param>
  1214. //public void GetImg(string userId)
  1215. //{
  1216. // UserEntity entity = GetEntityByUserId(userId);
  1217. // string img = "";
  1218. // string fileHeadImg = Config.GetValue("fileHeadImg");
  1219. // if (entity != null)
  1220. // {
  1221. // if (!string.IsNullOrEmpty(entity.F_HeadIcon))
  1222. // {
  1223. // string fileImg = string.Format("{0}/{1}{2}", fileHeadImg, entity.F_UserId, entity.F_HeadIcon);
  1224. // if (DirFileHelper.IsExistFile(fileImg))
  1225. // {
  1226. // img = fileImg;
  1227. // }
  1228. // }
  1229. // }
  1230. // else
  1231. // {
  1232. // img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1233. // }
  1234. // if (string.IsNullOrEmpty(img))
  1235. // {
  1236. // if (entity.F_Gender == 0)
  1237. // {
  1238. // img = string.Format("{0}/{1}", fileHeadImg, "on-girl.jpg");
  1239. // }
  1240. // else
  1241. // {
  1242. // img = string.Format("{0}/{1}", fileHeadImg, "on-boy.jpg");
  1243. // }
  1244. // }
  1245. // FileDownHelper.DownLoadnew(img);
  1246. //}
  1247. /// <summary>
  1248. /// 获取实体,账号身份证
  1249. /// </summary>
  1250. /// <param name="account">账号身份证</param>
  1251. /// <returns></returns>
  1252. public UserEntity GetEntityByIdCard(string account)
  1253. {
  1254. try
  1255. {
  1256. UserEntity userEntity;
  1257. userEntity = userService.GetEntityByIdCard(account);
  1258. return userEntity;
  1259. }
  1260. catch (Exception ex)
  1261. {
  1262. if (ex is ExceptionEx)
  1263. {
  1264. throw;
  1265. }
  1266. else
  1267. {
  1268. throw ExceptionEx.ThrowBusinessException(ex);
  1269. }
  1270. }
  1271. }
  1272. #endregion
  1273. }
  1274. }