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,55 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_library(
name = "go_default_library",
srcs = [
"alipay.go",
"antispam.go",
"geetest.go",
"realname.go",
],
importpath = "go-common/app/interface/main/account/dao/realname",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/account/conf:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["realname_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/account/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,91 @@
package realname
import (
"context"
"net/http"
"net/url"
"go-common/app/interface/main/account/conf"
"go-common/library/log"
"github.com/pkg/errors"
)
type respAlipay struct {
Code string `json:"code"`
Msg string `json:"msg"`
SubCode string `json:"sub_code"`
SubMsg string `json:"sub_msg"`
}
func (r *respAlipay) Error() error {
if r.Code == "10000" {
return nil
}
return errors.Errorf("alipay response failed , code : %s, msg : %s, sub_code : %s, sub_msg : %s", r.Code, r.Msg, r.SubCode, r.SubMsg)
}
// AlipayInit .
func (d *Dao) AlipayInit(c context.Context, param url.Values) (bizno string, err error) {
var (
req *http.Request
)
url := conf.Conf.Realname.Alipay.Gateway + "?" + param.Encode()
if req, err = http.NewRequest("GET", url, nil); err != nil {
err = errors.Wrapf(err, "http.NewRequest(GET,%s)", url)
return
}
var resp struct {
Resp struct {
respAlipay
Bizno string `json:"biz_no"`
} `json:"zhima_customer_certification_initialize_response"`
Sign string `json:"sign"`
}
if err = d.client.Do(c, req, &resp); err != nil {
return
}
log.Info("Realname alipay init \n\tparam : %+v \n\tresp : %+v", param, resp)
if err = resp.Resp.Error(); err != nil {
return
}
bizno = resp.Resp.Bizno
return
}
// AlipayQuery .
func (d *Dao) AlipayQuery(c context.Context, param url.Values) (pass bool, reason string, err error) {
var (
req *http.Request
)
url := conf.Conf.Realname.Alipay.Gateway + "?" + param.Encode()
if req, err = http.NewRequest("GET", url, nil); err != nil {
err = errors.Wrapf(err, "http.NewRequest(GET,%s)", url)
return
}
var resp struct {
Resp struct {
respAlipay
Passed string `json:"passed"`
FailedReason string `json:"failed_reason"`
IdentityInfo string `json:"identity_info"`
AttributeInfo string `json:"attribute_info"`
ChannelStatuses string `json:"channel_statuses"`
} `json:"zhima_customer_certification_query_response"`
Sign string `json:"sign"`
}
if err = d.client.Do(c, req, &resp); err != nil {
return
}
log.Info("Realname alipay query \n\tparam : %+v \n\tresp : %+v", param, resp)
if err = resp.Resp.Error(); err != nil {
return
}
if resp.Resp.Passed == "true" {
pass = true
} else {
pass = false
}
reason = resp.Resp.FailedReason
return
}

View File

@@ -0,0 +1,105 @@
package realname
import (
"context"
"fmt"
"go-common/app/interface/main/account/conf"
"go-common/library/cache/memcache"
"github.com/pkg/errors"
)
func alipayAntispamKey(mid int64) string {
return fmt.Sprintf("raa_%d", mid)
}
//AlipayAntispamValue 最低位为pass flag ,高位为计数
//计数:判断用户的申请次数
//flag:是否通过本次防刷验证(极验是否通过)
type AlipayAntispamValue int
// IncreaseCount add antispam count
func (a *AlipayAntispamValue) IncreaseCount() {
*a = AlipayAntispamValue((a.Count()+1)<<1 + a.Flag())
}
// SetPass set is antispam verified (such as when geetest passed)
func (a *AlipayAntispamValue) SetPass(pass bool) {
var flag int
if pass {
flag = 1
}
*a = AlipayAntispamValue(a.Count()<<1 + flag)
}
// Count return antispam hit count
func (a *AlipayAntispamValue) Count() int {
return int(*a) >> 1
}
// Flag return antispam pass flag
func (a *AlipayAntispamValue) Flag() int {
return int(*a) & 0x1
}
// Pass return is antispam passed (such as when geetest passed)
func (a *AlipayAntispamValue) Pass() bool {
return a.Flag() > 0
}
// AlipayAntispam get alipay antispam count by mid
func (d *Dao) AlipayAntispam(c context.Context, mid int64) (value *AlipayAntispamValue, err error) {
var (
key = alipayAntispamKey(mid)
conn = d.mc.Get(c)
item *memcache.Item
)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
value = new(AlipayAntispamValue)
if err = conn.Scan(item, &value); err != nil {
err = errors.Wrapf(err, "conn.Scan(%+v)", item)
return
}
return
}
// SetAlipayAntispam set alipay antispam count by mid
func (d *Dao) SetAlipayAntispam(c context.Context, mid int64, value *AlipayAntispamValue) (err error) {
var (
key = alipayAntispamKey(mid)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Set(&memcache.Item{Key: key, Object: value, Flags: memcache.FlagJSON, Expiration: conf.Conf.Realname.AlipayAntispamTTL}); err != nil {
err = errors.Wrapf(err, "conn.Set(%s,%+v)", key, value)
return
}
return
}
// DeleteAlipayAntispam delete alipay antispam count by mid
func (d *Dao) DeleteAlipayAntispam(c context.Context, mid int64) (err error) {
var (
key = alipayAntispamKey(mid)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Delete(%s)", key)
return
}
return
}

View File

@@ -0,0 +1,79 @@
package realname
import (
"context"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"go-common/app/interface/main/account/conf"
"go-common/library/log"
"go-common/library/net/metadata"
"github.com/pkg/errors"
)
// RealnameCaptchaGTRegister register a geetest apply
func (d *Dao) RealnameCaptchaGTRegister(c context.Context, mid int64, ip, clientType string, newCaptcha int) (challenge string, err error) {
var (
params = url.Values{}
req *http.Request
url = conf.Conf.Realname.Geetest.RegisterURL
)
params.Set("user_id", strconv.FormatInt(mid, 10))
params.Set("new_captcha", strconv.Itoa(newCaptcha))
params.Set("ip_address", ip)
params.Set("client_type", clientType)
params.Set("gt", conf.Conf.Realname.Geetest.CaptchaID)
if req, err = http.NewRequest("GET", url+"?"+params.Encode(), nil); err != nil {
err = errors.Wrapf(err, "dao.RealnameCaptchaGTRegister url(%s) params(%s)", url, params.Encode())
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var challengeBytes []byte
if challengeBytes, err = d.client.Raw(c, req); err != nil {
return
}
challenge = string(challengeBytes)
if len(challenge) != 32 {
log.Error("dao.RealnameCaptchaGTRegister challenge : %s ,length not equate 32bit", challenge)
}
return
}
// RealnameCaptchaGTRegisterValidate recheck the challenge code and get to seccode
func (d *Dao) RealnameCaptchaGTRegisterValidate(c context.Context, challenge, seccode, clientType, ip, captchaID string, mid int64) (realSeccode string, err error) {
var (
params = url.Values{}
req *http.Request
url = conf.Conf.Realname.Geetest.ValidateURL
)
params.Set("seccode", seccode)
params.Set("challenge", challenge)
params.Set("captchaid", captchaID)
params.Set("client_type", clientType)
params.Set("ip_address", metadata.String(c, metadata.RemoteIP))
params.Set("json_format", "1")
params.Set("sdk", "golang_3.0.0")
params.Set("user_id", strconv.FormatInt(mid, 10))
params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
log.Info("gt validate url : %s , params : %s", url, params.Encode())
if req, err = http.NewRequest("POST", url, strings.NewReader(params.Encode())); err != nil {
err = errors.Wrapf(err, "dao.RealnameCaptchaGTRegister url(%s) params(%s)", url, params.Encode())
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var resp struct {
Seccode string `json:"seccode"`
}
if err = d.client.Do(c, req, &resp); err != nil {
return
}
realSeccode = resp.Seccode
return
}

View File

@@ -0,0 +1,60 @@
package realname
import (
"context"
"fmt"
"net/url"
"go-common/app/interface/main/account/conf"
"go-common/library/cache/memcache"
"go-common/library/ecode"
bm "go-common/library/net/http/blademaster"
"github.com/pkg/errors"
)
var (
telInfoURI = "/intranet/acc/telInfo/mid"
)
// Dao dao
type Dao struct {
c *conf.Config
client *bm.Client
mc *memcache.Pool
}
// New new
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
client: bm.NewClient(c.HTTPClient.Normal),
mc: memcache.NewPool(c.AccMemcache),
}
return
}
// TelInfo tel info.
func (d *Dao) TelInfo(c context.Context, mid int64) (tel string, err error) {
params := url.Values{}
params.Set("mid", fmt.Sprintf("%d", mid))
var resp struct {
Code int `json:"code"`
Data struct {
Mid int64 `json:"mid"`
Tel string `json:"tel"`
JoinIP string `json:"join_ip"`
JoinTime int64 `json:"join_time"`
} `json:"data"`
}
if err = d.client.Get(c, d.c.Host.Passport+telInfoURI, "", params, &resp); err != nil {
err = errors.Errorf("realname TelInfo d.httpClient.Do() error(%+v)", err)
return
}
if resp.Code != 0 {
err = errors.Errorf("realname TelInfo url(%s) res(%+v) err(%+v)", telInfoURI+"?"+params.Encode(), resp, ecode.Int(resp.Code))
return
}
tel = resp.Data.Tel
return
}

View File

@@ -0,0 +1,50 @@
package realname
import (
"context"
"flag"
"testing"
"go-common/app/interface/main/account/conf"
"github.com/smartystreets/goconvey/convey"
)
var d *Dao
func init() {
flag.Parse()
flag.Set("conf", "../../cmd/account-interface-example.toml")
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
}
func TestTelInfo(t *testing.T) {
convey.Convey("ReplyHistoryList", t, func() {
res, err := d.TelInfo(context.TODO(), 46333)
convey.So(err, convey.ShouldBeNil)
convey.So(res, convey.ShouldNotBeNil)
})
}
func TestAntispam(t *testing.T) {
convey.Convey("antispam", t, func() {
anti := new(AlipayAntispamValue)
anti.IncreaseCount()
c := anti.Count()
convey.So(c, convey.ShouldEqual, 1)
p := anti.Pass()
convey.So(p, convey.ShouldBeFalse)
anti.IncreaseCount()
c = anti.Count()
convey.So(c, convey.ShouldEqual, 2)
anti.SetPass(true)
p = anti.Pass()
convey.So(p, convey.ShouldBeTrue)
})
}