mergefilter.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package filter
  2. import (
  3. "fmt"
  4. "app.yhyue.com/moapp/jypkg/jyutil"
  5. "log"
  6. "net/http"
  7. "strings"
  8. "app.yhyue.com/moapp/jybase/redis"
  9. "app.yhyue.com/moapp/jybase/mongodb"
  10. "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
  11. )
  12. //用户合并删除用户检测
  13. type mergeFilter struct {
  14. W http.ResponseWriter
  15. R *http.Request
  16. Session *httpsession.Session
  17. GetSession map[string]interface{}
  18. }
  19. func (l *mergeFilter) Do() bool {
  20. if uid := l.GetSession["userId"]; uid != nil && uid != "" {
  21. if val := redis.Get("session", fmt.Sprintf("usermerge_delete_%s", uid)); val != nil {
  22. if sUid, ok := val.(string); sUid != "" && ok { //自动更新当前session为合并后账户
  23. if _, sessionVal := jyutil.GetSessionVal(map[string]interface{}{"_id": mongodb.StringTOBsonId(sUid)}); len(sessionVal) > 0 {
  24. log.Printf("账户合并 mergeFilter 自动刷新session old:%s new:%s\n", uid, sUid)
  25. l.Session.SetMultiple(sessionVal)
  26. return true
  27. }
  28. }
  29. if l.R.Method == "GET" {
  30. sessId := l.Session.Id()
  31. redis.Del("session", sessId)
  32. if strings.HasPrefix(l.R.RequestURI, "/front/sess/") {
  33. http.Redirect(l.W, l.R, l.R.RequestURI, 302)
  34. } else {
  35. http.Redirect(l.W, l.R, "/", 302)
  36. }
  37. return false
  38. }
  39. }
  40. }
  41. //微信解绑-刷新session
  42. if sod, uid := l.GetSession["s_m_openid"], l.GetSession["userId"]; sod != nil && sod != "" && uid != nil && uid != "" {
  43. if mergerTimeStamp := redis.GetStr("session", fmt.Sprintf("accountInfo_unbindWx_%s_%s", uid, sod)); mergerTimeStamp != "" {
  44. //每个sessionid仅清除一次
  45. if doOncekey := fmt.Sprintf("accountInfo_unbindWx_%s_%s_%s_%s", uid, sod, l.Session.Id(), mergerTimeStamp); redis.Get("session", doOncekey) == nil {
  46. //删除企业相关session
  47. for _, entSessionKey := range []string{"entId", "entName", "entUserId", "frameworkEntId", "frameworkEntName"} {
  48. l.Session.Del(entSessionKey)
  49. }
  50. redis.Put("session", doOncekey, 1, 60*60*24)
  51. _, sessionVal := jyutil.GetSessionVal(map[string]interface{}{"s_m_openid": sod})
  52. if len(sessionVal) > 0 {
  53. l.Session.SetMultiple(sessionVal)
  54. }
  55. }
  56. }
  57. }
  58. return true
  59. }