|
|
@@ -1,15 +1,18 @@
|
|
|
package com.uas.platform.b2c.common.weixin.contoller;
|
|
|
|
|
|
import com.uas.platform.b2c.common.account.model.User;
|
|
|
+import com.uas.platform.b2c.common.account.service.UserService;
|
|
|
import com.uas.platform.b2c.common.weixin.model.MessageModel;
|
|
|
import com.uas.platform.b2c.common.weixin.service.WeChatService;
|
|
|
import com.uas.platform.b2c.common.weixin.util.CheckoutUtil;
|
|
|
+import com.uas.platform.b2c.core.utils.StringUtilB2C;
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.ui.Model;
|
|
|
import org.springframework.ui.ModelMap;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
@@ -32,10 +35,20 @@ public class WeChatController {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(WeChatController.class);
|
|
|
|
|
|
+ protected static ModelMap success(Object data) {
|
|
|
+ return new ModelMap("success", true).addAttribute("content", data);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static ModelMap error(String errMsg) {
|
|
|
+ return new ModelMap("error", true).addAttribute("errMsg", errMsg);
|
|
|
+ }
|
|
|
|
|
|
@Autowired
|
|
|
private WeChatService weChatService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
/**
|
|
|
* 与微信服务器接口配置
|
|
|
*
|
|
|
@@ -102,4 +115,55 @@ public class WeChatController {
|
|
|
logger.info("绑定用户 userUU: {}", user.getUserUU());
|
|
|
return weChatService.bindUser(user);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送手机验证码
|
|
|
+ * @param mobile 手机号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/sendSmsCode", method = RequestMethod.GET)
|
|
|
+ public ModelMap sendSmsCode(String mobile) {
|
|
|
+ // 验证手机号是否为空
|
|
|
+ mobile = StringUtils.trimAllWhitespace(mobile);
|
|
|
+ if (StringUtils.isEmpty(mobile)) {
|
|
|
+ return error("手机号不能为空");
|
|
|
+ }
|
|
|
+ // 验证是否存在该用户
|
|
|
+ try {
|
|
|
+ userService.findUserByUserTel(mobile);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return error(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 随机获得验证码
|
|
|
+ String code = StringUtilB2C.getRandomNumber(6);
|
|
|
+ logger.info("用户:{} 发送验证码,code:{}", mobile, code);
|
|
|
+
|
|
|
+ weChatService.sendSMS(mobile, code);
|
|
|
+ return success("发送成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据 验证码 和 手机号 绑定用户
|
|
|
+ * @param smsCode 验证码
|
|
|
+ * @param mobile 手机号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/bindUserBySmsCode", method = RequestMethod.POST)
|
|
|
+ public ModelMap bindUserBySmsCode(String smsCode, String mobile) {
|
|
|
+ smsCode = StringUtils.trimAllWhitespace(smsCode);
|
|
|
+ if (StringUtils.isEmpty(smsCode)) {
|
|
|
+ return error("验证码不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean verifyCode = weChatService.verifyCode(mobile, smsCode);
|
|
|
+
|
|
|
+ if (!verifyCode) {
|
|
|
+ return error("验证码错误");
|
|
|
+ }
|
|
|
+ User user = userService.findUserByUserTel(mobile);
|
|
|
+
|
|
|
+ logger.info("绑定用户 userUU: {}", user.getUserUU());
|
|
|
+ return weChatService.bindUser(user);
|
|
|
+ }
|
|
|
}
|