package services import ( "encoding/json" "errors" "fmt" "hongze/hongze_yb_en_api/models/msg_code" "hongze/hongze_yb_en_api/services/alarm_msg" "hongze/hongze_yb_en_api/utils" "io/ioutil" "net/http" "net/url" "time" ) // SendSmsCode 发送手机短信 func SendSmsCode(mobile, areaNum string) (err error, errMsg string) { if mobile == "" { err = errors.New("请输入手机号") return } msgCode := utils.GetRandDigit(4) var result bool if areaNum == "86" || areaNum == "" || areaNum == "0" { //国内的短信 result = SendSmsCodeGn(mobile, msgCode) } else { //国际短信 result = SendSmsCodeGj(mobile, msgCode, areaNum) } //发送成功 if result { item := &msg_code.MsgCode{ Mobile: mobile, Code: msgCode, ExpiredIn: time.Now().Add(15 * time.Minute).Unix(), Enabled: 1, CreatedTime: time.Time{}, LastUpdatedTime: time.Time{}, } err = item.Create() } else { err = errors.New("短信发送失败") } return } // SendSmsCode 发送国内短信 func SendSmsCodeGn(mobile, vCode string) bool { flag := false tplId := "65692" result, err := sendSms(mobile, tplId, vCode) if err != nil { fmt.Println("发送短信失败") return false } fmt.Println("result", string(result)) var netReturn map[string]interface{} err = json.Unmarshal(result, &netReturn) if err != nil { //go SendEmail("短信验证码发送失败", "err:"+err.Error()+" result"+string(result), utils.EmailSendToUsers) go alarm_msg.SendAlarmMsg("短信验证码发送失败, Err:"+err.Error()+";Result:"+string(result), 2) flag = false } if netReturn["error_code"].(float64) == 0 { fmt.Printf("接口返回result字段是:\r\n%v", netReturn["result"]) flag = true } else { // 忽略错误的手机号码这种错误 if netReturn["error_code"].(float64) != 205401 { go alarm_msg.SendAlarmMsg("短信验证码发送失败, Result:"+string(result), 2) } flag = false } return flag } // sendSms 发送国内短信 func sendSms(mobile, tplId, code string) (rs []byte, err error) { var Url *url.URL apiURL := "http://v.juhe.cn/sms/send" //初始化参数 param := url.Values{} //配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参 param.Set("mobile", mobile) //接受短信的用户手机号码 param.Set("tpl_id", tplId) //您申请的短信模板ID,根据实际情况修改 param.Set("tpl_value", "#code#="+code) //您设置的模板变量,根据实际情况 param.Set("key", utils.JhGnAppKey) //应用APPKEY(应用详细页查询) Url, err = url.Parse(apiURL) if err != nil { fmt.Printf("解析url错误:\r\n%v", err) return nil, err } //如果参数中有中文参数,这个方法会进行URLEncode Url.RawQuery = param.Encode() resp, err := http.Get(Url.String()) if err != nil { fmt.Println("err:", err) return nil, err } defer resp.Body.Close() return ioutil.ReadAll(resp.Body) } // SendSmsCodeGj 发送国际短信 func SendSmsCodeGj(mobile, vCode, areaNum string) bool { flag := false result, err := sendSmsGj(mobile, vCode, areaNum) if err != nil { fmt.Println("发送短信失败") return false } fmt.Println("result", string(result)) var netReturn map[string]interface{} err = json.Unmarshal(result, &netReturn) if err != nil { //go SendEmail("短信验证码发送失败", "err:"+err.Error()+" result"+string(result), utils.EmailSendToUsers) go alarm_msg.SendAlarmMsg("短信验证码发送失败, Err:"+err.Error()+";Result:"+string(result), 2) flag = false } if netReturn["error_code"].(float64) == 0 { fmt.Printf("接口返回result字段是:\r\n%v", netReturn["result"]) flag = true } else { // 忽略错误的手机号码这种错误 if netReturn["error_code"].(float64) != 205401 { go alarm_msg.SendAlarmMsg("短信验证码发送失败, Result:"+string(result), 2) } flag = false } return flag } // sendSmsGj 发送国际短信 func sendSmsGj(mobile, code, areaNum string) (rs []byte, err error) { var Url *url.URL apiURL := "http://v.juhe.cn/smsInternational/send.php" //初始化参数 param := url.Values{} //配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参 param.Set("mobile", mobile) //接受短信的用户手机号码 param.Set("tplId", "12415") //您申请的短信模板ID,根据实际情况修改 param.Set("tplValue", "#code#="+code) //您设置的模板变量,根据实际情况 param.Set("key", utils.JhGjAppKey) //应用APPKEY(应用详细页查询) param.Set("areaNum", areaNum) //应用APPKEY(应用详细页查询) Url, err = url.Parse(apiURL) if err != nil { fmt.Printf("解析url错误:\r\n%v", err) return nil, err } //如果参数中有中文参数,这个方法会进行URLEncode Url.RawQuery = param.Encode() resp, err := http.Get(Url.String()) if err != nil { fmt.Println("err:", err) return nil, err } defer resp.Body.Close() return ioutil.ReadAll(resp.Body) }