2020-07-10 10:36:35 +08:00
|
|
|
package response
|
2021-08-03 15:58:35 +08:00
|
|
|
|
|
|
|
import "reflect"
|
|
|
|
|
|
|
|
type JsonResponseGenerator struct {
|
|
|
|
Interface JsonInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JsonResponseGenerator) NewJsonByDefaultSuccess(data ...interface{}) JsonInterface {
|
|
|
|
return j.NewJson(true, 0, JsonMsgDefaultOk, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JsonResponseGenerator) NewJsonByDefaultFailed(data ...interface{}) JsonInterface {
|
|
|
|
return j.NewJson(false, 1, JsonMsgDefaultFailed, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JsonResponseGenerator) NewJonsByFailed(code int, msg string) JsonInterface {
|
|
|
|
return j.NewJson(false, code, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JsonResponseGenerator) NewJson(status bool, code int, msg string, data ...interface{}) JsonInterface {
|
|
|
|
jPtr := reflect.New(reflect.TypeOf(j.Interface).Elem())
|
|
|
|
if b, ok := jPtr.Interface().(JsonInterface); ok {
|
|
|
|
b.SetStatus(status)
|
|
|
|
b.SetCode(code)
|
|
|
|
b.SetMessage(msg)
|
|
|
|
if len(data) > 0 {
|
|
|
|
b.SetData(data[0])
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|