|
|
@@ -0,0 +1,349 @@
|
|
|
+package com.jpsoft.enterprise.modules.mobile.controller;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.jpsoft.enterprise.modules.base.entity.PersonInfo;
|
|
|
+import com.jpsoft.enterprise.modules.base.service.PersonInfoService;
|
|
|
+import com.jpsoft.enterprise.modules.common.dto.MessageResult;
|
|
|
+import com.jpsoft.enterprise.modules.common.utils.AESUtil;
|
|
|
+import com.jpsoft.enterprise.modules.common.utils.JwtUtil;
|
|
|
+import com.jpsoft.enterprise.modules.common.utils.SMSUtil;
|
|
|
+import io.swagger.annotations.ApiImplicitParam;
|
|
|
+import io.swagger.annotations.ApiImplicitParams;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import sun.security.provider.MD5;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author 墨鱼_mo
|
|
|
+ * @date 2020-12-31 9:08
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/mobile/personInfoApi")
|
|
|
+@Slf4j
|
|
|
+public class PersonInfoApiController {
|
|
|
+
|
|
|
+ @Value("${jwt.secret}")
|
|
|
+ private String jwtSecret;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PersonInfoService personInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ValueOperations<String, Object> valueOperations;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("findByPhone")
|
|
|
+ @ApiOperation(value = "通过手机号查询用户(公开接口)")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "phone", value = "电话号码", required = true, paramType = "form")
|
|
|
+ })
|
|
|
+ public MessageResult<String> findByPhone(String phone) {
|
|
|
+ MessageResult<String> messageResult = new MessageResult<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ String id = "";
|
|
|
+ if (StringUtils.isNotBlank(phone)) {
|
|
|
+ PersonInfo personInfo = personInfoService.findByPhone(phone);
|
|
|
+
|
|
|
+ if (personInfo == null) {
|
|
|
+ throw new Exception("该手机号未注册");
|
|
|
+ }
|
|
|
+ id = personInfo.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ messageResult.setData(id);
|
|
|
+ messageResult.setResult(true);
|
|
|
+ messageResult.setCode(200);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ messageResult.setCode(400);
|
|
|
+ messageResult.setResult(false);
|
|
|
+ messageResult.setMessage(ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return messageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("getVerifyCode")
|
|
|
+ @ApiOperation(value = "获取短信验证码(公开接口)")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "form")
|
|
|
+ })
|
|
|
+ public MessageResult<String> getVerifyCode(String phone) {
|
|
|
+ MessageResult<String> messageResult = new MessageResult<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ PersonInfo personInfo = personInfoService.findByPhone(phone);
|
|
|
+
|
|
|
+ if (personInfo == null) {
|
|
|
+ throw new Exception("用户不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ String key = "SMS_" + personInfo.getId();
|
|
|
+
|
|
|
+ String verifyCode = (String) valueOperations.get(key);
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(verifyCode)) {
|
|
|
+ verifyCode = SMSUtil.generateNumberString(6);
|
|
|
+ JSONObject verifyCodeJSON = new JSONObject();
|
|
|
+ verifyCodeJSON.put("code", verifyCode);
|
|
|
+
|
|
|
+ // messageResult = SMSUtil.send(merchantInfo.getContactPhone(), "SMS_49390047", verifyCodeJSON.toString());
|
|
|
+ messageResult = SMSUtil.send(personInfo.getPhone(), "校信达", "SMS_49390047", verifyCodeJSON);
|
|
|
+
|
|
|
+ //设置当前用户的验证码,5分钟内有效
|
|
|
+ valueOperations.set(key, verifyCode, 5, TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+
|
|
|
+ messageResult.setData(personInfo.getId());
|
|
|
+ messageResult.setResult(true);
|
|
|
+ messageResult.setCode(200);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ messageResult.setCode(400);
|
|
|
+ messageResult.setResult(false);
|
|
|
+ messageResult.setMessage(ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return messageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @ApiOperation(value = "密码登录")
|
|
|
+ @RequestMapping(value = "loginByPassword", method = RequestMethod.POST)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "userName", value = "用户名", required = true, paramType = "form"),
|
|
|
+ @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "form"),
|
|
|
+ @ApiImplicitParam(name = "openId", value = "微信ID", required = true, paramType = "query")
|
|
|
+ })
|
|
|
+ public MessageResult<Map> loginByPassword(String userName,String password,String openId) {
|
|
|
+ MessageResult<Map> messageResult = new MessageResult<>();
|
|
|
+
|
|
|
+ try{
|
|
|
+ PersonInfo personInfo = personInfoService.findByUserName(userName);
|
|
|
+ if (personInfo == null){
|
|
|
+ throw new Exception("用户名或密码错误");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(personInfo.getPassword())){
|
|
|
+ throw new Exception("该账户未设置密码,请使用验证码登录");
|
|
|
+ }
|
|
|
+ if (!AESUtil.encrypt(password, AESUtil.MYSQL_ENC_KEY).equals(personInfo.getPassword())){
|
|
|
+ throw new Exception("用户名或密码错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ personInfo.setOpenId(openId);
|
|
|
+ personInfo.setUpdateTime(new Date());
|
|
|
+ personInfoService.update(personInfo);
|
|
|
+
|
|
|
+
|
|
|
+ String token = JwtUtil.createToken(jwtSecret, personInfo.getId() + "", DateUtil.offsetHour(new Date(),6));
|
|
|
+
|
|
|
+ Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
+ dataMap.put("personInfo", personInfo);
|
|
|
+ dataMap.put("token", token);
|
|
|
+
|
|
|
+ messageResult.setData(dataMap);
|
|
|
+ messageResult.setResult(true);
|
|
|
+ messageResult.setCode(200);
|
|
|
+ }catch (Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ messageResult.setCode(400);
|
|
|
+ messageResult.setResult(false);
|
|
|
+ messageResult.setMessage(ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return messageResult;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("validateCode")
|
|
|
+ @ApiOperation(value = "验证短信验证码(公开接口)")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "phone", value = "phone", required = true, paramType = "query"),
|
|
|
+ @ApiImplicitParam(name = "openId", value = "微信ID", required = true, paramType = "query"),
|
|
|
+ @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query")
|
|
|
+ })
|
|
|
+ public MessageResult<Map> validateCode(String phone, String openId, String code) {
|
|
|
+
|
|
|
+ MessageResult<Map> messageResult = new MessageResult<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ PersonInfo personInfo = personInfoService.findByPhone(phone);
|
|
|
+ if (personInfo == null){
|
|
|
+ throw new Exception("用户不存在");
|
|
|
+ }
|
|
|
+ String smsKey = "SMS_" + personInfo.getId();
|
|
|
+
|
|
|
+ String beforeVerifyCode = (String) valueOperations.get(smsKey);
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(beforeVerifyCode)) {
|
|
|
+ throw new Exception("验证码已过期!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!beforeVerifyCode.equals(code)) {
|
|
|
+ throw new Exception("验证码错误!");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(openId)) {
|
|
|
+
|
|
|
+ personInfo.setOpenId(openId);
|
|
|
+ personInfo.setUpdateTime(new Date());
|
|
|
+ personInfoService.update(personInfo);
|
|
|
+
|
|
|
+
|
|
|
+ String token = JwtUtil.createToken(jwtSecret, personInfo.getId() + "", DateUtil.offsetHour(new Date(),6));
|
|
|
+
|
|
|
+ dataMap.put("personInfo", personInfo);
|
|
|
+ dataMap.put("token", token);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ messageResult.setData(dataMap);
|
|
|
+ messageResult.setResult(true);
|
|
|
+ messageResult.setCode(200);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ messageResult.setCode(400);
|
|
|
+ messageResult.setResult(false);
|
|
|
+ messageResult.setMessage(ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return messageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("findByOpenId")
|
|
|
+ @ApiOperation(value = "获取个人信息(公开接口)")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "openId", value = "微信ID", required = true, paramType = "query")
|
|
|
+ })
|
|
|
+ public MessageResult<Map> findByOpenId(String openId) {
|
|
|
+
|
|
|
+ MessageResult<Map> messageResult = new MessageResult<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
+ PersonInfo personInfo = personInfoService.findByOpenId(openId);
|
|
|
+ if (personInfo != null){
|
|
|
+ String token = JwtUtil.createToken(jwtSecret, personInfo.getId() + "", DateUtil.offsetHour(new Date(),6));
|
|
|
+ dataMap.put("personInfo", personInfo);
|
|
|
+ dataMap.put("token", token);
|
|
|
+ }
|
|
|
+
|
|
|
+ messageResult.setData(dataMap);
|
|
|
+ messageResult.setResult(true);
|
|
|
+ messageResult.setCode(200);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ messageResult.setCode(400);
|
|
|
+ messageResult.setResult(false);
|
|
|
+ messageResult.setMessage(ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return messageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("resetPassword ")
|
|
|
+ @ApiOperation(value = "重置密码")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "token", value = "令牌", required = true, paramType = "form"),
|
|
|
+ @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
|
|
|
+ @ApiImplicitParam(name = "firstPassword", value = "首次密码", required = true, paramType = "form"),
|
|
|
+ @ApiImplicitParam(name = "secondPassword", value = "二次密码", required = true, paramType = "form")
|
|
|
+ })
|
|
|
+ public MessageResult<Map> resetPassword(@RequestAttribute String subject, String token, String firstPassword,String secondPassword) {
|
|
|
+
|
|
|
+ MessageResult<Map> messageResult = new MessageResult<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
+ PersonInfo personInfo = personInfoService.get(subject);
|
|
|
+ if (!firstPassword.equals(secondPassword)){
|
|
|
+ throw new Exception("两次密码不一致");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (personInfo != null){
|
|
|
+ personInfo.setPassword(AESUtil.encrypt(secondPassword,AESUtil.MYSQL_ENC_KEY));
|
|
|
+ personInfo.setUpdateTime(new Date());
|
|
|
+ personInfoService.update(personInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ messageResult.setData(dataMap);
|
|
|
+ messageResult.setResult(true);
|
|
|
+ messageResult.setCode(200);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ messageResult.setCode(400);
|
|
|
+ messageResult.setResult(false);
|
|
|
+ messageResult.setMessage(ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return messageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("logOut")
|
|
|
+ @ApiOperation(value = "用户登出")
|
|
|
+ public MessageResult<Map> logOut(String token, @RequestAttribute String subject) {
|
|
|
+ MessageResult<Map> messageResult = new MessageResult<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ PersonInfo personInfo = personInfoService.get(subject);
|
|
|
+
|
|
|
+ if (personInfo == null) {
|
|
|
+ messageResult.setResult(true);
|
|
|
+ messageResult.setCode(200);
|
|
|
+ return messageResult;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
+
|
|
|
+
|
|
|
+ personInfo.setOpenId(" ");
|
|
|
+ personInfoService.update(personInfo);
|
|
|
+
|
|
|
+ messageResult.setData(dataMap);
|
|
|
+ messageResult.setResult(true);
|
|
|
+ messageResult.setCode(200);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error(ex.getMessage(), ex);
|
|
|
+ messageResult.setCode(400);
|
|
|
+ messageResult.setResult(false);
|
|
|
+ messageResult.setMessage(ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return messageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|