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