作者 yangfu

mod修改

... ... @@ -11,4 +11,5 @@ ENV GO111MODULE on
ENV GOPROXY https://goproxy.cn,direct
RUN ["go","mod","tidy"]
RUN ["go","build"]
EXPOSE 8080
ENTRYPOINT ["./partner"]
\ No newline at end of file
... ...
package svr
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/httplib"
"strconv"
"strings"
"time"
)
type httplibBaseServiceGateway struct {
baseURL string
connectTimeout time.Duration
readWriteTimeout time.Duration
}
func (serviceGateway *httplibBaseServiceGateway) createRequest(url string, method string) *httplib.BeegoHTTPRequest {
var request *httplib.BeegoHTTPRequest
switch method {
case "get":
request = httplib.Get(url)
break
case "post":
request = httplib.Post(url)
break
case "put":
request = httplib.Put(url)
break
case "delete":
request = httplib.Delete(url)
break
case "head":
request = httplib.Head(url)
break
default:
request = httplib.Get(url)
}
return request.SetTimeout(serviceGateway.connectTimeout, serviceGateway.readWriteTimeout)
}
func (serviceGateway *httplibBaseServiceGateway) responseHandle(response map[string]interface{}) (map[string]interface{}, error) {
data := make(map[string]interface{})
var err error
if code, ok := response["code"]; ok {
code := code.(float64)
if code == 0 {
data = response["data"].(map[string]interface{})
} else {
msg := response["msg"].(string)
data = response
err = fmt.Errorf(strings.Join([]string{strconv.FormatFloat(code, 'f', -1, 64), msg}, " "))
}
} else {
jsonBytes, marshalErr := json.Marshal(response)
if marshalErr != nil {
err = marshalErr
}
err = fmt.Errorf("无法解析的网关服务数据返回格式:%s", string(jsonBytes))
}
return data, err
}
... ...
package svr
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
"strings"
"time"
)
type HttplibMmmSmsApiServiceGateway struct {
httplibBaseServiceGateway
}
func (serviceGateway *HttplibMmmSmsApiServiceGateway) SendSms(phone string) (map[string]interface{}, error) {
url := strings.Join([]string{serviceGateway.baseURL, "service", "sendSms"}, "/")
request := serviceGateway.createRequest(url, "post")
options := make(map[string]interface{})
options["phone"] = strings.TrimSpace(phone)
request.JSONBody(options)
response := make(map[string]interface{})
request.ToJSON(&response)
data, err := serviceGateway.responseHandle(response)
return data, err
}
func (serviceGateway *HttplibMmmSmsApiServiceGateway) CheckSmsCode(phone string, code string) (map[string]interface{}, error) {
url := strings.Join([]string{serviceGateway.baseURL, "service", "checkSmsCode"}, "/")
request := serviceGateway.createRequest(url, "post")
options := make(map[string]interface{})
options["phone"] = phone
options["code"] = code
request.JSONBody(options)
response := make(map[string]interface{})
request.ToJSON(&response)
data, err := serviceGateway.responseHandle(response)
return data, err
}
func NewHttplibMmmSmsApiServiceGateway() *HttplibMmmSmsApiServiceGateway {
return &HttplibMmmSmsApiServiceGateway{
httplibBaseServiceGateway: httplibBaseServiceGateway{
baseURL: constant.MMM_SMS_SERVICE_HOST,
connectTimeout: 100 * time.Second,
readWriteTimeout: 30 * time.Second,
},
}
}
... ...