|
@@ -1,90 +0,0 @@
|
|
|
-/*
|
|
|
- * *************************************************
|
|
|
- * Copyright (c) 2018 QMTH. All Rights Reserved.
|
|
|
- * Created by Deason on 2018-07-31 17:19:06.
|
|
|
- * *************************************************
|
|
|
- */
|
|
|
-
|
|
|
-package cn.com.qmth.examcloud.app.service.impl;
|
|
|
-
|
|
|
-import cn.com.qmth.examcloud.app.core.utils.HttpUtils;
|
|
|
-import cn.com.qmth.examcloud.app.core.utils.JsonMapper;
|
|
|
-import cn.com.qmth.examcloud.app.core.utils.StrUtils;
|
|
|
-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.core.SysProperty;
|
|
|
-import cn.com.qmth.examcloud.app.service.SmsService;
|
|
|
-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;
|
|
|
-
|
|
|
-/**
|
|
|
- * 短信服务接口
|
|
|
- *
|
|
|
- * @author: fengdesheng
|
|
|
- * @since: 2018/7/16
|
|
|
- */
|
|
|
-@Service
|
|
|
-public class SmsServiceImpl implements SmsService {
|
|
|
- private static Logger log = LoggerFactory.getLogger(SmsServiceImpl.class);
|
|
|
- @Autowired
|
|
|
- private SysProperty sysProperty;
|
|
|
-
|
|
|
- @Override
|
|
|
- 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", sysProperty.getSmsUrl());
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
- params.put("phone", phone);//手机号码
|
|
|
- params.put("code", code);//验证码
|
|
|
- params.put("sign", sysProperty.getSmsSign());//签名
|
|
|
- params.put("templateCode", sysProperty.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);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- 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", sysProperty.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) {
|
|
|
- if (body.getSuccess() == true) {
|
|
|
- return new Result().success();
|
|
|
- } else {
|
|
|
- return new Result().error(body.getReturnMsg());
|
|
|
- }
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
-}
|