阿里云短信服务使用和注意事项

		//发送短信   阿里SDK调用
		client, err := sdk.NewClientWithAccessKey("cn-hangzhou", "LTAIu4sh9mfgqjjr", "sTPSi0Ybj0oFyqDTjQyQNqdq9I9akE")
		if err != nil {
			beego.Error("电话号码格式错误3")
			//2.给容器赋值
			resp["errno"] = 3
			resp["errmsg"] = "初始化短信错误"
			return
		} */
	//生成6位数随机数
	rand.Seed(time.Now().UnixNano())
	//阿里云sms短信服务会将开头的0优化掉,导致收到校验码不足6位
	//所以首个数字和后面五个分开处理
	randNumH := rand.Intn(9) + 1
	randNumL := fmt.Sprintf("%05d", rand.Intn(100000))
	randNum := strconv.Itoa(randNumH) + randNumL //go字符串拼接
	resp["randNum"] = randNum
	beego.Info(randNum)
	/*
		request := requests.NewCommonRequest()
		request.Method = "POST"
		request.Scheme = "https" // https | http
		request.Domain = "dysmsapi.aliyuncs.com"
		request.Version = "2017-05-25"
		request.ApiName = "SendSms"
		request.QueryParams["RegionId"] = "cn-hangzhou"
		request.QueryParams["PhoneNumbers"] = phone
		request.QueryParams["SignName"] = "品优购"
		request.QueryParams["TemplateCode"] = "SMS_164275022"
		request.QueryParams["TemplateParam"] = "{code:"+randNum+"}"

		response, err := client.ProcessCommonRequest(request)
		if err != nil {
			beego.Error("电话号码格式错误4")
			//2.给容器赋值
			resp["errno"] = 4
			resp["errmsg"] = "短信发送失败"
			return
		}

		//json数据解析
		var message Message
		json.Unmarshal(response.GetHttpContentBytes(),&message)
		if message.Message != "OK"{
			beego.Error("电话号码格式错误6")
			//2.给容器赋值
			resp["errno"] = 6
			resp["errmsg"] = message.Message
			return
		}

你可能感兴趣的:(go)