BE_ERPLogin.groovy 9.4 KB

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