提取公共包

master
shenzhuan 2 years ago
parent 446688b00d
commit c84e0691da

@ -6,7 +6,8 @@ import (
opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing"
"github.com/opentracing/opentracing-go"
"go-micro.dev/v4/web"
"goproduct/common"
//"goproduct/common"
common "git.mashibing.com/msb_47094/shopping-comm"
"goproduct/proto"
"log"
"strconv"

@ -1,60 +0,0 @@
package common
import (
"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)
}
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"`
// }
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
}

@ -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,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,86 +0,0 @@
package common
import (
"encoding/json"
"fmt"
"net/http"
)
/**
"{\"ID\":33,\"Name\":\"NeSugar小巢糖创意桌面无叶风扇usb台式风扇办公室喷雾风扇冷风机\",\"ProductType\":1,\"CategoryId\":52,\"StartingPrice\":92,\"TotalStock\":0,\"MainPicture\":\"https://msb-edu-dev.oss-cn-beijing.aliyuncs.com/mall-product/productO1CN01nsp1Wk1FIYRXXUXL8_!!4023510464-0-cib.jpg\",\"RemoteAreaPostage\":10,\"SingleBuyLimit\":0,\"IsEnable\":1,\"Remark\":\"NeSugar小巢糖创意桌面无叶风扇usb台式风扇办公室喷雾风扇冷风机\",\"CreateUser\":1,\"CreateTime\":\"2022-04-30T16:55:00+08:00\",\"Up"
**/
//通过json tag 进行结构体赋值
func SwapToStruct(req, target interface{}) (err error) {
dataByte, err := json.Marshal(req)
if err != nil {
return
}
err = json.Unmarshal(dataByte, target)
return
}
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)
}
//writer data message row total field
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,47 +0,0 @@
package repository
import (
"errors"
"goproduct/domain/model"
"gorm.io/gorm"
)
/**
int32 clientId = 1;
string phone = 2;
int32 systemId = 3;
string verificationCode = 4;
**/
//接口
type IUserRepository interface {
Login(int32, string, int32, string) (*model.User, error)
}
//创建实例
func NewUserRepository(db *gorm.DB) IUserRepository {
return &UserRepository{mysqlDB: db}
}
//数据DB
type UserRepository struct {
mysqlDB *gorm.DB
}
//重写接口方法
func (u *UserRepository) Login(clientId int32, phone string, systemId int32, verificationCode string) (user *model.User, err error) {
user = &model.User{}
if clientId == 0 && systemId == 0 && verificationCode == "6666" {
u.mysqlDB.Where("phone = ? ", phone).Find(user)
//未找到就注册一个
if user.ID == 0 {
user.Phone = phone
u.mysqlDB.Create(&user)
//u.mysqlDB.Select("Nickname", "Avatar", "Phone", "ClientId").Create(&user)
}
return user, nil
//return user, u.mysqlDB.Where("phone = ? ", phone).Find(user).Error
} else {
return user, errors.New("参数不匹配")
}
}

@ -1,29 +0,0 @@
package service
import (
"goproduct/domain/model"
"goproduct/domain/repository"
)
type IUserDataService interface {
Login(int32, string, int32, string) (*model.User, error)
}
type UserDataService struct {
userRepository repository.IUserRepository
}
func NewUserDataService(userRepository repository.IUserRepository) IUserDataService {
return &UserDataService{userRepository: userRepository}
}
//重写接口方法
func (u *UserDataService) Login(clientId int32, phone string, systemId int32, verificationCode string) (user *model.User, err error) {
return u.userRepository.Login(clientId, phone, systemId, verificationCode)
}
/* clientId, _ := strconv.Atoi(c.Request.FormValue("clientId"))
phone := c.Request.FormValue("phone")
systemId, _ := strconv.Atoi(c.Request.FormValue("systemId"))
verificationCode := c.Request.FormValue("verificationCode")
*/

@ -7,7 +7,7 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
go-micro.dev/v4 v4.8.0
google.golang.org/protobuf v1.28.0
gorm.io/gorm v1.23.8
gorm.io/gorm v1.24.0
)
require (
@ -25,25 +25,28 @@ require (
github.com/gin-gonic/gin v1.8.1
github.com/go-micro/plugins/v4/wrapper/trace/opentracing v1.1.0
github.com/opentracing/opentracing-go v1.2.0
github.com/spf13/viper v1.12.0
github.com/spf13/viper v1.13.0
github.com/uber/jaeger-client-go v2.30.0+incompatible
google.golang.org/grpc v1.49.0
gorm.io/driver/mysql v1.3.5
gorm.io/driver/mysql v1.4.3
)
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
git.mashibing.com/msb_47094/shopping-comm v0.0.0-20221031053804-df4c81cf5ca3 // indirect
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/armon/go-metrics v0.3.10 // indirect
github.com/bitly/go-simplejson v0.5.0 // 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/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/evanphx/json-patch/v5 v5.5.0 // indirect
github.com/fatih/color v1.13.0 // indirect
@ -57,6 +60,7 @@ require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
@ -96,7 +100,7 @@ require (
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/sagikazarmark/crypt v0.6.0 // indirect
@ -106,7 +110,7 @@ require (
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.3.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/urfave/cli/v2 v2.3.0 // indirect
@ -127,7 +131,7 @@ require (
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
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect

@ -3,7 +3,7 @@ package handler
import (
"context"
"fmt"
"goproduct/common"
comm "goproduct/common"
"goproduct/domain/model"
"goproduct/domain/service"
"goproduct/proto"
@ -45,7 +45,7 @@ func (u *ProductHandler) Page(ctx context.Context, req *proto.PageReq, resp *pro
func ObjForResp(obj *[]model.Product, resp *proto.PageResp) (err error) {
for _, v := range *obj {
product := &proto.Product{}
err := common.SwapToStruct(v, product)
err := comm.SwapToStruct(v, product)
if err != nil {
return err
}
@ -63,7 +63,7 @@ func (u *ProductHandler) ShowProductDetail(ctx context.Context, req *proto.Produ
println("ShowProductDetail err :", err)
}
productDetail := &proto.ProductDetail{}
err1 := common.SwapToStruct(obj, productDetail)
err1 := comm.SwapToStruct(obj, productDetail)
if err1 != nil {
println("ShowProductDetail SwapToStruct err :", err1)
}
@ -88,7 +88,7 @@ func (u *ProductHandler) ShowProductSku(ctx context.Context, req *proto.ProductS
func ObjSkuForResp(obj *[]model.ProductSku, resp *proto.ProductSkuResp) (err error) {
for _, v := range *obj {
productSku := &proto.ProductSku{}
err := common.SwapToStruct(v, productSku)
err := comm.SwapToStruct(v, productSku)
if err != nil {
return err
}
@ -105,7 +105,7 @@ func (u *ProductHandler) ShowDetailSku(ctx context.Context, req *proto.ProductDe
println("ShowDetailSku err :", err)
}
productSku := &proto.ProductSku{}
err = common.SwapToStruct(obj, productSku)
err = comm.SwapToStruct(obj, productSku)
if err != nil {
return err
}

@ -1,48 +0,0 @@
package handler
import (
"context"
"fmt"
"goproduct/common"
"goproduct/domain/model"
"goproduct/domain/service"
"goproduct/proto"
"log"
"time"
)
type User struct {
UserDataService service.IUserDataService
}
// 登录 (clientId int32, phone string, systemId int32, verifi
func (u *User) Login(ctx context.Context, loginRequest *proto.LoginRequest, loginResp *proto.LoginResp) error {
userInfo, err := u.UserDataService.Login(loginRequest.ClientId, loginRequest.GetPhone(), loginRequest.SystemId, loginRequest.VerificationCode)
if err != nil {
return err
}
fmt.Println(">>>>>>>>>>>>> login success :", userInfo)
UserForResp(userInfo, loginResp)
return nil
}
func UserForResp(userModel *model.User, resp *proto.LoginResp) *proto.LoginResp {
timeStr := fmt.Sprintf("%d", time.Now().Unix())
resp.Token = common.Md5Encode(timeStr) //"123456"
resp.User = &proto.User{}
log.Println(userModel)
resp.User.Id = userModel.ID
resp.User.Avatar = userModel.Avatar
resp.User.ClientId = userModel.ClientId
resp.User.EmployeeId = 1 //userModel.EmployeeId
resp.User.Nickname = userModel.Nickname
resp.User.SessionId = resp.Token
resp.User.Phone = userModel.Phone
//token 过期时间
tp, _ := time.ParseDuration("1h")
tokenExpireTime := time.Now().Add(tp)
expiretimeStr := tokenExpireTime.Format("2006-01-02 15:04:05")
resp.User.TokenExpireTime = expiretimeStr
resp.User.UnionId = userModel.UnionId
return resp
}

@ -12,7 +12,8 @@ import (
"github.com/opentracing/opentracing-go"
"go-micro.dev/v4/client"
"go-micro.dev/v4/web"
"goproduct/common"
//"goproduct/common"
common "git.mashibing.com/msb_47094/shopping-comm"
"goproduct/proto"
"log"
"net"

@ -1,60 +0,0 @@
package common
import (
"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)
}
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"`
// }
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
}

@ -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,86 +0,0 @@
package common
import (
"encoding/json"
"fmt"
"net/http"
)
/**
"{\"ID\":33,\"Name\":\"NeSugar小巢糖创意桌面无叶风扇usb台式风扇办公室喷雾风扇冷风机\",\"ProductType\":1,\"CategoryId\":52,\"StartingPrice\":92,\"TotalStock\":0,\"MainPicture\":\"https://msb-edu-dev.oss-cn-beijing.aliyuncs.com/mall-product/productO1CN01nsp1Wk1FIYRXXUXL8_!!4023510464-0-cib.jpg\",\"RemoteAreaPostage\":10,\"SingleBuyLimit\":0,\"IsEnable\":1,\"Remark\":\"NeSugar小巢糖创意桌面无叶风扇usb台式风扇办公室喷雾风扇冷风机\",\"CreateUser\":1,\"CreateTime\":\"2022-04-30T16:55:00+08:00\",\"Up"
**/
//通过json tag 进行结构体赋值
func SwapToStruct(req, target interface{}) (err error) {
dataByte, err := json.Marshal(req)
if err != nil {
return
}
err = json.Unmarshal(dataByte, target)
return
}
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)
}
//writer data message row total field
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
}

@ -7,7 +7,7 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
go-micro.dev/v4 v4.8.0
google.golang.org/protobuf v1.28.0
gorm.io/gorm v1.23.8
gorm.io/gorm v1.24.0
)
require (
@ -30,15 +30,16 @@ require (
github.com/go-micro/plugins/v4/wrapper/trace/opentracing v1.1.0
github.com/lithammer/shortuuid/v3 v3.0.7
github.com/opentracing/opentracing-go v1.2.0
github.com/spf13/viper v1.12.0
github.com/spf13/viper v1.13.0
github.com/uber/jaeger-client-go v2.30.0+incompatible
gorm.io/driver/mysql v1.3.5
gorm.io/driver/mysql v1.4.3
)
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
git.mashibing.com/msb_47094/shopping-comm v0.0.0-20221031053804-df4c81cf5ca3 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
@ -107,7 +108,7 @@ require (
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/crypt v0.6.0 // indirect
@ -116,7 +117,7 @@ require (
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.3.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/urfave/cli/v2 v2.3.0 // indirect
@ -144,7 +145,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220622171453-ea41d75dfa0f // indirect
google.golang.org/grpc v1.49.0 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect

@ -73,6 +73,8 @@ contrib.go.opencensus.io/exporter/prometheus v0.4.1/go.mod h1:t9wvfitlUjGXG2IXAZ
contrib.go.opencensus.io/exporter/zipkin v0.1.1/go.mod h1:GMvdSl3eJ2gapOaLKzTKE3qDgUkJ86k9k3yY2eqwkzc=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
dubbo.apache.org/dubbo-go/v3 v3.0.3-0.20220610080020-48691a404537/go.mod h1:O7eTHAilCWlqBjEkG2MW9khZFImiARb/tSOE8PJas+g=
git.mashibing.com/msb_47094/shopping-comm v0.0.0-20221031053804-df4c81cf5ca3 h1:5WyYaK54L8Bd2IjMj28D0YBKZdjVCkCAEhdLQWzlgY8=
git.mashibing.com/msb_47094/shopping-comm v0.0.0-20221031053804-df4c81cf5ca3/go.mod h1:xqoOsbP1u60b+IDUOcJTm6RMjA2YssKMTB3TOcgOSt0=
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4=
github.com/99designs/keyring v1.1.6/go.mod h1:16e0ds7LGQQcT59QqkTg72Hh5ShM51Byv5PEmW6uoRU=
github.com/99designs/keyring v1.2.0/go.mod h1:ETJn2A9cfvJKq1Q4FeOc+eetK52Ik0kUGog7Uy+xvX8=
@ -1678,6 +1680,8 @@ github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw=
github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI=
github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg=
github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas=
github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/peterh/liner v0.0.0-20170211195444-bf27d3ba8e1d/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc=
@ -1899,6 +1903,8 @@ github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH
github.com/spf13/viper v1.10.0/go.mod h1:SoyBPwAtKDzypXNDFKN5kzH7ppppbGZtls1UpIy5AsM=
github.com/spf13/viper v1.12.0 h1:CZ7eSOd3kZoaYDLbXnmzgQI5RlciuXBMA+18HwHRfZQ=
github.com/spf13/viper v1.12.0/go.mod h1:b6COn30jlNxbm/V2IqWiNWkJ+vZNiMNksliPCiuKtSI=
github.com/spf13/viper v1.13.0 h1:BWSJ/M+f+3nmdz9bxB+bWX28kkALN2ok11D0rSo8EJU=
github.com/spf13/viper v1.13.0/go.mod h1:Icm2xNL3/8uyh/wFuB1jI7TiTNKp8632Nwegu+zgdYw=
github.com/stathat/consistent v1.0.0/go.mod h1:uajTPbgSygZBJ+V+0mY7meZ8i0XAcZs7AQ6V121XSxw=
github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h1:AO3tvPzVZ/ayst6UlUKUv6rcPQInYe3IknH3jYhAKu8=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
@ -1928,6 +1934,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.3.0 h1:mjC+YW8QpAdXibNi+vNWgzmgBH4+5l5dCXv8cNysBLI=
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/supplyon/gremcos v0.1.0/go.mod h1:ZnXsXGVbGCYDFU5GLPX9HZLWfD+ZWkiPo30KUjNoOtw=
github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
@ -3004,6 +3012,8 @@ gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.66.6 h1:LATuAqN/shcYAOkv3wl2L4rkaKqkcgTBQjOyYDvcPKI=
gopkg.in/ini.v1 v1.66.6/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/jcmturner/aescts.v1 v1.0.1/go.mod h1:nsR8qBOg+OucoIW+WMhB3GspUQXq9XorLnQb9XtvcOo=
gopkg.in/jcmturner/dnsutils.v1 v1.0.1/go.mod h1:m3v+5svpVOhtFAP/wSz+yzh4Mc0Fg7eRhxkJMWSIz9Q=
gopkg.in/jcmturner/goidentity.v3 v3.0.0/go.mod h1:oG2kH0IvSYNIu80dVAyu/yoefjq1mNfM5bm88whjWx4=
@ -3043,12 +3053,16 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.0.3/go.mod h1:twGxftLBlFgNVNakL7F+P/x9oYqoymG3YYT8cAfI9oI=
gorm.io/driver/mysql v1.3.5 h1:iWBTVW/8Ij5AG4e0G/zqzaJblYkBI1VIL1LG2HUGsvY=
gorm.io/driver/mysql v1.3.5/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c=
gorm.io/driver/mysql v1.4.3 h1:/JhWJhO2v17d8hjApTltKNADm7K7YI2ogkR7avJUL3k=
gorm.io/driver/mysql v1.4.3/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c=
gorm.io/driver/postgres v1.2.1/go.mod h1:SHRZhu+D0tLOHV5qbxZRUM6kBcf3jp/kxPz2mYMTsNY=
gorm.io/gorm v1.20.4/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.22.0/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.22.2/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.24.0 h1:j/CoiSm6xpRpmzbFJsQHYj+I8bGYWLXVHeYEyyKlF74=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=

@ -2,7 +2,7 @@ package main
import (
"context"
"gouser/common"
common "git.mashibing.com/msb_47094/shopping-comm"
"gouser/proto"
"log"
"strconv"

@ -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,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,70 +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
}

@ -3,9 +3,9 @@ package repository
import (
"errors"
"fmt"
common "git.mashibing.com/msb_47094/shopping-comm"
"github.com/go-redis/redis/v8"
"gorm.io/gorm"
"gouser/common"
"gouser/domain/model"
"time"
)

@ -7,7 +7,7 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
go-micro.dev/v4 v4.7.0
google.golang.org/protobuf v1.28.0
gorm.io/gorm v1.23.8
gorm.io/gorm v1.24.0
)
require (
@ -24,14 +24,15 @@ require (
github.com/asim/go-micro/plugins/registry/consul/v4 v4.7.0
github.com/gin-gonic/gin v1.8.1
github.com/go-redis/redis/v8 v8.11.5
github.com/spf13/viper v1.12.0
gorm.io/driver/mysql v1.3.5
github.com/spf13/viper v1.13.0
gorm.io/driver/mysql v1.4.3
)
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
git.mashibing.com/msb_47094/shopping-comm v0.0.0-20221031053804-df4c81cf5ca3 // indirect
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
@ -81,10 +82,11 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/sagikazarmark/crypt v0.6.0 // indirect
@ -94,7 +96,9 @@ require (
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.3.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/urfave/cli/v2 v2.3.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
@ -115,7 +119,7 @@ require (
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
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect

@ -3,7 +3,7 @@ package handler
import (
"context"
"fmt"
"gouser/common"
common "git.mashibing.com/msb_47094/shopping-comm"
"gouser/domain/model"
"gouser/domain/service"
"gouser/proto"

Loading…
Cancel
Save