diff --git a/hai-cweb/src/main/java/com/cweb/config/AuthConfig.java b/hai-cweb/src/main/java/com/cweb/config/AuthConfig.java index 04c3b685..3913d329 100644 --- a/hai-cweb/src/main/java/com/cweb/config/AuthConfig.java +++ b/hai-cweb/src/main/java/com/cweb/config/AuthConfig.java @@ -103,6 +103,7 @@ public class AuthConfig implements WebMvcConfigurer { .excludePathPatterns("/highMerchantStore/getMerchantList") .excludePathPatterns("/highMerchantStore/getStoreListByMerchant") .excludePathPatterns("/sms/sendSmsCode") + .excludePathPatterns("/sms/getSmsCode") .excludePathPatterns("/wechat/*") ; } diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java b/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java index 1e5b1530..0095c2bd 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java @@ -1,7 +1,12 @@ package com.cweb.controller; +import com.hai.common.exception.ErrorCode; +import com.hai.common.exception.ErrorHelp; +import com.hai.common.exception.SysCode; import com.hai.common.security.SessionObject; import com.hai.common.security.UserCenter; +import com.hai.common.security.VerifyCode; +import com.hai.common.security.VerifyCodeStorage; import com.hai.common.utils.ResponseMsgUtil; import com.hai.entity.HighOrder; import com.hai.entity.HighUser; @@ -19,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; @@ -72,4 +78,40 @@ public class HighUserController { } } + + @RequestMapping(value = "/updateUserPhone", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "修改用户手机号") + public ResponseData updateUserPhone(@RequestParam(value = "phone", required = true) String phone, + @RequestParam(value = "code", required = true) String code, + HttpServletRequest request) { + try { + + VerifyCode verifyCode = VerifyCodeStorage.getDate(phone); + if(verifyCode == null) { + log.error("HighUserController --> updateUserPhone() error!", "验证码错误"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误"); + } + if(!verifyCode.getObject().equals(code)) { + log.error("HighUserController --> updateUserPhone() error!", "验证码错误"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误"); + } + // 用户 + SessionObject sessionObject = userCenter.getSessionObject(request); + HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject(); + HighUser highUser = highUserService.findByUserId(userInfoModel.getHighUser().getId()); + if (highUser == null) { + log.error("HighUserController --> updateUserPhone() error!", "未找到用户"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到用户"); + } + highUser.setPhone(phone); + highUserService.updateUser(highUser); + return ResponseMsgUtil.success("修改成功"); + + } catch (Exception e) { + log.error("HighUserController --> updateUserPhone() error!", e); + return ResponseMsgUtil.exception(e); + } + } + } diff --git a/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java b/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java index 4a639c48..ae441f6c 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java @@ -4,12 +4,15 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hai.common.Base64Util; +import com.hai.common.security.VerifyCode; +import com.hai.common.security.VerifyCodeStorage; import com.hai.common.utils.DateUtil; import com.hai.common.utils.HttpsUtils; import com.hai.common.utils.ResponseMsgUtil; import com.hai.entity.BsCompany; import com.hai.entity.SecRegion; import com.hai.model.ResponseData; +import com.hai.service.CommonService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import netscape.javascript.JSObject; @@ -22,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import javax.annotation.Resource; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -37,40 +41,49 @@ public class SmsContentController { private static Logger log = LoggerFactory.getLogger(CmsContentController.class); @RequestMapping(value = "/sendSmsCode", method = RequestMethod.GET) - @ApiOperation(value = "发生短信验证码") + @ApiOperation(value = "发送短信验证码") @ResponseBody public ResponseData sendSmsCode(@RequestParam(value = "phone", required = true) String phone) { try { - - Map map = new LinkedHashMap<>(); - map.put("", "88200409698"); - map.put("to", phone); - map.put("templateId", "f42261f78d54488391f1d1ac4bd5bd3f"); - ArrayList list = new ArrayList<>(); - list.add(phone); - map.put("signature", "嗨森逛"); - - String param = "88200409698&to="+phone+"&templateId=f42261f78d54488391f1d1ac4bd5bd3f&templateParas=[\"" + 123456 +"\"]"; - //JSON.toJSONString(map); + VerifyCode verifyCode = VerifyCodeStorage.getDate(phone); + String smsCode; + if (verifyCode != null){ + smsCode = verifyCode.getObject(); + }else{ + // 生成随机6位验证码 + smsCode = String.valueOf(new Random().nextInt(899999) + 100000); + } + String param = "88200409698&to="+phone+"&templateId=f42261f78d54488391f1d1ac4bd5bd3f&templateParas=[\"" + smsCode +"\"]"; Map body = new LinkedHashMap<>(); body.put("from", param); - String nonce = UUID.randomUUID().toString(); String created = DateUtil.date2String(new Date(), "yyyy-MM-dd'T'HH:mm:ss'Z'"); - - System.out.println(encodeBySHA256(nonce + created + "9CWFv4NlaRX49n43VPA6eucZ8xeQ")); - System.out.println(Base64Util.encode(encodeBySHA256(nonce + created + "9CWFv4NlaRX49n43VPA6eucZ8xeQ"))); String passwordDigest = Base64Util.encode(encodeBySHA256(nonce + created + "9CWFv4NlaRX49n43VPA6eucZ8xeQ")) + "=="; - - // 9CWFv4NlaRX49n43VPA6eucZ8xeQ Map header = new LinkedHashMap<>(); header.put("Authorization", "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\""); header.put("X-WSSE", "UsernameToken Username=\"QR5eMVrcW0Md6r634M6tf2i34cvb\",PasswordDigest=\""+passwordDigest+"\",Nonce=\""+nonce+"\",Created=\""+created+"\""); - System.out.println(header.get("X-WSSE")); JSONObject object = HttpsUtils.doSmsPost("https://rtcsms.cn-north-1.myhuaweicloud.com:10743/sms/batchSendSms/v1", body,header); + String code = object.getString("code"); + if (code.equals("000000")) { + verifyCode = new VerifyCode(phone,smsCode); + VerifyCodeStorage.save(verifyCode); + return ResponseMsgUtil.success("发送成功"); + } return ResponseMsgUtil.success(object); } catch (Exception e) { - log.error("CmsContentController --> getCorporateAdvertising() error!", e); + log.error("CmsContentController --> sendSmsCode() error!", e); + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/getSmsCode", method = RequestMethod.GET) + @ApiOperation(value = "发送短信验证码") + @ResponseBody + public ResponseData getSmsCode(@RequestParam(value = "phone", required = true) String phone) { + try { + return ResponseMsgUtil.success(VerifyCodeStorage.getDate(phone)); + } catch (Exception e) { + log.error("CmsContentController --> getSmsCode() error!", e); return ResponseMsgUtil.exception(e); } } diff --git a/hai-service/src/main/java/com/hai/common/security/VerifyCodeStorage.java b/hai-service/src/main/java/com/hai/common/security/VerifyCodeStorage.java index 3f251e23..3a90f3d2 100644 --- a/hai-service/src/main/java/com/hai/common/security/VerifyCodeStorage.java +++ b/hai-service/src/main/java/com/hai/common/security/VerifyCodeStorage.java @@ -7,7 +7,7 @@ public class VerifyCodeStorage { private static Logger log = LoggerFactory.getLogger(VerifyCodeStorage.class); - private static final int EXPIRE = 300;//cookie过期时间为五分钟,300秒 + private static final int EXPIRE = 600;//cookie过期时间为10分钟,600秒 /** diff --git a/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java b/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java index 157055e0..006feccb 100644 --- a/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java +++ b/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java @@ -316,7 +316,7 @@ public class HttpsUtils { for (Map.Entry entry : header.entrySet()) { httpPost.setHeader(entry.getKey(), entry.getValue().toString()); } - StringEntity stringEntity = new StringEntity(IOUtil.urlEncode("from=" +body.get("from").toString(), "UTF-8"));// 解决中文乱码问题 + StringEntity stringEntity = new StringEntity(body.get("from").toString());// 解决中文乱码问题 stringEntity.setContentEncoding("UTF-8"); stringEntity.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(stringEntity); diff --git a/hai-service/src/main/java/com/hai/service/CommonService.java b/hai-service/src/main/java/com/hai/service/CommonService.java index 90fd23e5..7b2ab953 100644 --- a/hai-service/src/main/java/com/hai/service/CommonService.java +++ b/hai-service/src/main/java/com/hai/service/CommonService.java @@ -162,4 +162,7 @@ public interface CommonService { List findByName(String name); SecRegion getParentByRegion(Long regionId); + + + }