|
|
|
@ -11,7 +11,6 @@ import com.daqing.framework.domain.hrms.UserEntity; |
|
|
|
|
import com.daqing.framework.domain.hrms.ext.EmployeeAndUserVO; |
|
|
|
|
import com.daqing.framework.domain.hrms.response.HrmsCode; |
|
|
|
|
import com.daqing.framework.exception.ExceptionCast; |
|
|
|
|
import com.daqing.framework.model.response.CommonCode; |
|
|
|
|
import com.daqing.framework.model.response.PromptSuccess; |
|
|
|
|
import com.daqing.framework.util.HttpUtils; |
|
|
|
|
import com.daqing.framework.util.Md5Util; |
|
|
|
@ -22,13 +21,18 @@ import org.springframework.beans.BeanUtils; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder; |
|
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes; |
|
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
|
import java.io.*; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@ -41,28 +45,25 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements |
|
|
|
|
@Autowired |
|
|
|
|
private HrmsAuthFeignService hrmsAuthFeignService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 当前登录的用户信息 |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public EmployeeAndUserVO info() { |
|
|
|
|
/*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
|
|
|
|
String token = request.getHeader("token"); |
|
|
|
|
String userId = RedisUtil.get("dq:token:"+token);*/ |
|
|
|
|
//TODO:测试环境模拟从redis中拿取用户id
|
|
|
|
|
String userId = "1"; |
|
|
|
|
String userId = this.getUserId(); |
|
|
|
|
EmployeeAndUserVO employeeAndUser = employeeDao.getEmployeeByUserId(Long.parseLong(userId)); |
|
|
|
|
if (employeeAndUser.getWechatId() == null || employeeAndUser.getWechatId().length() == 0){ |
|
|
|
|
JSONObject responseResult = hrmsAuthFeignService.getWeChatConfig(); |
|
|
|
|
JSONObject weChatConfig = responseResult.getJSONObject("data"); |
|
|
|
|
if (weChatConfig == null){ |
|
|
|
|
employeeAndUser.setWechatId("二维码获取失败,请稍后重试"); |
|
|
|
|
employeeAndUser.setWechatId(PromptSuccess.QR_CODE_FAIL); |
|
|
|
|
}else { |
|
|
|
|
employeeAndUser.setWechatId(String.format(weChatConfig.get("OPEN_QRCODE_URL").toString(),weChatConfig.get("openAppid"), |
|
|
|
|
weChatConfig.get("openRedirectUrl"),weChatConfig.get("openAppsecret"))); |
|
|
|
|
employeeAndUser.setWechatId(String.format(weChatConfig.get("open_QRCODE_URL").toString(),weChatConfig.get("openAppid"), |
|
|
|
|
weChatConfig.get("openRedirectUrl"),1)); // state=1作为回调时分辨绑定和登陆的标识
|
|
|
|
|
} |
|
|
|
|
}else { |
|
|
|
|
employeeAndUser.setWechatId("已绑定,微信扫码即可登录"); |
|
|
|
|
employeeAndUser.setWechatId(PromptSuccess.QR_CODE_BINDING_SUCCESS); |
|
|
|
|
} |
|
|
|
|
return employeeAndUser; |
|
|
|
|
} |
|
|
|
@ -74,59 +75,11 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements |
|
|
|
|
@Override |
|
|
|
|
public Boolean update(EmployeeAndUserVO employeeAndUserVO) { |
|
|
|
|
EmployeeEntity employee = new EmployeeEntity(); |
|
|
|
|
String userId = this.getUserId(); |
|
|
|
|
Long employeeId = this.getBaseMapper().getEmployeeIdByUserId(Long.parseLong(userId)); |
|
|
|
|
employeeAndUserVO.setId(employeeId); |
|
|
|
|
employeeAndUserVO.setUserId(Long.parseLong(userId)); |
|
|
|
|
Boolean result = true; |
|
|
|
|
if (employeeAndUserVO.getFile() != null && !employeeAndUserVO.getFile().isEmpty()){ |
|
|
|
|
//TODO:上传图像文件到oss服务器,返回一个路径地址
|
|
|
|
|
try { |
|
|
|
|
// 通过获取图片文件的宽度和高度来校验是否为图片文件
|
|
|
|
|
BufferedImage image = ImageIO.read(employeeAndUserVO.getFile().getInputStream()); |
|
|
|
|
if (image == null || image.getHeight() <= 0 || image.getWidth() <= 0){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_FILE_TYPE_MISTAKE); |
|
|
|
|
} |
|
|
|
|
}catch (IOException e){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_FILE_UPLOAD_FAIL); |
|
|
|
|
} |
|
|
|
|
OutputStream os = null; |
|
|
|
|
InputStream inputStream = null; |
|
|
|
|
String fileName = null; |
|
|
|
|
try { |
|
|
|
|
inputStream = employeeAndUserVO.getFile().getInputStream(); |
|
|
|
|
fileName = employeeAndUserVO.getFile().getOriginalFilename(); |
|
|
|
|
log.info("fileName="+fileName); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
String path = "D:\\image"; |
|
|
|
|
// 2、保存到临时文件
|
|
|
|
|
// 1K的数据缓冲
|
|
|
|
|
byte[] bs = new byte[1024]; |
|
|
|
|
// 读取到的数据长度
|
|
|
|
|
int len; |
|
|
|
|
// 输出的文件流保存到本地文件
|
|
|
|
|
File tempFile = new File(path); |
|
|
|
|
if (!tempFile.exists()) { |
|
|
|
|
tempFile.mkdirs(); |
|
|
|
|
} |
|
|
|
|
os = new FileOutputStream(tempFile.getPath() + File.separator + "MyFileName.png"); |
|
|
|
|
// 开始读取
|
|
|
|
|
while ((len = inputStream.read(bs)) != -1) { |
|
|
|
|
os.write(bs, 0, len); |
|
|
|
|
} |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_FILE_UPLOAD_FAIL); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} finally { |
|
|
|
|
// 完毕,关闭所有链接
|
|
|
|
|
try { |
|
|
|
|
os.close(); |
|
|
|
|
inputStream.close(); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
BeanUtils.copyProperties(employeeAndUserVO,employee); |
|
|
|
|
String phoneAccount = this.getBaseMapper().getPhoneAccount(employee.getUserId()); |
|
|
|
|
if ((phoneAccount == null || phoneAccount.length() == 0) |
|
|
|
@ -172,15 +125,44 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements |
|
|
|
|
return result && employeeDao.updateEmployeeAndUser(employee); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 图像文件上传 |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public String uploadFile(MultipartFile file) { |
|
|
|
|
if (file == null || file.isEmpty()){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_FILE_IS_NULL); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
// 通过获取图片文件的宽度和高度来校验是否为图片文件
|
|
|
|
|
BufferedImage image = ImageIO.read(file.getInputStream()); |
|
|
|
|
if (image == null || image.getHeight() <= 0 || image.getWidth() <= 0){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_FILE_TYPE_MISTAKE); |
|
|
|
|
} |
|
|
|
|
}catch (IOException e){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_FILE_UPLOAD_FAIL); |
|
|
|
|
} |
|
|
|
|
String filename = file.getOriginalFilename(); |
|
|
|
|
if (filename == null){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_FILE_TYPE_MISTAKE); |
|
|
|
|
} |
|
|
|
|
String substring = filename.substring(filename.lastIndexOf(".")); |
|
|
|
|
String filePrefix = UUID.randomUUID().toString(); |
|
|
|
|
String filePath = PromptSuccess.UPLOAD_FILE_PATH+filePrefix+substring; |
|
|
|
|
try { |
|
|
|
|
file.transferTo(new File(filePath)); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_FILE_UPLOAD_FAIL); |
|
|
|
|
} |
|
|
|
|
return filePath; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 修改密码 |
|
|
|
|
*/ |
|
|
|
|
@Transactional |
|
|
|
|
@Override |
|
|
|
|
public Boolean updatePassword(Long id, String primaryPassword, String newPassword, String affirmNewPassword) { |
|
|
|
|
if (id == null){ |
|
|
|
|
ExceptionCast.cast(CommonCode.INVALID_PARAM); |
|
|
|
|
} |
|
|
|
|
public Boolean updatePassword(String primaryPassword, String newPassword, String affirmNewPassword) { |
|
|
|
|
if (primaryPassword == null || primaryPassword.length() == 0 |
|
|
|
|
|| newPassword == null || newPassword.length() == 0 |
|
|
|
|
|| affirmNewPassword == null || affirmNewPassword.length() == 0){ |
|
|
|
@ -190,7 +172,8 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements |
|
|
|
|
if (!Objects.equals(affirmNewPassword, newPassword)){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_PASSWORD_NO_CONSISTENT); |
|
|
|
|
} |
|
|
|
|
if (!Objects.equals(Md5Util.md5(primaryPassword + id),this.getBaseMapper().getPasswordById(id))){ |
|
|
|
|
String userId = this.getUserId(); |
|
|
|
|
if (!Objects.equals(Md5Util.md5(primaryPassword + Long.parseLong(userId)),this.getBaseMapper().getPasswordById(Long.parseLong(userId)))){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_PASSWORD_MISTAKE); |
|
|
|
|
} |
|
|
|
|
// 正则校验
|
|
|
|
@ -198,10 +181,10 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_PASSWORD_MATCHES); |
|
|
|
|
} |
|
|
|
|
// 是否与账号相同
|
|
|
|
|
if (Objects.equals(newPassword,this.getBaseMapper().getAccountById(id))){ |
|
|
|
|
if (Objects.equals(newPassword,this.getBaseMapper().getAccountById(Long.parseLong(userId)))){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_PASSWORD_IS_NO_ACCOUNT); |
|
|
|
|
} |
|
|
|
|
return this.getBaseMapper().updatePassword(id,Md5Util.md5(newPassword + id)); |
|
|
|
|
return this.getBaseMapper().updatePassword(Long.parseLong(userId),Md5Util.md5(newPassword + Long.parseLong(userId))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -227,10 +210,7 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements |
|
|
|
|
*/ |
|
|
|
|
@Transactional |
|
|
|
|
@Override |
|
|
|
|
public Boolean bindingPhoneAccount(Long id,String phone, String code) { |
|
|
|
|
if (id == null){ |
|
|
|
|
ExceptionCast.cast(CommonCode.INVALID_PARAM); |
|
|
|
|
} |
|
|
|
|
public Boolean bindingPhoneAccount(String phone, String code) { |
|
|
|
|
if (phone == null || phone.length() == 0 || code == null || code.length() == 0){ |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_PHONE_OR_CODE_NOT_NULL); |
|
|
|
|
} |
|
|
|
@ -239,43 +219,25 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements |
|
|
|
|
ExceptionCast.cast(HrmsCode.USER_BINDING_FAIL); |
|
|
|
|
} |
|
|
|
|
RedisUtil.del(phone); |
|
|
|
|
return this.getBaseMapper().updatePhoneAccount(id, phone); |
|
|
|
|
String userId = this.getUserId(); |
|
|
|
|
return this.getBaseMapper().updatePhoneAccount(Long.parseLong(userId), phone); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 绑定微信回调接收接口 |
|
|
|
|
* @param code 微信端返回的code,用于申请openid |
|
|
|
|
* @return |
|
|
|
|
* 取消微信绑定 |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public Boolean weChatCallback(String code, HttpServletResponse response) { |
|
|
|
|
log.info("微信回调成功---------------------------------------"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject responseResult = hrmsAuthFeignService.getWeChatConfig(); |
|
|
|
|
JSONObject weChatConfig = responseResult.getJSONObject("data"); |
|
|
|
|
String accessTokenUrl = String.format(weChatConfig.get("OPEN_ACCESS_TOKEN_URL").toString(), weChatConfig.get("openAppid"), weChatConfig.get("openAppsecret"), code); |
|
|
|
|
|
|
|
|
|
//获取access_token
|
|
|
|
|
Map<String ,Object> baseMap = HttpUtils.doGet(accessTokenUrl); |
|
|
|
|
public Boolean cancel() { |
|
|
|
|
String userId = this.getUserId(); |
|
|
|
|
return this.getBaseMapper().updateWeChat(Long.parseLong(userId),null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(baseMap == null || baseMap.isEmpty()){ return false; } |
|
|
|
|
String openId = (String) baseMap.get("openid"); |
|
|
|
|
/*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
|
|
|
|
/** |
|
|
|
|
* 获取当前登录用户信息 |
|
|
|
|
*/ |
|
|
|
|
private String getUserId(){ |
|
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
|
|
|
|
String token = request.getHeader("token"); |
|
|
|
|
String userId = RedisUtil.get("dq:token:"+token);*/ |
|
|
|
|
//TODO:测试环境模拟从redis中拿取用户id
|
|
|
|
|
String userId = "1"; |
|
|
|
|
Boolean result = this.getBaseMapper().updateWeChat(Long.parseLong(userId), openId); |
|
|
|
|
log.info("绑定成功---------------------------------------"); |
|
|
|
|
if (result){ |
|
|
|
|
try { |
|
|
|
|
log.info("转发成功---------------------------------------"); |
|
|
|
|
response.sendRedirect("https://www.baidu.com?openId="+openId); |
|
|
|
|
}catch (IOException e){ |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
return RedisUtil.get("dq:token:"+token); |
|
|
|
|
} |
|
|
|
|
} |