master
shenzhuan 2 years ago
commit 3ffb329a1a

@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "golang",
"type": "go",
"request": "launch",
"mode": "auto",
//{workspaceFolder}{file}
"program": "${workspaceFolder}",
"env": {},
"args": []
}
]
}

@ -0,0 +1,69 @@
package main
import (
"context"
"goproduct/common"
"goproduct/proto"
"log"
"strconv"
consul "github.com/asim/go-micro/plugins/registry/consul/v4"
"go-micro.dev/v4/web"
"github.com/gin-gonic/gin"
"go-micro.dev/v4"
"go-micro.dev/v4/registry"
)
//获取远程服务的客户端
func getClient() proto.LoginService {
//注册到consul
consulReg := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{"192.168.137.131:8500"}
})
rpcServer := micro.NewService(
micro.Registry(consulReg),
)
return proto.NewLoginService("shop-user", rpcServer.Client())
}
func main() {
router := gin.Default()
router.Handle("GET", "toLogin", func(context *gin.Context) {
context.String(200, "to Loging ....")
})
router.GET("/login", func(c *gin.Context) {
//获取远程服务的客户端
client := getClient()
//获取页面参数
clientId, _ := strconv.Atoi(c.Request.FormValue("clientId"))
phone := c.Request.FormValue("phone")
systemId, _ := strconv.Atoi(c.Request.FormValue("systemId"))
verificationCode := c.Request.FormValue("verificationCode")
//拼接请求信息
req := &proto.LoginRequest{
ClientId: int32(clientId),
Phone: phone,
SystemId: int32(systemId),
VerificationCode: verificationCode,
}
//远程调用服务
resp, err := client.Login(context.TODO(), req)
//根据响应做输出
if err != nil {
log.Println(err.Error())
//c.String(http.StatusBadRequest, "search failed !")
common.RespFail(c.Writer, resp, "登录失败")
return
}
common.RespOK(c.Writer, resp, "登录成功")
})
service := web.NewService(
web.Address(":8081"),
web.Handler(router),
)
service.Run()
//router.Run(":6666")
}

@ -0,0 +1,60 @@
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
}

@ -0,0 +1,33 @@
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
}

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

@ -0,0 +1,27 @@
package model
import "time"
type Product struct {
//gorm.Model
ID int32
Name string
ProductType int32 `gorm:"default:1"`
CategoryId int32
StartingPrice float32
TotalStock int32 `gorm:"default:'1234'"`
MainPicture string `gorm:"default:1"`
RemoteAreaPostage float32
SingleBuyLimit int32
IsEnable int32 `gorm:"default:0"`
Remark string `gorm:"default:'1'"`
CreateUser int32 `gorm:"default:'1'"`
CreateTime time.Time
UpdateUser int32
UpdateTime time.Time
IsDeleted bool
}
func (table *Product) TableName() string {
return "product"
}

@ -0,0 +1,23 @@
package model
/**
"attributeSymbolList": "",
"name": "",
"sellPrice": 0,
"skuId": 0,
"stock": 0
}
**/
type ProductSku struct {
//gorm.Model
SkuId int32 `gorm:"column:id"`
Name string
ProductId int32 `gorm:"default:1"`
AttributeSymbolList string
SellPrice float32
Stock int32 `gorm:"default:1"`
}
func (table *ProductSku) TableName() string {
return "product_sku"
}

@ -0,0 +1,22 @@
package model
import "time"
type User struct {
//gorm.Model
ID int32
Avatar string `gorm:"default:'https://msb-edu-dev.oss-cn-beijing.aliyuncs.com/default-headimg.png'"`
ClientId int32 `gorm:"default:1"`
Nickname string `gorm:"default:'随机名称'"`
Phone string
Password string `gorm:"default:'1234'"`
SystemId string `gorm:"default:1"`
LastLoginTime time.Time
CreateTime time.Time
IsDeleted int32 `gorm:"default:0"`
UnionId string `gorm:"default:'1'"`
}
func (table *User) TableName() string {
return "user"
}

@ -0,0 +1,47 @@
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("参数不匹配")
}
}

@ -0,0 +1,29 @@
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")
*/

@ -0,0 +1,119 @@
module goproduct
go 1.17
require (
github.com/jinzhu/inflection v1.0.0 // indirect
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
)
require (
github.com/google/uuid v1.3.0 // indirect
github.com/miekg/dns v1.1.50 // indirect
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e // indirect
golang.org/x/tools v0.1.11 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require (
github.com/asim/go-micro/plugins/registry/consul/v4 v4.7.0
github.com/gin-gonic/gin v1.8.1
github.com/spf13/viper v1.12.0
gorm.io/driver/mysql v1.3.5
)
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/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/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/emirpasic/gods v1.12.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
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-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // 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/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/leodido/go-urn v1.2.1 // 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/hashstructure 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/nxadm/tail v1.4.8 // 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/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/sagikazarmark/crypt v0.6.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.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.3.0 // 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
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/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // 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
gopkg.in/ini.v1 v1.66.4 // 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
)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,48 @@
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
}

@ -0,0 +1,52 @@
package main
import (
"goproduct/common"
"goproduct/domain/repository"
"goproduct/domain/service"
"goproduct/handler"
"goproduct/proto"
"log"
"time"
consul "github.com/asim/go-micro/plugins/registry/consul/v4"
"go-micro.dev/v4"
"go-micro.dev/v4/registry"
)
const (
consulStr = "http://192.168.137.131:8500"
fileKey = "mysql-user"
)
func main() {
//0 配置中心
consulConfig, err := common.GetConsulConfig(consulStr, fileKey)
if err != nil {
log.Println("consulConfig err :", err)
}
// 1.consul注册中心
consulReist := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{consulStr}
})
repcService := micro.NewService(
micro.RegisterTTL(time.Second*30),
micro.RegisterInterval(time.Second*30),
micro.Name("shop-user"),
micro.Address(":8081"),
micro.Version("v1"),
micro.Registry(consulReist),
)
//2.初始化db
db, _ := common.GetMysqlFromConsul(consulConfig)
//3.创建服务实例
userDataService := service.NewUserDataService(repository.NewUserRepository(db))
//4.注册handler
proto.RegisterLoginHandler(repcService.Server(), &handler.User{userDataService})
//5.启动服务
if err := repcService.Run(); err != nil {
log.Println("start user service err :", err)
}
}

@ -0,0 +1,418 @@
//*
// @Auth:ShenZ
// @Description:
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.18.1
// source: user.proto
package proto
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
//*
//"avatar": "",
//"clientId": 0,
//"employeeId": 0,
//"id": 0,
//"nickname": "",
//"phone": "",
//"sessionId": "",
//"systemId": 0,
//"token": "",
//"tokenExpireTime": "",
//"unionId": ""
type User struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Avatar string `protobuf:"bytes,1,opt,name=avatar,proto3" json:"avatar,omitempty"`
ClientId int32 `protobuf:"varint,2,opt,name=clientId,proto3" json:"clientId,omitempty"`
EmployeeId int32 `protobuf:"varint,3,opt,name=employeeId,proto3" json:"employeeId,omitempty"`
Nickname string `protobuf:"bytes,4,opt,name=nickname,proto3" json:"nickname,omitempty"`
Phone string `protobuf:"bytes,5,opt,name=phone,proto3" json:"phone,omitempty"`
SessionId string `protobuf:"bytes,6,opt,name=sessionId,proto3" json:"sessionId,omitempty"`
Token string `protobuf:"bytes,7,opt,name=token,proto3" json:"token,omitempty"`
TokenExpireTime string `protobuf:"bytes,8,opt,name=tokenExpireTime,proto3" json:"tokenExpireTime,omitempty"`
UnionId string `protobuf:"bytes,9,opt,name=unionId,proto3" json:"unionId,omitempty"`
Id int32 `protobuf:"varint,10,opt,name=id,proto3" json:"id,omitempty"`
}
func (x *User) Reset() {
*x = User{}
if protoimpl.UnsafeEnabled {
mi := &file_user_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *User) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*User) ProtoMessage() {}
func (x *User) ProtoReflect() protoreflect.Message {
mi := &file_user_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use User.ProtoReflect.Descriptor instead.
func (*User) Descriptor() ([]byte, []int) {
return file_user_proto_rawDescGZIP(), []int{0}
}
func (x *User) GetAvatar() string {
if x != nil {
return x.Avatar
}
return ""
}
func (x *User) GetClientId() int32 {
if x != nil {
return x.ClientId
}
return 0
}
func (x *User) GetEmployeeId() int32 {
if x != nil {
return x.EmployeeId
}
return 0
}
func (x *User) GetNickname() string {
if x != nil {
return x.Nickname
}
return ""
}
func (x *User) GetPhone() string {
if x != nil {
return x.Phone
}
return ""
}
func (x *User) GetSessionId() string {
if x != nil {
return x.SessionId
}
return ""
}
func (x *User) GetToken() string {
if x != nil {
return x.Token
}
return ""
}
func (x *User) GetTokenExpireTime() string {
if x != nil {
return x.TokenExpireTime
}
return ""
}
func (x *User) GetUnionId() string {
if x != nil {
return x.UnionId
}
return ""
}
func (x *User) GetId() int32 {
if x != nil {
return x.Id
}
return 0
}
//请求 request struct
type LoginRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ClientId int32 `protobuf:"varint,1,opt,name=clientId,proto3" json:"clientId,omitempty"`
Phone string `protobuf:"bytes,2,opt,name=phone,proto3" json:"phone,omitempty"`
SystemId int32 `protobuf:"varint,3,opt,name=systemId,proto3" json:"systemId,omitempty"`
VerificationCode string `protobuf:"bytes,4,opt,name=verificationCode,proto3" json:"verificationCode,omitempty"`
}
func (x *LoginRequest) Reset() {
*x = LoginRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_user_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *LoginRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*LoginRequest) ProtoMessage() {}
func (x *LoginRequest) ProtoReflect() protoreflect.Message {
mi := &file_user_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use LoginRequest.ProtoReflect.Descriptor instead.
func (*LoginRequest) Descriptor() ([]byte, []int) {
return file_user_proto_rawDescGZIP(), []int{1}
}
func (x *LoginRequest) GetClientId() int32 {
if x != nil {
return x.ClientId
}
return 0
}
func (x *LoginRequest) GetPhone() string {
if x != nil {
return x.Phone
}
return ""
}
func (x *LoginRequest) GetSystemId() int32 {
if x != nil {
return x.SystemId
}
return 0
}
func (x *LoginRequest) GetVerificationCode() string {
if x != nil {
return x.VerificationCode
}
return ""
}
//响应 resp struct
type LoginResp struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
User *User `protobuf:"bytes,2,opt,name=user,proto3" json:"user,omitempty"`
}
func (x *LoginResp) Reset() {
*x = LoginResp{}
if protoimpl.UnsafeEnabled {
mi := &file_user_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *LoginResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*LoginResp) ProtoMessage() {}
func (x *LoginResp) ProtoReflect() protoreflect.Message {
mi := &file_user_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use LoginResp.ProtoReflect.Descriptor instead.
func (*LoginResp) Descriptor() ([]byte, []int) {
return file_user_proto_rawDescGZIP(), []int{2}
}
func (x *LoginResp) GetToken() string {
if x != nil {
return x.Token
}
return ""
}
func (x *LoginResp) GetUser() *User {
if x != nil {
return x.User
}
return nil
}
var File_user_proto protoreflect.FileDescriptor
var file_user_proto_rawDesc = []byte{
0x0a, 0x0a, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x22, 0x94, 0x02, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, 0x12, 0x16, 0x0a, 0x06,
0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x61, 0x76,
0x61, 0x74, 0x61, 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64,
0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64,
0x12, 0x1e, 0x0a, 0x0a, 0x65, 0x6d, 0x70, 0x6c, 0x6f, 0x79, 0x65, 0x65, 0x49, 0x64, 0x18, 0x03,
0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x65, 0x6d, 0x70, 0x6c, 0x6f, 0x79, 0x65, 0x65, 0x49, 0x64,
0x12, 0x1a, 0x0a, 0x08, 0x6e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01,
0x28, 0x09, 0x52, 0x08, 0x6e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05,
0x70, 0x68, 0x6f, 0x6e, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x70, 0x68, 0x6f,
0x6e, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18,
0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64,
0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52,
0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x28, 0x0a, 0x0f, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x45,
0x78, 0x70, 0x69, 0x72, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52,
0x0f, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x45, 0x78, 0x70, 0x69, 0x72, 0x65, 0x54, 0x69, 0x6d, 0x65,
0x12, 0x18, 0x0a, 0x07, 0x75, 0x6e, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x09, 0x20, 0x01, 0x28,
0x09, 0x52, 0x07, 0x75, 0x6e, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64,
0x18, 0x0a, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x69, 0x64, 0x22, 0x88, 0x01, 0x0a, 0x0c, 0x4c,
0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x63,
0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x63,
0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65,
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x12, 0x1a, 0x0a,
0x08, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52,
0x08, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x2a, 0x0a, 0x10, 0x76, 0x65, 0x72,
0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x04, 0x20,
0x01, 0x28, 0x09, 0x52, 0x10, 0x76, 0x65, 0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
0x6e, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x42, 0x0a, 0x09, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65,
0x73, 0x70, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x1f, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72,
0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x55,
0x73, 0x65, 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x32, 0x39, 0x0a, 0x05, 0x4c, 0x6f, 0x67,
0x69, 0x6e, 0x12, 0x30, 0x0a, 0x05, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x13, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x1a, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65,
0x73, 0x70, 0x22, 0x00, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_user_proto_rawDescOnce sync.Once
file_user_proto_rawDescData = file_user_proto_rawDesc
)
func file_user_proto_rawDescGZIP() []byte {
file_user_proto_rawDescOnce.Do(func() {
file_user_proto_rawDescData = protoimpl.X.CompressGZIP(file_user_proto_rawDescData)
})
return file_user_proto_rawDescData
}
var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_user_proto_goTypes = []interface{}{
(*User)(nil), // 0: proto.User
(*LoginRequest)(nil), // 1: proto.LoginRequest
(*LoginResp)(nil), // 2: proto.LoginResp
}
var file_user_proto_depIdxs = []int32{
0, // 0: proto.LoginResp.user:type_name -> proto.User
1, // 1: proto.Login.Login:input_type -> proto.LoginRequest
2, // 2: proto.Login.Login:output_type -> proto.LoginResp
2, // [2:3] is the sub-list for method output_type
1, // [1:2] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_user_proto_init() }
func file_user_proto_init() {
if File_user_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_user_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*User); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_user_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*LoginRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_user_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*LoginResp); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_user_proto_rawDesc,
NumEnums: 0,
NumMessages: 3,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_user_proto_goTypes,
DependencyIndexes: file_user_proto_depIdxs,
MessageInfos: file_user_proto_msgTypes,
}.Build()
File_user_proto = out.File
file_user_proto_rawDesc = nil
file_user_proto_goTypes = nil
file_user_proto_depIdxs = nil
}

@ -0,0 +1,89 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: user.proto
package proto
import (
fmt "fmt"
proto "google.golang.org/protobuf/proto"
math "math"
)
import (
context "context"
api "go-micro.dev/v4/api"
client "go-micro.dev/v4/client"
server "go-micro.dev/v4/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Login service
func NewLoginEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Login service
type LoginService interface {
//rpc 服务
Login(ctx context.Context, in *LoginRequest, opts ...client.CallOption) (*LoginResp, error)
}
type loginService struct {
c client.Client
name string
}
func NewLoginService(name string, c client.Client) LoginService {
return &loginService{
c: c,
name: name,
}
}
func (c *loginService) Login(ctx context.Context, in *LoginRequest, opts ...client.CallOption) (*LoginResp, error) {
req := c.c.NewRequest(c.name, "Login.Login", in)
out := new(LoginResp)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Login service
type LoginHandler interface {
//rpc 服务
Login(context.Context, *LoginRequest, *LoginResp) error
}
func RegisterLoginHandler(s server.Server, hdlr LoginHandler, opts ...server.HandlerOption) error {
type login interface {
Login(ctx context.Context, in *LoginRequest, out *LoginResp) error
}
type Login struct {
login
}
h := &loginHandler{hdlr}
return s.Handle(s.NewHandler(&Login{h}, opts...))
}
type loginHandler struct {
LoginHandler
}
func (h *loginHandler) Login(ctx context.Context, in *LoginRequest, out *LoginResp) error {
return h.LoginHandler.Login(ctx, in, out)
}

@ -0,0 +1,59 @@
/**
* @Auth:ShenZ
* @Description:
*/
syntax = "proto3"; //
option go_package="./;proto"; //1 2 package
package proto ; //
//
/**
"avatar": "",
"clientId": 0,
"employeeId": 0,
"id": 0,
"nickname": "",
"phone": "",
"sessionId": "",
"systemId": 0,
"token": "",
"tokenExpireTime": "",
"unionId": ""
**/
message User {
string avatar = 1;
int32 clientId = 2;
int32 employeeId =3;
string nickname = 4;
string phone = 5;
string sessionId = 6;
string token = 7;
string tokenExpireTime = 8;
string unionId = 9;
int32 id = 10;
}
/**
{
"clientId": 0,
"phone": "",
"systemId": 0,
"verificationCode": ""
}
**/
// request struct
message LoginRequest {
int32 clientId = 1;
string phone = 2;
int32 systemId = 3;
string verificationCode = 4;
}
// resp struct
message LoginResp{
string token = 1;
User user = 2;
}
//RPC
service Login {
//rpc
rpc Login (LoginRequest) returns (LoginResp){}
}

@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "golang",
"type": "go",
"request": "launch",
"mode": "auto",
//{workspaceFolder}{file}
"program": "${workspaceFolder}",
"env": {},
"args": []
}
]
}

@ -0,0 +1,69 @@
package main
import (
"context"
"gouser/common"
"gouser/proto"
"log"
"strconv"
consul "github.com/asim/go-micro/plugins/registry/consul/v4"
"go-micro.dev/v4/web"
"github.com/gin-gonic/gin"
"go-micro.dev/v4"
"go-micro.dev/v4/registry"
)
//获取远程服务的客户端
func getClient() proto.LoginService {
//注册到consul
consulReg := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{"192.168.137.131:8500"}
})
rpcServer := micro.NewService(
micro.Registry(consulReg),
)
return proto.NewLoginService("shop-user", rpcServer.Client())
}
func main() {
router := gin.Default()
router.Handle("GET", "toLogin", func(context *gin.Context) {
context.String(200, "to Loging ....")
})
router.GET("/login", func(c *gin.Context) {
//获取远程服务的客户端
client := getClient()
//获取页面参数
clientId, _ := strconv.Atoi(c.Request.FormValue("clientId"))
phone := c.Request.FormValue("phone")
systemId, _ := strconv.Atoi(c.Request.FormValue("systemId"))
verificationCode := c.Request.FormValue("verificationCode")
//拼接请求信息
req := &proto.LoginRequest{
ClientId: int32(clientId),
Phone: phone,
SystemId: int32(systemId),
VerificationCode: verificationCode,
}
//远程调用服务
resp, err := client.Login(context.TODO(), req)
//根据响应做输出
if err != nil {
log.Println(err.Error())
//c.String(http.StatusBadRequest, "search failed !")
common.RespFail(c.Writer, resp, "登录失败")
return
}
common.RespOK(c.Writer, resp, "登录成功")
})
service := web.NewService(
web.Address(":8081"),
web.Handler(router),
)
service.Run()
//router.Run(":6666")
}

@ -0,0 +1,60 @@
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
}

@ -0,0 +1,33 @@
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
}

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

@ -0,0 +1,22 @@
package model
import "time"
type User struct {
//gorm.Model
ID int32
Avatar string `gorm:"default:'https://msb-edu-dev.oss-cn-beijing.aliyuncs.com/default-headimg.png'"`
ClientId int32 `gorm:"default:1"`
Nickname string `gorm:"default:'随机名称'"`
Phone string
Password string `gorm:"default:'1234'"`
SystemId string `gorm:"default:1"`
LastLoginTime time.Time
CreateTime time.Time
IsDeleted int32 `gorm:"default:0"`
UnionId string `gorm:"default:'1'"`
}
func (table *User) TableName() string {
return "user"
}

@ -0,0 +1,47 @@
package repository
import (
"errors"
"gouser/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("参数不匹配")
}
}

@ -0,0 +1,29 @@
package service
import (
"gouser/domain/model"
"gouser/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")
*/

@ -0,0 +1,119 @@
module gouser
go 1.17
require (
github.com/jinzhu/inflection v1.0.0 // indirect
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
)
require (
github.com/google/uuid v1.3.0 // indirect
github.com/miekg/dns v1.1.50 // indirect
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e // indirect
golang.org/x/tools v0.1.11 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require (
github.com/asim/go-micro/plugins/registry/consul/v4 v4.7.0
github.com/gin-gonic/gin v1.8.1
github.com/spf13/viper v1.12.0
gorm.io/driver/mysql v1.3.5
)
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/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/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/emirpasic/gods v1.12.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
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-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // 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/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/leodido/go-urn v1.2.1 // 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/hashstructure 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/nxadm/tail v1.4.8 // 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/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/sagikazarmark/crypt v0.6.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.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.3.0 // 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
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/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // 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
gopkg.in/ini.v1 v1.66.4 // 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
)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,48 @@
package handler
import (
"context"
"fmt"
"gouser/common"
"gouser/domain/model"
"gouser/domain/service"
"gouser/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
}

@ -0,0 +1,52 @@
package main
import (
"gouser/common"
"gouser/domain/repository"
"gouser/domain/service"
"gouser/handler"
"gouser/proto"
"log"
"time"
consul "github.com/asim/go-micro/plugins/registry/consul/v4"
"go-micro.dev/v4"
"go-micro.dev/v4/registry"
)
const (
consulStr = "http://192.168.137.131:8500"
fileKey = "mysql-user"
)
func main() {
//0 配置中心
consulConfig, err := common.GetConsulConfig(consulStr, fileKey)
if err != nil {
log.Println("consulConfig err :", err)
}
// 1.consul注册中心
consulReist := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{consulStr}
})
repcService := micro.NewService(
micro.RegisterTTL(time.Second*30),
micro.RegisterInterval(time.Second*30),
micro.Name("shop-user"),
micro.Address(":8081"),
micro.Version("v1"),
micro.Registry(consulReist),
)
//2.初始化db
db, _ := common.GetMysqlFromConsul(consulConfig)
//3.创建服务实例
userDataService := service.NewUserDataService(repository.NewUserRepository(db))
//4.注册handler
proto.RegisterLoginHandler(repcService.Server(), &handler.User{userDataService})
//5.启动服务
if err := repcService.Run(); err != nil {
log.Println("start user service err :", err)
}
}

@ -0,0 +1,418 @@
//*
// @Auth:ShenZ
// @Description:
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.18.1
// source: user.proto
package proto
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
//*
//"avatar": "",
//"clientId": 0,
//"employeeId": 0,
//"id": 0,
//"nickname": "",
//"phone": "",
//"sessionId": "",
//"systemId": 0,
//"token": "",
//"tokenExpireTime": "",
//"unionId": ""
type User struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Avatar string `protobuf:"bytes,1,opt,name=avatar,proto3" json:"avatar,omitempty"`
ClientId int32 `protobuf:"varint,2,opt,name=clientId,proto3" json:"clientId,omitempty"`
EmployeeId int32 `protobuf:"varint,3,opt,name=employeeId,proto3" json:"employeeId,omitempty"`
Nickname string `protobuf:"bytes,4,opt,name=nickname,proto3" json:"nickname,omitempty"`
Phone string `protobuf:"bytes,5,opt,name=phone,proto3" json:"phone,omitempty"`
SessionId string `protobuf:"bytes,6,opt,name=sessionId,proto3" json:"sessionId,omitempty"`
Token string `protobuf:"bytes,7,opt,name=token,proto3" json:"token,omitempty"`
TokenExpireTime string `protobuf:"bytes,8,opt,name=tokenExpireTime,proto3" json:"tokenExpireTime,omitempty"`
UnionId string `protobuf:"bytes,9,opt,name=unionId,proto3" json:"unionId,omitempty"`
Id int32 `protobuf:"varint,10,opt,name=id,proto3" json:"id,omitempty"`
}
func (x *User) Reset() {
*x = User{}
if protoimpl.UnsafeEnabled {
mi := &file_user_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *User) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*User) ProtoMessage() {}
func (x *User) ProtoReflect() protoreflect.Message {
mi := &file_user_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use User.ProtoReflect.Descriptor instead.
func (*User) Descriptor() ([]byte, []int) {
return file_user_proto_rawDescGZIP(), []int{0}
}
func (x *User) GetAvatar() string {
if x != nil {
return x.Avatar
}
return ""
}
func (x *User) GetClientId() int32 {
if x != nil {
return x.ClientId
}
return 0
}
func (x *User) GetEmployeeId() int32 {
if x != nil {
return x.EmployeeId
}
return 0
}
func (x *User) GetNickname() string {
if x != nil {
return x.Nickname
}
return ""
}
func (x *User) GetPhone() string {
if x != nil {
return x.Phone
}
return ""
}
func (x *User) GetSessionId() string {
if x != nil {
return x.SessionId
}
return ""
}
func (x *User) GetToken() string {
if x != nil {
return x.Token
}
return ""
}
func (x *User) GetTokenExpireTime() string {
if x != nil {
return x.TokenExpireTime
}
return ""
}
func (x *User) GetUnionId() string {
if x != nil {
return x.UnionId
}
return ""
}
func (x *User) GetId() int32 {
if x != nil {
return x.Id
}
return 0
}
//请求 request struct
type LoginRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ClientId int32 `protobuf:"varint,1,opt,name=clientId,proto3" json:"clientId,omitempty"`
Phone string `protobuf:"bytes,2,opt,name=phone,proto3" json:"phone,omitempty"`
SystemId int32 `protobuf:"varint,3,opt,name=systemId,proto3" json:"systemId,omitempty"`
VerificationCode string `protobuf:"bytes,4,opt,name=verificationCode,proto3" json:"verificationCode,omitempty"`
}
func (x *LoginRequest) Reset() {
*x = LoginRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_user_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *LoginRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*LoginRequest) ProtoMessage() {}
func (x *LoginRequest) ProtoReflect() protoreflect.Message {
mi := &file_user_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use LoginRequest.ProtoReflect.Descriptor instead.
func (*LoginRequest) Descriptor() ([]byte, []int) {
return file_user_proto_rawDescGZIP(), []int{1}
}
func (x *LoginRequest) GetClientId() int32 {
if x != nil {
return x.ClientId
}
return 0
}
func (x *LoginRequest) GetPhone() string {
if x != nil {
return x.Phone
}
return ""
}
func (x *LoginRequest) GetSystemId() int32 {
if x != nil {
return x.SystemId
}
return 0
}
func (x *LoginRequest) GetVerificationCode() string {
if x != nil {
return x.VerificationCode
}
return ""
}
//响应 resp struct
type LoginResp struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
User *User `protobuf:"bytes,2,opt,name=user,proto3" json:"user,omitempty"`
}
func (x *LoginResp) Reset() {
*x = LoginResp{}
if protoimpl.UnsafeEnabled {
mi := &file_user_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *LoginResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*LoginResp) ProtoMessage() {}
func (x *LoginResp) ProtoReflect() protoreflect.Message {
mi := &file_user_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use LoginResp.ProtoReflect.Descriptor instead.
func (*LoginResp) Descriptor() ([]byte, []int) {
return file_user_proto_rawDescGZIP(), []int{2}
}
func (x *LoginResp) GetToken() string {
if x != nil {
return x.Token
}
return ""
}
func (x *LoginResp) GetUser() *User {
if x != nil {
return x.User
}
return nil
}
var File_user_proto protoreflect.FileDescriptor
var file_user_proto_rawDesc = []byte{
0x0a, 0x0a, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x22, 0x94, 0x02, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, 0x12, 0x16, 0x0a, 0x06,
0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x61, 0x76,
0x61, 0x74, 0x61, 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64,
0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64,
0x12, 0x1e, 0x0a, 0x0a, 0x65, 0x6d, 0x70, 0x6c, 0x6f, 0x79, 0x65, 0x65, 0x49, 0x64, 0x18, 0x03,
0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x65, 0x6d, 0x70, 0x6c, 0x6f, 0x79, 0x65, 0x65, 0x49, 0x64,
0x12, 0x1a, 0x0a, 0x08, 0x6e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01,
0x28, 0x09, 0x52, 0x08, 0x6e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05,
0x70, 0x68, 0x6f, 0x6e, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x70, 0x68, 0x6f,
0x6e, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18,
0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64,
0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52,
0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x28, 0x0a, 0x0f, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x45,
0x78, 0x70, 0x69, 0x72, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52,
0x0f, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x45, 0x78, 0x70, 0x69, 0x72, 0x65, 0x54, 0x69, 0x6d, 0x65,
0x12, 0x18, 0x0a, 0x07, 0x75, 0x6e, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x09, 0x20, 0x01, 0x28,
0x09, 0x52, 0x07, 0x75, 0x6e, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64,
0x18, 0x0a, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x69, 0x64, 0x22, 0x88, 0x01, 0x0a, 0x0c, 0x4c,
0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x63,
0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x63,
0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65,
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x12, 0x1a, 0x0a,
0x08, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52,
0x08, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x2a, 0x0a, 0x10, 0x76, 0x65, 0x72,
0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x04, 0x20,
0x01, 0x28, 0x09, 0x52, 0x10, 0x76, 0x65, 0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
0x6e, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x42, 0x0a, 0x09, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65,
0x73, 0x70, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x1f, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72,
0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x55,
0x73, 0x65, 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x32, 0x39, 0x0a, 0x05, 0x4c, 0x6f, 0x67,
0x69, 0x6e, 0x12, 0x30, 0x0a, 0x05, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x13, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x1a, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65,
0x73, 0x70, 0x22, 0x00, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_user_proto_rawDescOnce sync.Once
file_user_proto_rawDescData = file_user_proto_rawDesc
)
func file_user_proto_rawDescGZIP() []byte {
file_user_proto_rawDescOnce.Do(func() {
file_user_proto_rawDescData = protoimpl.X.CompressGZIP(file_user_proto_rawDescData)
})
return file_user_proto_rawDescData
}
var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_user_proto_goTypes = []interface{}{
(*User)(nil), // 0: proto.User
(*LoginRequest)(nil), // 1: proto.LoginRequest
(*LoginResp)(nil), // 2: proto.LoginResp
}
var file_user_proto_depIdxs = []int32{
0, // 0: proto.LoginResp.user:type_name -> proto.User
1, // 1: proto.Login.Login:input_type -> proto.LoginRequest
2, // 2: proto.Login.Login:output_type -> proto.LoginResp
2, // [2:3] is the sub-list for method output_type
1, // [1:2] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_user_proto_init() }
func file_user_proto_init() {
if File_user_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_user_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*User); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_user_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*LoginRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_user_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*LoginResp); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_user_proto_rawDesc,
NumEnums: 0,
NumMessages: 3,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_user_proto_goTypes,
DependencyIndexes: file_user_proto_depIdxs,
MessageInfos: file_user_proto_msgTypes,
}.Build()
File_user_proto = out.File
file_user_proto_rawDesc = nil
file_user_proto_goTypes = nil
file_user_proto_depIdxs = nil
}

@ -0,0 +1,89 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: user.proto
package proto
import (
fmt "fmt"
proto "google.golang.org/protobuf/proto"
math "math"
)
import (
context "context"
api "go-micro.dev/v4/api"
client "go-micro.dev/v4/client"
server "go-micro.dev/v4/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Login service
func NewLoginEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Login service
type LoginService interface {
//rpc 服务
Login(ctx context.Context, in *LoginRequest, opts ...client.CallOption) (*LoginResp, error)
}
type loginService struct {
c client.Client
name string
}
func NewLoginService(name string, c client.Client) LoginService {
return &loginService{
c: c,
name: name,
}
}
func (c *loginService) Login(ctx context.Context, in *LoginRequest, opts ...client.CallOption) (*LoginResp, error) {
req := c.c.NewRequest(c.name, "Login.Login", in)
out := new(LoginResp)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Login service
type LoginHandler interface {
//rpc 服务
Login(context.Context, *LoginRequest, *LoginResp) error
}
func RegisterLoginHandler(s server.Server, hdlr LoginHandler, opts ...server.HandlerOption) error {
type login interface {
Login(ctx context.Context, in *LoginRequest, out *LoginResp) error
}
type Login struct {
login
}
h := &loginHandler{hdlr}
return s.Handle(s.NewHandler(&Login{h}, opts...))
}
type loginHandler struct {
LoginHandler
}
func (h *loginHandler) Login(ctx context.Context, in *LoginRequest, out *LoginResp) error {
return h.LoginHandler.Login(ctx, in, out)
}

@ -0,0 +1,59 @@
/**
* @Auth:ShenZ
* @Description:
*/
syntax = "proto3"; //
option go_package="./;proto"; //1 2 package
package proto ; //
//
/**
"avatar": "",
"clientId": 0,
"employeeId": 0,
"id": 0,
"nickname": "",
"phone": "",
"sessionId": "",
"systemId": 0,
"token": "",
"tokenExpireTime": "",
"unionId": ""
**/
message User {
string avatar = 1;
int32 clientId = 2;
int32 employeeId =3;
string nickname = 4;
string phone = 5;
string sessionId = 6;
string token = 7;
string tokenExpireTime = 8;
string unionId = 9;
int32 id = 10;
}
/**
{
"clientId": 0,
"phone": "",
"systemId": 0,
"verificationCode": ""
}
**/
// request struct
message LoginRequest {
int32 clientId = 1;
string phone = 2;
int32 systemId = 3;
string verificationCode = 4;
}
// resp struct
message LoginResp{
string token = 1;
User user = 2;
}
//RPC
service Login {
//rpc
rpc Login (LoginRequest) returns (LoginResp){}
}
Loading…
Cancel
Save