快捷搜索: 王者荣耀 脱发

JAVA对接发送SMS短信服务

短信服务申请

网页进入阿里云页面,搜索“短信”,进入以下页面进行购买即可。

再购买完成后进入“控制台”查看具体购买的信息,有以下信息代表购买成功了。

JAVA对接

在购买的短信对接服务中有API的调用,根据具体的API调用即可。

代码编写

配置类 SmsComponent

package com.tomalen.gulimall.thirty.component;

import com.tomalen.gulimall.thirty.util.HttpUtils;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

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

@Data
@Component
@ConfigurationProperties(prefix = "spring.cloud.alicloud.sms")
public class SmsComponent {
          
   

    private String host;
    private String path;
    private String templateId;
    private String value;
    private String appCode;

    public String sendSmsCode(String phone, String code) {
          
   
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        querys.put("templateId", templateId);
        querys.put("value", code);
        Map<String, String> bodys = new HashMap<String, String>();
        try {
          
   
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            String smsResponse = EntityUtils.toString(response.getEntity());
            return smsResponse;
        } catch (Exception e) {
          
   
            e.printStackTrace();
        }
        return null;
    }
}

nacos配置中心–对应上面读取的参数

spring:
  application:
    name: gulimall-third-party
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
    alicloud:
      access-key: xxxx
      secret-key: xxxx
      oss:
        endpoint: oss-cn-beijing.aliyuncs.com
        bucket: gulimall-zhongweihong
      sms:
        host: https://jumsendsms.market.alicloudapi.com
        path: /sms/send-upgrade
        templateId: M72CB42894
        value: 1
        appCode: 自己的appendId

server:
  port: 30000

调用

    Feign远程调用第三方发送信息服务
package com.tomalen.gulimall.product.feign;

import com.tomalen.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("gulimall-third-party")
public interface ThirdFeignService {
          
   

    @GetMapping("/sms/sendSmsCode")
    R sendSmsCode(@RequestParam("phone") String phone, @RequestParam("code") String code);

}
    调用
@GetMapping("/sendSmsCode")
    public R sendSmsCode(@RequestParam("phone") String phone, @RequestParam("code") String code) {
          
   
        R result = thirdFeignService.sendSmsCode(phone, code);
        return R.ok(result);
    }

Vue前端测试代码

<el-button
              v-if="isAuth(product:brand:delete)"
              type="warning"
              @click="sendSmsCode()"
              >发送短信</el-button
            >
sendSmsCode() {
          
   
      const phone = "你的手机号";
      const code = "66666";
      this.$http({
          
   
        url: this.$http.adornUrl("/product/attrgroup/sendSmsCode"),
        methods: "get",
        params: this.$http.adornParams({
          
   
          phone: phone,
          code: code
        }),
      }).then(({
           
     data }) => {
          
   
        if (data && data.code === 0) {
          
   
          this.$message({
          
   
            message: 发送短信成功,
            type: success
        });
        } else {
          
   
            this.$message.error(发送短信失败);
        }
      });

效果

结语

调用第三方接口千篇一律,只要了解到调用API就可以实现相应的功能,所以要熟练应用官方提供的各种API文档。

经验分享 程序员 微信小程序 职场和发展