package sms // * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // * Copyright 2023 The Geek-AI Authors. All rights reserved. // * Use of this source code is governed by a Apache-2.0 license // * that can be found in the LICENSE file. // * @Author yangjian102621@163.com // * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ import ( "context" "fmt" "geekai/core/types" "geekai/utils" "net/url" "strconv" "strings" "time" ) type BaoSmsService struct { config types.SmsConfigBao domain string } func NewBaoSmsService(sysConfig *types.SystemConfig) *BaoSmsService { return &BaoSmsService{ config: sysConfig.SMS.Bao, domain: "api.smsbao.com", } } func (s *BaoSmsService) UpdateConfig(config types.SmsConfigBao) { s.config = config } var errMsg = map[string]string{ "0": "短信发送成功", "-1": "参数不全", "-2": "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间", "30": "密码错误", "40": "账号不存在", "41": "余额不足", "42": "账户已过期", "43": "IP地址限制", "50": "内容含有敏感词", } func (s *BaoSmsService) SendVerifyCode(mobile string, code int) error { content := fmt.Sprintf("%s%s", s.config.Sign, s.config.CodeTemplate) content = strings.ReplaceAll(content, "{code}", strconv.Itoa(code)) password := utils.Md5(s.config.Password) params := url.Values{} params.Set("u", s.config.Username) params.Set("p", password) params.Set("m", mobile) params.Set("c", content) apiURL := fmt.Sprintf("https://%s/sms?%s", s.domain, params.Encode()) body, status, err := utils.FetchURLBytes(context.Background(), apiURL, "", 30*time.Second, 2, 2<<20) if err != nil { return fmt.Errorf("smsbao request failed: status=%d: %w", status, err) } result := string(body) logger.Debugf("send SmsBao result: %v", errMsg[result]) if result != "0" { return fmt.Errorf("failed to send SMS:%v", errMsg[result]) } return nil } var _ Service = &BaoSmsService{}