httplib_sms_serve.go 2.8 KB
package sms_serve

import (
	"encoding/json"
	"fmt"
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway"
)

//公共短信服务模块
type HttplibSmsServe struct {
	service_gateway.BaseServiceGateway
	baseUrL string
}

func NewHttplibHttplibSmsServe() *HttplibSmsServe {
	return &HttplibSmsServe{
		BaseServiceGateway: service_gateway.BaseServiceGateway{
			ConnectTimeout:   100 * time.Second,
			ReadWriteTimeout: 30 * time.Second,
		},
		baseUrL: constant.ALLIED_CREATION_USER_HOST,
	}

}

//SendSms 公共短信验证码服务 发送验证码
func (smsServe HttplibSmsServe) SendSms(phone string) error {
	url := smsServe.baseUrL + "/service/sendSms"
	method := "post"
	req := smsServe.CreateRequest(url, method)
	param := map[string]string{
		"phone": phone,
	}
	log.Logger.Debug("向公共短信验证码服务请求数据:短信验证码接口。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return fmt.Errorf("请求公共短信验证码服务失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return fmt.Errorf("获取公共短信验证码服务失败:%w", err)
	}
	log.Logger.Debug("获取公共短信验证码服务请求数据", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return fmt.Errorf("解析更新组织:%w", err)
	}
	if result.Code != 0 {
		return fmt.Errorf(result.Msg)
	}
	return nil
}

//CheckSmsCode 公共短信验证码服务 校验验证码
func (smsServe HttplibSmsServe) CheckSmsCode(phone string, code string) error {
	url := smsServe.baseUrL + "/service/sendSms"
	method := "post"
	req := smsServe.CreateRequest(url, method)
	param := map[string]string{
		"phone": phone,
		"code":  code,
	}
	log.Logger.Debug("向公共短信验证码服务请求数据:短信验证码接口。", map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return fmt.Errorf("请求公共短信验证码服务失败:%w", err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return fmt.Errorf("获取公共短信验证码服务失败:%w", err)
	}
	log.Logger.Debug("获取公共短信验证码服务请求数据", map[string]interface{}{
		"result": string(byteResult),
	})
	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return fmt.Errorf("解析更新组织:%w", err)
	}
	if result.Code != 0 {
		return fmt.Errorf(result.Msg)
	}
	return nil
}