Create & Init Project...
This commit is contained in:
34
app/interface/bbq/wechat/internal/service/BUILD
Normal file
34
app/interface/bbq/wechat/internal/service/BUILD
Normal file
@ -0,0 +1,34 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["service.go"],
|
||||
importpath = "go-common/app/interface/bbq/wechat/internal/service",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/interface/bbq/wechat/dao:go_default_library",
|
||||
"//app/interface/bbq/wechat/internal/conf:go_default_library",
|
||||
"//app/interface/bbq/wechat/internal/model: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"],
|
||||
)
|
68
app/interface/bbq/wechat/internal/service/service.go
Normal file
68
app/interface/bbq/wechat/internal/service/service.go
Normal file
@ -0,0 +1,68 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"go-common/app/interface/bbq/wechat/dao"
|
||||
"go-common/app/interface/bbq/wechat/internal/conf"
|
||||
"go-common/app/interface/bbq/wechat/internal/model"
|
||||
"go-common/library/log"
|
||||
)
|
||||
|
||||
// Service struct
|
||||
type Service struct {
|
||||
c *conf.Config
|
||||
dao *dao.Dao
|
||||
}
|
||||
|
||||
// New init
|
||||
func New(c *conf.Config) (s *Service) {
|
||||
s = &Service{
|
||||
c: c,
|
||||
dao: dao.New(c),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Ping Service
|
||||
func (s *Service) Ping(ctx context.Context) (err error) {
|
||||
return s.dao.Ping(ctx)
|
||||
}
|
||||
|
||||
// Close Service
|
||||
func (s *Service) Close() {
|
||||
s.dao.Close()
|
||||
}
|
||||
|
||||
// TokenGet .
|
||||
func (s *Service) TokenGet(ctx context.Context, arg *model.TokenReq, url string) (sig string, err error) {
|
||||
token := ""
|
||||
|
||||
if token, err = s.dao.TokenGetLast(ctx); err != nil {
|
||||
log.Warnv(ctx, log.KV("log", "weixin token get last fail"))
|
||||
return
|
||||
}
|
||||
|
||||
if token == "" {
|
||||
if token, err = s.dao.TokenGet(ctx); err != nil {
|
||||
log.Warnv(ctx, log.KV("log", "weixin token get fail"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
sig = s.TokenCalc(ctx, token, arg.Noncestr, arg.Timestamp, url)
|
||||
return
|
||||
}
|
||||
|
||||
// TokenCalc 计算签名
|
||||
func (s *Service) TokenCalc(ctx context.Context, ticket string, noncestr string, timestamp string, url string) (sig string) {
|
||||
str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s", ticket, noncestr, timestamp, url)
|
||||
|
||||
hash := sha1.New()
|
||||
hash.Write([]byte(str))
|
||||
bs := hash.Sum(nil)
|
||||
|
||||
sig = fmt.Sprintf("%x", bs)
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user