| | |
| | | package com.matrix.system.app.action; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.matrix.component.redis.RedisClient; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.matrix.component.tools.HttpCurlUtil; |
| | | import com.matrix.core.pojo.AjaxResult; |
| | | import com.matrix.core.tools.UUIDUtil; |
| | | import com.matrix.system.app.dto.LoginDto; |
| | | import com.matrix.core.tools.*; |
| | | import com.matrix.system.app.authority.AppAuthorityManager; |
| | | import com.matrix.system.app.dto.*; |
| | | import com.matrix.system.app.utils.Sms106Send; |
| | | import com.matrix.system.app.vo.UserInfoVo; |
| | | import com.matrix.system.common.authority.DefaultAuthorityManager; |
| | | import com.matrix.system.common.authority.strategy.AccountPasswordLogin; |
| | | import com.matrix.system.common.authority.strategy.LoginStrategy; |
| | | import com.matrix.system.common.bean.SysCompany; |
| | | import com.matrix.system.common.bean.SysUsers; |
| | | import com.matrix.system.common.constance.AppConstance; |
| | | import com.matrix.system.common.dao.BusParameterSettingsDao; |
| | | import com.matrix.system.common.init.LocalCache; |
| | | import com.matrix.system.common.init.UserCacheManager; |
| | | import com.matrix.system.common.interceptor.HostInterceptor; |
| | | import com.matrix.system.common.service.SysCompanyService; |
| | | import com.matrix.system.common.service.SysUsersService; |
| | | import com.matrix.system.common.tools.PasswordUtil; |
| | | import com.matrix.system.common.tools.UploadUtil; |
| | | import com.matrix.system.hive.bean.AppVersion; |
| | | import com.matrix.system.hive.bean.SysShopInfo; |
| | | import com.matrix.system.hive.plugin.util.CollectionUtils; |
| | | import com.matrix.system.hive.plugin.util.ImageUtil; |
| | | import com.matrix.system.hive.service.SysShopInfoService; |
| | | import com.matrix.system.shopXcx.api.WeChatApiTools; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import io.swagger.annotations.ApiResponse; |
| | | import io.swagger.annotations.ApiResponses; |
| | | import net.sf.json.JSONObject; |
| | | import org.apache.commons.fileupload.FileUploadException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @author wzy |
| | | * @date 2020-12-21 |
| | | **/ |
| | | @CrossOrigin(origins = "*") |
| | | @Api(value = "CommonAction", tags = "手机端公共请求类(含登陆)") |
| | | @RestController |
| | | @RequestMapping(value = "/api/common") |
| | |
| | | private SysShopInfoService sysShopInfoService; |
| | | |
| | | @Autowired |
| | | private DefaultAuthorityManager authorityManager; |
| | | private AppAuthorityManager authorityManager; |
| | | |
| | | @Autowired |
| | | private RedisClient redisClient; |
| | | private BusParameterSettingsDao busParameterSettingsDao; |
| | | |
| | | @Autowired |
| | | private SysCompanyService sysCompanyService;; |
| | | |
| | | @Autowired |
| | | WeChatApiTools weChatApiTools; |
| | | |
| | | @Autowired |
| | | private UserCacheManager userCacheManager; |
| | | |
| | | @Value("${file_storage_path}") |
| | | private String fileStoragePath; |
| | | @Value("${static_resource_url}") |
| | | private String nginxUrl; |
| | | |
| | | @Value("${login_public_key}") |
| | | private String publicKey; |
| | | |
| | | @ApiOperation(value = "登陆接口", notes = "手机端登陆接口") |
| | | @ApiResponses({ |
| | | @ApiResponse(code = 200, message = "OK", response = UserInfoVo.class) |
| | | }) |
| | | @PostMapping(value = "/login") |
| | | public AjaxResult login(@RequestBody @Validated LoginDto loginDto) { |
| | | public AjaxResult login(@RequestBody @Validated LoginDto loginDto) throws Exception { |
| | | |
| | | SysUsers user = new SysUsers(); |
| | | user.setSuAccount(loginDto.getUsername()); |
| | |
| | | LoginStrategy apLogin = new AccountPasswordLogin(user, sysUsersService); |
| | | user = authorityManager.login(apLogin); |
| | | |
| | | UserInfoVo userInfoVo = new UserInfoVo(); |
| | | if(user.getShopId()!=null){ |
| | | user.setShopName(sysShopInfoService.findById(user.getShopId()).getShopName()); |
| | | SysShopInfo shopInfo = sysShopInfoService.findById(user.getShopId()); |
| | | user.setShopName(shopInfo.getShopName()); |
| | | userInfoVo.setShopName(shopInfo.getShopShortName()); |
| | | } |
| | | user.setSuPassword(null); |
| | | |
| | | String token = UUIDUtil.getRandomID(); |
| | | redisClient.saveValue(token, JSONObject.toJSONString(user), 360000); |
| | | redisClient.saveValue(user.getSuId().toString(), token, 3600000); |
| | | LocalCache.save(token, user); |
| | | |
| | | UserInfoVo userInfoVo = new UserInfoVo(); |
| | | userInfoVo.setId(user.getSuId()); |
| | | userInfoVo.setName(user.getSuName()); |
| | | userInfoVo.setRoleName(user.getRoleName()); |
| | | userInfoVo.setPhoto(user.getSuPhoto()); |
| | | |
| | | String sb = token + "_1" + "_/api/vip/findVipInfoById/972"; |
| | | AjaxResult result = AjaxResult.buildSuccessInstance("登陆成功"); |
| | | authorityManager.initUserPower(result,user); |
| | | result.putInMap("user", userInfoVo); |
| | | result.putInMap("token", token); |
| | | result.putInMap("rasToken", RSAUtils.encryptByPublicKey(sb, publicKey)); |
| | | return result; |
| | | } |
| | | |
| | | @ApiOperation(value = "图片上传接口", notes = "图片上传接口") |
| | | @PostMapping(value = "/uploadPhotoBase64") |
| | | public AjaxResult uploadPhotoBase64(@RequestBody @Validated UploadPhotoDto uploadPhotoDto) { |
| | | // 文件保存目录路径 |
| | | String savePath = fileStoragePath; |
| | | String abstractPath= DateUtil.dateFormatStr(new Date(),DateUtil.DATE_FORMAT_NO_SPLITE_DD); |
| | | savePath=savePath+File.separator+abstractPath; |
| | | |
| | | String fileName =UUIDUtil.getRandomID() + ".png"; |
| | | |
| | | |
| | | // 文件保存目录URL |
| | | String saveUrl = nginxUrl; |
| | | LogUtil.debug("图片保存目录路径={}",fileStoragePath); |
| | | LogUtil.debug("图片保存目录URL={}",nginxUrl); |
| | | // 保存和访问路径检查 |
| | | if (StringUtils.isBlank(saveUrl) || StringUtils.isBlank(savePath)) { |
| | | return AjaxResult.buildFailInstance("文件上传失败错误代码:001"); |
| | | } |
| | | |
| | | // 检查目录 |
| | | File uploadDir = new File(savePath); |
| | | if (!uploadDir.isDirectory()) { |
| | | uploadDir.mkdir(); |
| | | } |
| | | |
| | | ImageUtil.base64ToFile(uploadPhotoDto.getBase64(),savePath , fileName); |
| | | LogUtil.info("fileName : {}", fileName); |
| | | |
| | | AjaxResult ajaxResult = AjaxResult.buildSuccessInstance("上传成功"); |
| | | ajaxResult.putInMap("file", nginxUrl + abstractPath+"/"+fileName); |
| | | return ajaxResult; |
| | | } |
| | | |
| | | |
| | | String folderType = "image"; |
| | | public final static List<FileType> EXT_LIST = new ArrayList<FileType>(); |
| | | static { |
| | | EXT_LIST.add(FileType.JPEG); |
| | | EXT_LIST.add(FileType.PNG); |
| | | } |
| | | |
| | | @ApiOperation(value = "表单图片上传接口", notes = "表单图片上传接口") |
| | | @PostMapping(value = "/uploadImg") |
| | | public AjaxResult uploadImg(HttpServletResponse response, MultipartHttpServletRequest request) |
| | | throws IOException, FileUploadException, NoSuchAlgorithmException { |
| | | Map<String, String> fileMap = UploadUtil.doUpload(request, EXT_LIST, folderType, 1L); |
| | | AjaxResult ajaxResult = AjaxResult.buildSuccessInstance("上传成功"); |
| | | ajaxResult.putInMap("file", fileMap.get("visitPath")); |
| | | return ajaxResult; |
| | | } |
| | | |
| | | |
| | | @ApiOperation(value = "短信验证码发送", notes = "短信验证码发送") |
| | | @PostMapping(value = "/sendSmsCode") |
| | | public AjaxResult sendSmsCode(@RequestBody @Validated SmsCodeDto smsCodeDto) { |
| | | SysUsers user = new SysUsers(); |
| | | user.setSuTel(smsCodeDto.getTelphone()); |
| | | List<SysUsers> users = sysUsersService.findByModel(user); |
| | | if (CollectionUtils.isEmpty(users)) { |
| | | return AjaxResult.buildFailInstance("该手机号不存在"); |
| | | } |
| | | |
| | | String codeExist = LocalCache.get(smsCodeDto.getTelphone()); |
| | | if (StringUtils.isNotBlank(codeExist)) { |
| | | return AjaxResult.buildFailInstance("请勿重复发送验证码"); |
| | | } |
| | | |
| | | Integer code =(int) ((Math.random() * 9 + 1) * 100000); |
| | | Sms106Send.sendVerifyCode(smsCodeDto.getTelphone(), code.toString(), 2); |
| | | LocalCache.save(smsCodeDto.getTelphone(), code.toString(), 120 * 1000); |
| | | return AjaxResult.buildSuccessInstance("发送成功"); |
| | | } |
| | | |
| | | @ApiOperation(value = "重置登陆密码", notes = "重置登陆密码") |
| | | @PostMapping(value = "/resetLoginPwd") |
| | | public AjaxResult resetLoginPwd(@RequestBody @Validated PwdResetDto pwdResetDto) throws UnsupportedEncodingException, NoSuchAlgorithmException { |
| | | SysUsers user = new SysUsers(); |
| | | user.setSuTel(pwdResetDto.getTelphone()); |
| | | List<SysUsers> users = sysUsersService.findByModel(user); |
| | | if (CollectionUtils.isEmpty(users)) { |
| | | return AjaxResult.buildFailInstance("该手机号不存在"); |
| | | } |
| | | |
| | | String code = LocalCache.get(pwdResetDto.getTelphone()); |
| | | if (StringUtils.isBlank(code)) { |
| | | return AjaxResult.buildFailInstance("验证码已失效,请重新发送"); |
| | | } |
| | | |
| | | if (!code.equals(pwdResetDto.getCode())) { |
| | | return AjaxResult.buildFailInstance("验证码错误"); |
| | | } |
| | | |
| | | user = users.get(0); |
| | | user.setSuPassword(pwdResetDto.getNewPwd()); |
| | | String pwd = PasswordUtil.getEncrypUserPwd(user); |
| | | |
| | | int i = sysUsersService.updateUserPassword(user.getSuId(), pwd); |
| | | if (i > 0) { |
| | | LocalCache.remove(pwdResetDto.getTelphone()); |
| | | return AjaxResult.buildSuccessInstance("修改成功"); |
| | | } |
| | | return AjaxResult.buildFailInstance("修改失败"); |
| | | } |
| | | |
| | | @ApiOperation(value = "获取app版本", notes = "获取app版本") |
| | | @ApiResponses({ |
| | | @ApiResponse(code = 200, message = "ok", response = AppVersion.class) |
| | | }) |
| | | @GetMapping(value = "/findAppVersion") |
| | | public AjaxResult findAppVersion() { |
| | | return AjaxResult.buildSuccessInstance(sysUsersService.findAppVersion()); |
| | | } |
| | | |
| | | |
| | | @ApiOperation(value = "获取api接口地址", notes = "获取api接口地址") |
| | | @GetMapping(value = "/findApiUrl") |
| | | public AjaxResult findApiUrl() { |
| | | List<UrlDto> data = new ArrayList<>(); |
| | | UrlDto urlDto = new UrlDto(); |
| | | urlDto.setUrl("http://myapp.jyymatrix.cc"); |
| | | urlDto.setName("正式环境"); |
| | | data.add(urlDto); |
| | | |
| | | urlDto = new UrlDto(); |
| | | urlDto.setUrl("http://test.hive.jyymatrix.cc"); |
| | | urlDto.setName("测试环境"); |
| | | data.add(urlDto); |
| | | |
| | | urlDto = new UrlDto(); |
| | | urlDto.setUrl("http://hive.mydoline.com"); |
| | | urlDto.setName("美度专线"); |
| | | data.add(urlDto); |
| | | |
| | | urlDto = new UrlDto(); |
| | | urlDto.setUrl("http://localhost:8080"); |
| | | urlDto.setName("开发环境"); |
| | | data.add(urlDto); |
| | | |
| | | return AjaxResult.buildSuccessInstance(data); |
| | | } |
| | | |
| | | |
| | | @ApiOperation(value = "根据企业编码获取对应的请求地址") |
| | | @PostMapping(value = "/getUrlByCompanyCode") |
| | | public AjaxResult getUrlByCompanyCode(@RequestBody SettingDto codeDto) { |
| | | SysCompany query = new SysCompany(); |
| | | query.setComCode(codeDto.getCode()); |
| | | List<SysCompany> list = sysCompanyService.findByModel(query); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return new AjaxResult(AjaxResult.STATUS_FAIL, "企业编码有误"); |
| | | } |
| | | |
| | | AjaxResult ajaxResult = new AjaxResult(); |
| | | ajaxResult.setStatus(AjaxResult.STATUS_SUCCESS); |
| | | ajaxResult.putInMap("url", list.get(0).getComWebUrl()); |
| | | return ajaxResult; |
| | | } |
| | | |
| | | @ApiOperation(value = "管理端小程序登录") |
| | | @PostMapping(value = "/wxLogin/{code}") |
| | | public AjaxResult wxLogin(@PathVariable("code") String code) throws IOException { |
| | | if (StrUtil.isBlank(code)) { |
| | | return AjaxResult.buildFailInstance("code不存在"); |
| | | } |
| | | AjaxResult res = new AjaxResult(); |
| | | String requrl = weChatApiTools.getManagerXcxLoginUrl(code); |
| | | |
| | | String resultData = HttpCurlUtil.sendGetHttp(requrl, null); |
| | | JSONObject json = JSONObject.fromObject(resultData); |
| | | LogUtil.debug("管理端小程序登录获取到登录信息={}", json); |
| | | |
| | | if (json.containsKey("errcode")) { |
| | | res.setStatus(AjaxResult.STATUS_FAIL); |
| | | res.setInfo("自动登录失败"); |
| | | LogUtil.info("微信登录获取到异常信息errcode"); |
| | | return res; |
| | | } |
| | | |
| | | String openId = json.getString("openid"); |
| | | res.setStatus(AjaxResult.STATUS_SUCCESS); |
| | | res.putInMap("openId", openId); |
| | | SysUsers hasBind = sysUsersService.findByOpenId(openId, HostInterceptor.getCompanyId()); |
| | | if (hasBind == null) { |
| | | res.setInfo("未绑定用户"); |
| | | return res; |
| | | } |
| | | |
| | | String token = userCacheManager.saveUserInfo(hasBind); |
| | | LogUtil.info("用户token={}", token); |
| | | |
| | | authorityManager.initUserPower(res, hasBind); |
| | | |
| | | UserInfoVo userInfoVo = new UserInfoVo(); |
| | | if(hasBind.getShopId()!=null){ |
| | | SysShopInfo shopInfo = sysShopInfoService.findById(hasBind.getShopId()); |
| | | hasBind.setShopName(shopInfo.getShopName()); |
| | | userInfoVo.setShopName(shopInfo.getShopShortName()); |
| | | } |
| | | |
| | | userInfoVo.setId(hasBind.getSuId()); |
| | | userInfoVo.setName(hasBind.getSuName()); |
| | | userInfoVo.setRoleName(hasBind.getRoleName()); |
| | | userInfoVo.setPhoto(hasBind.getSuPhoto()); |
| | | |
| | | res.putInMap("token", token); |
| | | res.putInMap("userInfo", userInfoVo); |
| | | return res; |
| | | } |
| | | |
| | | @ApiOperation(value = "绑定用户") |
| | | @PostMapping(value = "/bindUser") |
| | | public AjaxResult bindUser(@RequestBody @Validated LoginDto loginDto) { |
| | | SysUsers user = new SysUsers(); |
| | | user.setSuAccount(loginDto.getUsername()); |
| | | user.setSuPassword(loginDto.getPassword()); |
| | | LoginStrategy apLogin = new AccountPasswordLogin(user, sysUsersService); |
| | | user = authorityManager.login(apLogin); |
| | | user.setSuPassword(null); |
| | | user.setOpenIds(null); |
| | | String token = userCacheManager.saveUserInfo(user); |
| | | |
| | | AjaxResult result = AjaxResult.buildSuccessInstance("登陆成功"); |
| | | authorityManager.initUserPower(result, user); |
| | | |
| | | UserInfoVo userInfoVo = new UserInfoVo(); |
| | | if(user.getShopId()!=null){ |
| | | SysShopInfo shopInfo = sysShopInfoService.findById(user.getShopId()); |
| | | user.setShopName(shopInfo.getShopName()); |
| | | userInfoVo.setShopName(shopInfo.getShopShortName()); |
| | | } |
| | | |
| | | userInfoVo.setId(user.getSuId()); |
| | | userInfoVo.setName(user.getSuName()); |
| | | userInfoVo.setRoleName(user.getRoleName()); |
| | | userInfoVo.setPhoto(user.getSuPhoto()); |
| | | |
| | | result.putInMap("token", token); |
| | | result.putInMap("userInfo", userInfoVo); |
| | | |
| | | synchronized (this) { |
| | | SysUsers hasBind = sysUsersService.findByOpenId(loginDto.getOpenId(), HostInterceptor.getCompanyId()); |
| | | if (hasBind != null) { |
| | | if(StrUtil.isNotBlank(hasBind.getOpenIds())) { |
| | | List<String> openIds = StrUtil.split(hasBind.getOpenIds(), ','); |
| | | openIds.remove(loginDto.getOpenId()); |
| | | |
| | | sysUsersService.modifyUserOpenId(CollUtil.join(openIds, ","), hasBind.getSuId()); |
| | | } |
| | | } |
| | | |
| | | List<String> openIds = StrUtil.split(user.getOpenIds(), ','); |
| | | openIds.add(loginDto.getOpenId()); |
| | | user.setOpenIds(CollUtil.join(openIds, ",")); |
| | | sysUsersService.modifyByModel(user); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | } |