util.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package utility
  2. import (
  3. util "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. "context"
  6. "fmt"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/net/ghttp"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "reflect"
  11. "regexp"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. )
  16. // ConversionMoney 金额格式化
  17. func ConversionMoney(i_money interface{}) string {
  18. if i_money == nil {
  19. return ""
  20. }
  21. m := ""
  22. if reflect.TypeOf(i_money).Name() == "float64" {
  23. m = strconv.FormatFloat(gconv.Float64(i_money), 'f', -1, 64)
  24. } else {
  25. m = gconv.String(i_money)
  26. }
  27. if m == "" {
  28. return m
  29. }
  30. m_arr := strings.Split(m, ".")
  31. m_1 := m_arr[0]
  32. len_m1 := len([]rune(m_1))
  33. if len_m1 >= 9 {
  34. m = m_1[0:len_m1-8] + "." + m_1[len_m1-8:len_m1-6] + "亿元"
  35. } else if len_m1 >= 5 {
  36. m = m_1[0:len_m1-4] + "." + m_1[len_m1-4:len_m1-2] + "万元"
  37. } else {
  38. if len(m_arr) == 1 {
  39. return m + ".00元"
  40. }
  41. m_2 := m_arr[1]
  42. if len([]rune(m_2)) > 1 {
  43. m_2 = m_2[0:2]
  44. } else {
  45. m_2 = m_2[0:1] + "0"
  46. }
  47. m = m_1 + "." + m_2 + "元"
  48. }
  49. return m
  50. }
  51. var mobileReg = regexp.MustCompile("(?i)(Android|Mobile|Phone)")
  52. func GetCommonRenderPath(agent, value string) string {
  53. if IsMobile(agent) {
  54. return fmt.Sprintf("mobile/%s", value)
  55. }
  56. return fmt.Sprintf("pc/%s", value)
  57. }
  58. var getCacheRenderFilePaths = getRenderFilePaths()
  59. // GetCommonRenderPaths 当移动端未找到模版,渲染pc模版
  60. func GetCommonRenderPaths(agent, filename string) []string {
  61. if IsMobile(agent) {
  62. return []string{fmt.Sprintf("mobile/%s", filename), fmt.Sprintf("pc/%s", filename)}
  63. }
  64. return []string{fmt.Sprintf("pc/%s", filename)}
  65. }
  66. func GetCommonRenderCachePaths(agent, value string) []string {
  67. return getCacheRenderFilePaths(IsMobile(agent), value)
  68. }
  69. func getRenderFilePaths() func(bool, string) []string {
  70. cacheMap := map[string]map[bool][]string{}
  71. lock := sync.Mutex{}
  72. return func(isMobile bool, filename string) []string {
  73. lock.Lock()
  74. defer lock.Unlock()
  75. if m, ok := cacheMap[filename]; ok && len(m) == 2 {
  76. return m[isMobile]
  77. }
  78. cacheMap[filename] = map[bool][]string{
  79. true: {fmt.Sprintf("mobile/%s", filename), fmt.Sprintf("pc/%s", filename)},
  80. false: {fmt.Sprintf("pc/%s", filename)},
  81. }
  82. return cacheMap[filename][isMobile]
  83. }
  84. }
  85. func IsMobile(agent string) bool {
  86. if mobileReg.MatchString(agent) {
  87. return true
  88. }
  89. return false
  90. }
  91. func EncodeId(sid string) string {
  92. if sid == "" || sid == "-" { //不存在的id为-
  93. return ""
  94. }
  95. return encrypt.EncodeArticleId2ByCheck(sid)
  96. }
  97. func DecodeId(eid string) string {
  98. if eid == "" {
  99. return ""
  100. }
  101. return encrypt.DecodeArticleId2ByCheck(eid)[0]
  102. }
  103. // JySessionLoginEd 是否已经登录
  104. func JySessionLoginEd(r *ghttp.Request) bool {
  105. val := r.Cookie.Get("SESSIONID")
  106. if val.IsNil() {
  107. return false
  108. }
  109. sessionId := val.String()
  110. if sessionId == "" {
  111. return false
  112. }
  113. if gVal, _ := g.Redis("session").Get(context.Background(), sessionId); !gVal.IsEmpty() {
  114. if gconv.String(gVal.Map()["userId"]) != "" {
  115. return true
  116. }
  117. }
  118. return false
  119. }
  120. func GetJySessionVal(r *ghttp.Request) (hasLogin bool, sessVal map[string]interface{}) {
  121. val := r.Cookie.Get("SESSIONID")
  122. if val.IsNil() {
  123. return
  124. }
  125. sessionId := val.String()
  126. if sessionId == "" {
  127. return
  128. }
  129. if gVal, _ := g.Redis("session").Get(context.Background(), sessionId); !gVal.IsEmpty() {
  130. sessVal = gVal.Map()
  131. if gconv.String(sessVal["userId"]) != "" {
  132. hasLogin = true
  133. return
  134. }
  135. }
  136. return
  137. }
  138. func StrLimitRune(str string, length int, suffix ...string) string {
  139. runes := []rune(str)
  140. if len(runes) < length {
  141. return str
  142. }
  143. suffixStr := "..."
  144. if len(suffix) > 0 {
  145. suffixStr = suffix[0]
  146. }
  147. return string(runes[0:length]) + suffixStr
  148. }
  149. // 金额转化 金额:0-万元以下单位为元 ,万元以上至亿元以下单位为万元 ,亿元以上单位为亿元。保留 小数点后 2 位,不进行四舍五入。
  150. func ConversionMoeny(i_money interface{}) string {
  151. m := ""
  152. if reflect.TypeOf(i_money).Name() == "float64" {
  153. m = strconv.FormatFloat(util.Float64All(i_money), 'f', -1, 64)
  154. } else {
  155. m = util.ObjToString(i_money)
  156. }
  157. if m == "" {
  158. return m
  159. }
  160. m_arr := strings.Split(m, ".")
  161. m_1 := m_arr[0]
  162. len_m1 := len([]rune(m_1))
  163. if len_m1 >= 9 {
  164. m = m_1[0:len_m1-8] + "." + m_1[len_m1-8:len_m1-6] + "亿元"
  165. } else if len_m1 >= 5 {
  166. m = m_1[0:len_m1-4] + "." + m_1[len_m1-4:len_m1-2] + "万元"
  167. } else {
  168. if len(m_arr) == 1 {
  169. return m + ".00元"
  170. }
  171. m_2 := m_arr[1]
  172. if len([]rune(m_2)) > 1 {
  173. m_2 = m_2[0:2]
  174. } else {
  175. m_2 = m_2[0:1] + "0"
  176. }
  177. m = m_1 + "." + m_2 + "元"
  178. }
  179. return m
  180. }