shenzhuan 2 years ago
parent 957ef3c89d
commit 914bb2ff02

@ -1,29 +0,0 @@
package common
import (
"github.com/opentracing/opentracing-go"
jaeger "github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"io"
"time"
)
/*
@Auth:ShenZ
@Description: Jaeger
*/
func NewTracer(serviceName string, addr string) (opentracing.Tracer, io.Closer, error) {
cfg := &config.Configuration{
ServiceName: serviceName,
Sampler: &config.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &config.ReporterConfig{
BufferFlushInterval: 1 * time.Second,
LogSpans: true,
LocalAgentHostPort: addr,
},
}
return cfg.NewTracer()
}

@ -1,107 +0,0 @@
package common
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"log"
"os"
"time"
"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func GetConsulConfig(url string, fileKey string) (*viper.Viper, error) {
conf := viper.New()
conf.AddRemoteProvider("consul", url, fileKey)
conf.SetConfigType("json")
err := conf.ReadRemoteConfig()
if err != nil {
log.Println("viper conf err :", err)
//}else {
//log.Println("viper conf :", conf)
}
return conf, nil
}
/**
{
"host": "192.168.137.131",
"port": "3306",
"user": "root",
"pwd": "mashibing123",
"database": "user_center"
}
**/
// type MySQLConfig struct {
// Host string `json:"host"`
// Post string `json:"port"`
// User string `json:"user"`
// Pwd string `json:"pwd"`
// Database string `json:"database"`
// }
//
// 获取 MySQL配置
func GetMysqlFromConsul(vip *viper.Viper) (db *gorm.DB, err error) {
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Info,
Colorful: true,
},
)
str := vip.GetString("user") + ":" + vip.GetString("pwd") + "@tcp(" + vip.GetString("host") + ":" + vip.GetString("port") + ")/" + vip.GetString("database") + "?charset=utf8mb4&parseTime=True&loc=Local"
db, errr := gorm.Open(mysql.Open(str), &gorm.Config{Logger: newLogger}) //"root:mashibing123@tcp(8.142.25.43:3306)/user_center?charset=utf8mb4&parseTime=True&loc=Local"), &gorm.Config{Logger: newLogger})
if errr != nil {
log.Println("db err :", errr)
}
return db, nil
}
/**
{
"addr": "192.168.100.131",
"password": "",
"DB": "0",
"poolSize": "30",
"minIdleConn": "30"
}
*/
// 获取redis 配置
func GetRedisFromConsul(vip *viper.Viper) (red *redis.Client, err error) {
red = redis.NewClient(&redis.Options{
Addr: vip.GetString("addr"),
Password: vip.GetString("password"),
DB: vip.GetInt("DB"),
PoolSize: vip.GetInt("poolSize"),
MinIdleConns: vip.GetInt("minIdleConn"),
})
//集群
clusterClients := redis.NewClusterClient(
&redis.ClusterOptions{
Addrs: []string{"192.168.100.131:6380", "192.168.100.131:6381", "192.168.100.131:6382"},
})
fmt.Println(clusterClients)
return red, nil
}
// 设置用户登录信息
func SetUserToken(red *redis.Client, key string, val []byte, timeTTL time.Duration) {
red.Set(context.Background(), key, val, timeTTL)
}
// 获取用户登录信息
func GetUserToken(red *redis.Client, key string) string {
res, err := red.Get(context.Background(), key).Result()
if err != nil {
log.Print("GetUserToken err ", err)
}
return res
}

@ -1,84 +0,0 @@
module git.mashibing.com/msb_47094/utils
go 1.19
require (
github.com/go-redis/redis/v8 v8.11.5
github.com/spf13/viper v1.13.0
github.com/uber/jaeger-client-go v2.30.0+incompatible
gorm.io/driver/mysql v1.4.3
gorm.io/gorm v1.24.0
)
require (
cloud.google.com/go v0.100.2 // indirect
cloud.google.com/go/compute v1.6.1 // indirect
cloud.google.com/go/firestore v1.6.1 // indirect
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/armon/go-metrics v0.3.10 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/hashicorp/consul/api v1.12.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/serf v0.9.7 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sagikazarmark/crypt v0.6.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.etcd.io/etcd/api/v3 v3.5.4 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.4 // indirect
go.etcd.io/etcd/client/v2 v2.305.4 // indirect
go.etcd.io/etcd/client/v3 v3.5.4 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/api v0.81.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
google.golang.org/grpc v1.46.2 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

@ -1,33 +0,0 @@
package common
import (
"crypto/md5"
"encoding/hex"
"fmt"
"strings"
)
//小写
func Md5Encode(data string) string {
h := md5.New()
h.Write([]byte(data))
tempStr := h.Sum(nil)
return hex.EncodeToString(tempStr)
}
//大写
func MD5Encode(data string) string {
return strings.ToUpper(Md5Encode(data))
}
//加密
func MakePassword(plainpwd, salt string) string {
return Md5Encode(plainpwd + salt)
}
//解密
func ValidPassword(plainpwd, salt string, password string) bool {
md := Md5Encode(plainpwd + salt)
fmt.Println(md + " " + password)
return md == password
}

@ -1,73 +0,0 @@
package common
import (
"encoding/json"
"fmt"
"net/http"
)
type H struct {
Code string
Message string
TraceId string
Data interface{}
Rows interface{}
Total interface{}
SkyWalkingDynamicField string
}
func Resp(w http.ResponseWriter, code string, data interface{}, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
h := H{
Code: code,
Data: data,
Message: message,
}
ret, err := json.Marshal(h)
if err != nil {
fmt.Println(err)
}
w.Write(ret)
}
func RespList(w http.ResponseWriter, code string, data interface{}, message string, rows interface{}, total interface{}, skyWalkingDynamicField string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
h := H{
Code: code,
Data: data,
Message: message,
Rows: rows,
Total: total,
SkyWalkingDynamicField: skyWalkingDynamicField,
}
ret, err := json.Marshal(h)
if err != nil {
fmt.Println(err)
}
w.Write(ret)
}
/*
*
200 OKLoginSuccessVO
201 Created
401 Unauthorized
403 Forbidden
404 Not Found
*
*/
func RespOK(w http.ResponseWriter, data interface{}, message string) {
Resp(w, "SUCCESS", data, message)
}
func RespFail(w http.ResponseWriter, data interface{}, message string) {
Resp(w, "TOKEN_FAIL", data, message)
}
func RespListOK(w http.ResponseWriter, data interface{}, message string, rows interface{}, total interface{}, skyWalkingDynamicField string) {
RespList(w, "SUCCESS", data, message, rows, total, skyWalkingDynamicField)
}
func RespListFail(w http.ResponseWriter, data interface{}, message string, rows interface{}, total interface{}, skyWalkingDynamicField string) {
RespList(w, "TOKEN_FAIL", data, message, rows, total, skyWalkingDynamicField)
}

@ -1,65 +0,0 @@
package common
import (
"math"
"strconv"
)
/*
@Auth:ShenZ
@Description: UUID
*/
func StringToArray(intput string) []int {
output := []int{}
for _, v := range intput {
output = append(output, int(v))
}
for i, j := 0, len(output)-1; i < j; i, j = i+1, j-1 {
output[i], output[j] = output[j], output[i]
}
return output
}
func GetInput(intput string) <-chan int {
out := make(chan int)
go func() {
for _, b := range StringToArray(intput) {
out <- b
}
close(out)
}()
return out
}
func SQ(in <-chan int) <-chan int {
out := make(chan int)
var base, i float64 = 2, 0
go func() {
for n := range in {
out <- (n - 48) * int(math.Pow(base, i))
i++
}
close(out)
}()
return out
}
func ToInt(intput string) int {
//intput := "101010101110110"
c := GetInput(intput)
out := SQ(c)
sum := 0
for o := range out {
sum += o
}
return sum
}
// int 转 二进制的字符串
func ConverToBinary(n int) string {
res := ""
for ; n > 0; n /= 2 {
lsb := n % 2
res = strconv.Itoa(lsb) + res
}
return res
}
Loading…
Cancel
Save