/** * @auther jream.lu * @intro 入参验证 * @logic * @todo 返回值 * @meta meta map[string][]string rawMetaHeader * @data data ...interface{} 切片指针 rawDataBody * @return 返回 true, metaMap, error */ func InputParamsCheck(meta map[string][]string, data ...interface{}) (result Result, err error) { //MetaHeader check metaCheckResult, err := MetaHeaderCheck(meta) if err != nil { return metaCheckResult, err } //DataParams check valid := validation.Validation{} for _, val := range data { is, err := valid.Valid(val) //日志 //检查参数 if err != nil { // handle error log.Println(i18n.Tr(global.Lang, "outputParams.SYSTEMILLEGAL"), err) } if !is { for _, err := range valid.Errors { log.Println(i18n.Tr(global.Lang, "outputParams.DATAPARAMSILLEGAL"), err.Key, ":", err.Message) result.MetaCheckResult = nil result.RequestID = metaCheckResult.MetaCheckResult["request-id"] result.Message = i18n.Tr(global.Lang, "outputParams.DATAPARAMSILLEGAL") + " " + err.Key + ":" + err.Message return result, errors.New(i18n.Tr(global.Lang, "outputParams.DATAPARAMSILLEGAL")) } } } return metaCheckResult, nil }
func (this *BaseController) ValidForm(form interface{}) bool { log.Println("ValidForm") valid := validation.Validation{} ok, _ := valid.Valid(form) if !ok { for _, err := range valid.Errors { /* set error message */ slice := strings.Split(err.Key, ".") name := slice[0] + "Error" message := "valid." + err.Message this.Data[name] = i18n.Tr(this.Locale.Lang, message) log.Println(name) log.Println(message) log.Println(this.Locale.Lang) log.Println(i18n.Tr(this.Locale.Lang, message)) return false } } return true }
func timesince(lang string, t time.Time) string { now := time.Now() seonds := int(now.Sub(t).Seconds()) if seonds < 60 { return i18n.Tr(lang, "seconds_ago", seonds) } else if seonds < 60*60 { return i18n.Tr(lang, "minutes_ago", seonds/60) } else if seonds < 60*60*24 { return i18n.Tr(lang, "hours_ago", seonds/(60*60)) } else { return i18n.Tr(lang, "days_ago", seonds/(60*60*24)) } }
func getbulletintype(lang string, t int) string { var typeStr string switch t { case setting.BULLETIN_FRIEND_LINK: typeStr = i18n.Tr(lang, "model.bulletin_friend_link") case setting.BULLETIN_MOBILE_APP: typeStr = i18n.Tr(lang, "model.bulletin_mobile_app") case setting.BULLETIN_NEW_COMER: typeStr = i18n.Tr(lang, "model.bulletin_new_comer") case setting.BULLETIN_OPEN_SOURCE: typeStr = i18n.Tr(lang, "model.bulletin_open_source") } return typeStr }
func timesince(lang string, t time.Time) string { seconds := int(time.Since(t).Seconds()) switch { case seconds < 60: return i18n.Tr(lang, "seconds_ago", seconds) case seconds < 60*60: return i18n.Tr(lang, "minutes_ago", seconds/60) case seconds < 60*60*24: return i18n.Tr(lang, "hours_ago", seconds/(60*60)) case seconds < 60*60*24*100: return i18n.Tr(lang, "days_ago", seconds/(60*60*24)) default: return beego.Date(t, setting.DateFormat) } }
func (this *LoginController) Post() { this.TplNames = "user/login.html" var user LoginForm this.ParseForm(&user) log.Println("Login UserName: "******"Login Password: "******"Login Remember: ", user.Remember) if this.ValidForm(&user) == false { return } if models.VerifyUser(user.UserName, user.Password) { log.Println("login Success") // set login session this.SetSession("login_user", user.UserName) this.Redirect("/", 302) return } else { this.Data["Error"] = i18n.Tr(this.Locale.Lang, "auth.sign_connect_error") } return }
func (this *BaseController) Lang(key string) string { lang := this.Ctx.GetCookie("lang") if len(lang) == 0 { this.Ctx.SetCookie("lang", "zh-CN", 1<<31-1, "/") lang = "zh-CN" } LANG = lang return i18n.Tr(lang, key) }
/** * @auther jream.lu * @intro 出参成功 * @logic * @todo 返回值 * @params params ...interface{} 切片指针 * @return ? */ func OutputSuccess(data interface{}, requestID string) Output { var op Output op.Meta.RequestId = requestID op.Meta.UpdatedAt = time.Now().Format("2006-01-02 15:04:05") op.StatusCode = SUCCESS op.Message = i18n.Tr(global.Lang, "outputParams.SUCCESS") op.Data = data return op }
func i18nGetString(key string) string { return i18n.Tr(controllers.LANG, key) }
//帮助 func (this *Main) Help() { this.seo.Title = i18n.Tr(this.Lang, "help") this.setTplNames("help") }
// get HTML i18n string func i18nHTML(lang, format string, args ...interface{}) template.HTML { return template.HTML(i18n.Tr(lang, format, args...)) }
/** * meta参数验证 * 1.map转json * 2.json转slice * 3.解析到struct * 4.将header 放入map 返回 * * @meta meta map[string][]string header信息 map格式 */ func MetaHeaderCheck(meta map[string][]string) (result Result, err error) { rawMetaHeader, _ := ffjson.Marshal(meta) beego.Trace("入参meta:" + string(rawMetaHeader)) var metaHeader MetaHeader ffjson.Unmarshal(rawMetaHeader, &metaHeader) //日志 fmt.Println("meta json解析:", metaHeader) for key, val := range meta { fmt.Println("meta 解析", key, ":", val[0]) } valid := validation.Validation{} is, err := valid.Valid(&metaHeader) //日志 //Content-Type if val, ok := meta["Content-Type"]; ok { if val[0] != beego.AppConfig.String("Content-Type") { result.MetaCheckResult = nil result.Message = i18n.Tr(global.Lang, "outputParams.CONTENTTYPEILLEGAL") if val, ok := meta["request-id"]; ok { result.RequestID = val[0] } return result, errors.New(i18n.Tr(global.Lang, "outputParams.CONTENTTYPEILLEGAL ")) } } //检查参数 if err != nil { // handle error log.Println(i18n.Tr(global.Lang, "outputParams.SYSTEMILLEGAL"), err) } if !is { for _, err := range valid.Errors { log.Println(i18n.Tr(global.Lang, "outputParams.METAPARAMSILLEGAL"), err.Key, ":", err.Message) result.MetaCheckResult = nil result.Message = i18n.Tr(global.Lang, "outputParams.METAPARAMSILLEGAL") + " " + err.Key + ":" + err.Message if val, ok := meta["request-id"]; ok { result.RequestID = val[0] } return result, errors.New(i18n.Tr(global.Lang, "outputParams.METAPARAMSILLEGAL ")) } } //把meta参数放入新的struct 返回 var metaMap = make(map[string]string) for key, val := range meta { metaMap[key] = val[0] } //日志 if len(metaMap["request-id"]) == 0 { metaMap["request-id"] = getRequestID() } result.MetaCheckResult = metaMap result.Message = "" result.RequestID = "" return result, nil }
func init() { beego.AddFuncMap("i18n", i18n.Tr) beego.AddFuncMap("i18nja", func(format string, args ...interface{}) string { return i18n.Tr("ja-JP", format, args...) }) beego.AddFuncMap("datenow", func(format string) string { return time.Now().Add(time.Duration(9) * time.Hour).Format(format) }) beego.AddFuncMap("dateformatJst", func(in time.Time) string { in = in.Add(time.Duration(9) * time.Hour) return in.Format("2006/01/02 15:04") }) beego.AddFuncMap("qescape", func(in string) string { return url.QueryEscape(in) }) beego.AddFuncMap("nl2br", func(in string) string { return strings.Replace(in, "\n", "<br>", -1) }) beego.AddFuncMap("tostr", func(in interface{}) string { return convert.ToStr(reflect.ValueOf(in).Interface()) }) beego.AddFuncMap("first", func(in interface{}) interface{} { return reflect.ValueOf(in).Index(0).Interface() }) beego.AddFuncMap("last", func(in interface{}) interface{} { s := reflect.ValueOf(in) return s.Index(s.Len() - 1).Interface() }) beego.AddFuncMap("truncate", func(in string, length int) string { return runewidth.Truncate(in, length, "...") }) beego.AddFuncMap("noname", func(in string) string { if in == "" { return "(未入力)" } return in }) beego.AddFuncMap("cleanurl", func(in string) string { return strings.Trim(strings.Trim(in, " "), " ") }) beego.AddFuncMap("append", func(data map[interface{}]interface{}, key string, value interface{}) template.JS { if _, ok := data[key].([]interface{}); !ok { data[key] = []interface{}{value} } else { data[key] = append(data[key].([]interface{}), value) } return template.JS("") }) beego.AddFuncMap("appendmap", func(data map[interface{}]interface{}, key string, name string, value interface{}) template.JS { v := map[string]interface{}{name: value} if _, ok := data[key].([]interface{}); !ok { data[key] = []interface{}{v} } else { data[key] = append(data[key].([]interface{}), v) } return template.JS("") }) }
func (this *BaseController) Tr(format string) string { return i18n.Tr(Lang, format) }