1. 程式人生 > 其它 >UI自動化測試4.8

UI自動化測試4.8

1.基本傳送簡訊

1.1登入阿里雲開通簡訊功能

1.2配置檔案application.yml中新增阿里雲簡訊的配置

ali:
  # 阿里雲簡訊
  sms:
    accessKeyId: LTAI5tEDc4wNDKo4AAzekNgk #AccessKey ID
    accessKeySecret: xml8zusZTqTRYE3yKRKmvIiWGo9C1p  #AccessKey Secret
    templateCode: SMS_154950909 #模板
    signName: 阿里雲簡訊測試 #簽名
    domain: dysmsapi.aliyuncs.com #簡訊產品域名
    version: 
2017-05-25 #版本-固定值 regionId: default #地區-預設即可

1.3pet-service 新增配置檔案對應的配置類

@Configuration
@ConfigurationProperties(prefix = "ali.sms")
public class AliSmsConfig {

    private String accessKeyId;
    private String accessKeySecret;
    private String templateCode;
    private String signName;
    
private String domain; private String regionId; private String version; public String getAccessKeyId() { return accessKeyId; } public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; } public String getAccessKeySecret() {
return accessKeySecret; } public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; } public String getTemplateCode() { return templateCode; } public void setTemplateCode(String templateCode) { this.templateCode = templateCode; } public String getSignName() { return signName; } public void setSignName(String signName) { this.signName = signName; } public String getDomain() { return domain; } public void setDomain(String domain) { this.domain = domain; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getRegionId() { return regionId; } public void setRegionId(String regionId) { this.regionId = regionId; } }

1.4pet-service新增連線第三方平臺阿里雲的介面

package cn.pet.service.thirdparty.aliyun.sms;

import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class AliYunService {
    private static final Logger logger= LoggerFactory.getLogger(AliYunService.class);
    @Autowired
    private AliYunConfig aliYunConfig;
    //定義方法呼叫阿里雲簡訊介面 sendSms發簡訊
    public String sendSms(String phone,String code){
        Config config = new Config();
        config.setAccessKeyId(aliYunConfig.getAccessKeyId());
        config.setAccessKeySecret(aliYunConfig.getAccessKeySecret());
      //  config.endpoint=aliYunConfig.getDomain();
        config.endpoint = "dysmsapi.aliyuncs.com";


        //建立客戶端
        Client client=null;
        try {
             client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }

        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);
        request.setSignName(aliYunConfig.getSignName());
        request.setTemplateCode(aliYunConfig.getTemplateCode());

        //封裝驗證碼
        Map<String,String> map=new HashMap<>();
        map.put("code",code);
        //request.setTemplateParam(code);
        request.setTemplateParam(JSON.toJSONString(map));
        SendSmsResponse response=null;
        try {
             response = client.sendSms(request);
             logger.info("傳送簡訊之後返回結果:{}",response.getBody().getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response.getBody().getMessage();
    }
}

1.5pet-service添加發送簡訊的方法sendSms()

package cn.pet.service.impl;

import cn.pet.constant.Constants;
import cn.pet.service.UmsSmsService;
import cn.pet.service.thirdparty.aliyun.sms.AliYunService;
import cn.pet.utils.MathUtils;
import com.sun.org.apache.bcel.internal.generic.MULTIANEWARRAY;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UmsSmsServiceImpl implements UmsSmsService {
    private static final Logger logger= LoggerFactory.getLogger(AliYunService.class);
    @Autowired
    private AliYunService aliYunService;
    @Override
    public Boolean sendSms(String phone, Integer codeType) {
        //需要生成驗證碼
        String code = MathUtils.random();
        logger.info("手機號碼:{} 簡訊驗證碼為:{}",phone,code);
        String result = aliYunService.sendSms(phone, code);
        if (result.equals(Constants.Sms.ALI_SMS_RESULT)){
            return true;
        }
        return false;
    }
}

.6pet-web添加發送簡訊的介面方法sendSms()

package cn.pet.controller;

import cn.pet.result.Result;
import cn.pet.result.ResultEnum;
import cn.pet.result.ResultUtils;
import cn.pet.service.UmsSmsService;
import cn.pet.utils.UmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/ums/sms")
@RestController
public class UmsSmsController {
    @Autowired
    private UmsSmsService umsSmsService;
    @PostMapping("/sendSms")
    public Result sendSms(String phone,Integer codeType){
        //判斷手機號
        if (StringUtils.isEmpty(phone) | !UmsUtils.checkPhone(phone)){
            return ResultUtils.returnResult(ResultEnum.FAIL_UMS_PHONE_ERROR);
        }

        if (!UmsUtils.checkCodeType(codeType)){
            return ResultUtils.returnResult(ResultEnum.FAIL_UMS_SMS_CODE_TYPE_ERROR);
        }
        //呼叫service介面
        Boolean aBoolean = umsSmsService.sendSms(phone, codeType);
        if (aBoolean){
            return ResultUtils.returnSuccess();
        }else {
            return ResultUtils.returnFail();
        }
    }
}