package utility import ( util "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" "context" "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/util/gconv" "reflect" "regexp" "strconv" "strings" "sync" ) // ConversionMoney 金额格式化 func ConversionMoney(i_money interface{}) string { if i_money == nil { return "" } m := "" if reflect.TypeOf(i_money).Name() == "float64" { m = strconv.FormatFloat(gconv.Float64(i_money), 'f', -1, 64) } else { m = gconv.String(i_money) } if m == "" { return m } m_arr := strings.Split(m, ".") m_1 := m_arr[0] len_m1 := len([]rune(m_1)) if len_m1 >= 9 { m = m_1[0:len_m1-8] + "." + m_1[len_m1-8:len_m1-6] + "亿元" } else if len_m1 >= 5 { m = m_1[0:len_m1-4] + "." + m_1[len_m1-4:len_m1-2] + "万元" } else { if len(m_arr) == 1 { return m + ".00元" } m_2 := m_arr[1] if len([]rune(m_2)) > 1 { m_2 = m_2[0:2] } else { m_2 = m_2[0:1] + "0" } m = m_1 + "." + m_2 + "元" } return m } var mobileReg = regexp.MustCompile("(?i)(Android|Mobile|Phone)") func GetCommonRenderPath(agent, value string) string { if IsMobile(agent) { return fmt.Sprintf("mobile/%s", value) } return fmt.Sprintf("pc/%s", value) } var getCacheRenderFilePaths = getRenderFilePaths() // GetCommonRenderPaths 当移动端未找到模版,渲染pc模版 func GetCommonRenderPaths(agent, filename string) []string { if IsMobile(agent) { return []string{fmt.Sprintf("mobile/%s", filename), fmt.Sprintf("pc/%s", filename)} } return []string{fmt.Sprintf("pc/%s", filename)} } func GetCommonRenderCachePaths(agent, value string) []string { return getCacheRenderFilePaths(IsMobile(agent), value) } func getRenderFilePaths() func(bool, string) []string { cacheMap := map[string]map[bool][]string{} lock := sync.Mutex{} return func(isMobile bool, filename string) []string { lock.Lock() defer lock.Unlock() if m, ok := cacheMap[filename]; ok && len(m) == 2 { return m[isMobile] } cacheMap[filename] = map[bool][]string{ true: {fmt.Sprintf("mobile/%s", filename), fmt.Sprintf("pc/%s", filename)}, false: {fmt.Sprintf("pc/%s", filename)}, } return cacheMap[filename][isMobile] } } func IsMobile(agent string) bool { if mobileReg.MatchString(agent) { return true } return false } func EncodeId(sid string) string { if sid == "" || sid == "-" { //不存在的id为- return "" } return encrypt.EncodeArticleId2ByCheck(sid) } func DecodeId(eid string) string { if eid == "" { return "" } return encrypt.DecodeArticleId2ByCheck(eid)[0] } // JySessionLoginEd 是否已经登录 func JySessionLoginEd(r *ghttp.Request) bool { val := r.Cookie.Get("SESSIONID") if val.IsNil() { return false } sessionId := val.String() if sessionId == "" { return false } if gVal, _ := g.Redis("session").Get(context.Background(), sessionId); !gVal.IsEmpty() { if gconv.String(gVal.Map()["userId"]) != "" { return true } } return false } func GetJySessionVal(r *ghttp.Request) (hasLogin bool, sessVal map[string]interface{}) { val := r.Cookie.Get("SESSIONID") if val.IsNil() { return } sessionId := val.String() if sessionId == "" { return } if gVal, _ := g.Redis("session").Get(context.Background(), sessionId); !gVal.IsEmpty() { sessVal = gVal.Map() if gconv.String(sessVal["userId"]) != "" { hasLogin = true return } } return } func StrLimitRune(str string, length int, suffix ...string) string { runes := []rune(str) if len(runes) < length { return str } suffixStr := "..." if len(suffix) > 0 { suffixStr = suffix[0] } return string(runes[0:length]) + suffixStr } // 金额转化 金额:0-万元以下单位为元 ,万元以上至亿元以下单位为万元 ,亿元以上单位为亿元。保留 小数点后 2 位,不进行四舍五入。 func ConversionMoeny(i_money interface{}) string { m := "" if reflect.TypeOf(i_money).Name() == "float64" { m = strconv.FormatFloat(util.Float64All(i_money), 'f', -1, 64) } else { m = util.ObjToString(i_money) } if m == "" { return m } m_arr := strings.Split(m, ".") m_1 := m_arr[0] len_m1 := len([]rune(m_1)) if len_m1 >= 9 { m = m_1[0:len_m1-8] + "." + m_1[len_m1-8:len_m1-6] + "亿元" } else if len_m1 >= 5 { m = m_1[0:len_m1-4] + "." + m_1[len_m1-4:len_m1-2] + "万元" } else { if len(m_arr) == 1 { return m + ".00元" } m_2 := m_arr[1] if len([]rune(m_2)) > 1 { m_2 = m_2[0:2] } else { m_2 = m_2[0:1] + "0" } m = m_1 + "." + m_2 + "元" } return m }