util.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package util
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "strings"
  8. "time"
  9. )
  10. var (
  11. mobileReg = regexp.MustCompile("(?i)(Android|Mobile|Phone)")
  12. )
  13. func CheckPlatform(r *http.Request) (p string) {
  14. p = "PC"
  15. if CheckIsMobile(r) {
  16. if CheckWxBrowser(r) {
  17. p = "WX"
  18. } else {
  19. p = "APP"
  20. }
  21. }
  22. return
  23. }
  24. // 判断是否是微信访问
  25. func CheckWxBrowser(Request *http.Request) bool {
  26. if strings.Index(Request.UserAgent(), "MicroMessenger") > -1 || strings.Index(Request.UserAgent(), "Wechat") > -1 {
  27. return true
  28. } else {
  29. return false
  30. }
  31. }
  32. // 是否是移动端
  33. func CheckIsMobile(r *http.Request) bool {
  34. client := r.UserAgent()
  35. if mobileReg.MatchString(client) {
  36. return true
  37. }
  38. return false
  39. }
  40. func GetFlag(r *http.Request, w http.ResponseWriter, limitFlag string) (string, bool) {
  41. if limitFlag == "" {
  42. c, _ := r.Cookie("limitSearchTextFlag")
  43. if c != nil {
  44. limitFlag = c.Value
  45. //if limitFlag == "" { 微服务无法获取Session 只从cookie里面获取
  46. // limitFlag, _ = s.Get("limitSearchTextFlag").(string)
  47. //}
  48. }
  49. }
  50. if limitFlag != "" {
  51. return limitFlag, true
  52. }
  53. limitFlag = MC.GetLetterRandom(5) + fmt.Sprint(time.Now().UnixNano())
  54. //s.Set("limitSearchTextFlag", limitFlag)
  55. c := &http.Cookie{
  56. Name: "limitSearchTextFlag",
  57. Value: limitFlag,
  58. Path: "/",
  59. HttpOnly: false,
  60. MaxAge: 2592000, //一个月
  61. }
  62. http.SetCookie(w, c)
  63. return limitFlag, false
  64. }