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.
 
 
 
 
 
 

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