Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["dao_test.go"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/job/main/up/conf:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"account_cache.go",
"dao.go",
"rpc.go",
],
importpath = "go-common/app/job/main/up/dao/account",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/up/conf:go_default_library",
"//app/service/main/account/model:go_default_library",
"//app/service/main/account/rpc/client:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,76 @@
package account
import (
"context"
"sync"
"time"
account "go-common/app/service/main/account/model"
"go-common/library/log"
)
var (
infoCache = make(map[int64]*account.Info)
nextClearCacheTime time.Time
lock = sync.Mutex{}
)
const (
cacheClearInterval = 60 * time.Minute
)
//GetCachedInfos get cache info
func (d *Dao) GetCachedInfos(c context.Context, mids []int64, ip string) (infos map[int64]*account.Info, err error) {
d.checkClearCache()
var needFindMids []int64
infos = make(map[int64]*account.Info)
lock.Lock()
for _, v := range mids {
var info, ok = infoCache[v]
if !ok {
needFindMids = append(needFindMids, v)
continue
}
log.Info("hit cache! mid=%d", v)
infos[v] = info
}
lock.Unlock()
if len(needFindMids) == 0 {
return
}
var findinfo, e = d.Infos(c, needFindMids, ip)
err = e
if e != nil {
log.Error("try get uid info fail, err=%v", e)
return
}
lock.Lock()
for k, v := range findinfo {
infos[k] = v
infoCache[k] = v
}
lock.Unlock()
return
}
func (d *Dao) checkClearCache() {
var now = time.Now()
if now.Before(nextClearCacheTime) {
return
}
d.clearCache()
}
func (d *Dao) clearCache() {
nextClearCacheTime = time.Now().Add(cacheClearInterval)
if len(infoCache) == 0 {
return
}
lock.Lock()
infoCache = make(map[int64]*account.Info)
lock.Unlock()
}

View File

@@ -0,0 +1,70 @@
package account
import (
"context"
"go-common/app/job/main/up/conf"
"go-common/app/service/main/account/model"
account "go-common/app/service/main/account/rpc/client"
"go-common/library/ecode"
"go-common/library/log"
)
// Dao dao is account dao.
type Dao struct {
// config
c *conf.Config
// rpc
acc *account.Service3
}
// New new a account dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// config
c: c,
// rpc
acc: account.New3(c.AccountRPC),
}
return
}
// Close close resource.
func (d *Dao) Close() {
}
// Ping ping success.
func (d *Dao) Ping(c context.Context) (err error) {
return
}
// IdentifyInfo 获取用户实名认证状态
func (d *Dao) IdentifyInfo(c context.Context, ak, ck, ip string, mid int64) (err error) {
var mf *model.Profile
if mf, err = d.Profile(c, mid, ip); err != nil {
log.Error("d.Profile mid(%d),ip(%s),error(%v)", mid, ip, err)
err = ecode.CreativeAccServiceErr
return
}
if mf.Identification == 1 {
return
}
//switch for FrontEnd return json format, return OldPhone, and newError
if err = d.switchIDInfoRet(mf.TelStatus); err != nil {
log.Error("switchIDInfoRet url(%s) res(%v)", mf.TelStatus)
return
}
return
}
func (d *Dao) switchIDInfoRet(phoneRet int32) (err error) {
switch phoneRet {
case 0:
err = ecode.UserCheckNoPhone
case 1:
err = nil
case 2:
err = ecode.UserCheckInvalidPhone
}
return
}

View File

@@ -0,0 +1,62 @@
package account
import (
"context"
"flag"
"path/filepath"
"testing"
"go-common/library/ecode"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/job/main/up/conf"
)
var (
d *Dao
)
func init() {
dir, _ := filepath.Abs("../../cmd/up-admin.toml")
flag.Set("conf", dir)
conf.Init()
d = New(conf.Conf)
}
func WithDao(f func(d *Dao)) func() {
return func() {
Reset(func() {})
f(d)
}
}
func Test_IdentifyInfo(t *testing.T) {
var (
c = context.Background()
err error
mid = int64(27515256)
ak = "efbf2e093c3c04008acbd9906ba970db"
ck = ""
ip = "127.0.0.1"
)
Convey("IdentifyInfo", t, WithDao(func(d *Dao) {
err = d.IdentifyInfo(c, ak, ck, ip, mid)
So(err, ShouldNotBeNil)
So(err, ShouldEqual, ecode.CreativeAccServiceErr)
}))
}
func Test_GetCachedInfos(t *testing.T) {
var (
c = context.Background()
err error
mids = []int64{1234, 12345}
ip = "127.0.0.1"
)
Convey("GetCachedInfos", t, WithDao(func(d *Dao) {
_, err = d.GetCachedInfos(c, mids, ip)
So(err, ShouldNotBeNil)
}))
}

View File

@@ -0,0 +1,33 @@
package account
import (
"context"
account "go-common/app/service/main/account/model"
"go-common/library/ecode"
"go-common/library/log"
)
// Profile get profile from rpc
func (d *Dao) Profile(c context.Context, mid int64, ip string) (p *account.Profile, err error) {
arg := &account.ArgMid{
Mid: mid,
}
if p, err = d.acc.Profile3(c, arg); err != nil {
log.Error("d.acc.Profile3 error(%v) | mid(%d) ip(%s) arg(%v)", err, mid, ip, arg)
err = ecode.CreativeAccServiceErr
}
return
}
//Infos get up infos
func (d *Dao) Infos(c context.Context, mids []int64, ip string) (infos map[int64]*account.Info, err error) {
arg := &account.ArgMids{
Mids: mids,
}
if infos, err = d.acc.Infos3(c, arg); err != nil {
log.Error("d.acc.info3 error(%v) arg(%v)", err, arg)
err = ecode.CreativeAccServiceErr
}
return
}