package com.szwl.controller; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.sun.javafx.logging.PulseLogger; import com.szwl.annotation.Audit; import com.szwl.constant.AuditEnum; import com.szwl.constant.ResponseCodesEnum; import com.szwl.exception.BizException; import com.szwl.feign.bean.PayFeign; import com.szwl.manager.TokenManager; import com.szwl.model.bo.R; import com.szwl.model.bo.ResponseModel; import com.szwl.model.bo.UserDetailBO; import com.szwl.model.dto.RegisterParamDTO; import com.szwl.model.entity.*; import com.szwl.model.param.AddLoginUserParam; import com.szwl.model.param.UpdateLoginUserParam; import com.szwl.model.utils.AdminUtils; import com.szwl.service.*; import com.szwl.util.IDGenerator; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import java.util.*; import java.util.regex.Pattern; import java.util.stream.Collectors; /** *

* 短信验证码 前端控制器 *

* * @author wuhs * @since 2023-09-26 */ @Slf4j @Api(value = "/tAdmin", tags = {"账户"}) @RestController @RequestMapping("/tAdmin") public class TAdminController { SysRoleService sysRoleService; SysUserRoleService sysUserRoleService; TokenManager tokenManager; TAdminService tAdminService; TAdminEquipmentService tAdminEquipmentService; TMessageCodeService tMessageCodeService; TAirwallexWalletService airwallexWalletService; PayFeign payFeign; @Autowired TWechatService wechatService; public TAdminController(SysRoleService sysRoleService, SysUserRoleService sysUserRoleService, TokenManager tokenManager, TAdminService tAdminService, TAdminEquipmentService tAdminEquipmentService, TMessageCodeService tMessageCodeService, TAirwallexWalletService airwallexWalletService, PayFeign payFeign) { this.sysRoleService = sysRoleService; this.sysUserRoleService = sysUserRoleService; this.tokenManager = tokenManager; this.tAdminService = tAdminService; this.tAdminEquipmentService = tAdminEquipmentService; this.tMessageCodeService = tMessageCodeService; this.airwallexWalletService = airwallexWalletService; this.payFeign = payFeign; } private static final Logger logger = LoggerFactory.getLogger(TAdminController.class); @ApiOperation(value = "通过username查看用户的id和ifForeign") @GetMapping("/getIdIfForeign") public ResponseModel getIdIfForeign(@RequestParam String username) { if (StringUtils.isEmpty(username)) { return R.fail("username为空"); } LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.eq(TAdmin::getUsername, username); List list = tAdminService.list(lqw); if (list.isEmpty()) { return R.fail("username有误"); } else if (list.size() > 1) { return R.fail("username不唯一"); } else { return R.ok(list.get(0)); } } @ApiOperation(value = "获取账户类型") @GetMapping("/getAdminType") public ResponseModel getAdminType(@RequestParam Long adminId) { if (adminId != null) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(TAdmin::getId, adminId); TAdmin tAdmin = tAdminService.getOne(wrapper); Integer type = tAdmin.getType(); return R.ok(type); } return R.fail("adminId无效"); } @ApiOperation(value = "判断是否海外用户") @GetMapping("/getIfForeign/{adminId}") public ResponseModel getIfForeign(@PathVariable Long adminId) { if (adminId != null) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(TAdmin::getId, adminId); TAdmin one = tAdminService.getOne(wrapper); String ifForeign = one.getIfForeign(); return R.ok(ifForeign); } return R.fail("未登录"); } @ApiOperation(value = "添加子账号") @PostMapping("/addLoginUser") @Transactional @Audit(type = AuditEnum.INSERT, content = "#loginUser.name + '添加账号'") public ResponseModel addLoginUser(@RequestBody @Valid AddLoginUserParam param) { if (StringUtils.isEmpty(param.getEquipmentIds())) { return R.fail(ResponseCodesEnum.A0001, "机器不能为空"); } if (param.getRoleList().size() < 1) { return R.fail(ResponseCodesEnum.A0001, "角色不能为空"); } //获取当前操作人员 UserDetailBO loginUser = tokenManager.getLoginUserDetails(); // 保存用户实体 Date now = new Date(); Long parentId = param.getAdminId(); if (parentId == null) { return R.fail(ResponseCodesEnum.A0001); } param.setAdminId(null); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getUsername, param.getUsername()); // List list = tAdminService.list(query); TAdmin admin = tAdminService.getOne(query); if (admin != null) { return R.fail(ResponseCodesEnum.A0201); } TAdmin entity = BeanUtil.copyProperties(param, TAdmin.class); entity.setParentId(parentId); if (parentId.toString().equals("1")) { entity.setType(1); } else { entity.setType(3); } // entity.setParentId(34l); entity.setIsAdmined(false); entity.setCreateDate(now); entity.setModifyDate(now); entity.setIsLocked(false); entity.setIsEnabled(true); entity.setLoginFailureCount(0); entity.setEmail(param.getEmail()); entity.setPhone(param.getPhone()); entity.setCompanyType(param.getCompanyType()); // 公司平台 //获取父账号信息 TAdmin parentAdmin = tAdminService.getById(parentId); entity.setIfForeign(parentAdmin.getIfForeign()); tAdminService.save(entity); String ifForeign = parentAdmin.getIfForeign(); if ("1".equals(ifForeign)) { // 海外子账户,新建一个 Airwallex 钱包账户 TAirwallexWallet wallet = new TAirwallexWallet(); wallet.setAdminId(entity.getId()); payFeign.saveAirwallexWallet(wallet); } // 管理的机器 需要再建立一个关系表 type区分全部还是部分 String equipmentIds = param.getEquipmentIds(); if (StringUtils.isNotEmpty(equipmentIds)) { if (equipmentIds.equals("all")) { //管理全部机器 TAdminEquipment tAdminEquipment = new TAdminEquipment(); tAdminEquipment.setAdminId(entity.getId()); //0:全部机器,1:部分机器 tAdminEquipment.setType("0"); tAdminEquipmentService.save(tAdminEquipment); } else { //部分机器 TAdminEquipment tAdminEquipment = new TAdminEquipment(); tAdminEquipment.setAdminId(entity.getId()); //0:全部机器,1:部分机器 tAdminEquipment.setType("1"); tAdminEquipment.setEquipmentIds(param.getEquipmentIds()); tAdminEquipmentService.save(tAdminEquipment); } } log.debug("添加账号 id:{},TAdmin:{}", entity.getId(), entity); // 保存用户角色关系 List userRoleList = param.getRoleList() .stream() .map(e -> new SysUserRole().setRoleId(e).setUserId(String.valueOf(entity.getId()))) .collect(Collectors.toList()); if (CollUtil.isNotEmpty(userRoleList)) { sysUserRoleService.saveBatch(userRoleList); } return R.ok(entity); } @ApiOperation(value = "修改子账号") @PostMapping("/updateLoginUser") @Transactional @Audit(type = AuditEnum.UPDATE, content = "#loginUser.name + '修改账号'") public ResponseModel updateLoginUser(@RequestBody @Valid UpdateLoginUserParam param) { // public ResponseModel addLoginUser(@RequestBody AddLoginUserParam param) { //获取当前操作人员 UserDetailBO loginUser = tokenManager.getLoginUserDetails(); // 保存用户实体 Date now = new Date(); Long parentId = param.getAdminId(); if (parentId == null) { return R.fail(ResponseCodesEnum.A0001); } param.setAdminId(null); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getParentId, parentId); query.eq(TAdmin::getId, param.getId()); List list = tAdminService.list(query); if (list.size() > 0) { TAdmin admin = list.get(0); // 管理的机器 需要再建立一个关系表 type区分全部还是部分 String equipmentIds = param.getEquipmentIds(); if (StringUtils.isNotEmpty(equipmentIds)) { LambdaQueryWrapper query1 = Wrappers.lambdaQuery(); query1.eq(TAdminEquipment::getAdminId, admin.getId()); List list1 = tAdminEquipmentService.list(query1); if (list1.size() > 0) { TAdminEquipment tAdminEquipment1 = list1.get(0); if (equipmentIds.equals("all")) { //管理全部机器 //0:全部机器,1:部分机器 tAdminEquipment1.setType("0"); tAdminEquipment1.setEquipmentIds(null); } else { //部分机器 //0:全部机器,1:部分机器 tAdminEquipment1.setType("1"); tAdminEquipment1.setEquipmentIds(param.getEquipmentIds()); } tAdminEquipmentService.updateById(tAdminEquipment1); } } LambdaQueryWrapper query1 = Wrappers.lambdaQuery(); query1.eq(SysUserRole::getUserId, admin.getId()); List list1 = sysUserRoleService.list(query1); if (list1.size() > 0) { for (SysUserRole sysUserRole : list1) { sysUserRoleService.removeById(sysUserRole.getId()); } } // 保存用户角色关系 List userRoleList = param.getRoleList() .stream() .map(e -> new SysUserRole().setRoleId(e).setUserId(String.valueOf(admin.getId()))) .collect(Collectors.toList()); if (CollUtil.isNotEmpty(userRoleList)) { sysUserRoleService.saveBatch(userRoleList); } admin.setIsEnabled(param.getIsEnabled()); admin.setPhone(param.getPhone()); admin.setEmail(param.getEmail()); admin.setIsEnabled(param.getIsEnabled()); if (StringUtils.isNotEmpty(param.getPassword())) { admin.setPassword(param.getPassword()); } tAdminService.updateById(admin); log.debug("修改账号 id:{},TAdmin:{}", admin.getId(), admin); return R.ok(admin); } return R.fail(ResponseCodesEnum.A0100); } @ApiOperation(value = "删除子账号") @PostMapping("/deleteLoginUser") @Transactional @Audit(type = AuditEnum.DELETE, content = "#loginUser.name + '删除账号'") public ResponseModel deleteLoginUser(@RequestBody @Valid UpdateLoginUserParam param) { //获取当前操作人员 UserDetailBO loginUser = tokenManager.getLoginUserDetails(); // 保存用户实体 Date now = new Date(); Long parentId = param.getAdminId(); if (parentId == null) { return R.fail(ResponseCodesEnum.A0001); } param.setAdminId(null); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.like(TAdmin::getParentId, parentId); query.like(TAdmin::getId, param.getId()); List list = tAdminService.list(query); if (list.size() > 0) { TAdmin admin = list.get(0); // 管理的机器 需要再建立一个关系表 type区分全部还是部分 String equipmentIds = param.getEquipmentIds(); if (StringUtils.isNotEmpty(equipmentIds)) { LambdaQueryWrapper query1 = Wrappers.lambdaQuery(); query1.eq(TAdminEquipment::getAdminId, admin.getId()); List list1 = tAdminEquipmentService.list(query1); if (list1.size() > 0) { TAdminEquipment tAdminEquipment1 = list1.get(0); tAdminEquipmentService.removeById(tAdminEquipment1.getAdminId()); } } // 用户角色关系 LambdaQueryWrapper query2 = Wrappers.lambdaQuery(); query2.eq(SysUserRole::getUserId, admin.getId()); List list2 = sysUserRoleService.list(query2); if (list2.size() > 0) { SysUserRole sysUserRole = list2.get(0); sysUserRoleService.removeById(sysUserRole.getId()); } tAdminService.removeById(admin.getId()); log.debug("删除账号 id:{},TAdmin:{}", admin.getId(), admin); return R.ok(admin); } return R.fail(ResponseCodesEnum.A0100); } @ApiOperation(value = "获取所有子账号信息") @GetMapping("/getChildDrenAdminList") public ResponseModel getChildDrenAdminList(String adminId, String userName, String name, String isUse) { List returnList = new ArrayList<>(); LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getParentId, adminId); if (StringUtils.isNotEmpty(userName)) { query.eq(TAdmin::getUsername, userName); } if (StringUtils.isNotEmpty(name)) { query.eq(TAdmin::getName, name); } if (StringUtils.isNotEmpty(adminId)) { if (adminId.equals("1")) { // query.in(TAdmin::getType,"1","3"); query.eq(TAdmin::getType, "1"); } else { query.eq(TAdmin::getType, "3"); } } else { return R.fail(ResponseCodesEnum.A0100); } query.eq(TAdmin::getIsAdmined, false); List list = tAdminService.list(query); if (list.size() > 0) { for (TAdmin admin : list) { AddLoginUserParam addLoginUserParam = new AddLoginUserParam(); JSONObject jsonObject = new JSONObject(); addLoginUserParam.setUsername(admin.getUsername()); addLoginUserParam.setName(admin.getName()); addLoginUserParam.setEmail(admin.getEmail()); addLoginUserParam.setPhone(admin.getPhone()); addLoginUserParam.setIsEnabled(admin.getIsEnabled()); addLoginUserParam.setId(admin.getId()); LambdaQueryWrapper query2 = Wrappers.lambdaQuery(); query2.eq(TAdminEquipment::getAdminId, admin.getId()); List list1 = tAdminEquipmentService.list(query2); if (list1.size() > 0) { TAdminEquipment tAdminEquipment1 = list1.get(0); if (tAdminEquipment1.getType().equals("0")) { //管理全部机器 //0:全部机器,1:部分机器 tAdminEquipment1.setType("0"); tAdminEquipment1.setEquipmentIds(null); addLoginUserParam.setEquipmentIds("all"); } else { //部分机器 //0:全部机器,1:部分机器 tAdminEquipment1.setType("1"); addLoginUserParam.setEquipmentIds(tAdminEquipment1.getEquipmentIds()); } } LambdaQueryWrapper query1 = Wrappers.lambdaQuery(); query1.eq(SysUserRole::getUserId, admin.getId()); List roleList = sysUserRoleService.list(query1); List ids = new ArrayList<>(); StringBuilder roleName = new StringBuilder(); if (roleList.size() > 0) { for (int i = 0; i < roleList.size(); i++) { SysUserRole sysUserRole = roleList.get(0); ids.add(sysUserRole.getRoleId()); SysRole sysRole = sysRoleService.getById(sysUserRole.getRoleId()); if (i < roleList.size() - 1) { roleName.append(sysRole.getRoleName()).append(","); } else { roleName.append(sysRole.getRoleName()); } // addLoginUserParam.setRoleName(roleName); // List list2 = Arrays.asList(sysRole.getMenuCodesJson().split(",")); // addLoginUserParam.setRoleList(list2); } } addLoginUserParam.setRoleName(roleName.toString()); addLoginUserParam.setRoleList(ids); returnList.add(addLoginUserParam); } } return R.ok(returnList); } @ApiOperation(value = "注册账号/用户注册") @PostMapping("/save") @Transactional @Audit(type = AuditEnum.INSERT, content = "#loginUser.name + '注册账号'") public ResponseModel save(@RequestBody RegisterParamDTO registerParam) { if (StringUtils.isEmpty(registerParam.getUsername()) || StringUtils.isEmpty(registerParam.getName()) || StringUtils.isEmpty(registerParam.getPassword())) { return R.fail(ResponseCodesEnum.A0100, "数据有空!"); } String phoneOrEmail = registerParam.getPhoneOrEmail(); // 定义国内手机号和邮箱的正则表达式 String phoneReg = "^1[3-9]\\d{9}$"; String emailReg = "^[a-zA-Z0-9_-]+([a-zA-Z0-9_.-]*[a-zA-Z0-9])*@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"; if (registerParam.getIfForeign().isEmpty()) { return R.fail(ResponseCodesEnum.A0001, "是否国内外为空!"); } // 国内用户注册 if (registerParam.getIfForeign().equals("0")) { // 判断 phoneOrEmail 是手机号还是邮箱 if (Pattern.matches(phoneReg, phoneOrEmail)) { System.out.println("这是一个手机号"); if (StringUtils.isEmpty(registerParam.getPhoneOrEmail())) { return R.fail(ResponseCodesEnum.A0100, "手机号为空!"); } } else if (Pattern.matches(emailReg, phoneOrEmail)) { System.out.println("这是一个邮箱地址"); if (StringUtils.isEmpty(registerParam.getPhoneOrEmail())) { return R.fail(ResponseCodesEnum.A0100, "邮箱为空!"); } } else { System.out.println("不是有效的手机号或邮箱地址"); return R.fail(ResponseCodesEnum.A0100, "不是有效的手机号或邮箱地址"); } } else { //海外用户注册 if (StringUtils.isEmpty(registerParam.getPhoneOrEmail())) { return R.fail(ResponseCodesEnum.A0100, "邮箱为空!"); } } if (StringUtils.isEmpty(registerParam.getCode())) { return R.fail(ResponseCodesEnum.A0100, "验证码为空!"); } //校验是否有重复的 LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getUsername, registerParam.getUsername()); List list = tAdminService.list(query); if (list.size() > 0) { return R.fail(ResponseCodesEnum.A0201, "用户登录名已存在"); } LambdaQueryWrapper query1 = Wrappers.lambdaQuery(); if (registerParam.getIfForeign().equals("0")) { // 国内分两种 if (Pattern.matches(phoneReg, phoneOrEmail)) { query1.eq(TAdmin::getPhone, registerParam.getPhoneOrEmail()); } else { query1.eq(TAdmin::getEmail, registerParam.getPhoneOrEmail()); } } else { query1.eq(TAdmin::getEmail, registerParam.getPhoneOrEmail()); } List list1 = tAdminService.list(query1); if (list1.size() > 0) { return R.fail(ResponseCodesEnum.A0203, "用户手机/邮箱已存在"); } //校验验证码是否正确 LambdaQueryWrapper query2 = Wrappers.lambdaQuery(); if (registerParam.getIfForeign().equals("0")) { // 国内分两种 if (Pattern.matches(phoneReg, phoneOrEmail)) { query2.eq(TMessageCode::getPhone, registerParam.getPhoneOrEmail()); } else { query2.eq(TMessageCode::getPhone, registerParam.getPhoneOrEmail()); } } else { query2.eq(TMessageCode::getPhone, registerParam.getPhoneOrEmail()); } // 0,代表注册验证码 query2.eq(TMessageCode::getType, "0"); query2.eq(TMessageCode::getStatus, "0"); List messageCodeList = tMessageCodeService.list(query2); TAdmin admin = new TAdmin(); if (messageCodeList.size() > 0) { TMessageCode tMessageCode = messageCodeList.get(messageCodeList.size() - 1); if (!tMessageCode.getCode().equals(registerParam.getCode())) { return R.fail(ResponseCodesEnum.A0002, "验证码错误"); } tMessageCode.setStatus("1"); // admin.setId(IDGenerator.commonID()); // admin 表改回原来的自增ID admin.setCreateDate(new Date()); admin.setModifyDate(new Date()); admin.setIsAdmined(true); admin.setIsEnabled(true); admin.setLoginFailureCount(0); admin.setIsLocked(false); //默认是商家 admin.setType(2); admin.setPassword(registerParam.getPassword()); admin.setUsername(registerParam.getUsername()); admin.setName(registerParam.getName()); admin.setIfForeign(registerParam.getIfForeign()); if (registerParam.getIfForeign().equals("1")) { admin.setPromoCodeOpen("0"); } admin.setCompanyType(registerParam.getCompanyType()); if (Pattern.matches(phoneReg, phoneOrEmail)) { admin.setPhone(registerParam.getPhoneOrEmail()); } else if (Pattern.matches(emailReg, phoneOrEmail)) { admin.setEmail(registerParam.getPhoneOrEmail()); } else { return R.fail(ResponseCodesEnum.A0100, "不是有效的手机号或邮箱地址"); } if (registerParam.getCompanyType().equals("1")) { // 如果是七云平台 admin.setTradeMerchantNo("777138500523174"); //七云商户报备号 admin.setPayPlatform("1"); } else { admin.setPayPlatform("0"); } boolean b = tAdminService.save(admin); tMessageCodeService.saveOrUpdate(tMessageCode); if (b) { // 绑定商家角色 LambdaQueryWrapper query4 = Wrappers.lambdaQuery(); query4.eq(SysRole::getAdminId, "1"); query4.eq(SysRole::getRoleName, "商家"); List list2 = sysRoleService.list(query4); if (list2.size() > 0) { SysRole sysRole = list2.get(0); SysRole newSysRole = new SysRole(); newSysRole.setAdminId(admin.getId()); newSysRole.setMenuCodesJson(sysRole.getMenuCodesJson()); newSysRole.setRoleName("商家"); sysRoleService.save(newSysRole); SysUserRole sysUserRole = new SysUserRole(); sysUserRole.setRoleId(newSysRole.getRoleId()); sysUserRole.setUserId(String.valueOf(admin.getId())); boolean save = sysUserRoleService.save(sysUserRole); } tMessageCode.setModifyDate(new Date()); if (admin.getId() != null) { String managerId = AdminUtils.encrypt(false, admin.getId()); admin.setManagerId(managerId); tAdminService.getById(admin); // 新建海外账户的时候创建一个 Airwallex 钱包账户 try { if ("1".equals(registerParam.getIfForeign())) { TAirwallexWallet wallet = new TAirwallexWallet(); wallet.setAdminId(admin.getId()); // wallet.setAccountCurrency("USD"); // wallet.setAccountAmount(new BigDecimal("0.00")); // airwallexWalletService.save(wallet); // System.out.println("wallet >>> " + wallet); payFeign.saveAirwallexWallet(wallet); } } catch (Exception e) { logger.error("发生异常》》》:" + e.getMessage(), e); } } } return R.ok(b); } else { return R.fail(ResponseCodesEnum.A0002, "没有找到验证码"); } } @ApiOperation(value = "登录") @PostMapping("/login") @Audit(type = AuditEnum.LOGIN, content = "#username + '请求登录'") public ResponseModel login(String username, String password, String hostName, HttpServletRequest request) { if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) { return R.fail(ResponseCodesEnum.A0001, "参数有空"); } //验证用户名登录 LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getPassword, password); query.and( wrapper -> { wrapper.eq(TAdmin::getUsername, username) .or().eq(TAdmin::getPhone, username); } ); TAdmin tAdmin = Optional.ofNullable(tAdminService.getOnly(query)) .orElseThrow(() -> new BizException(ResponseCodesEnum.L0002)); // 判断是为申泽用户还是七云用户 String companyType = tAdmin.getCompanyType(); String SZ = "Sunzee"; String SC = "Sevencloud"; // 如果不为管理员 if (tAdmin.getType() >= 1) { if (SZ.equals(hostName)) { // 如果companyType不为空,且不等于“0” if (StringUtils.isNotEmpty(companyType) && !companyType.equals("0")) { return R.fail(ResponseCodesEnum.L0002); } } if (SC.equals(hostName)) { // 如果companyType为空,或者不等于“1” if (StringUtils.isEmpty(companyType) || !(companyType.equals("1"))) { return R.fail(ResponseCodesEnum.L0002); } } } // 添加系统id if (StringUtils.isEmpty(tAdmin.getManagerId())) { String managerId = AdminUtils.encrypt(false, tAdmin.getId()); tAdmin.setManagerId(managerId); } // 设置登录时间 tAdmin.setLoginDate(new Date()); // 登录IP String ipAddress = null; // 获取通过代理服务器传递的真实IP地址 String xForwardedForHeader = request.getHeader("X-Forwarded-For"); if (xForwardedForHeader == null) { ipAddress = request.getRemoteAddr(); } else { // 多次反向代理后会有多个IP值,第一个IP才是真实IP String[] ips = xForwardedForHeader.split(","); ipAddress = ips[0].trim(); } tAdmin.setLoginIp(ipAddress); tAdminService.updateById(tAdmin); UserDetailBO userDetailBO = tAdminService.getUserDetailBO(tAdmin); return R.ok(userDetailBO); } @ApiOperation(value = "切换自动登录") @GetMapping("/autoLogin") @Audit(type = AuditEnum.LOGIN, content = "切换登录'") public ResponseModel autoLogin(@RequestParam String id) { if (StringUtils.isEmpty(id)) { return R.fail(ResponseCodesEnum.A0001, "参数有空"); } //验证用户名登录 Long adminId = Long.valueOf(id); TAdmin admin = tAdminService.getById(adminId); TAdmin tAdmin = Optional.ofNullable(admin) .orElseThrow(() -> new BizException(ResponseCodesEnum.L0002)); // 添加系统id if (StringUtils.isEmpty(tAdmin.getManagerId())) { String managerId = AdminUtils.encrypt(false, tAdmin.getId()); tAdmin.setManagerId(managerId); tAdminService.getById(tAdmin); } UserDetailBO userDetailBO = tAdminService.getUserDetailBO(tAdmin); return R.ok(userDetailBO); } @ApiOperation(value = "修改密码") @PostMapping("/updatePassword") public ResponseModel updatePassword(String username, String password) { if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) { return R.fail(ResponseCodesEnum.A0001, "参数有空"); } //查找用户名 LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getUsername, username); List list = tAdminService.list(query); if (list.size() > 0) { TAdmin admin = list.get(0); admin.setPassword(password); boolean b = tAdminService.saveOrUpdate(admin); return R.ok(b); } return R.fail(ResponseCodesEnum.A0001, "修改失败"); } @ApiOperation(value = "获取账号信息") @GetMapping("/getAdmin") public ResponseModel getAdmin(@RequestParam String id) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getId, id); TAdmin tAdmin = Optional.ofNullable(tAdminService.getOnly(query)) .orElseThrow(() -> new BizException(ResponseCodesEnum.L0002)); if (StringUtils.isEmpty(tAdmin.getManagerId())) { String managerId = AdminUtils.encrypt(false, tAdmin.getId()); tAdmin.setManagerId(managerId); tAdminService.getById(tAdmin); } UserDetailBO userDetailBO = BeanUtil.copyProperties(tAdmin, UserDetailBO.class); return R.ok(userDetailBO); } @ApiOperation(value = "获取账号列表 分页") @GetMapping("/pageAdmin") public ResponseModel> pageAdmin(String id, String name, String userName, String ifForeign, long current, long size) { if (StringUtils.isEmpty(id)) { return R.fail(ResponseCodesEnum.A0001, "null参数"); } LambdaQueryWrapper query = Wrappers.lambdaQuery(); if (StringUtils.isNotEmpty(name)) { query.like(TAdmin::getName, name); } if (StringUtils.isNotEmpty(userName)) { query.like(TAdmin::getUsername, userName); } if (StringUtils.isNotEmpty(ifForeign)) { query.eq(TAdmin::getIfForeign, ifForeign); } if (StringUtils.isNotEmpty(id)) { TAdmin admin = tAdminService.getById(id); if (admin == null || admin.getId() == null) { return R.fail(ResponseCodesEnum.A0001, "没有找到商家"); } // 申泽平台管理员 if (admin.getId() == 2738) { query.isNull(TAdmin::getCompanyType).or() .eq(TAdmin::getCompanyType, "0"); } // 七云平台管理员 if (admin.getId() == 2739) { query.eq(TAdmin::getCompanyType, "1"); } if (admin.getType() > 1) { query.eq(TAdmin::getRelationAdminId, id); } } query.eq(TAdmin::getType, "2"); query.orderByDesc(TAdmin::getCreateDate); Page page = new Page<>(current, size, true); IPage iPage = tAdminService.page(page, query); return R.ok(iPage); } @ApiOperation(value = "获取账号列表 分页") @GetMapping("/pageAdmin2") public ResponseModel> pageAdmin2(@RequestParam(value = "current") long current, @RequestParam(value = "size") long size) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); Page page = new Page<>(current, size, true); IPage iPage = tAdminService.page(page, query); return R.ok(iPage); } @ApiOperation(value = "获取账号列表") @GetMapping("/listAdmin") public ResponseModel listAdmin(String adminId) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); if (StringUtils.isNotEmpty(adminId)) { query.like(TAdmin::getRelationAdminId, adminId); } List list = tAdminService.list(query); return R.ok(list); } @ApiOperation(value = "修改账号") @PostMapping("/update") @Transactional public ResponseModel update(@RequestBody TAdmin admin) { if (StringUtils.isEmpty(admin.getUsername()) && admin.getId() == null) { return R.fail(ResponseCodesEnum.A0001, "参数空"); } //校验是否有重复的 LambdaQueryWrapper query = Wrappers.lambdaQuery(); if (StringUtils.isNotEmpty(admin.getUsername())) { query.eq(TAdmin::getUsername, admin.getUsername()); } if (admin.getId() != null) { query.eq(TAdmin::getId, admin.getId()); } List list = tAdminService.list(query); TAdmin oldAdmin = list.get(0); if (StringUtils.isNotEmpty(admin.getPassword())) { oldAdmin.setPassword(admin.getPassword()); } if (StringUtils.isNotEmpty(admin.getName())) { oldAdmin.setName(admin.getName()); } if (admin.getAreaId() != null) { oldAdmin.setAreaId(admin.getAreaId()); } if (StringUtils.isNotEmpty(admin.getIfForeign())) { oldAdmin.setIfForeign(admin.getIfForeign()); } if (StringUtils.isNotEmpty(admin.getTradeMerchantNo())) { oldAdmin.setTradeMerchantNo(admin.getTradeMerchantNo()); } if (StringUtils.isNotEmpty(admin.getPayPlatform())) { oldAdmin.setPayPlatform(admin.getPayPlatform()); } if (StringUtils.isNotEmpty(admin.getPromoCodeOpen())) { oldAdmin.setPromoCodeOpen(admin.getPromoCodeOpen()); } if (StringUtils.isNotEmpty(admin.getPhone())) { oldAdmin.setPhone(admin.getPhone()); } if (StringUtils.isNotEmpty(admin.getEmail())) { oldAdmin.setEmail(admin.getEmail()); } if (StringUtils.isNotEmpty(admin.getCurrencySymbol())) { oldAdmin.setCurrencySymbol(admin.getCurrencySymbol()); } else { oldAdmin.setCurrencySymbol("¥"); } if (admin.getApplyStartTime() != null) { oldAdmin.setApplyStartTime(admin.getApplyStartTime()); } if (admin.getApplyEndTime() != null) { oldAdmin.setApplyEndTime(admin.getApplyEndTime()); } if (StringUtils.isNotEmpty(admin.getRelationAdminId())) { oldAdmin.setRelationAdminId(admin.getRelationAdminId()); } if (StringUtils.isNotEmpty(admin.getIsDistribution())) { oldAdmin.setIsDistribution(admin.getIsDistribution()); } oldAdmin.setModifyDate(new Date()); tAdminService.updateById(oldAdmin); return R.ok(null, "修改成功"); } @ApiOperation(value = "设置关联上级") @PostMapping("/setRelationAdminId") public ResponseModel setRelationAdminId(@RequestBody Map params) { String managerId = params.get("managerId"); String username = params.get("username"); String relationUsername = params.get("relationUsername"); if (StringUtils.isEmpty(managerId) || StringUtils.isEmpty(username)) { return R.fail(ResponseCodesEnum.A0001, "参数不能为空"); } LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.eq(TAdmin::getUsername, username); TAdmin admin = tAdminService.getOne(lqw); if (Objects.isNull(admin)) { return R.fail(ResponseCodesEnum.A0206, "用户不存在"); } String managerId1 = admin.getManagerId(); if (!managerId1.equals(managerId)) { return R.fail(ResponseCodesEnum.B0001, "用户名或系统id不匹配"); } if (username.equals(relationUsername)) { return R.fail(ResponseCodesEnum.B0001, "同一账户不能构成上下级"); } if (StringUtils.isEmpty(relationUsername) || "".equals(relationUsername)) { // 无上级,或取消上级 admin.setRelationAdminId(""); } else { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(TAdmin::getUsername, relationUsername); TAdmin relationAdmin = tAdminService.getOne(wrapper); Long relationAdminId = relationAdmin.getId(); admin.setRelationAdminId(String.valueOf(relationAdminId)); } tAdminService.saveOrUpdate(admin); return R.ok("关联成功"); } @ApiOperation(value = "绑定上级账户") @GetMapping("/setRelationAdmin") @Transactional public ResponseModel setRelationAdmin(Long adminId, String username) { TAdmin admin = tAdminService.getById(adminId); if (StringUtils.isNotEmpty(username)) { if (StringUtils.isNotEmpty(admin.getRelationAdminId())) { return R.fail("已有绑定关系"); } LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getUsername, username); List list = tAdminService.list(query); if (list.size() > 0) { TAdmin parentAdmin = list.get(0); if (StringUtils.isNotEmpty(parentAdmin.getRelationAdminId()) && parentAdmin.getRelationAdminId().equals(admin.getId().toString())) { return R.fail("不能互为绑定关系"); } else { admin.setRelationAdminId(String.valueOf(parentAdmin.getId())); tAdminService.updateById(admin); } } else { return R.fail("找不到账户"); } } else { return R.fail(ResponseCodesEnum.A0001); } return R.ok(null, "绑定成功"); } @ApiOperation(value = "获取所有关联(下级)账号信息") @GetMapping("/getAdminIdList") public ResponseModel> getAdminIdList(@RequestParam String adminId) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getRelationAdminId, adminId); List list = tAdminService.list(query); List idList = new ArrayList<>(); for (TAdmin admin : list) { idList.add(admin.getId()); } return R.ok(idList); } @ApiOperation(value = "获取所有关联(下级)账号信息") @GetMapping("/getAdminList") public ResponseModel getAdminList(String adminId) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getRelationAdminId, adminId); List list = tAdminService.list(query); return R.ok(list); } @ApiOperation(value = "获取admin信息") @GetMapping("/getAdminOne/{adminId}") public ResponseModel getAdminOne(@PathVariable Long adminId) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(TAdmin::getId, adminId); TAdmin admin = tAdminService.getOne(wrapper); if (Objects.isNull(admin)) { return R.fail("当前用户不存在"); } return R.ok(admin); } @ApiOperation(value = "获取账号信息byname") @GetMapping("/getAdminByUsername") public ResponseModel getAdminByUsername(@RequestParam String username) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getUsername, username); List list = tAdminService.list(query); if (list.size() > 0) { TAdmin tAdmin = list.get(0); return R.ok(tAdmin); } return R.ok(); } @ApiOperation(value = "通过 adminId 获取上级账号信息") @GetMapping("/getRelation") public ResponseModel getRelation(String adminId) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(TAdmin::getId, adminId); TAdmin admin = tAdminService.getOne(wrapper); if (Objects.nonNull(admin)) { String relationAdminId = admin.getRelationAdminId(); if (StringUtils.isNotEmpty(relationAdminId) && !"".equals(relationAdminId)) { TAdmin relationAdmin = tAdminService.getById(relationAdminId); return R.ok(relationAdmin); } } return R.ok(); } @ApiOperation(value = "通过 username 获取账号的 id") @GetMapping("/getRelationAdminId") public ResponseModel getRelationAdminId(String relationUsername) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(TAdmin::getUsername, relationUsername); TAdmin admin = tAdminService.getOne(wrapper); if (Objects.nonNull(admin)) { Long adminId = admin.getId(); return R.ok(String.valueOf(adminId)); } return R.ok(); } @ApiOperation(value = "获取上级账号名") @GetMapping("/getRelationAdminUsername") public String getRelationAdminUsername(String relationAdminId) { TAdmin admin = tAdminService.getById(relationAdminId); return admin.getUsername(); } @ApiOperation(value = "批量设置账户的角色") @GetMapping("/setRole") public ResponseModel setRole() { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getType, "2"); List list = tAdminService.list(query); if (list.size() > 0) { for (TAdmin admin : list) { // 绑定商家角色 LambdaQueryWrapper query4 = Wrappers.lambdaQuery(); query4.eq(SysRole::getAdminId, "1"); query4.eq(SysRole::getRoleName, "商家"); List list2 = sysRoleService.list(query4); if (list2.size() > 0) { SysRole sysRole = list2.get(0); SysRole newSysRole = new SysRole(); newSysRole.setAdminId(admin.getId()); newSysRole.setMenuCodesJson(sysRole.getMenuCodesJson()); newSysRole.setRoleName("商家"); sysRoleService.save(newSysRole); SysUserRole sysUserRole = new SysUserRole(); sysUserRole.setRoleId(newSysRole.getRoleId()); sysUserRole.setUserId(String.valueOf(admin.getId())); boolean save = sysUserRoleService.save(sysUserRole); } } } return R.ok(); } @ApiOperation(value = "批量设置账户的关联上级") @GetMapping("/setRelation") public ResponseModel setRelation() { List list = tAdminService.list(); if (list.size() > 0) { for (TAdmin admin : list) { if (admin.getParentId() != null) { admin.setRelationAdminId(admin.getParentId().toString()); tAdminService.updateById(admin); } } } return R.ok(); } @ApiOperation(value = "批量修改支付平台") @GetMapping("/updatePayPlatform") public ResponseModel updatePayPlatform(@RequestParam String id, @RequestParam String payPlatform) { Integer adminId = Integer.valueOf(id); UpdateWrapper updateWrapper = new UpdateWrapper<>(); // 申泽管理员 if (adminId == 2738) { updateWrapper.isNull("company_type").or().eq("company_type", 0); } // 七云管理员 if (adminId == 2739) { updateWrapper.eq("company_type", 1); } updateWrapper.set("pay_platform", payPlatform); tAdminService.update(null, updateWrapper); return R.ok(); } @ApiOperation(value = "一键迁移") @GetMapping("/oneKeyMigration") public ResponseModel oneKeyMigration(Long adminId) { LambdaQueryWrapper query4 = Wrappers.lambdaQuery(); query4.eq(SysRole::getAdminId, "1"); query4.eq(SysRole::getRoleName, "商家"); List list2 = sysRoleService.list(query4); if (list2.size() > 0) { SysRole sysRole = list2.get(0); SysRole newSysRole = new SysRole(); newSysRole.setAdminId(adminId); newSysRole.setMenuCodesJson(sysRole.getMenuCodesJson()); newSysRole.setRoleName("商家"); sysRoleService.save(newSysRole); SysUserRole sysUserRole = new SysUserRole(); sysUserRole.setRoleId(newSysRole.getRoleId()); sysUserRole.setUserId(String.valueOf(adminId)); boolean save = sysUserRoleService.save(sysUserRole); return R.ok("success"); } return R.fail(ResponseCodesEnum.A0001, "fail"); } @ApiOperation(value = "检验用户名是否存在") @GetMapping("/checkUserName") public ResponseModel checkUserName(String username) { LambdaQueryWrapper query = Wrappers.lambdaQuery(); query.eq(TAdmin::getUsername, username); TAdmin admin = tAdminService.getOne(query); if (admin != null) { return R.fail(ResponseCodesEnum.A0001); } return R.ok(); } @ApiOperation(value = "切换订单通知开关") @GetMapping("/updateOrderNotice") public ResponseModel updateOrderNotice(String adminId, String orderNotice) { if (StringUtils.isEmpty(adminId) || StringUtils.isEmpty(orderNotice)) { return R.fail(ResponseCodesEnum.A0001); } // 查询是否有绑定微信 if (orderNotice.equals("1")) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(TWechat::getAdminId, adminId); TWechat wechat = wechatService.getOne(wrapper); if (wechat == null) { return R.fail(ResponseCodesEnum.A0001, "请先绑定微信"); } } TAdmin admin = tAdminService.getById(adminId); admin.setOrderNotice(orderNotice); tAdminService.updateById(admin); return R.ok(); } }