18 changed files with 1384 additions and 256 deletions
@ -0,0 +1,486 @@ |
|||
package com.yxt.portal.apiwx; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.auth0.jwt.JWT; |
|||
import com.auth0.jwt.interfaces.DecodedJWT; |
|||
import com.yxt.common.base.config.RedisUtil; |
|||
import com.yxt.common.base.config.component.FileUploadComponent; |
|||
import com.yxt.common.base.utils.*; |
|||
import com.yxt.common.core.result.ResultBean; |
|||
import com.yxt.portal.biz.sysorganization.SysOrganization; |
|||
import com.yxt.portal.biz.sysorganization.SysOrganizationService; |
|||
import com.yxt.portal.biz.sysrole.SysRoleService; |
|||
import com.yxt.portal.biz.sysstafforg.SysStaffOrg; |
|||
import com.yxt.portal.biz.sysuser.SysUser; |
|||
import com.yxt.portal.biz.sysuser.SysUserQuery; |
|||
import com.yxt.portal.biz.sysuser.SysUserService; |
|||
import com.yxt.portal.biz.sysuser.SysUserVo; |
|||
import com.yxt.portal.biz.sysuser.app.AppMySysUserInfo; |
|||
import com.yxt.portal.biz.sysuser.app.MyInfoQuery; |
|||
import io.swagger.annotations.Api; |
|||
import io.swagger.annotations.ApiOperation; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Controller; |
|||
import org.springframework.web.bind.annotation.*; |
|||
import org.springframework.web.multipart.MultipartFile; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @Description |
|||
* @Author liuguohui |
|||
* @Date 2021/9/4 |
|||
*/ |
|||
@Api(tags = "用户表-移动端") |
|||
@Controller |
|||
@RequestMapping("apiwx/appuser") |
|||
public class AppSysUserRest{ |
|||
|
|||
@Autowired |
|||
private SysUserService sysUserService; |
|||
|
|||
@Autowired |
|||
private RedisUtil redisUtil; |
|||
@Autowired(required = false) |
|||
private HttpServletRequest httpServletRequest; |
|||
@Autowired |
|||
private FileUploadComponent fileUploadComponent; |
|||
@Autowired |
|||
private SysRoleService sysRoleService; |
|||
@Autowired |
|||
private SysOrganizationService sysOrganizationService; |
|||
|
|||
@PostMapping("/login") |
|||
@ResponseBody |
|||
@ApiOperation(value = "1、app用户登录") |
|||
public ResultBean<SysUserVo> login(SysUserQuery userQuery) { |
|||
/* |
|||
1、根据传参设备appId查询绑定账号 |
|||
1、1未查询到绑定的账号: |
|||
1、1、1、查询用户登录账号(不存在-->返回“账号不存在”); |
|||
1、1、2、账号存在,获取当前账号绑定的设备值: |
|||
1)设备值为空,验证登录成功后即绑定设备,返回用户信息,验证不成功返回错误信息(验证码、密码错误) |
|||
2)设备值存在,向表sys_exception_log插入数据,返回:当前账号已被其它设备绑定(错误码101(说明:错误码为sys_exception_log的id)) |
|||
1、2查询到绑定的账号 |
|||
1、2、1、根据传参设备appId查询到绑定的账号与用户传递的账号不一致 |
|||
1)向表sys_exception_log插入数据,返回:当前设备已绑定其它账号(错误码102) |
|||
1、2、2、根据传参设备appId查询到绑定的账号与用户传递的账号一致,验证登录信息 |
|||
1)验证登录信息,成功后返回用户信息,验证不成功返回错误信息(验证码、密码错误) |
|||
*/ |
|||
ResultBean<SysUserVo> rb = ResultBean.fireFail(); |
|||
String userName = userQuery.getUserName(); |
|||
String password = userQuery.getPassword(); |
|||
String appId = userQuery.getAppId(); |
|||
String verifyCode = userQuery.getVerifyCode(); |
|||
String type = userQuery.getType(); // type:1、密码登录2、验证码登录
|
|||
|
|||
if (StringUtils.isBlank(userName)) return new ResultBean<SysUserVo>().fail().setMsg("用户名不能为空"); |
|||
// if (StringUtils.isBlank(appId)) return new ResultBean<SysUserVo>().fail().setMsg("appId不能为空");
|
|||
if (type.equals("1")) { |
|||
if (StringUtils.isBlank(password)) return new ResultBean<SysUserVo>().fail().setMsg("密码不能为空"); |
|||
} else { |
|||
if (StringUtils.isBlank(verifyCode)) return new ResultBean<SysUserVo>().fail().setMsg("验证码不能为空"); |
|||
// 访问后清理key
|
|||
} |
|||
boolean isTest = false; |
|||
if(userQuery.getIsTest() != null){ |
|||
isTest = userQuery.getIsTest(); |
|||
} |
|||
isTest = true; |
|||
if (isTest) {//添加测试版
|
|||
//将该appId的所有账号的appId都置为空
|
|||
if(StringUtils.isNotBlank(appId)){ |
|||
sysUserService.updateAppIdNull(appId); |
|||
} |
|||
SysUser sysUser = sysUserService.selectByUserNameApp(userName); |
|||
if (sysUser == null) { |
|||
sysUser = sysUserService.selectByMobile(userName); |
|||
if (sysUser == null) { |
|||
return rb.setMsg("用户名或密码错误"); |
|||
} |
|||
} |
|||
if (type.equals("1")) { |
|||
String md5 = Encodes.md5(password); |
|||
if (md5.equals(sysUser.getPassword())) { |
|||
if(StringUtils.isNotBlank(appId)){ |
|||
sysUserService.updateAppId(sysUser.getSid(), appId); |
|||
} |
|||
SysUserVo sysUserVo = sysUserService.selectUser(sysUser); |
|||
if (sysUserVo == null) { |
|||
return rb.setMsg("用户名或密码错误"); |
|||
} |
|||
insertLoginLog(sysUser); |
|||
return new ResultBean<SysUserVo>().success().setData(sysUserVo); |
|||
} else { |
|||
return new ResultBean<SysUserVo>().fail().setMsg("用户名或密码错误"); |
|||
} |
|||
} else { |
|||
String codeFromRedis = redisUtil.get("loginCode" + userName); |
|||
if (StringUtils.isBlank(codeFromRedis)) |
|||
return new ResultBean<SysUserVo>().fail().setMsg("短信验证码已失效,请重新发送"); |
|||
if (verifyCode.equals(codeFromRedis.substring(0, 4))) { |
|||
if(StringUtils.isNotBlank(appId)){ |
|||
sysUserService.updateAppId(sysUser.getSid(), appId); |
|||
} |
|||
SysUserVo sysUserVo = sysUserService.selectUser(sysUser); |
|||
if (sysUserVo == null) { |
|||
return rb.setMsg("用户名或密码错误"); |
|||
} |
|||
redisUtil.remove("loginCode" + userName); |
|||
insertLoginLog(sysUser); |
|||
return new ResultBean<SysUserVo>().success().setData(sysUserVo); |
|||
} else { |
|||
return new ResultBean<SysUserVo>().fail().setMsg("短信验证码错误或已失效,请重新获取"); |
|||
} |
|||
} |
|||
} else { |
|||
SysUser sysUser = sysUserService.selectByAppId(appId); |
|||
if (null == sysUser) { // 未查询到绑定的账号
|
|||
sysUser = sysUserService.selectByUserNameApp(userName); |
|||
if (sysUser == null) { |
|||
sysUser = sysUserService.selectByMobile(userName); |
|||
if (sysUser == null) { |
|||
return rb.setMsg("用户名或密码错误"); |
|||
} |
|||
} |
|||
String appIdVal = sysUser.getAppId(); |
|||
if (StringUtils.isBlank(appIdVal)) { // 设备值为空
|
|||
if (type.equals("1")) { |
|||
String md5 = Encodes.md5(password); |
|||
if (md5.equals(sysUser.getPassword())) { |
|||
sysUserService.updateAppId(sysUser.getSid(), appId); |
|||
SysUserVo sysUserVo = sysUserService.selectUser(sysUser); |
|||
if (sysUserVo == null) { |
|||
return rb.setMsg("用户名或密码错误"); |
|||
} |
|||
insertLoginLog(sysUser); |
|||
return new ResultBean<SysUserVo>().success().setData(sysUserVo); |
|||
} else { |
|||
return new ResultBean<SysUserVo>().fail().setMsg("用户名或密码错误"); |
|||
} |
|||
} else { |
|||
String codeFromRedis = redisUtil.get("loginCode" + userName); |
|||
if (StringUtils.isBlank(codeFromRedis)) |
|||
return new ResultBean<SysUserVo>().fail().setMsg("短信验证码已失效,请重新发送"); |
|||
if (verifyCode.equals(codeFromRedis.substring(0, 4))) { |
|||
sysUserService.updateAppId(sysUser.getSid(), appId); |
|||
SysUserVo sysUserVo = sysUserService.selectUser(sysUser); |
|||
if (sysUserVo == null) { |
|||
return rb.setMsg("用户名或密码错误"); |
|||
} |
|||
redisUtil.remove("loginCode" + userName); |
|||
insertLoginLog(sysUser); |
|||
return new ResultBean<SysUserVo>().success().setData(sysUserVo); |
|||
} else { |
|||
return new ResultBean<SysUserVo>().fail().setMsg("短信验证码错误或已失效,请重新获取"); |
|||
} |
|||
} |
|||
} else { // 设备值存在
|
|||
int res = insertSysExceptionLog("b", JSON.toJSON(userQuery).toString(), "当前账号已被其它设备绑定"); |
|||
return new ResultBean<SysUserVo>().fail().setMsg("当前账号已被其它设备绑定(错误码:" + res + ")"); |
|||
} |
|||
} else { // 查询到绑定的账号
|
|||
if (userName.equals(sysUser.getUserName()) || userName.equals(sysUser.getMobile())) { // 设备appId一致,验证用户名
|
|||
if (type.equals("1")) { |
|||
String md5 = Encodes.md5(password); |
|||
if (md5.equals(sysUser.getPassword())) { |
|||
SysUserVo sysUserVo = sysUserService.selectUser(sysUser); |
|||
if (sysUserVo == null) { |
|||
return rb.setMsg("用户名或密码错误"); |
|||
} |
|||
insertLoginLog(sysUser); |
|||
return new ResultBean<SysUserVo>().success().setData(sysUserVo); |
|||
} else { |
|||
return new ResultBean<SysUserVo>().fail().setMsg("用户名或密码错误"); |
|||
} |
|||
} else { |
|||
String codeFromRedis = redisUtil.get("loginCode" + userName); |
|||
if (StringUtils.isBlank(codeFromRedis)) |
|||
return new ResultBean<SysUserVo>().fail().setMsg("短信验证码已失效,请重新发送"); |
|||
if (verifyCode.equals(codeFromRedis.substring(0, 4))) { |
|||
SysUserVo sysUserVo = sysUserService.selectUser(sysUser); |
|||
if (sysUserVo == null) { |
|||
return rb.setMsg("该用户不是公司员工"); |
|||
} |
|||
redisUtil.remove("loginCode" + userName); |
|||
insertLoginLog(sysUser); |
|||
return new ResultBean<SysUserVo>().success().setData(sysUserVo); |
|||
} else { |
|||
return new ResultBean<SysUserVo>().fail().setMsg("短信验证码错误或已失效,请重新获取"); |
|||
} |
|||
} |
|||
} else { // 用户名不一致
|
|||
int res = insertSysExceptionLog("a", JSON.toJSON(userQuery).toString(), "当前设备已绑定其它账号"); |
|||
return new ResultBean<SysUserVo>().fail().setMsg("当前设备已绑定其它账号(错误码:" + res + ")"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 插入登录日志 |
|||
* |
|||
* @param user |
|||
*/ |
|||
private void insertLoginLog(SysUser user) { |
|||
// SystemLog systemLog = new SystemLog();
|
|||
// systemLog.setUserName(user.getUserName());
|
|||
// String ip = WebUtil.getIpAddr(httpServletRequest);
|
|||
// systemLog.setUserIp(ip);
|
|||
// systemLog.setEventUrl("portal/v1/appuser/login");
|
|||
// systemLog.setEventContent("用户登录");
|
|||
// systemLog.setEventName("用户登录");
|
|||
// systemLogService.save(systemLog);
|
|||
} |
|||
|
|||
/** |
|||
* 手机发送短信验证码 |
|||
* |
|||
* @param mobile |
|||
* @param type 1、登录2、修改密码3、找回密码 |
|||
* @param appId 绑定手机的appid |
|||
* @return |
|||
*/ |
|||
@ApiOperation(value = "手机发送验证码(登录、修改密码、找回密码)") |
|||
@ResponseBody |
|||
@PostMapping("/sendVerificationCodeForApp") |
|||
public ResultBean sendVerificationCodeForApp(String mobile, String type, String appId) { |
|||
if (StringUtils.isBlank(mobile) || !RegexUtil.isMobile(mobile)) { |
|||
return new ResultBean().fail().setMsg("请输入正确的手机号"); |
|||
} |
|||
if (type.equals("1") && StringUtils.isBlank(appId)) { |
|||
return new ResultBean().fail().setMsg("appId不能为空"); |
|||
} |
|||
// 登录、修改密码、重置密码先验证mobile与appId是否一致
|
|||
// if(type.equals("1") && !appId.equals(sysUser.getAppId())){
|
|||
// return new ResultBean().fail().setMsg("当前设备与账号不匹配,请解绑后再试");
|
|||
// }
|
|||
if (type.equals("2") || type.equals("3")) { |
|||
SysUser sysUser = sysUserService.selectByUserNameApp(mobile); |
|||
if (null == sysUser) { |
|||
sysUser = sysUserService.selectByMobile(mobile); |
|||
if (sysUser == null) { |
|||
return new ResultBean().fail().setMsg("该手机号暂时还未注册,请先注册"); |
|||
} |
|||
|
|||
} |
|||
} else { // 登录
|
|||
SysUser sysUser = sysUserService.selectByAppId(appId); |
|||
if (sysUser == null) { // 设备未绑定账号
|
|||
sysUser = sysUserService.selectByUserNameApp(mobile); |
|||
if (sysUser == null) { |
|||
sysUser = sysUserService.selectByMobile(mobile); |
|||
if (sysUser == null) { |
|||
return new ResultBean().fail().setMsg("该手机号暂时还未注册,请先注册"); |
|||
} |
|||
} |
|||
/*if (StringUtils.isNotBlank(sysUser.getAppId())) { |
|||
Map<String, Object> map = new HashMap<>(); |
|||
map.put("mobile", mobile); |
|||
map.put("appId", appId); |
|||
int res = insertSysExceptionLog("b", JSON.toJSON(map).toString(), "当前账号已被其它设备绑定"); |
|||
return new ResultBean<SysUserVo>().fail().setMsg("当前账号已被其它设备绑定(错误码:" + res + ")"); |
|||
}*/ |
|||
} else { // 设备已绑定账号
|
|||
/* if (!mobile.equals(sysUser.getMobile())) { |
|||
Map<String, Object> map = new HashMap<>(); |
|||
map.put("mobile", mobile); |
|||
map.put("appId", appId); |
|||
int res = insertSysExceptionLog("a", JSON.toJSON(map).toString(), "当前设备已绑定其它账号"); |
|||
return new ResultBean<SysUserVo>().fail().setMsg("当前设备已绑定其它账号(错误码:" + res + ")"); |
|||
}*/ |
|||
} |
|||
} |
|||
return sysUserService.sendVerificationCodeForApp(mobile, type); |
|||
} |
|||
|
|||
/** |
|||
* 插入错误的信息 |
|||
* |
|||
* @param operChar |
|||
* @param operArg |
|||
* @param remark |
|||
*/ |
|||
private int insertSysExceptionLog(String operChar, String operArg, String remark) { |
|||
// SysExceptionLog sysExceptionLog = new SysExceptionLog();
|
|||
// sysExceptionLog.setExceptionCode("M00001"); // 当前设备已绑定其它账号
|
|||
// sysExceptionLog.setExceptionType("移动端登陆");
|
|||
// sysExceptionLog.setOperChar(operChar);
|
|||
// sysExceptionLog.setOperArg(operArg);
|
|||
// sysExceptionLog.setRemark(remark);
|
|||
// sysExceptionLogService.insert(sysExceptionLog);
|
|||
return 1; |
|||
} |
|||
|
|||
@ApiOperation(value = "我的信息") |
|||
@ResponseBody |
|||
@GetMapping("/getMyInfo/{userSid}") |
|||
public ResultBean getMyInfo(String userSid) { |
|||
if (StringUtils.isBlank(userSid)) return new ResultBean().fail().setMsg("userSid不能为空"); |
|||
SysUserVo sysUserVo = sysUserService.fetchBySidVo(userSid); |
|||
if (null == sysUserVo) new ResultBean().fail().setMsg("信息错误"); |
|||
AppMySysUserInfo userInfo = new AppMySysUserInfo(); |
|||
String headImage = ""; |
|||
if (StringUtils.isNotBlank(sysUserVo.getHeadImage())) { |
|||
headImage = fileUploadComponent.getUrlPrefix() + sysUserVo.getHeadImage(); |
|||
} |
|||
userInfo.setHeadImage(headImage); |
|||
userInfo.setName(StringUtils.isBlank(sysUserVo.getName()) ? "" : sysUserVo.getName()); |
|||
userInfo.setId("ID:" + sysUserVo.getId()); |
|||
// Map<String, Object> map = new HashMap<>();
|
|||
// map.put("headImage", StringUtils.isBlank(sysUserVo.getHeadImage())?"":sysUserVo.getHeadImage());
|
|||
// map.put("name", StringUtils.isBlank(sysUserVo.getName())?"":sysUserVo.getName());
|
|||
// map.put("ID", "ID:"+sysUserVo.getId()); // 是否为jobNumber,暂时为空
|
|||
return new ResultBean().success().setData(userInfo); |
|||
} |
|||
|
|||
@ApiOperation(value = "我的信息:切换机构") |
|||
@ResponseBody |
|||
@GetMapping("/getMyInfo") |
|||
public ResultBean getMyInfo(MyInfoQuery myInfoQuery) { |
|||
ResultBean rb = ResultBean.fireFail(); |
|||
String userSid = myInfoQuery.getUserSid(); |
|||
String orgPath = myInfoQuery.getOrgPath(); |
|||
SysUserVo sysUserVo = sysUserService.fetchBySidVo(userSid); |
|||
if (null == sysUserVo) rb.setMsg("信息错误"); |
|||
AppMySysUserInfo userInfo = new AppMySysUserInfo(); |
|||
String headImage = ""; |
|||
if (StringUtils.isNotBlank(sysUserVo.getHeadImage())) { |
|||
headImage = fileUploadComponent.getUrlPrefix() + sysUserVo.getHeadImage(); |
|||
} |
|||
userInfo.setHeadImage(headImage); |
|||
userInfo.setName(StringUtils.isBlank(sysUserVo.getName()) ? "" : sysUserVo.getName()); |
|||
userInfo.setId("ID:" + sysUserVo.getJobNumber()); |
|||
//根据组织机构sid查询该用户的职位以及分公司的部门
|
|||
List<String> strings = new ArrayList<>(); |
|||
List<SysStaffOrg> list = sysUserService.selectOrgBySid(orgPath, sysUserVo.getStaffSid()); |
|||
for (SysStaffOrg s : list) { |
|||
String orgSid = s.getOrgSid(); |
|||
SysOrganization sysOrganization = sysOrganizationService.fetchBySid(orgSid); |
|||
// String orgName = s.getOrgName();
|
|||
strings.add(sysOrganization.getName()); |
|||
} |
|||
strings.removeAll(Collections.singleton(null)); |
|||
if (!strings.isEmpty()) { |
|||
userInfo.setDepartment(String.join("|", strings)); |
|||
} |
|||
String isAdmin = ""; |
|||
SysUser sysUser = sysUserService.fetchBySid(userSid); |
|||
if(sysUser != null){ |
|||
isAdmin = sysUser.getIsAdmin(); |
|||
} |
|||
if("1".equals(isAdmin)){ |
|||
String roleName = sysRoleService.selectByUserSid(userSid); |
|||
userInfo.setPosition(roleName); |
|||
|
|||
}else{ |
|||
//根据用户sid查询岗位
|
|||
List<String> stringList = sysUserService.getPost(sysUserVo.getStaffSid()); |
|||
stringList.removeAll(Collections.singleton(null)); |
|||
if (!stringList.isEmpty()) { |
|||
userInfo.setPosition(String.join("|", stringList)); |
|||
} |
|||
} |
|||
|
|||
return new ResultBean().success().setData(userInfo); |
|||
} |
|||
|
|||
@ApiOperation(value = "手机更改密码") |
|||
@ResponseBody |
|||
@PostMapping("/updatePassword") |
|||
public ResultBean<SysUserVo> updatePassword(String userSid, String appId, String oldPassword, String newPassword, HttpServletRequest httpServletRequest) { |
|||
ResultBean<SysUserVo> rb = ResultBean.fireFail(); |
|||
String token = httpServletRequest.getHeader("token"); |
|||
SysUser user = sysUserService.fetchBySid(userSid); |
|||
if (user == null) { |
|||
return rb.setMsg("抱歉,用户不存在"); |
|||
} |
|||
if (!appId.equals(user.getAppId())) { |
|||
return rb.setMsg("当前设备与账号不匹配,请解绑后再试"); |
|||
} |
|||
return sysUserService.updatePasswordApp(user, oldPassword, newPassword, token); |
|||
} |
|||
|
|||
@ApiOperation(value = "手机找回密码验证验证码") |
|||
@ResponseBody |
|||
@PostMapping("/checkResetPwdCode") |
|||
public ResultBean checkResetPwdCode(String resetPwdCode, String mobile) { |
|||
if (StringUtils.isBlank(resetPwdCode)) return new ResultBean().fail().setMsg("验证码不能为空"); |
|||
String codeFromRedis = redisUtil.get("resetPwdCode" + mobile); |
|||
if (StringUtils.isBlank(codeFromRedis)) return new ResultBean<SysUserVo>().fail().setMsg("短信验证码已失效,请重新发送"); |
|||
if (resetPwdCode.equals(codeFromRedis.substring(0, 4))) { |
|||
redisUtil.remove("resetPwdCode" + mobile); |
|||
return new ResultBean<SysUserVo>().success(); |
|||
} else { |
|||
return new ResultBean<SysUserVo>().fail().setMsg("短信验证码错误或已失效,请重新获取"); |
|||
} |
|||
} |
|||
|
|||
|
|||
@ApiOperation(value = "手机端找回密码") |
|||
@ResponseBody |
|||
@PostMapping("/resetPwd") |
|||
public ResultBean resetPwdApp(String mobile, String newPwd, HttpServletRequest httpServletRequest) { |
|||
String token = httpServletRequest.getHeader("token"); |
|||
if (StringUtils.isBlank(mobile) || !RegexUtil.isMobile(mobile)) |
|||
return new ResultBean().fail().setMsg("请输入正确的手机号"); |
|||
if (StringUtils.isBlank(newPwd)) return new ResultBean().fail().setMsg("请输入密码"); |
|||
return sysUserService.resetPwdApp(mobile, newPwd, token); |
|||
} |
|||
|
|||
@ApiOperation(value = "手机修改用户头像") |
|||
@ResponseBody |
|||
@PostMapping("/updateHeadImage") |
|||
public ResultBean updateHeadImage(MultipartFile multipartFile, String userSid) { |
|||
SysUser user = sysUserService.fetchBySid(userSid); |
|||
if (user == null) return new ResultBean().fail().setMsg("抱歉,用户不存在"); |
|||
if (multipartFile.isEmpty()) { |
|||
return new ResultBean().fail().setMsg("上传图片不能为空"); |
|||
} |
|||
String filename = multipartFile.getOriginalFilename(); |
|||
String suffix = filename.substring(filename.lastIndexOf(".")); |
|||
if (!suffix.equals(".jpg") && !suffix.equals(".jpeg") && !suffix.equals(".png") && !suffix.equals(".bmp") && !suffix.equals(".tif") && !suffix.equals(".gif")) { |
|||
return new ResultBean().fail().setMsg("图片格式不正确"); |
|||
} |
|||
return sysUserService.updateHeadImage(multipartFile, user); |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 手机端根据token获取userSid |
|||
* |
|||
* @return |
|||
*/ |
|||
@ApiOperation(value = "手机端根据token获取userSid") |
|||
@ResponseBody |
|||
@PostMapping("/getUserSidByToken") |
|||
public ResultBean<String> getUserSidByToken(String token) { |
|||
DecodedJWT decode = JWT.decode(token.substring(3)); |
|||
String userSid = JWTUtil.getUserSid(decode); |
|||
if (StringUtils.isBlank(userSid)) { |
|||
return new ResultBean().fail().setMsg("用户sid不能为空"); |
|||
} else { |
|||
return new ResultBean<String>().success().setData(userSid); |
|||
} |
|||
} |
|||
|
|||
@ApiOperation(value = "解绑某个用户或解绑所有的用户") |
|||
@ResponseBody |
|||
@PutMapping("/updateAppId") |
|||
public ResultBean updateAppId(String mobile) { |
|||
return sysUserService.updateAppIdByMobile(mobile); |
|||
} |
|||
|
|||
// @ApiOperation(value = "移动端:查询该菜单是否允许创建,是否有提醒消息")
|
|||
// @GetMapping(value = "/selectAppHaveMessage")
|
|||
// public ResultBean selectAppHaveMessage(String menuSid, String orgPath) {
|
|||
// return sysUserService.selectAppHaveMessage(menuSid,orgPath);
|
|||
// }
|
|||
|
|||
|
|||
// 解绑设备关联的账号
|
|||
// 解绑账号绑定的设备
|
|||
} |
@ -0,0 +1,578 @@ |
|||
package com.yxt.portal.apiwx; |
|||
|
|||
import cn.hutool.core.codec.Base64; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.alibaba.fastjson.TypeReference; |
|||
import com.auth0.jwt.JWT; |
|||
import com.auth0.jwt.interfaces.DecodedJWT; |
|||
import com.yxt.common.base.config.RedisUtil; |
|||
import com.yxt.common.base.config.component.FileUploadComponent; |
|||
import com.yxt.common.base.utils.*; |
|||
import com.yxt.common.core.result.ResultBean; |
|||
import com.yxt.portal.biz.sysorganization.SysOrganization; |
|||
import com.yxt.portal.biz.sysorganization.SysOrganizationService; |
|||
import com.yxt.portal.biz.sysstaffinfo.SysStaffinfoService; |
|||
import com.yxt.portal.biz.sysuser.SysUser; |
|||
import com.yxt.portal.biz.sysuser.SysUserService; |
|||
import com.yxt.portal.biz.sysuser.wx.*; |
|||
import com.yxt.portal.config.DictCommonType; |
|||
import io.swagger.annotations.Api; |
|||
import io.swagger.annotations.ApiImplicitParam; |
|||
import io.swagger.annotations.ApiImplicitParams; |
|||
import io.swagger.annotations.ApiOperation; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Controller; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.ResponseBody; |
|||
|
|||
import javax.crypto.Mac; |
|||
import javax.crypto.spec.SecretKeySpec; |
|||
import javax.servlet.http.HttpServletRequest; |
|||
import java.io.BufferedReader; |
|||
import java.io.DataOutputStream; |
|||
import java.io.InputStreamReader; |
|||
import java.io.UnsupportedEncodingException; |
|||
import java.net.HttpURLConnection; |
|||
import java.net.URL; |
|||
import java.net.URLEncoder; |
|||
import java.security.InvalidKeyException; |
|||
import java.security.Key; |
|||
import java.security.NoSuchAlgorithmException; |
|||
import java.text.DateFormat; |
|||
import java.text.ParseException; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.*; |
|||
|
|||
/** |
|||
* @author dimengzhe |
|||
* @date 2021/10/5 13:09 |
|||
* @description 客户端用户接口 |
|||
*/ |
|||
@Controller |
|||
@RequestMapping("apiwx/wxuser") |
|||
@Api(tags = "用户表-小程序端") |
|||
public class WxSysUserRest{ |
|||
|
|||
@Autowired |
|||
private SysUserService sysUserService; |
|||
@Autowired |
|||
private SysStaffinfoService sysStaffinfoService; |
|||
|
|||
@Autowired |
|||
private RedisUtil redisUtil; |
|||
|
|||
@Autowired |
|||
private SysOrganizationService sysOrganizationService; |
|||
@Autowired |
|||
private HttpServletRequest request; |
|||
@Autowired |
|||
private FileUploadComponent fileUploadComponent; |
|||
@Autowired |
|||
private HttpServletRequest httpServletRequest; |
|||
|
|||
/** |
|||
* 调用发送短信接口返回值 |
|||
*/ |
|||
static final String RESULT_CODE = "1"; |
|||
|
|||
/** |
|||
* @param mobile 手机号 |
|||
* @param type 类型:1、注册,2、登录 3、忘记密码,4旧手机号获取验证码、5新手机号 |
|||
* @description: 获取验证码(按类型区分是哪块验证码) |
|||
* @return: |
|||
* @Author: dimengzhe |
|||
* @Date: 2021/10/9 9:12 |
|||
*/ |
|||
@ApiImplicitParams({ |
|||
@ApiImplicitParam(name = "mobile", value = "手机号", required = true), |
|||
@ApiImplicitParam(name = "type", value = "类型:1、注册,2、登录 3、忘记密码 4、旧手机号获取验证码、5新手机号", required = true), |
|||
}) |
|||
@ApiOperation(value = "获取验证码(1、注册,2、登录 3、忘记密码)") |
|||
@GetMapping("/sendMessageCode") |
|||
@ResponseBody |
|||
public ResultBean sendMessageCode(String mobile, String type) { |
|||
//1、注册,2、登录 3、忘记密码
|
|||
ResultBean rb = ResultBean.fireFail(); |
|||
//验证手机号是否正确
|
|||
if (StringUtils.isBlank(mobile) || !RegexUtil.isMobile(mobile)) { |
|||
return rb.setMsg("请输入正确的手机号"); |
|||
} |
|||
if ("2".equals(type) || "3".equals(type)) { |
|||
//查询该手机号是否已注册
|
|||
SysUser sysUser = sysUserService.selectByMobileAndType(mobile, 2); |
|||
if (null == sysUser) { |
|||
return rb.setMsg("该手机号还未注册或已被停用"); |
|||
} |
|||
} |
|||
if ("4".equals(type)) { |
|||
//将token解密userSid
|
|||
String token = request.getHeader("token"); |
|||
DecodedJWT decodedJWT = JWT.decode(token.substring(2, token.length())); |
|||
String userSid = JWTUtil.getUserSid(decodedJWT); |
|||
SysUser sysUser = sysUserService.fetchBySid(userSid); |
|||
if (sysUser == null) { |
|||
return rb.setMsg("请登录"); |
|||
} |
|||
if (!sysUser.getUserName().equals(mobile)) { |
|||
return rb.setMsg("手机号错误"); |
|||
} |
|||
} |
|||
//验证是否已注册
|
|||
if ("5".equals(type)) { |
|||
SysUser sysUser = sysUserService.selectByMobileAndType(mobile, 2); |
|||
if (sysUser != null) { |
|||
return rb.setMsg("该手机号已注册"); |
|||
} |
|||
} |
|||
String verificationCode = ""; |
|||
return sysUserService.getWxVerificationCode(mobile, verificationCode, type); |
|||
} |
|||
|
|||
/** |
|||
* @param wxSysUserLoginQuery 数据传输对象 |
|||
* @description: 免密码登录、账号密码登录 |
|||
* @return: |
|||
* @Author: dimengzhe |
|||
* @Date: 2021/10/9 9:15 |
|||
*/ |
|||
@ApiOperation(value = "登录") |
|||
@ResponseBody |
|||
@PostMapping("/login") |
|||
public ResultBean<WxSysUserVo> login(WxSysUserLoginQuery wxSysUserLoginQuery) { |
|||
WxSysUserVo wxSysUserVo = new WxSysUserVo(); |
|||
ResultBean rb = ResultBean.fireFail(); |
|||
String type = wxSysUserLoginQuery.getType();//手机登录类型:1、密码登录,2、验证码登录
|
|||
String verifyCode = wxSysUserLoginQuery.getVerifyCode();//验证码
|
|||
String password = wxSysUserLoginQuery.getPassword();//密码
|
|||
String userName = wxSysUserLoginQuery.getUserName(); |
|||
if ("1".equals(type)) {//密码登录
|
|||
if (StringUtils.isBlank(password)) { |
|||
return rb.setMsg("密码不能为空"); |
|||
} |
|||
} else if ("2".equals(type)) {//验证码登录
|
|||
if (StringUtils.isBlank(verifyCode)) { |
|||
return rb.setMsg("验证码不能为空"); |
|||
} |
|||
} |
|||
SysUser sysUser = sysUserService.selectByMobileAndType(userName, 2); |
|||
if (null == sysUser) { |
|||
return rb.setMsg("账号不存在或已被停用"); |
|||
} |
|||
if ("1".equals(type)) {//密码登录
|
|||
String md5 = Encodes.md5(password); |
|||
if (md5.equals(sysUser.getPassword())) {//匹配
|
|||
wxSysUserVo = sysUserService.selectByWxUserName(userName, 2); |
|||
} else { |
|||
return rb.setMsg("用户名或密码错误"); |
|||
} |
|||
} else if ("2".equals(type)) {//验证码登录
|
|||
String redisCode = redisUtil.get(DictCommonType.WX_LOGIN + userName); |
|||
if (StringUtils.isBlank(redisCode)) { |
|||
return rb.setMsg("短信验证码错误或已失效,请重新获取"); |
|||
} |
|||
if (verifyCode.equals(redisCode.substring(0, 4))) { |
|||
wxSysUserVo = sysUserService.selectByWxUserName(userName, 2); |
|||
redisUtil.remove(DictCommonType.WX_LOGIN + userName); |
|||
|
|||
} else { |
|||
return rb.setMsg("短信验证码错误或已失效,请重新获取"); |
|||
} |
|||
} |
|||
return new ResultBean<WxSysUserVo>().success().setData(wxSysUserVo); |
|||
} |
|||
|
|||
/** |
|||
* @param wxSysUserDto 数据传输对象 |
|||
* @description: 客户端注册 |
|||
* @return: |
|||
* @Author: dimengzhe |
|||
* @Date: 2021/10/9 9:21 |
|||
*/ |
|||
@ApiOperation(value = "注册") |
|||
@ResponseBody |
|||
@PostMapping("/registsUser") |
|||
public ResultBean registsUser(WxSysUserDto wxSysUserDto) { |
|||
ResultBean rb = ResultBean.fireFail(); |
|||
String mobile = wxSysUserDto.getMobile();//手机号
|
|||
String idNo = wxSysUserDto.getIdNo();//身份证号
|
|||
String verificationCode = wxSysUserDto.getVerificationCode();//验证码
|
|||
String name = wxSysUserDto.getName();//姓名
|
|||
String result = ""; |
|||
//判断身份证号是否正确
|
|||
try { |
|||
if (StringUtils.isNotBlank(RegexUtil.IDCardValidate(idNo))) { |
|||
return rb.setMsg(RegexUtil.IDCardValidate(idNo)); |
|||
} |
|||
} catch (ParseException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
//判断是否已注册
|
|||
int userType = 2;//用户类型
|
|||
SysUser sysUser = sysUserService.selectByMobileAndType(mobile, userType); |
|||
if (sysUser != null) { |
|||
return rb.setMsg("该手机号已注册"); |
|||
} |
|||
//验证码是否正确
|
|||
Map<String, Object> map = sysUserService.mobileValidateWxRegister(mobile, verificationCode); |
|||
String code = map.get("code").toString(); |
|||
boolean isSave = false; |
|||
if (Tools.CODE_SUCCESS.equals(code)) { |
|||
//需验证手机号+姓名+身份证号是否匹配
|
|||
WxSysUserRegistQuery wxSysUserRegistQuery = new WxSysUserRegistQuery(); |
|||
wxSysUserRegistQuery.setMobile(mobile); |
|||
wxSysUserRegistQuery.setName(name); |
|||
wxSysUserRegistQuery.setIdcard(idNo); |
|||
try { |
|||
ResultBean resultBean = isTr2(wxSysUserRegistQuery); |
|||
if (!resultBean.getSuccess()) { |
|||
return ResultBean.fireFail().setMsg(resultBean.getMsg()); |
|||
} |
|||
} catch (UnsupportedEncodingException e) { |
|||
e.printStackTrace(); |
|||
} catch (NoSuchAlgorithmException e) { |
|||
e.printStackTrace(); |
|||
} catch (InvalidKeyException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
SysUser sysUser1 = new SysUser(); |
|||
sysUser1.setUserName(mobile); |
|||
sysUser1.setMobile(mobile); |
|||
sysUser1.setUserType(userType); |
|||
String password = mobile.substring(5, 11); |
|||
String md5 = Encodes.md5(password); |
|||
sysUser1.setPassword(md5); |
|||
//查询客户表中身份证号是否存在,若存在直接更新客户信息。若不存在,则根据姓名和手机号查询,
|
|||
// 若存在,则更新身份证号,若不存在,则直接创建客户信息
|
|||
// WxCrmCustomerQuery wxCrmCustomerQuery = new WxCrmCustomerQuery();
|
|||
// wxCrmCustomerQuery.setMobile(mobile);
|
|||
// wxCrmCustomerQuery.setName(name);
|
|||
// wxCrmCustomerQuery.setIdNo(idNo);
|
|||
// //查询最大一级的组织机构
|
|||
// SysOrganization sysOrganization = sysOrganizationService.selectOrgCode();
|
|||
// if (StringUtils.isBlank(sysOrganization.getOrgCode())) {
|
|||
// wxCrmCustomerQuery.setOrgCode("" + 1);
|
|||
// wxCrmCustomerQuery.setOrgSid(sysOrganization.getSid());
|
|||
// } else {
|
|||
// wxCrmCustomerQuery.setOrgSid(sysOrganization.getSid());
|
|||
// wxCrmCustomerQuery.setOrgCode(sysOrganization.getOrgCode() + 1);
|
|||
// }
|
|||
//
|
|||
// ResultBean resultBean = crmCustomerFeign.wxSaveCustomer(wxCrmCustomerQuery);
|
|||
// sysUser1.setStaffSid(resultBean.getData().toString());
|
|||
sysUser1.setHeadImage("http://120.46.131.15:8111/upload/appImage/headImage.jpg"); |
|||
isSave = sysUserService.save(sysUser1); |
|||
String regiMsg = "客户端账号为:" + mobile + "的账号,已注册成功,登录密码为" + password; |
|||
result = MsgWs.SendWaitWorkMsg(mobile, regiMsg); |
|||
} else { |
|||
return ResultBean.fireFail().setMsg(map.get("details").toString()); |
|||
} |
|||
if (!isSave) { |
|||
return ResultBean.fireSuccess().setMsg("注册失败"); |
|||
} |
|||
if (!RESULT_CODE.equals(result)) { |
|||
return ResultBean.fireFail().setMsg("注册成功,未发送密码"); |
|||
} |
|||
return ResultBean.fireSuccess().setMsg("注册成功"); |
|||
} |
|||
|
|||
/** |
|||
* @param userSid 用户sid |
|||
* @description: 客户端获取我的信息 |
|||
* @return: 头像+保密后的姓名 |
|||
* @Author: dimengzhe |
|||
* @Date: 2021/10/6 12:19 |
|||
*/ |
|||
@ApiOperation(value = "我的信息") |
|||
@ResponseBody |
|||
@GetMapping("/myInfo/{userSid}") |
|||
public ResultBean<WxMySysUserInfoVo> selectMyInfo(String userSid) { |
|||
ResultBean rb = ResultBean.fireFail(); |
|||
SysUser sysUser = sysUserService.fetchBySid(userSid); |
|||
if (null == sysUser) { |
|||
return rb.setMsg("该用户不存在"); |
|||
} |
|||
WxMySysUserInfoVo infoVo = sysUserService.selectMyInfo(userSid); |
|||
// ResultBean<CrmCustomer> resultBean = crmCustomerFeign.selectBySid(sysUser.getStaffSid());
|
|||
// if (resultBean.getSuccess()) {
|
|||
// infoVo.setName(resultBean.getData().getName());
|
|||
// }
|
|||
if (StringUtils.isBlank(infoVo.getHeadImage())) { |
|||
//默认头像
|
|||
infoVo.setHeadImage("http://120.46.131.15:8111/upload/appImage/headImage.jpg"); |
|||
} else { |
|||
infoVo.setHeadImage(fileUploadComponent.getUrlPrefix() + infoVo.getHeadImage()); |
|||
} |
|||
return rb.success().setData(infoVo); |
|||
} |
|||
|
|||
@ApiOperation(value = "首页") |
|||
@ResponseBody |
|||
@GetMapping("/selectHomePage/{userSid}") |
|||
public ResultBean<WxHomePageVo> selectHomePage(String userSid) { |
|||
ResultBean rb = ResultBean.fireFail(); |
|||
SysUser sysUser = sysUserService.fetchBySid(userSid); |
|||
if (null == sysUser) { |
|||
return rb.setMsg("该用户不存在"); |
|||
} |
|||
boolean isHave = false; |
|||
WxHomePageVo wxHomePageVo = sysUserService.selectHomePage(userSid); |
|||
// ResultBean<CrmCustomer> resultBean = crmCustomerFeign.selectBySid(wxHomePageVo.getStaffSid());
|
|||
// if (resultBean.getSuccess()) {
|
|||
// wxHomePageVo.setIdTerm(resultBean.getData().getEndDate());
|
|||
// }
|
|||
if (StringUtils.isBlank(wxHomePageVo.getHeadImage())) { |
|||
//默认头像
|
|||
wxHomePageVo.setHeadImage("http://120.46.131.15:8111/upload/appImage/headImage.jpg"); |
|||
} else { |
|||
wxHomePageVo.setHeadImage(fileUploadComponent.getUrlPrefix() + wxHomePageVo.getHeadImage()); |
|||
} |
|||
//2018.08.24-2038.08.24
|
|||
/*String idTerm = wxHomePageVo.getIdTerm(); |
|||
List<String> split = Arrays.asList(idTerm.split("-")); |
|||
if (split.size() == 2) { |
|||
String idTermEnd = split.get(1); |
|||
String date = idTermEnd.replace('.', '-'); |
|||
int days = days(date); |
|||
if (days == 30 || days == 60) { |
|||
isHave = true; |
|||
wxHomePageVo.setMessage("您的身份证将在" + days + "天后到期,请及时更新信息"); |
|||
} |
|||
}*/ |
|||
wxHomePageVo.setIsHave(isHave); |
|||
return new ResultBean<WxHomePageVo>().success().setData(wxHomePageVo); |
|||
} |
|||
|
|||
@ApiOperation(value = "客户端验证验证码是否正确") |
|||
@ResponseBody |
|||
@PostMapping("/checkResetPwdCode") |
|||
@ApiImplicitParams({ |
|||
@ApiImplicitParam(name = "code", value = "验证码", required = true), |
|||
@ApiImplicitParam(name = "mobile", value = "手机号", required = true), |
|||
@ApiImplicitParam(name = "type", value = "类型:1、找回密码 2、验证旧手机号的验证码、3、验证新手机好的验证码", required = true) |
|||
}) |
|||
public ResultBean checkResetPwdCode(String code, String mobile, String type) { |
|||
ResultBean rb = ResultBean.fireFail(); |
|||
String key = ""; |
|||
if (!"1".equals(type) && !"2".equals(type) && !"3".equals(type)) { |
|||
return rb.setMsg("类型错误"); |
|||
} |
|||
if ("1".equals(type)) { |
|||
key = DictCommonType.WX_FORGET + mobile; |
|||
} else if ("2".equals(type)) { |
|||
key = DictCommonType.WX_UPDATE + mobile; |
|||
} else if ("3".equals(type)) { |
|||
key = DictCommonType.WX_NEW + mobile; |
|||
} |
|||
String keyOne = redisUtil.get(key); |
|||
String redisCode = redisUtil.get(key).substring(0, 4); |
|||
// 判断验证码是否失效
|
|||
if (StringUtils.isEmpty(redisCode)) { |
|||
return rb.setMsg("验证码失效"); |
|||
} else if (!"".equals(redisCode) && !code.equals(redisCode)) { |
|||
return rb.setMsg("验证码错误"); |
|||
} |
|||
return rb.success(); |
|||
} |
|||
|
|||
/** |
|||
* @param mobile 手机号 |
|||
* @param newPwd 新密码 |
|||
* @description: 客户端找回密码 |
|||
* @return: |
|||
* @Author: dimengzhe |
|||
* @Date: 2021/10/9 9:21 |
|||
*/ |
|||
@ApiOperation(value = "客户端找回密码") |
|||
@ResponseBody |
|||
@PostMapping("/resetPwd") |
|||
public ResultBean resetPwdWx(String mobile, String newPwd) { |
|||
if (StringUtils.isBlank(mobile) || !RegexUtil.isMobile(mobile)) { |
|||
return new ResultBean().fail().setMsg("请输入正确的手机号"); |
|||
} |
|||
if (StringUtils.isBlank(newPwd)) { |
|||
return new ResultBean().fail().setMsg("请输入密码"); |
|||
} |
|||
return sysUserService.resetPwdWx(mobile, newPwd); |
|||
} |
|||
|
|||
@ApiOperation(value = "根据用户sid查询用户信息") |
|||
@ResponseBody |
|||
@PostMapping("/selectBySid") |
|||
public ResultBean<SysUser> selectBySid(String sid) { |
|||
SysUser sysUser = sysUserService.fetchBySid(sid); |
|||
if (sysUser == null) { |
|||
sysUser = new SysUser(); |
|||
return new ResultBean<SysUser>().fail().setData(sysUser).setMsg("用户不存在"); |
|||
} |
|||
return new ResultBean<SysUser>().success().setData(sysUser); |
|||
} |
|||
|
|||
/** |
|||
* @param end 结束日期 |
|||
* @description: 计算两个日期相差多少天 |
|||
* @return: |
|||
* @Author: dimengzhe |
|||
* @Date: 2021/10/12 14:36 |
|||
*/ |
|||
private int days(String end) { |
|||
int i = 0; |
|||
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd"); |
|||
try { |
|||
Date star = new Date();//开始时间
|
|||
Date endDay = dft.parse(end);//结束时间
|
|||
Date nextDay = star; |
|||
while (nextDay.before(endDay)) {//当明天不在结束时间之前是终止循环
|
|||
Calendar cld = Calendar.getInstance(); |
|||
cld.setTime(star); |
|||
cld.add(Calendar.DATE, 1); |
|||
star = cld.getTime(); |
|||
//获得下一天日期字符串
|
|||
nextDay = star; |
|||
i++; |
|||
} |
|||
System.out.println("相差天数为:" + i); |
|||
} catch (ParseException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return i; |
|||
} |
|||
|
|||
public ResultBean isTr2(WxSysUserRegistQuery wxSysUserRegistQuery) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { |
|||
String result = ""; |
|||
String msg = ""; |
|||
//云市场分配的密钥Id
|
|||
String secretId = "AKID7rl35uAlxzadzo1I299iEiYnMH111I0beWu"; |
|||
//云市场分配的密钥Key
|
|||
String secretKey = "atdsoC8Q2fjuhf31agmub4g8fzh93n8V9yoXQJtk"; |
|||
String source = "market"; |
|||
|
|||
Calendar cd = Calendar.getInstance(); |
|||
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US); |
|||
sdf.setTimeZone(TimeZone.getTimeZone("GMT")); |
|||
String datetime = sdf.format(cd.getTime()); |
|||
// 签名
|
|||
String auth = calcAuthorization(source, secretId, secretKey, datetime); |
|||
// 请求方法
|
|||
String method = "GET"; |
|||
// 请求头
|
|||
Map<String, String> headers = new HashMap<String, String>(); |
|||
headers.put("X-Source", source); |
|||
headers.put("X-Date", datetime); |
|||
headers.put("Authorization", auth); |
|||
|
|||
// 查询参数
|
|||
Map<String, String> queryParams = new HashMap<String, String>(); |
|||
queryParams.put("idCard", wxSysUserRegistQuery.getIdcard()); |
|||
queryParams.put("mobile", wxSysUserRegistQuery.getMobile()); |
|||
queryParams.put("realName", wxSysUserRegistQuery.getName()); |
|||
// body参数
|
|||
Map<String, String> bodyParams = new HashMap<String, String>(); |
|||
|
|||
// url参数拼接
|
|||
String url = "https://service-08ijl3bf-1301232119.bj.apigw.tencentcs.com/release/phonecheck"; |
|||
if (!queryParams.isEmpty()) { |
|||
url += "?" + urlencode(queryParams); |
|||
} |
|||
|
|||
BufferedReader in = null; |
|||
try { |
|||
URL realUrl = new URL(url); |
|||
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); |
|||
conn.setConnectTimeout(5000); |
|||
conn.setReadTimeout(5000); |
|||
conn.setRequestMethod(method); |
|||
|
|||
// request headers
|
|||
for (Map.Entry<String, String> entry : headers.entrySet()) { |
|||
conn.setRequestProperty(entry.getKey(), entry.getValue()); |
|||
} |
|||
|
|||
// request body
|
|||
Map<String, Boolean> methods = new HashMap<>(); |
|||
methods.put("POST", true); |
|||
methods.put("PUT", true); |
|||
methods.put("PATCH", true); |
|||
Boolean hasBody = methods.get(method); |
|||
if (hasBody != null) { |
|||
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
|||
|
|||
conn.setDoOutput(true); |
|||
DataOutputStream out = new DataOutputStream(conn.getOutputStream()); |
|||
out.writeBytes(urlencode(bodyParams)); |
|||
out.flush(); |
|||
out.close(); |
|||
} |
|||
|
|||
// 定义 BufferedReader输入流来读取URL的响应
|
|||
in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
|||
String line; |
|||
while ((line = in.readLine()) != null) { |
|||
result += line; |
|||
} |
|||
|
|||
System.out.println(result); |
|||
} catch (Exception e) { |
|||
System.out.println(e); |
|||
e.printStackTrace(); |
|||
} finally { |
|||
try { |
|||
if (in != null) { |
|||
in.close(); |
|||
} |
|||
} catch (Exception e2) { |
|||
e2.printStackTrace(); |
|||
} |
|||
} |
|||
//判断result
|
|||
JSONObject object = JSONObject.parseObject(result); |
|||
Map<String, Object> map = JSONObject.parseObject(object.toJSONString(), new TypeReference<Map<String, Object>>() { |
|||
}); |
|||
int code = ConstantUtils.getInteger(map, "error_code"); |
|||
if (code == 0) { |
|||
Map<String, Object> mapo = ConstantUtils.getMap(map, "result"); |
|||
String VerificationResult = mapo.get("VerificationResult").toString(); |
|||
if ("1".equals(VerificationResult)) { |
|||
msg = "信息匹配"; |
|||
} else if ("-1".equals(VerificationResult)) { |
|||
return ResultBean.fireFail().setMsg("信息不匹配"); |
|||
} else if ("0".equals(VerificationResult)) { |
|||
return ResultBean.fireFail().setMsg("运营商系统中无记录"); |
|||
} |
|||
} else { |
|||
return ResultBean.fireFail().setMsg(ConstantUtils.getString(map, "reason", "错误")); |
|||
} |
|||
return ResultBean.fireSuccess().setMsg(msg); |
|||
|
|||
} |
|||
|
|||
public static String calcAuthorization(String source, String secretId, String secretKey, String datetime) |
|||
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { |
|||
String signStr = "x-date: " + datetime + "\n" + "x-source: " + source; |
|||
Mac mac = Mac.getInstance("HmacSHA1"); |
|||
Key sKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), mac.getAlgorithm()); |
|||
mac.init(sKey); |
|||
byte[] hash = mac.doFinal(signStr.getBytes("UTF-8")); |
|||
String sig = Base64.encode(hash); |
|||
|
|||
String auth = "hmac id=\"" + secretId + "\", algorithm=\"hmac-sha1\", headers=\"x-date x-source\", signature=\"" + sig + "\""; |
|||
return auth; |
|||
} |
|||
|
|||
public static String urlencode(Map<?, ?> map) throws UnsupportedEncodingException { |
|||
StringBuilder sb = new StringBuilder(); |
|||
for (Map.Entry<?, ?> entry : map.entrySet()) { |
|||
if (sb.length() > 0) { |
|||
sb.append("&"); |
|||
} |
|||
sb.append(String.format("%s=%s", |
|||
URLEncoder.encode(entry.getKey().toString(), "UTF-8"), |
|||
URLEncoder.encode(entry.getValue().toString(), "UTF-8") |
|||
)); |
|||
} |
|||
return sb.toString(); |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue