parent
446688b00d
commit
c84e0691da
@ -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")
|
||||
*/
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
Loading…
Reference in new issue