package com.glxp.api.controller.auth; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.PageInfo; import com.glxp.api.annotation.AuthRuleAnnotation; import com.glxp.api.annotation.Log; import com.glxp.api.common.enums.ResultEnum; import com.glxp.api.common.res.BaseResponse; import com.glxp.api.common.util.ResultVOUtils; import com.glxp.api.constant.BusinessType; import com.glxp.api.dao.auth.AuthAdminDao; import com.glxp.api.entity.auth.AuthAdmin; import com.glxp.api.entity.auth.CustomerInfoEntity; import com.glxp.api.entity.auth.UserRegisterEntity; import com.glxp.api.entity.basic.BasicUnitMaintainEntity; import com.glxp.api.entity.purchase.CustomerContactEntity; import com.glxp.api.entity.system.CompanyEntity; import com.glxp.api.entity.system.SystemParamConfigEntity; import com.glxp.api.req.auth.CustomerInfoFilterRequest; import com.glxp.api.req.auth.RegisterCheckRequest; import com.glxp.api.req.auth.ResetPasswdRequest; import com.glxp.api.req.auth.UserResisterFilterRequest; import com.glxp.api.req.info.FilterAliCertRequest; import com.glxp.api.req.system.DeleteRequest; import com.glxp.api.res.PageSimpleResponse; import com.glxp.api.res.auth.CheckCodeRequest; import com.glxp.api.service.auth.AuthAdminService; import com.glxp.api.service.auth.AuthRoleAdminService; import com.glxp.api.service.auth.CustomerInfoService; import com.glxp.api.service.auth.UserRegisterService; import com.glxp.api.service.basic.BasicUnitMaintainService; import com.glxp.api.service.purchase.CustomerContactService; import com.glxp.api.service.system.CompanyService; import com.glxp.api.service.system.SystemParamConfigService; import com.glxp.api.util.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import javax.validation.Valid; import java.util.*; @RestController @Slf4j public class RegisterController { private static String Url = "http://106.ihuyi.com/webservice/sms.php?method=Submit"; @Resource UserRegisterService userRegisterService; @Resource private AuthRoleAdminService authRoleAdminService; @Resource private AuthAdminService authAdminService; @Resource private CustomerInfoService customerInfoService; @Resource private CustomerContactService customerContactService; @Resource private CompanyService companyService; @Resource private BasicUnitMaintainService basicUnitMaintainService; @Resource private SystemParamConfigService systemParamConfigService; @Resource RedisUtil redisUtil; @AuthRuleAnnotation("") @GetMapping("salewarehouse/register/list") public BaseResponse filterList(UserResisterFilterRequest userResisterFilterRequest) { List userRegisterEntities = userRegisterService.filterUserRegisters(userResisterFilterRequest); PageInfo pageInfo; pageInfo = new PageInfo<>(userRegisterEntities); PageSimpleResponse pageSimpleResponse = new PageSimpleResponse<>(); pageSimpleResponse.setTotal(pageInfo.getTotal()); pageSimpleResponse.setList(userRegisterEntities); return ResultVOUtils.success(pageSimpleResponse); } @PostMapping("salewarehouse/register/getCheckcode") public BaseResponse getCheckcode(@RequestBody CheckCodeRequest checkCodeRequest, HttpSession httpSession) { if (checkCodeRequest.getUserName() != null) { AuthAdmin authAdmin = authAdminService.findByUserName(checkCodeRequest.getUserName()); if (authAdmin == null) { return ResultVOUtils.error(500, "未找到该用户"); } UserRegisterEntity userRegisterEntity = userRegisterService.selectByCustomerId(String.valueOf(authAdmin.getCustomerId())); if (!userRegisterEntity.getMobile().equals(checkCodeRequest.getPhoneNum())) { CustomerContactEntity customerContactEntity = customerContactService.selectById(Long.valueOf(String.valueOf(authAdmin.getCustomerId()))); if (customerContactEntity != null) { if (!customerContactEntity.getMobile().equals(checkCodeRequest.getPhoneNum())) { return ResultVOUtils.error(500, "手机号与注册时不一致"); } } else { return ResultVOUtils.error(500, "手机号与注册时不一致"); } } } RestTemplate restTemplate = new RestTemplate(); int mobile_code = (int) ((Math.random() * 9 + 1) * 100000); MultiValueMap postParameters = new LinkedMultiValueMap<>(); int time = 5; String content = new String("您正在进行手机验证,验证码是" + mobile_code + "," + time + "分钟内有效。"); postParameters.add("account", "C07086222"); postParameters.add("password", "2dddbbf73636c193c5903324bdb47c5c"); postParameters.add("mobile", checkCodeRequest.getPhoneNum()); postParameters.add("content", content); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/x-www-form-urlencoded;charset=GBK"); HttpEntity> httpEntity = new HttpEntity<>(postParameters, headers); String response = restTemplate.postForObject(Url, httpEntity, String.class); if (response.contains("提交成功")) { JSONObject json = new JSONObject(); json.put("memPhone", checkCodeRequest.getPhoneNum()); json.put("code", mobile_code); json.put("createTime", System.currentTimeMillis() + ""); //验证码存入redis中 redisUtil.set(Constant.CAPTCHAS + checkCodeRequest.getPhoneNum(), json, 300L); return ResultVOUtils.success("发送成功" + " " + mobile_code); } else { return ResultVOUtils.error(500, "验证码发送失败"); } } @GetMapping("salewarehouse/register/checkCompany") public BaseResponse checkCompany(@RequestParam("companyName") String companyName) { if (StringUtils.isBlank(companyName)) return ResultVOUtils.error(ResultEnum.DATA_ERROR, "企业名称不能为空"); if (basicUnitMaintainService.selectByName(companyName) != null) { return ResultVOUtils.success("校验成功"); } else { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, "校验失败"); } } @GetMapping("salewarehouse/register/getRegisterConfig") public BaseResponse getRegisterConfig() { Map rMap = new HashMap<>(); SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey("register_company_check"); if (systemParamConfigEntity == null || StringUtils.isBlank(systemParamConfigEntity.getParamValue()) || systemParamConfigEntity.getParamValue().equals("0")) { rMap.put("companyCheckStatus", false); } else { rMap.put("companyCheckStatus", true); } SystemParamConfigEntity systemParamConfigEntity1 = systemParamConfigService.selectByParamKey("register_udplat_check"); if (systemParamConfigEntity1 == null || StringUtils.isBlank(systemParamConfigEntity1.getParamValue()) || systemParamConfigEntity1.getParamValue().equals("0")) { rMap.put("udplatCheckStatus", false); } else { rMap.put("udplatCheckStatus", true); } return ResultVOUtils.success(rMap); } @AuthRuleAnnotation("") @PostMapping("salewarehouse/register/deleteResiter") @Log(title = "用户管理", businessType = BusinessType.DELETE) public BaseResponse deleteResiter(@RequestBody DeleteRequest deleteRequest, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } String id = deleteRequest.getId(); userRegisterService.deleteById(Long.parseLong(id)); return ResultVOUtils.success("删除成功"); } @PostMapping("salewarehouse/register/update") @Log(title = "用户管理", businessType = BusinessType.UPDATE) public BaseResponse updateRegister(@RequestBody @Valid UserRegisterEntity userRegisterEntity, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } boolean b = userRegisterService.updateUserRegister(userRegisterEntity); if (!b) { return ResultVOUtils.error(ResultEnum.NOT_NETWORK); } return ResultVOUtils.success("添加成功"); } @PostMapping("salewarehouse/register/save") public BaseResponse saveRegister(@RequestBody @Valid UserRegisterEntity userRegisterEntity, HttpSession httpSession, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } String codeStr = String.valueOf(redisUtil.get(Constant.CAPTCHAS + userRegisterEntity.getMobile())); if (StrUtil.isBlank(codeStr) || "null".equals(codeStr)) { return ResultVOUtils.error(500, "验证码已过期,请重新获取"); } JSONObject userCode = JSONObject.parseObject(codeStr); if (userCode == null) { return ResultVOUtils.error(500, "验证码已失效,请重新获取"); } long lastTime = userCode.getLong("createTime"); int checkCode = (Integer) userCode.get("code"); String phoneNum = userCode.getString("memPhone"); if ((System.currentTimeMillis() - lastTime) > 1000 * 60 * 5) { return ResultVOUtils.error(500, "验证码已过期,请重新获取"); } if (checkCode != userRegisterEntity.getCheckCode()) { return ResultVOUtils.error(500, "验证码错误,请重新获取"); } if (!phoneNum.equals(userRegisterEntity.getMobile())) { return ResultVOUtils.error(500, "手机号与获取验证码不一致"); } boolean isExitPhone = userRegisterService.isExit(userRegisterEntity.getMobile()); if (isExitPhone) { return ResultVOUtils.error(500, "该手机号已被注册"); } //校验邮箱是否正确 if (StrUtil.isNotBlank(userRegisterEntity.getEmail())) { boolean match = ReUtil.isMatch("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", userRegisterEntity.getEmail()); if (!match) { return ResultVOUtils.error(500, "邮箱格式错误"); } } String company_check = systemParamConfigService.selectValueByParamKey("register_company_check"); if (StrUtil.isNotBlank(company_check) && company_check.equals("1")) { //新用户注册时根据往来单位字典进行企业名称校验 CustomerInfoEntity customerInfoEntity = customerInfoService.selectById(userRegisterEntity.getCompanyId()); if (customerInfoEntity != null) { return ResultVOUtils.error(500, "该企业已被注册!"); } } UserRegisterEntity userRegisterEntity2 = userRegisterService.selectByCname(userRegisterEntity.getCompanyName()); if (userRegisterEntity2 != null) { return ResultVOUtils.error(500, "该企业已被注册!"); } List unifyWarehouse = userRegisterEntity.getUnifyWarehouse(); if (unifyWarehouse == null || unifyWarehouse.size() == 0){ return ResultVOUtils.error(500, "配送医院不可以为空!"); } userRegisterEntity.setRegisterTime(DateUtil.getDateTime()); userRegisterEntity.setCheckType(0);//未审核 String userName = UuidUtils.getShortUuid(4); switch (userRegisterEntity.getBussinessStatus()) { case 1: userName = "YY" + userName; break; case 2: userName = "JYQY" + userName; break; default: userName = "JYQY" + userName; break; } userRegisterEntity.setUserName(userName); UserRegisterEntity userRegisterEntity1 = userRegisterService.selectByMobile(userRegisterEntity.getMobile()); if (userRegisterEntity1 != null) { userRegisterService.deleteByMobile(userRegisterEntity.getMobile(), 2); } boolean b = userRegisterService.insertUserRegister(userRegisterEntity); if (!b) { return ResultVOUtils.error(ResultEnum.NOT_NETWORK); } //注册成功,删除验证码缓存 redisUtil.del(Constant.CAPTCHAS + userRegisterEntity.getMobile()); return ResultVOUtils.success(userRegisterEntity.getUserName()); } @PostMapping("salewarehouse/reset/passwd") public BaseResponse resetPasswd(@RequestBody @Valid ResetPasswdRequest resetPasswdRequest, HttpSession httpSession, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } log.info(httpSession.getId()); JSONObject userCode = JSONObject.parseObject(String.valueOf(redisUtil.get(Constant.CAPTCHAS + resetPasswdRequest.getMobile()))); if (userCode == null) { return ResultVOUtils.error(500, "验证码已失效,请重新获取"); } long lastTime = userCode.getLong("createTime"); int checkCode = (Integer) userCode.get("code"); String phoneNum = userCode.getString("memPhone"); if ((System.currentTimeMillis() - lastTime) > 1000 * 60 * 5) { return ResultVOUtils.error(500, "验证码已过期,请重新获取"); } if (checkCode != resetPasswdRequest.getCheckCode()) { return ResultVOUtils.error(500, "验证码错误,请重新获取"); } if (!phoneNum.equals(resetPasswdRequest.getMobile())) { return ResultVOUtils.error(500, "手机号与获取验证码不一致"); } AuthAdmin authAdmin = authAdminService.findByUserName(resetPasswdRequest.getUserName()); if (authAdmin == null) { return ResultVOUtils.error(500, "该用户不存在"); } CustomerContactEntity customerContactEntity = customerContactService.selectById(Long.valueOf(String.valueOf(authAdmin.getCustomerId()))); if (customerContactEntity != null && customerContactEntity.getMobile().equals(phoneNum)) { String newPwd = PasswordUtils.authAdminPwd(resetPasswdRequest.getPassword()); authAdmin.setPassWord(newPwd); authAdmin.setLastModifyTime(new Date()); authAdminService.updateAuthAdmin(authAdmin); } else { return ResultVOUtils.error(500, "手机号与该用户不匹配"); } return ResultVOUtils.success("密码修改成功"); } @Resource AuthAdminDao authAdminDao; @AuthRuleAnnotation("") @PostMapping("salewarehouse/register/check") @Log(title = "用户管理", businessType = BusinessType.INSERT) public BaseResponse checkRegister(@RequestBody @Valid RegisterCheckRequest registerCheckRequest, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage()); } Long customerId = 0L; UserRegisterEntity userRegisterEntity = userRegisterService.selectById(registerCheckRequest.getId()); //判断是否选择供应商 if (StrUtil.isBlank(registerCheckRequest.getCompanyId()) && StrUtil.isBlank(registerCheckRequest.getCompanyName())) { //根据公司名称查询往来单位信息 BasicUnitMaintainEntity basicUnitMaintainEntity = basicUnitMaintainService.selectByName(userRegisterEntity.getCompanyName()); //没有就新建 if (basicUnitMaintainEntity == null) { if (registerCheckRequest.getCheckType() == 1) { //注册时新增到往来单位字典 BasicUnitMaintainEntity basicUnitMaintainEntity1 = new BasicUnitMaintainEntity(); basicUnitMaintainEntity1.setId(IdUtil.getSnowflakeNextId()); basicUnitMaintainEntity1.setName(userRegisterEntity.getCompanyName()); basicUnitMaintainEntity1.setErpId(generateUserId() + ""); basicUnitMaintainEntity1.setCorpType(2); basicUnitMaintainEntity1.setOutType(0); basicUnitMaintainEntity1.setCreditNo(userRegisterEntity.getCreditNum()); basicUnitMaintainEntity1.setAddr(userRegisterEntity.getArea() + userRegisterEntity.getDetailAddr()); basicUnitMaintainEntity1.setContact(userRegisterEntity.getRealName()); basicUnitMaintainEntity1.setMobile(userRegisterEntity.getMobile()); basicUnitMaintainEntity1.setUpdateTime(new Date()); basicUnitMaintainService.insertBasicUnitMaintain(basicUnitMaintainEntity1); customerId = Long.parseLong(basicUnitMaintainEntity1.getErpId()); registerCheckRequest.setCompanyId(basicUnitMaintainEntity1.getErpId()); registerCheckRequest.setCompanyName(basicUnitMaintainEntity1.getName()); } } else { customerId = Long.parseLong(basicUnitMaintainEntity.getErpId()); registerCheckRequest.setCompanyId(basicUnitMaintainEntity.getErpId()); registerCheckRequest.setCompanyName(basicUnitMaintainEntity.getName()); } } else { customerId = Long.parseLong(registerCheckRequest.getCompanyId()); } if (registerCheckRequest.getCheckType() == 1) { //通过就更新用户id 供应商用户 供应商id userRegisterEntity.setCompanyId(registerCheckRequest.getCompanyId()); userRegisterEntity.setCompanyName(registerCheckRequest.getCompanyName()); userRegisterEntity.setUserId(generateUserId() + ""); } userRegisterEntity.setCheckType(registerCheckRequest.getCheckType()); userRegisterEntity.setCheckTime(DateUtil.getDateTime()); boolean b = userRegisterService.updateUserRegister(userRegisterEntity); if (registerCheckRequest.getCheckType() != 1) { return ResultVOUtils.success("修改成功"); } //创建新用户 AuthAdmin authAdmin = new AuthAdmin(); authAdmin.setId(generateUserId()); authAdmin.setCustomerId(customerId + ""); String newPwd = PasswordUtils.authAdminPwd(userRegisterEntity.getPassword()); authAdmin.setPassWord(newPwd); authAdmin.setCreateTime(DateUtil.parseDate(userRegisterEntity.getCheckTime())); authAdmin.setUserName(userRegisterEntity.getUserName()); //用手机号当用户名,通过手机号登录 authAdmin.setEmployeeName(userRegisterEntity.getNickName()); authAdmin.setCustomerId(userRegisterEntity.getCompanyId()); //默认给部门、仓库 默认给随机用户ID,避免数据同步时出错覆盖 authAdmin.setLocDeptCode("1000"); authAdmin.setLocInvCode("1000000"); authAdmin.setUserFlag(1); authAdmin.setLastModifyTime(new Date()); authAdminDao.insert(authAdmin); // authAdminService.insertAuthAdmin(authAdmin); authAdmin = authAdminService.findByUserName(authAdmin.getUserName()); //分配角色 // List roles = new ArrayList<>(); // roles.add(Long.valueOf(13));//默认角色 //生产客户信息表 CustomerInfoEntity customerInfoEntity = new CustomerInfoEntity(); // customerInfoEntity.setCustomerId(Long.valueOf(String.valueOf(authAdmin.getCustomerId()))); customerInfoEntity.setCustomerId(customerId); customerInfoEntity.setArea(userRegisterEntity.getArea()); customerInfoEntity.setDetailAddr(userRegisterEntity.getDetailAddr()); customerInfoEntity.setBussinessStatus(userRegisterEntity.getBussinessStatus()); customerInfoEntity.setCreditNum(userRegisterEntity.getCreditNum()); customerInfoEntity.setCustomerName(userRegisterEntity.getNickName()); customerInfoEntity.setUserFlag(1); customerInfoEntity.setUserMax(2); customerInfoEntity.setRoleId(registerCheckRequest.getRoles() + ""); customerInfoEntity.setCompanyName(userRegisterEntity.getCompanyName()); List roles = new ArrayList<>(); if (registerCheckRequest.getRoles() != null) { roles.add(registerCheckRequest.getRoles()); authRoleAdminService.insertRolesAdminIdAll(roles, authAdmin.getId()); } //生产客户信息联系人表 CustomerContactEntity customerContactEntity = new CustomerContactEntity(); customerContactEntity.setContacts(userRegisterEntity.getRealName()); customerContactEntity.setCustomerId(customerId + ""); customerContactEntity.setEmail(userRegisterEntity.getEmail()); customerContactEntity.setMobile(userRegisterEntity.getMobile()); customerContactEntity.setTel(userRegisterEntity.getTel()); CustomerContactEntity customerContactEntity1 = customerContactService.selectById(customerId); if (customerContactEntity1 == null) { customerContactService.insertCustomerContact(customerContactEntity); } //生产本企业信息 CompanyEntity companyEntity = new CompanyEntity(); companyEntity.setCustomerId(customerId + ""); companyEntity.setArea(userRegisterEntity.getArea()); companyEntity.setAreaCode(userRegisterEntity.getAreaCode()); companyEntity.setDetailAddr(userRegisterEntity.getDetailAddr()); companyEntity.setContacts(customerContactEntity.getContacts()); companyEntity.setEmail(userRegisterEntity.getEmail()); companyEntity.setMobile(userRegisterEntity.getMobile()); companyEntity.setTel(userRegisterEntity.getTel()); companyEntity.setCompanyName(userRegisterEntity.getCompanyName()); companyEntity.setUnitIdFk(userRegisterEntity.getCompanyId()); companyEntity.setClasses(userRegisterEntity.getBussinessStatus() + ""); companyEntity.setBussinessStatus(userRegisterEntity.getBussinessStatus()); companyEntity.setCreditNum(userRegisterEntity.getCreditNum()); BasicUnitMaintainEntity basicUnitMaintainEntity = basicUnitMaintainService.selectByErpId(userRegisterEntity.getCompanyId()); if (basicUnitMaintainEntity != null) { basicUnitMaintainEntity.setThirdId1(userRegisterEntity.getUdplatCompanyId()); basicUnitMaintainEntity.setThirdName1(userRegisterEntity.getUdplatCompanyName()); basicUnitMaintainService.updateById(basicUnitMaintainEntity); } FilterAliCertRequest filterAliCertRequest = new FilterAliCertRequest(); filterAliCertRequest.setId(registerCheckRequest.getAliCertId()); CustomerInfoFilterRequest request = new CustomerInfoFilterRequest(); request.setCompanyName(registerCheckRequest.getCompanyName()); List customerInfoEntities = customerInfoService.filterCustomerInfo(request); int i = 0; for (CustomerInfoEntity infoEntity : customerInfoEntities) { if (registerCheckRequest.getCompanyName().equals(infoEntity.getCompanyName())) { i++; } } if (i <= 0) { customerInfoService.insertCustomerInfo(customerInfoEntity); companyService.insertCompany(companyEntity);//TODO !!!!!!!!!!! } if (!b) { return ResultVOUtils.error(ResultEnum.NOT_NETWORK); } return ResultVOUtils.success("添加成功"); } public long generateUserId() { String userId = System.currentTimeMillis() + ""; userId = userId.substring(1, userId.length()); int max = 100, min = 1; int ran2 = (int) (Math.random() * (max - min) + min); userId = userId + ran2; return Long.parseLong(userId); } }