BE_ERPLogin.groovy 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import com.dySweetFishPlugin.sql.dao.TunaService
  2. import com.dySweetFishPlugin.tool.crypto.EncryptUtil
  3. import com.sweetfish.convert.json.JsonConvert
  4. import com.sweetfish.service.RetResult
  5. import com.yinjie.heating.business.dao.LoginDao
  6. import com.yinjie.heating.common.api.BusinessExecutor
  7. import com.yinjie.heating.common.api.DeptService
  8. import com.yinjie.heating.common.api.ERPService
  9. import com.yinjie.heating.common.api.SysMessageService
  10. import com.yinjie.heating.common.datas.ERPModule
  11. import com.yinjie.heating.common.entity.base.DataBaseMultiItemEx
  12. import com.yinjie.heating.common.entity.base.ProcessEntityItem
  13. import com.yinjie.heating.common.entity.site.ERPTokenUser
  14. import com.yinjie.heating.common.entity.site.LoginRequest
  15. import com.yinjie.heating.common.entity.site.LoginResult
  16. import com.yinjie.heating.common.entity.system.LoginUser
  17. import com.yinjie.heating.common.tool.ERPUtils
  18. import org.apache.commons.lang3.StringUtils
  19. import org.apache.logging.log4j.LogManager
  20. import org.apache.logging.log4j.Logger
  21. import org.rex.RMap
  22. import javax.annotation.Resource
  23. /**
  24. * ERP系统登录脚本,随着业务复杂度提高,登录功能统一一个接口提供
  25. */
  26. @SuppressWarnings("unused")
  27. class BE_ERPLogin implements BusinessExecutor<ProcessEntityItem<LoginRequest>, LoginResult> {
  28. protected final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())
  29. @Resource
  30. DeptService deptService
  31. @Resource
  32. ERPService erpService
  33. @Resource
  34. TunaService tunaService
  35. @Resource
  36. SysMessageService sysMessageService
  37. @Resource
  38. JsonConvert jsonConvert
  39. @Resource(name = "property.passwordSalt")
  40. private String passwordSalt
  41. @Resource(name = "property.erpToken.commonkey")
  42. private String ssoERPTokenCommonKey
  43. @Resource(name = "property.deliver.Store.commonkey")
  44. private String ssoStoreTokenCommonKey
  45. @Resource(name = "property.dnyMall.commonkey")
  46. private String ssoMallTokenCommonKey
  47. private LoginDao loginDao
  48. //登录策略,当前数据库那一个地方需要重启服务,可改成不需要的,参考TunaDaoService动态sql执行
  49. def loginStrategies = [
  50. "0" : [
  51. "name" : "后台管理系统登录",
  52. "resourceLogin": { LoginRequest loginInfo ->
  53. if (loginInfo.password == "D227E2334F89C81FDE0967B115D4F29C") {
  54. LoginUser info = new LoginUser()
  55. info.setId(-999L)
  56. info.setLoginName("SuperResourcer")
  57. info.setUserName("资源管理员")
  58. info.setDeptName("资源管理")
  59. return RetResult.<ERPTokenUser> successT().result(new ERPTokenUser(info))
  60. } else {
  61. return RetResult.<ERPTokenUser> errorT().retinfo("用户账号或密码错误,登录失败")
  62. }
  63. },
  64. "login" : { LoginRequest loginInfo, RMap mapParams, DataBaseMultiItemEx supplierItem ->
  65. mapParams.put("companyId", 0L)
  66. LoginUser info = loginDao.login(mapParams, supplierItem.dataBaseAlias, Long.parseLong(supplierItem.shardingKey))
  67. if (info == null) {
  68. return RetResult.<ERPTokenUser> errorT().retinfo("用户账号或密码错误,登录失败")
  69. }
  70. if (info.status == 1) {
  71. return RetResult.<ERPTokenUser> errorT().retinfo("用户账号已冻结,登录失败")
  72. }
  73. info.deptName = deptService.getRedisDeptName(info.getDeptId(), Long.parseLong(supplierItem.shardingKey))
  74. sysMessageService.syncSysGroupMessage(info.getId(), Long.parseLong(supplierItem.shardingKey))
  75. return RetResult.<ERPTokenUser> successT().result(new ERPTokenUser(info))
  76. },
  77. "tokenKey" : ssoERPTokenCommonKey
  78. ]
  79. ]
  80. @Override
  81. String scriptName() {
  82. return "ERP系统登录"
  83. }
  84. @Override
  85. ERPModule module() {
  86. return ERPModule.SYSTEM
  87. }
  88. @Override
  89. RetResult<LoginResult> execute(ProcessEntityItem<LoginRequest> source) {
  90. //直接定义Resource不好使,还是需要赋值
  91. loginStrategies.get("0").tokenKey = ssoERPTokenCommonKey
  92. loginStrategies.get("80").tokenKey = ssoStoreTokenCommonKey
  93. loginStrategies.get("81").tokenKey = ssoStoreTokenCommonKey
  94. if (source.inputItem == null) {
  95. return RetResult.<LoginResult> errorT().retinfo("未传入用户信息")
  96. }
  97. if (source.inputItem.supplierId <= 0L) {
  98. return RetResult.<LoginResult> errorT().retinfo("请选择登录公司信息")
  99. }
  100. DataBaseMultiItemEx supplierItem = erpService.getERPSupplierInfo(source.inputItem.supplierId)
  101. if (supplierItem == null) {
  102. return RetResult.<LoginResult> errorT().retinfo("无效的登录公司信息")
  103. }
  104. if (StringUtils.isEmpty(source.inputItem.loginName)) {
  105. return RetResult.<LoginResult> errorT().retinfo("请输入账号")
  106. }
  107. if (StringUtils.isEmpty(source.inputItem.password)) {
  108. return RetResult.<LoginResult> errorT().retinfo("请输入密码")
  109. }
  110. if (!loginStrategies.containsKey(String.valueOf(source.inputItem.loginFrom))) {
  111. return RetResult.<LoginResult> errorT().retinfo("无效的登录来源")
  112. }
  113. source.inputItem.password = EncryptUtil.md5Digest(source.inputItem.password + passwordSalt)
  114. loginDao = tunaService.generate(LoginDao.class)
  115. RetResult<ERPTokenUser> userResult
  116. def loginStrategy = loginStrategies.get(String.valueOf(source.inputItem.loginFrom))
  117. if (source.inputItem.loginName.equalsIgnoreCase("SuperResourcer")) {
  118. userResult = loginStrategy.resourceLogin.call(source.inputItem)
  119. } else {
  120. RMap<String, Object> paramMap = new RMap<>()
  121. paramMap.put("loginName", source.inputItem.loginName)
  122. paramMap.put("password", source.inputItem.password)
  123. userResult = loginStrategy.login.call(source.inputItem, paramMap, supplierItem)
  124. }
  125. if (userResult.isSuccess()) {
  126. userResult.result.dataSourceId = supplierItem.dataBaseAlias
  127. userResult.result.supplierCode = Long.parseLong(supplierItem.shardingKey)
  128. String accessToken = ERPUtils.parseTokenFromERPUser(userResult.result, loginStrategy.tokenKey, jsonConvert)
  129. return RetResult.<LoginResult> successT().result(new LoginResult(userResult.result, accessToken))
  130. } else {
  131. return RetResult.<LoginResult> errorT().retinfo(userResult.retinfo)
  132. }
  133. }
  134. }