BE_ERPLogin.groovy 6.8 KB

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