refreshim.go 837 字节
package command

import (
	"fmt"
	"reflect"
	"strings"

	"github.com/beego/beego/v2/core/validation"
)

type RefreshIMCommand struct {
	// 手机号码
	Phone string `cname:"手机号码" json:"phone" valid:"Required"`
}

func (refreshIMCommand *RefreshIMCommand) Valid(validation *validation.Validation) {

}

func (refreshIMCommand *RefreshIMCommand) ValidateCommand() error {
	valid := validation.Validation{}
	b, err := valid.Valid(refreshIMCommand)
	if err != nil {
		return err
	}
	if !b {
		elem := reflect.TypeOf(refreshIMCommand).Elem()
		for _, validErr := range valid.Errors {
			field, isExist := elem.FieldByName(validErr.Field)
			if isExist {
				return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
			} else {
				return fmt.Errorf(validErr.Message)
			}
		}
	}
	return nil
}