// This file is auto-generated, don't edit it. Thanks.
package GoUtil

import (
	"fmt"
	"server/pkg/github.com/name5566/leaf/log"

	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v5/client"
	console "github.com/alibabacloud-go/tea-console/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	credential "github.com/aliyun/credentials-go/credentials"
)

const (
	SIGN_NAME     = "阿里云短信测试"
	TEMPLATE_CODE = "SMS_154950909"
)

// Description:
//
// 使用凭据初始化账号Client
//
// @return Client
//
// @throws Exception
func CreateSmsClient() (_result *dysmsapi20170525.Client, _err error) {
	// 工程代码建议使用更安全的无AK方式，凭据配置方式请参见：https://help.aliyun.com/document_detail/378661.html。
	credential, _err := credential.NewCredential(&credential.Config{
		Type:            tea.String("access_key"),
		AccessKeyId:     tea.String(ACCESS_KEY_ID),
		AccessKeySecret: tea.String(ACCESS_KEY_SECRET),
	})
	if _err != nil {
		return _result, _err
	}

	config := &openapi.Config{
		Credential: credential,
	}
	// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
	config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
	_result = &dysmsapi20170525.Client{}
	_result, _err = dysmsapi20170525.NewClient(config)
	return _result, _err
}

func SmsCode(Phone, Code string) (_err error) {
	client, _err := CreateSmsClient()
	if _err != nil {
		return _err
	}
	console.Log(tea.String("生成的验证码：" + Code))
	sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
		PhoneNumbers:  tea.String(Phone),
		SignName:      tea.String(SIGN_NAME),
		TemplateCode:  tea.String(TEMPLATE_CODE),
		TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%s\"}", Code)),
	}
	runtime := &util.RuntimeOptions{}
	resp, _err := client.SendSmsWithOptions(sendSmsRequest, runtime)
	log.Debug("SendSmsWithOptions resp: %v", resp)
	if _err != nil {
		return _err
	}
	return nil
}
