| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- /*
- @Time : 2019-06-26 9:31
- @Author : zr
- @Software: GoLand
- */
- package controller
- import (
- "fmt"
- "net/http"
- "strconv"
- "strings"
- "time"
- "video_course/errors"
- "video_course/model"
- "video_course/utils"
- "gitee.com/zr233/bsf"
- )
- type Controller interface {
- }
- type BaseController struct {
- *bsf.ControllerBase
- }
- type ResponseBase struct {
- Code errors.ErrorCode
- Memo string
- }
- func newResponseBase() ResponseBase {
- return ResponseBase{
- Code: errors.CodeSUCCESS,
- Memo: "执行成功",
- }
- }
- func (b BaseController) getSession() *model.Session {
- if s, ok := b.Ctx().Get("session"); ok {
- s, ok := s.(*model.Session)
- if ok {
- return s
- }
- }
- return nil
- }
- func (b *BaseController) postInt(key string) (value *int) {
- valueStr := b.Ctx().PostForm(key)
- if valueStr != "" {
- valueInt, err := strconv.Atoi(valueStr)
- if err != nil {
- panic(errors.NewParamErr(err))
- }
- value = &valueInt
- }
- return
- }
- func (b *BaseController) postIntNecessary(key string) (value int) {
- valueStr := b.Ctx().PostForm(key)
- var err error
- if valueStr != "" {
- value, err = strconv.Atoi(valueStr)
- if err != nil {
- panic(errors.FromParamErr(key, err))
- }
- } else {
- panic(errors.FromParamErr(key, fmt.Errorf("参数不能为空。")))
- }
- return
- }
- func (b *BaseController) postString(key string, necessary bool) string {
- str := b.Ctx().PostForm(key)
- if necessary && str == "" {
- err := errors.FromParamErr(key, fmt.Errorf("参数不能为空。"))
- panic(err)
- }
- return str
- }
- // 日期校验
- func (b *BaseController) getPostFromDate(key string) (value time.Time) {
- valueStr := b.Ctx().PostForm(key)
- value, e := time.Parse(utils.TimeFormatterDate(), valueStr)
- if e != nil {
- err := errors.FromParamErr(key, e)
- panic(err)
- }
- return value
- }
- func (b *BaseController) postFloatToInt(key string, necessary bool) int {
- str := b.Ctx().PostForm(key)
- if necessary && str == "" {
- err := errors.FromParamErr(key, fmt.Errorf("参数不能为空。"))
- panic(err)
- }
- wt, err := strconv.ParseFloat(str, 32)
- if err != nil {
- err := errors.FromParamErr(key, err)
- panic(err)
- }
- i := int(wt * 10) // 乘以10转int型,保留1位小数
- return i
- }
- // 返回json
- func (b BaseController) json(obj interface{}) {
- b.Ctx().JSON(http.StatusOK, obj)
- }
- // 获取tcp/ip
- func (b BaseController) getIp() string {
- ip := strings.TrimSpace(b.Ctx().GetHeader("X-Real-Ip"))
- //addr := b.Ctx().Request.RemoteAddr
- //if ip, _, err := net.SplitHostPort(strings.TrimSpace(addr)); err == nil {
- // return ip
- //}
- return ip
- }
|