فهرست منبع

引入dubbo服务,发送短信

liuam 7 سال پیش
والد
کامیت
0090f8e267

+ 26 - 0
pom.xml

@@ -393,6 +393,32 @@
 			<groupId>com.uas.dfs</groupId>
 			<artifactId>dfs-api</artifactId>
 		</dependency>
+
+		<!-- dubbo -->
+		<dependency>
+			<groupId>com.alibaba</groupId>
+			<artifactId>dubbo</artifactId>
+			<exclusions>
+				<exclusion>
+					<groupId>javax.servlet</groupId>
+					<artifactId>javax.servlet-api</artifactId>
+				</exclusion>
+				<exclusion>
+					<groupId>org.apache.httpcomponents</groupId>
+					<artifactId>httpcore</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+
+		<dependency>
+			<groupId>org.apache.zookeeper</groupId>
+			<artifactId>zookeeper</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.github.sgroschupf</groupId>
+			<artifactId>zkclient</artifactId>
+		</dependency>
+
 		<!-- search -->
 		<dependency>
 			<groupId>com.uas.search</groupId>

+ 64 - 0
src/main/java/com/uas/platform/b2c/common/weixin/contoller/WeChatController.java

@@ -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);
+    }
 }

+ 15 - 0
src/main/java/com/uas/platform/b2c/common/weixin/service/WeChatService.java

@@ -34,4 +34,19 @@ public interface WeChatService {
      * @return
      */
     ModelMap bindUser(User user);
+
+    /**
+     * 发送验证码
+     * @param mobile 手机号
+     * @param code 验证码
+     */
+    void sendSMS(String mobile, String code);
+
+    /**
+     * 对验证码进行验证
+     * @param mobile 手机号
+     * @param code 需要验证的验证码
+     * @return true 验证成功
+     */
+    boolean verifyCode(String mobile, String code);
 }

+ 40 - 2
src/main/java/com/uas/platform/b2c/common/weixin/service/impl/WeChatServiceImpl.java

@@ -15,6 +15,7 @@ import com.uas.platform.b2c.common.weixin.model.resp.TemplateMsgResult;
 import com.uas.platform.b2c.common.weixin.service.WeChatService;
 import com.uas.platform.b2c.common.weixin.util.HttpReqUtil;
 import com.uas.platform.b2c.common.weixin.util.WeChatUtil;
+import com.uas.platform.b2c.core.utils.MessageUtils;
 import com.uas.platform.core.exception.IllegalOperatorException;
 import com.uas.platform.core.util.serializer.FlexJsonUtils;
 import com.uas.sso.entity.UserAccount;
@@ -51,10 +52,15 @@ public class WeChatServiceImpl implements WeChatService{
     private Logger logger = LoggerFactory.getLogger(WeChatServiceImpl.class);
 
     /**
-     * 保存到 redis 里的过期时间(second)
+     * 保存到 redis 里的 ACCESS_TOKEN 过期时间(second)
      */
     private static final Integer ACCESS_TOKEN_EXPIRES_IN = 3600;
 
+    /**
+     * 保存到 redis 里的验证码过期时间(second)
+     */
+    private static final Integer SMS_EXPIRES_IN = 10 * 60;
+
     @Override
     public ModelMap getWxUserInfo(String code, String state, String openId) {
         logger.info("得到用户信息 code: {}, state: {}, openId: {}", code, state, openId);
@@ -166,7 +172,7 @@ public class WeChatServiceImpl implements WeChatService{
         List<User> oldUsers = userDao.findUserByUserTel(user.getUserTel());
         if (CollectionUtils.isEmpty(oldUsers)) {
             throw new IllegalOperatorException("未找到用户信息");
-        }
+    }
         List<User> judgeUser = userDao.findUsersByOpenId(user.getOpenId());
         if (!CollectionUtils.isEmpty(judgeUser)) {
             throw new IllegalOperatorException("该微信号已被绑定");
@@ -202,6 +208,36 @@ public class WeChatServiceImpl implements WeChatService{
         }
     }
 
+    /**
+     * 发送验证码
+     * @param mobile 手机号
+     * @param code 验证码
+     */
+    @Override
+    public void sendSMS(String mobile, String code) {
+        redisTemplate.opsForValue().set(mobile, mobile, SMS_EXPIRES_IN, TimeUnit.SECONDS);
+        logger.info("用户:{},验证码:{},redis's key:{},redis's expires_in:{}", mobile, code, mobile, SMS_EXPIRES_IN);
+        MessageUtils.sendSms(mobile, code);
+    }
+
+    /**
+     * 对验证码进行验证
+     * @param mobile 手机号
+     * @param code 需要验证的验证码
+     * @return true 验证成功
+     */
+    @Override
+    public boolean verifyCode(String mobile, String code) {
+        logger.info("WeChatServiceImpl#verifyCode 对验证码进行验证:mobile:{},code:{}", mobile, code);
+        String redisCode = (String) redisTemplate.opsForValue().get(mobile);
+        if (code.equals(redisCode)) {
+            logger.info("WeChatServiceImpl#verifyCode 验证成功:mobile:{},code:{}", mobile, code);
+            return true;
+        }
+        logger.warn("WeChatServiceImpl#verifyCode 验证失败:mobile:{},code:{}", mobile, code);
+        return false;
+    }
+
     /**
      * 通过code获取用户openId
      * @param code
@@ -264,4 +300,6 @@ public class WeChatServiceImpl implements WeChatService{
         redisTemplate.opsForValue().set("WX_ACCESS_TOKEN", access_token, ACCESS_TOKEN_EXPIRES_IN, TimeUnit.SECONDS);
         return access_token;
     }
+
+
 }

+ 40 - 0
src/main/java/com/uas/platform/b2c/core/utils/MessageUtils.java

@@ -0,0 +1,40 @@
+package com.uas.platform.b2c.core.utils;
+
+import com.uas.message.sms.service.SmsService;
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * @author wangmh
+ * @create 2018-06-05 17:17
+ * @desc 发送验证码工具类
+ **/
+public class MessageUtils {
+
+    private static SmsService smsService;
+
+    static {
+        smsService = (SmsService) ContextUtils.getBean("smsService");
+    }
+
+    /**
+     * 验证码模板ID
+     */
+    private static String TEMPLATE_ID = "f32af236-7211-4399-bf2c-bee41ed97a32";
+
+    /**
+     * 发送短信
+     * @param mobile 手机号
+     * @param data 发送短信适配数据,按顺序添加
+     */
+    public static void sendSms(String mobile, Object... data) {
+        try {
+            if (!StringUtils.isEmpty(mobile)) {
+                smsService.send(TEMPLATE_ID, mobile, data);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
+}

+ 13 - 0
src/main/java/com/uas/platform/b2c/core/utils/StringUtilB2C.java

@@ -405,5 +405,18 @@ public class StringUtilB2C {
 		}
 	}
 
+	/**
+	 * 随机数字
+	 *
+	 * @param len
+	 *            数字长度
+	 * @return
+	 */
+	public static String getRandomNumber(int len) {
+		int max = (int) Math.pow(10, len) - 1;
+		int min = (int) Math.pow(10, len - 1);
+		return String.valueOf(new Random().nextInt(max) % (max - min + 1) + min);
+	}
+
 
 }

+ 1 - 0
src/main/resources/spring/context.xml

@@ -206,4 +206,5 @@
 	<import resource="classpath:spring/jpa.xml"/>
 	<import resource="classpath:spring/redis.xml"/>
 	<import resource="classpath:spring/task.xml"/>
+	<import resource="classpath:spring/dubbo.xml"/>
 </beans>

+ 19 - 0
src/main/resources/spring/dubbo.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
+
+    <dubbo:application name="b2c_consumer" />
+
+    <dubbo:registry address="${zk.url}" check="false" />
+
+    <!-- 分布式文件服务 -->
+    <!--<dubbo:reference id="fileClient" interface="com.uas.dfs.service.FileClient" timeout="100000"/>-->
+
+    <!-- 邮件服务 -->
+    <!--<dubbo:reference id="mailService" interface="com.uas.message.mail.service.MailService" timeout="30000" />-->
+
+    <!-- 短信服务 -->
+    <dubbo:reference id="smsService" interface="com.uas.message.sms.service.SmsService" timeout="30000" />
+</beans>