This commit is contained in:
2021-12-20 19:17:24 +08:00
parent 179c6a3afa
commit 0df3603ef2
11 changed files with 470 additions and 27 deletions

45
user/cache.go Normal file
View File

@@ -0,0 +1,45 @@
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: cache
* @Date: 2021/12/20 6:39 下午
* @package: user
* @Version: v1.0.0
*
* @Description:
*
*/
package user
import (
"github.com/jageros/hawox/attribute"
"sync"
)
var users sync.Map
func LoadAllUserIntoCache() error {
attrs, err := attribute.LoadAll("user")
if err != nil {
return err
}
for _, attr := range attrs {
u := &User{attr: attr}
users.Store(attr.GetAttrID(), u)
}
return nil
}
func GetUser(openId string) (*User, error) {
u, ok := users.Load(openId)
if ok {
return u.(*User), nil
}
us, err := newUser(openId)
if err != nil {
return nil, err
}
users.Store(openId, us)
return us, nil
}

114
user/user.go Normal file
View File

@@ -0,0 +1,114 @@
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: user
* @Date: 2021/12/20 4:54 下午
* @package: user
* @Version: v1.0.0
*
* @Description:
*
*/
package user
import (
"github.com/jageros/hawox/attribute"
"time"
)
type User struct {
attr *attribute.AttrMgr
}
func newUser(openId string) (*User, error) {
u := &User{
attr: attribute.NewAttrMgr("user", openId),
}
err := u.attr.Load(true)
if err == attribute.NotExistsErr {
u.attr.SetInt64("create_time", time.Now().Unix())
err = u.attr.Save(false)
}
return u, err
}
func (u *User) OpenID() string {
return u.attr.GetAttrID().(string)
}
func (u *User) Codes(isFund bool) []string {
key := "stock"
if isFund {
key = "fund"
}
attr := u.attr.GetMapAttr(key)
if attr == nil {
return nil
}
var codes []string
attr.ForEachKey(func(key string) bool {
codes = append(codes, key)
return true
})
return codes
}
// HasSubscribed 查询用户是否订阅此票
func (u *User) HasSubscribed(isFund bool, code string) bool {
key := "stock"
if isFund {
key = "fund"
}
attr := u.attr.GetMapAttr(key)
if attr == nil {
return false
}
sAttr := attr.GetMapAttr(code)
if sAttr == nil {
return false
}
return sAttr.GetBool("notify")
}
// Subscribe 订阅股票或基金
func (u *User) Subscribe(isFund bool, codes ...string) {
key := "stock"
if isFund {
key = "fund"
}
attr := u.attr.GetMapAttr(key)
if attr == nil {
attr = attribute.NewMapAttr()
u.attr.SetMapAttr(key, attr)
}
for _, code := range codes {
sAttr := attr.GetMapAttr(code)
if sAttr == nil {
sAttr = attribute.NewMapAttr()
attr.SetMapAttr(code, sAttr)
}
sAttr.SetBool("notify", true)
}
}
// UnSubscribe 取消订阅股票或基金
func (u *User) UnSubscribe(isFund bool, codes ...string) {
key := "stock"
if isFund {
key = "fund"
}
attr := u.attr.GetMapAttr(key)
if attr == nil {
return
}
for _, code := range codes {
sAttr := attr.GetMapAttr(code)
if sAttr == nil {
return
}
sAttr.SetBool("notify", false)
}
}