Browse Source

add sms api.

deason 7 years ago
parent
commit
1d52bbbe5e

+ 18 - 0
pom.xml

@@ -34,6 +34,10 @@
             <id>aliyun-public</id>
             <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
         </pluginRepository>
+        <pluginRepository>
+            <id>central-repository</id>
+            <url>https://repo.maven.apache.org/maven2</url>
+        </pluginRepository>
     </pluginRepositories>
 
     <dependencies>
@@ -105,10 +109,24 @@
             <groupId>com.upyun</groupId>
             <artifactId>java-sdk</artifactId>
             <version>4.0.1</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>com.squareup.okhttp3</groupId>
+                    <artifactId>okhttp</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
     </dependencies>
 
     <build>
+        <resources>
+            <resource>
+                <directory>${basedir}/src/main/resources</directory>
+                <includes>
+                    <include>**/*.properties</include>
+                </includes>
+            </resource>
+        </resources>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>

+ 15 - 0
src/main/java/cn/com/qmth/examcloud/app/controller/v1/UserAuthRestController.java

@@ -8,6 +8,7 @@
 package cn.com.qmth.examcloud.app.controller.v1;
 
 import cn.com.qmth.examcloud.app.model.Result;
+import cn.com.qmth.examcloud.app.service.SmsService;
 import cn.com.qmth.examcloud.app.service.UserAuthService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -25,6 +26,8 @@ import org.springframework.web.bind.annotation.*;
 public class UserAuthRestController {
     @Autowired
     private UserAuthService userAuthService;
+    @Autowired
+    private SmsService smsService;
 
     @ApiOperation(value = "登录接口")
     @RequestMapping(value = "/user/login", method = {RequestMethod.GET, RequestMethod.POST})
@@ -50,4 +53,16 @@ public class UserAuthRestController {
         return userAuthService.updatePassword(key, token, userId, password);
     }
 
+    @ApiOperation(value = "获取短信验证码接口")
+    @RequestMapping(value = "/send/sms/code", method = {RequestMethod.GET, RequestMethod.POST})
+    public Result sendSmsCode(@RequestHeader String key, @RequestHeader String token, @RequestParam String phone) throws Exception {
+        return smsService.sendSmsCode(key, token, phone);
+    }
+
+    @ApiOperation(value = "校验短信验证码接口")
+    @RequestMapping(value = "/check/sms/code", method = {RequestMethod.GET, RequestMethod.POST})
+    public Result checkSmsCode(@RequestHeader String key, @RequestHeader String token, @RequestParam String phone, @RequestParam String code) throws Exception {
+        return smsService.checkSmsCode(key, token, phone, code);
+    }
+
 }

+ 23 - 0
src/main/java/cn/com/qmth/examcloud/app/model/ResBody.java

@@ -11,11 +11,18 @@ import java.io.Serializable;
 
 public class ResBody implements Serializable {
     private static final long serialVersionUID = 1L;
+    /* 考务、题库等接口返回结果 */
     private String code;//状态码
     private String desc;//描述信息
+
+    /* 网考接口返回结果 */
     private String errorMsg;
     private String content;
 
+    /* 短信接口返回结果 */
+    private Boolean success;
+    private String returnMsg;
+
     public String getCode() {
         return code;
     }
@@ -48,4 +55,20 @@ public class ResBody implements Serializable {
         this.content = content;
     }
 
+    public Boolean getSuccess() {
+        return success;
+    }
+
+    public void setSuccess(Boolean success) {
+        this.success = success;
+    }
+
+    public String getReturnMsg() {
+        return returnMsg;
+    }
+
+    public void setReturnMsg(String returnMsg) {
+        this.returnMsg = returnMsg;
+    }
+
 }

+ 18 - 0
src/main/java/cn/com/qmth/examcloud/app/service/PropertyService.java

@@ -28,6 +28,12 @@ public class PropertyService {
     private String userAuthUrl;//认证中心服务
     @Value("${examcloud.upyun.url}")
     private String upYunUrl;//又拍云文件服务
+    @Value("${examcloud.sms.url}")
+    private String smsUrl;//短信服务地址
+    @Value("${examcloud.sms.sign}")
+    private String smsSign;//短信签名
+    @Value("${examcloud.sms.template}")
+    private String smsTemplate;//短信模板Code
 
     public String getExamAdminUrl() {
         return examAdminUrl;
@@ -49,4 +55,16 @@ public class PropertyService {
         return upYunUrl;
     }
 
+    public String getSmsUrl() {
+        return smsUrl;
+    }
+
+    public String getSmsSign() {
+        return smsSign;
+    }
+
+    public String getSmsTemplate() {
+        return smsTemplate;
+    }
+
 }

+ 79 - 0
src/main/java/cn/com/qmth/examcloud/app/service/SmsService.java

@@ -0,0 +1,79 @@
+/*
+ * *************************************************
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
+ * Created by Deason on 2018-07-23 14:55:35.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.app.service;
+
+import cn.com.qmth.examcloud.app.model.Constants;
+import cn.com.qmth.examcloud.app.model.ResBody;
+import cn.com.qmth.examcloud.app.model.Result;
+import cn.com.qmth.examcloud.app.utils.HttpUtils;
+import cn.com.qmth.examcloud.app.utils.JsonMapper;
+import cn.com.qmth.examcloud.app.utils.StrUtils;
+import okhttp3.FormBody;
+import okhttp3.MediaType;
+import okhttp3.RequestBody;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.Assert;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 短信服务接口
+ */
+@Service
+public class SmsService {
+    private static Logger log = LoggerFactory.getLogger(SmsService.class);
+    @Autowired
+    private PropertyService propertyService;
+
+    public Result<String> sendSmsCode(String key, String token, String phone) throws Exception {
+        Assert.notNull(phone, "Phone must be not null.");
+        Assert.isTrue(StrUtils.isMobile(phone), "Phone is wrong number.");
+        //生成code
+        String code = StrUtils.randomNumber().toString();
+        //封装请求参数
+        final String requestUrl = String.format("%s/api/exchange/inner/sendSms/sendIdentifyingCode", propertyService.getSmsUrl());
+        Map<String, String> params = new HashMap<>();
+        params.put("phone", phone);//手机号码
+        params.put("code", code);//验证码
+        params.put("sign", propertyService.getSmsSign());//签名
+        params.put("templateCode", propertyService.getSmsTemplate());//短信模板
+        String json = new JsonMapper().toJson(params);
+        RequestBody formBody = FormBody.create(MediaType.parse(Constants.CHARSET_JSON_UTF8), json);
+        //发送短信
+        Result<String> result = HttpUtils.doPost(requestUrl, formBody, key, token);
+        return this.parseResult(result);
+    }
+
+    public Result<String> checkSmsCode(String key, String token, String phone, String code) throws Exception {
+        //封装请求参数
+        final String requestUrl = String.format("%s/api/exchange/inner/sendSms/checkIdentifyingCode", propertyService.getSmsUrl());
+        Map<String, String> params = new HashMap<>();
+        params.put("phone", phone);
+        params.put("code", code);
+        String json = new JsonMapper().toJson(params);
+        RequestBody formBody = FormBody.create(MediaType.parse(Constants.CHARSET_JSON_UTF8), json);
+        Result<String> result = HttpUtils.doPost(requestUrl, formBody, key, token);
+        return this.parseResult(result);
+    }
+
+    private Result<String> parseResult(Result<String> result) {
+        if (!result.isSuccess()) {
+            return result;
+        }
+        ResBody body = new JsonMapper().fromJson(result.getData(), ResBody.class);
+        if (body != null && body.getSuccess() != null && body.getSuccess() == false) {
+            return new Result().error(body.getReturnMsg());
+        }
+        return result;
+    }
+
+}

+ 37 - 0
src/main/java/cn/com/qmth/examcloud/app/utils/StrUtils.java

@@ -0,0 +1,37 @@
+/*
+ * *************************************************
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
+ * Created by Deason on 2018-07-23 15:05:46.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.app.utils;
+
+import java.util.Random;
+
+public class StrUtils {
+
+    public static Integer randomNumber() {
+        return randomNumber(100000, 999999);
+    }
+
+    public static int randomNumber(int min, int max) {
+        Random random = new Random();
+        return random.nextInt(max) % (max - min + 1) + min;
+    }
+
+    /**
+     * 校验手机号码格式
+     */
+    public static boolean isMobile(String str) {
+        return check(str, "^[1][3,4,5,6,7,8,9][0-9]{9}$");
+    }
+
+    public static boolean check(String str, String reg) {
+        if (str != null && str.matches(reg)) {
+            return true;
+        }
+        return false;
+    }
+
+}

+ 37 - 0
src/main/resources/application-dev.properties

@@ -0,0 +1,37 @@
+# mvc config
+server.port=8090
+server.context-path=/
+server.tomcat.uri-encoding=UTF-8
+spring.freemarker.request-context-attribute=request
+spring.freemarker.settings.number_format=#
+spring.freemarker.suffix=.ftl
+# logs config
+logging.file=logs/logging.txt
+logging.level.org.springframework=WARN
+logging.level.org.apache=WARN
+logging.level.com.qmth=DEBUG
+logging.level.cn.com.qmth=DEBUG
+# mysql config
+spring.datasource.url=jdbc:mysql://192.168.10.30:3306/app_api?useUnicode=true&characterEncoding=utf-8&useSSL=false
+spring.datasource.username=root
+spring.datasource.password=root
+spring.datasource.driverClassName=com.mysql.jdbc.Driver
+spring.jpa.show-sql=false
+spring.jpa.hibernate.ddl-auto=update
+spring.datasource.sql-script-encoding=UTF-8
+spring.datasource.continue-on-error=false
+spring.datasource.initialize=false
+# 考务服务地址
+examcloud.exam.admin.url=http://192.168.10.30:8001
+# 网考服务地址
+examcloud.net.exam.url=http://192.168.10.30:8003
+# 题库服务地址
+examcloud.question.pool.url=http://192.168.10.30:8868
+# 认证中心服务地址
+examcloud.user.auth.url=http://192.168.10.30
+# 又拍云文件服务地址
+examcloud.upyun.url=https://ecs-test-static.qmth.com.cn
+# 短信服务配置
+examcloud.sms.url=http://192.168.10.30:8007
+examcloud.sms.sign=\u8003\u8BD5\u4E91\u5E73\u53F0
+examcloud.sms.template=SMS_138073780

+ 37 - 0
src/main/resources/application-prod.properties

@@ -0,0 +1,37 @@
+# mvc config
+server.port=8090
+server.context-path=/
+server.tomcat.uri-encoding=UTF-8
+spring.freemarker.request-context-attribute=request
+spring.freemarker.settings.number_format=#
+spring.freemarker.suffix=.ftl
+# logs config
+logging.file=logs/logging.txt
+logging.level.org.springframework=WARN
+logging.level.org.apache=WARN
+logging.level.com.qmth=DEBUG
+logging.level.cn.com.qmth=DEBUG
+# mysql config
+spring.datasource.url=jdbc:mysql://192.168.10.30:3306/app_api?useUnicode=true&characterEncoding=utf-8&useSSL=false
+spring.datasource.username=root
+spring.datasource.password=root
+spring.datasource.driverClassName=com.mysql.jdbc.Driver
+spring.jpa.show-sql=false
+spring.jpa.hibernate.ddl-auto=update
+spring.datasource.sql-script-encoding=UTF-8
+spring.datasource.continue-on-error=false
+spring.datasource.initialize=false
+# 考务服务地址
+examcloud.exam.admin.url=http://192.168.10.30:8001
+# 网考服务地址
+examcloud.net.exam.url=http://192.168.10.30:8003
+# 题库服务地址
+examcloud.question.pool.url=http://192.168.10.30:8868
+# 认证中心服务地址
+examcloud.user.auth.url=http://192.168.10.30
+# 又拍云文件服务地址
+examcloud.upyun.url=https://ecs-test-static.qmth.com.cn
+# 短信服务配置
+examcloud.sms.url=http://192.168.10.30:8007
+examcloud.sms.sign=\u8003\u8BD5\u4E91\u5E73\u53F0
+examcloud.sms.template=SMS_138073780

+ 2 - 35
src/main/resources/application.properties

@@ -1,35 +1,2 @@
-#mvc config
-server.port=8090
-server.context-path=/
-server.tomcat.uri-encoding=UTF-8
-spring.freemarker.request-context-attribute=request
-spring.freemarker.settings.number_format=#
-spring.freemarker.suffix=.ftl
-#logs config
-logging.file=logs/logging.txt
-logging.level.org.springframework=WARN
-logging.level.org.apache=WARN
-logging.level.com.qmth=DEBUG
-logging.level.cn.com.qmth=DEBUG
-#mysql config
-spring.datasource.url=jdbc:mysql://192.168.10.30:3306/app_api?useUnicode=true&characterEncoding=utf-8&useSSL=false
-spring.datasource.username=root
-spring.datasource.password=root
-spring.datasource.driverClassName=com.mysql.jdbc.Driver
-spring.jpa.show-sql=false
-spring.jpa.hibernate.ddl-auto=update
-spring.datasource.testWhileIdle=true
-spring.datasource.validationQuery=SELECT 1
-spring.datasource.sql-script-encoding=UTF-8
-spring.datasource.continue-on-error=false
-spring.datasource.initialize=false
-#蕉昢督昢華硊
-examcloud.exam.admin.url=http://192.168.10.30:8001
-#厙蕉督昢華硊
-examcloud.net.exam.url=http://192.168.10.30:8003
-#枙踱督昢華硊
-examcloud.question.pool.url=http://192.168.10.30:8868
-#'痐笢陑督昢華硊
-examcloud.user.auth.url=http://192.168.10.30
-#衱鼴堁恅璃督昢華硊
-examcloud.upyun.url=https://ecs-test-static.qmth.com.cn
+# profile config
+spring.profiles.active=dev

+ 1 - 1
src/main/resources/templates/deviceRecord/list.ftl

@@ -107,7 +107,7 @@
         params.loginKey = $('#loginKey').val();
         params.loginToken = $('#loginToken').val();
         $.ajax({
-            url: '${base}/device/record/list?pageSize=5&pageNo=' + curPageNo,
+            url: '${base}/device/record/list?pageSize=10&pageNo=' + curPageNo,
             contentType: "application/json; charset=UTF-8",
             dataType: "json",
             type: "POST",