httplib_ucenter_service_gateway.go 4.0 KB
package svr

import (
	"fmt"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
	"strconv"
	"strings"
	"time"
)

type HttplibUCenterApiServiceGateway struct {
	httplibBaseServiceGateway
}

// 服务登录
func (serviceGateway *HttplibUCenterApiServiceGateway) ServerLogin(phone, password string, loginType int) (int64, error) {
	url := strings.Join([]string{serviceGateway.baseURL, "auth", "serverLogin"}, "/")
	request := serviceGateway.createRequest(url, "post")
	request.Header("appKey", constant.UCENTER_APP_KEY)
	options := make(map[string]interface{})
	options["phone"] = strings.TrimSpace(phone)
	options["password"] = strings.TrimSpace(password)
	options["type"] = loginType
	request.JSONBody(options)
	response := make(map[string]interface{})
	err := request.ToJSON(&response)
	var openid int64
	if err != nil {
		log.Error("Service Gateway Fail:", err)
		return 0, err
	}
	if data, ok := response["data"]; ok {
		if data, ok := data.(map[string]interface{}); ok {
			if data, ok := data["id"]; ok {
				openid = int64(data.(float64))
				//openid,err=strconv.ParseInt(fmt.Sprintf("%v",data.(float64)),10,64)
			}
		}
	}
	_, err = serviceGateway.handlerError(response)
	return openid, err
}

// 修改密码
func (serviceGateway *HttplibUCenterApiServiceGateway) UpdateUser(uid int64, phone, password string) (int, error) {
	url := strings.Join([]string{serviceGateway.baseURL, "users", fmt.Sprintf("%v", uid)}, "/")
	request := serviceGateway.createRequest(url, "put")
	//request.Header("appKey", constant.UCENTER_APP_KEY)
	options := make(map[string]interface{})
	if len(phone) > 0 {
		options["phone"] = strings.TrimSpace(phone)
	}
	if len(password) > 0 {
		options["password"] = strings.TrimSpace(password)
	}
	request.JSONBody(options)
	response := make(map[string]interface{})
	err := request.ToJSON(&response)
	if err != nil {
		log.Error("Service Gateway Fail:", err)
		return 0, err
	}
	return serviceGateway.handlerError(response)
}
func (serviceGateway *HttplibUCenterApiServiceGateway) ChangePassword(phone, newPwd, oldPwd string) (int, error) {
	url := strings.Join([]string{serviceGateway.baseURL, "users", "changePassword"}, "/")
	request := serviceGateway.createRequest(url, "post")
	request.Header("appKey", constant.UCENTER_APP_KEY)
	options := make(map[string]interface{})
	options["phone"] = strings.TrimSpace(phone)
	options["newPassword"] = strings.TrimSpace(newPwd)
	//options["confirmPwd"] = strings.TrimSpace(confirmPwd)
	options["password"] = strings.TrimSpace(oldPwd)
	request.JSONBody(options)
	response := make(map[string]interface{})
	err := request.ToJSON(&response)
	if err != nil {
		log.Error("Service Gateway Fail:", err)
		return 0, err
	}
	return serviceGateway.handlerError(response)
}

func (serviceGateway *HttplibUCenterApiServiceGateway) ChangePhone(newPhone, oldPhone string) (int, error) {
	url := strings.Join([]string{serviceGateway.baseURL, "user", "change-phone"}, "/")
	request := serviceGateway.createRequest(url, "post")
	options := make(map[string]interface{})
	options["phone"] = strings.TrimSpace(newPhone)
	options["oldPhone"] = strings.TrimSpace(oldPhone)
	request.JSONBody(options)
	response := make(map[string]interface{})
	err := request.ToJSON(&response)
	if err != nil {
		log.Error("Service Gateway Fail:", err)
		return 0, err
	}
	return serviceGateway.handlerError(response)
}

func (serviceGateway *HttplibUCenterApiServiceGateway) handlerError(in map[string]interface{}) (int, error) {
	var rspCode int
	var err error
	if code, ok := in["code"]; ok {
		rspCode, _ = strconv.Atoi(fmt.Sprintf("%v", code))
	} else {
		err = fmt.Errorf("网关解析错误")
	}
	if msg, ok := in["msg"]; ok {
		msg := msg.(string)
		if rspCode != 0 && len(msg) > 0 {
			err = fmt.Errorf(msg)
		}
	}
	return rspCode, err
}

func NewHttplibUCenterApiServiceGateway() *HttplibUCenterApiServiceGateway {
	return &HttplibUCenterApiServiceGateway{
		httplibBaseServiceGateway: httplibBaseServiceGateway{
			baseURL:          constant.UCENTER_SERVICE_HOST,
			connectTimeout:   100 * time.Second,
			readWriteTimeout: 30 * time.Second,
		},
	}
}