zhou.xiaoning 3 years ago
parent
commit
8c061d5045

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+log/

+ 1 - 0
api/v1/account/login.go

@@ -0,0 +1 @@
+package account

+ 4 - 4
config.yaml

@@ -12,7 +12,7 @@ zap:
 
 # jwt configuration
 jwt:
-  signing-key: 'Muchinfo'
+  signing-key: 'IVThJraI1R52mE7b'
   expires-time: 604800
   buffer-time: 86400
   issuer: 'Muchinfo'
@@ -20,7 +20,7 @@ jwt:
 # redis configuration
 redis:
   db: 0
-  addr: '127.0.0.1:6379'
+  addr: '192.168.31.201:5007'
   password: ''
 
 # system configuration
@@ -39,14 +39,14 @@ oracle:
   address: '192.168.31.88'
   name: 'orcl'
   port: '1521'
-  user: 'mtp2_test176'
+  user: 'mtp2_test201'
   pwd: 'muchinfo'
   max-idle-conns: 10
   max-open-conns: 100
 
 # rabbitmq configuration
 rabbitmq:
-  url: 'amqp://guest:guest@192.168.31.203:5020/test'
+  url: 'amqp://guest:guest@192.168.31.201:5020/test'
   exchange: 'entry'
 
 # 跨域配置

+ 30 - 1
core/server.go

@@ -1,10 +1,39 @@
 package core
 
-import "mtp20access/initialize"
+import (
+	"fmt"
+	"mtp20access/global"
+	"mtp20access/initialize"
+	"net/http"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.uber.org/zap"
+)
+
+type server interface {
+	ListenAndServe() error
+}
 
 func RunServer() {
 	// 初始化redis服务
 	initialize.Redis()
 	// 从redis加载jwt数据
 
+	// 启动服务
+	Router := initialize.Routers()
+	address := fmt.Sprintf(":%d", global.M2A_CONFIG.System.Addr)
+	s := initServer(address, Router)
+	global.M2A_LOG.Info("server run success on ", zap.String("address", address))
+	global.M2A_LOG.Error(s.ListenAndServe().Error())
+}
+
+func initServer(address string, router *gin.Engine) server {
+	return &http.Server{
+		Addr:           address,
+		Handler:        router,
+		ReadTimeout:    20 * time.Second,
+		WriteTimeout:   20 * time.Second,
+		MaxHeaderBytes: 1 << 20,
+	}
 }

+ 12 - 6
global/global.go

@@ -6,18 +6,24 @@ import (
 	"github.com/go-redis/redis/v8"
 	"github.com/songzhibin97/gkit/cache/local_cache"
 	"github.com/spf13/viper"
+	"github.com/streadway/amqp"
 	"go.uber.org/zap"
 	"golang.org/x/sync/singleflight"
 	"xorm.io/xorm"
 )
 
-var (
-	M2A_DB     *xorm.Engine
-	M2A_REDIS  *redis.Client
-	M2A_CONFIG config.Server
-	M2A_VP     *viper.Viper
-	M2A_LOG    *zap.Logger
+type RabbitMQ struct {
+	Connection *amqp.Connection
+	Channel    *amqp.Channel
+}
 
+var (
+	M2A_DB                  *xorm.Engine
+	M2A_REDIS               *redis.Client
+	M2A_CONFIG              config.Server
+	M2A_VP                  *viper.Viper
+	M2A_LOG                 *zap.Logger
+	M2A_RABBITMQ            *RabbitMQ
 	M2A_Concurrency_Control = &singleflight.Group{}
 	BlackCache              local_cache.Cache
 )

+ 3 - 2
go.mod

@@ -7,14 +7,15 @@ require (
 	github.com/gin-gonic/gin v1.8.1
 	github.com/go-redis/redis/v8 v8.11.5
 	github.com/golang-jwt/jwt/v4 v4.4.2
-	github.com/google/uuid v1.3.0
 	github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
+	github.com/mattn/go-oci8 v0.1.1
 	github.com/songzhibin97/gkit v1.2.5
 	github.com/spf13/viper v1.12.0
+	github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271
 	github.com/swaggo/gin-swagger v1.3.0
+	github.com/xormplus/xorm v0.0.0-20210822100304-4e1d4fcc1e67
 	go.uber.org/zap v1.21.0
 	golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
-	gorm.io/gorm v1.23.8
 	xorm.io/xorm v1.3.1
 )
 

+ 5 - 5
go.sum

@@ -328,9 +328,6 @@ github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0f
 github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
 github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
 github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
-github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
-github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
-github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
 github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
 github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
 github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
@@ -394,6 +391,8 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
 github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
 github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
 github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
+github.com/mattn/go-oci8 v0.1.1 h1:aEUDxNAyDG0tv8CA3TArnDQNyc4EhnWlsfxRgDHABHM=
+github.com/mattn/go-oci8 v0.1.1/go.mod h1:wjDx6Xm9q7dFtHJvIlrI99JytznLw5wQ4R+9mNXJwGI=
 github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
 github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
 github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw=
@@ -520,6 +519,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
 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/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
+github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271 h1:WhxRHzgeVGETMlmVfqhRn8RIeeNoPr2Czh33I4Zdccw=
 github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
 github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -555,6 +555,8 @@ github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95
 github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
 github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
 github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
+github.com/xormplus/xorm v0.0.0-20210822100304-4e1d4fcc1e67 h1:POBVR/O5wPVI0u7ZZGqu+HFPVTYpZlSL3Uo/9g+he/w=
+github.com/xormplus/xorm v0.0.0-20210822100304-4e1d4fcc1e67/go.mod h1:+v6b10b4x5IcQmp1/Cbo9IqaknxVeuhQng+fhya6bdI=
 github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
 github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
 github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -982,8 +984,6 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
-gorm.io/gorm v1.23.8 h1:h8sGJ+biDgBA1AD1Ha9gFCx7h8npU7AsLdlkX0n2TpE=
-gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
 honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

+ 0 - 21
initialize/internal/logger.go

@@ -1,22 +1 @@
 package internal
-
-import (
-	"fmt"
-	"mtp20access/global"
-
-	"gorm.io/gorm/logger"
-)
-
-type writer struct {
-	logger.Writer
-}
-
-// NewWriter writer 构造函数
-func NewWriter(w logger.Writer) *writer {
-	return &writer{Writer: w}
-}
-
-// Printf 格式化打印日志
-func (w *writer) Printf(message string, data ...interface{}) {
-	global.M2A_LOG.Info(fmt.Sprintf(message+"\n", data...))
-}

+ 28 - 0
initialize/rabbitmq.go

@@ -0,0 +1,28 @@
+package initialize
+
+import (
+	"mtp20access/global"
+
+	"github.com/streadway/amqp"
+	"go.uber.org/zap"
+)
+
+func RabbitMQ() *global.RabbitMQ {
+	url := global.M2A_CONFIG.Rabbitmq.Url
+	connection, err := amqp.Dial(url)
+	if err != nil {
+		global.M2A_LOG.Error("rabbitmq connect failed, err:", zap.Error(err))
+		return nil
+	}
+	channel, err := connection.Channel()
+	if err != nil {
+		global.M2A_LOG.Error("rabbitmq open channel failed, err:", zap.Error(err))
+		return nil
+	}
+	global.M2A_LOG.Info("rabbitmq connect successed.")
+
+	return &global.RabbitMQ{
+		Connection: connection,
+		Channel:    channel,
+	}
+}

+ 10 - 0
initialize/xorm_oracle.go

@@ -4,6 +4,8 @@ import (
 	"fmt"
 	"mtp20access/global"
 
+	_ "github.com/mattn/go-oci8"
+	"go.uber.org/zap"
 	"xorm.io/xorm"
 )
 
@@ -18,11 +20,19 @@ func XormOracle() *xorm.Engine {
 		global.M2A_CONFIG.Oracle.Name)
 
 	if engine, err := xorm.NewEngine(driver, dbsource); err != nil {
+		global.M2A_LOG.Error("oracle connect failed, err:", zap.Error(err))
 		return nil
 	} else {
 		engine.SetMaxIdleConns(global.M2A_CONFIG.Oracle.MaxIdleConns)
 		engine.SetMaxOpenConns(global.M2A_CONFIG.Oracle.MaxOpenConns)
 
+		// 控制台打印出生成的SQL语句
+		engine.ShowSQL(true)
+		// 不仅打印到控制台,而是保存为文件
+		// engine.SetLogger(log.NewSimpleLogger(global.M2A_LOG))
+
+		global.M2A_LOG.Info("oracle connect successed.")
+
 		return engine
 	}
 }

+ 13 - 1
main.go

@@ -22,7 +22,19 @@ func main() {
 	if global.M2A_DB != nil {
 		// 程序结束前关闭数据库链接
 		defer global.M2A_DB.DB().Close()
+	} else {
+		return
 	}
 
-	//
+	// 连接RabbitMQ
+	global.M2A_RABBITMQ = initialize.RabbitMQ()
+	if global.M2A_RABBITMQ != nil {
+		defer global.M2A_RABBITMQ.Connection.Close()
+	} else {
+		return
+	}
+
+	// 启动服务
+	core.RunServer()
+	defer global.M2A_REDIS.Close()
 }

+ 1 - 0
middleware/jwt.go

@@ -12,6 +12,7 @@ import (
 	"go.uber.org/zap"
 )
 
+// FIXME - Token校验机制不适用,应重新设计
 func JWTAuth() gin.HandlerFunc {
 	return func(c *gin.Context) {
 		// 我们这里jwt鉴权取头部信息 x-token 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localStorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录

+ 38 - 0
model/account/account.go

@@ -0,0 +1,38 @@
+package account
+
+import "time"
+
+// Loginaccount 登录账户表
+type Loginaccount struct {
+	LOGINID            int64     `json:"loginid" xorm:"LOGINID"`                       // 登陆账号
+	USERID             int64     `json:"userid" xorm:"USERID"`                         // 用户ID
+	LOGINFAILNUM       int32     `json:"loginfailnum" xorm:"LOGINFAILNUM"`             // 连续登录失败次数(登录成功时清零)
+	PASSWORD           string    `json:"password" xorm:"PASSWORD"`                     // 登陆密码
+	LOGINUSERTYPE      int32     `json:"loginusertype" xorm:"LOGINUSERTYPE"`           // 登录账号类型 - 1:投资者 2:机构交易员
+	LOGINSTATUS        int32     `json:"loginstatus" xorm:"LOGINSTATUS"`               // 登录账号状态 1:正常 2:冻结(停用) 3:无效(注销)
+	LASTLOGINTIME      time.Time `json:"lastlogintime" xorm:"LASTLOGINTIME"`           // 最新登录时间
+	LASTLOGINMODE      int32     `json:"lastloginmode" xorm:"LASTLOGINMODE"`           // 最新登录方式(客户端类型) -  0:保留为未填终端类型 1:PC管理端 2:PC交易端 3:手机客户端_安卓 4:网页客户端 5:微信客户端 6:手机客户端_苹果 7:网上开户客户端 8:无效终端编号 9:报价终端(中江)
+	LASTLOGINIP        string    `json:"lastloginip" xorm:"LASTLOGINIP"`               // 最新登录地址
+	LOGINPORT          int32     `json:"loginport" xorm:"LOGINPORT"`                   // 最新登录端口
+	LASTLOGINREMARK    string    `json:"lastloginremark" xorm:"LASTLOGINREMARK"`       // 最新登录描述
+	MODIFYTIME         time.Time `json:"modifytime" xorm:"MODIFYTIME"`                 // 修改时间
+	MODIFIERID         int64     `json:"modifierid" xorm:"MODIFIERID"`                 // 修改人
+	PWDWRONGCOUNT      int32     `json:"pwdwrongcount" xorm:"PWDWRONGCOUNT"`           // 密码错误次数
+	UNFREEZETIME       time.Time `json:"unfreezetime" xorm:"UNFREEZETIME"`             // 账户解冻时间
+	HASLOGINED         int32     `json:"haslogined" xorm:"HASLOGINED"`                 // 是否已登录 - 0:未登录 1:已登录
+	HASUPDATEDPWD      int32     `json:"hasupdatedpwd" xorm:"HASUPDATEDPWD"`           // 是否已更改密码 - 0:未修改 1:已修改
+	LOGINCODE          string    `json:"logincode" xorm:"LOGINCODE"`                   // 登录代码
+	CANOUTIN           int32     `json:"canoutin" xorm:"CANOUTIN"`                     // 是否可出入金 - 0:不可 1:可
+	CLIENTROLEID       int32     `json:"clientroleid" xorm:"CLIENTROLEID"`             // 终端角色ID
+	TAACCOUNTRIGHTTYPE int32     `json:"taaccountrighttype" xorm:"TAACCOUNTRIGHTTYPE"` // 资金账号权限类型 - 1:不选默认为所有权限 2:不选默认为无权限
+	MOBILE             string    `json:"mobile" xorm:"MOBILE"`                         // 手机号码(加密存储)
+	ACCOUNTNAME        string    `json:"accountname" xorm:"ACCOUNTNAME"`               // 账户名称
+	MODIFYSRC          int32     `json:"modifysrc" xorm:"MODIFYSRC"`                   // 修改人来源 - 1:管理端 2:终端
+	RELATEDUSERID      int64     `json:"relateduserid" xorm:"RELATEDUSERID"`           // 关联用户ID(机构)
+	SUBJECTID          int64     `json:"subjectid" xorm:"SUBJECTID"`                   // 所属部门(管理部门)
+}
+
+// TableName is LOGINACCOUNT
+func (r *Loginaccount) TableName() string {
+	return "LOGINACCOUNT"
+}

+ 8 - 0
model/account/request/login.go

@@ -0,0 +1,8 @@
+package request
+
+// LoginReq 登录请求入参
+type LoginReq struct {
+	UserName string `from:"userName" binding:"required"` // 用户名,可以是LoginID/LoginCode/手机号码
+	Password string `from:"password" binding:"required"` // 密码
+	Captcha  string `from:"captcha"`                     // 验证码
+}

+ 1 - 0
model/account/response/login.go

@@ -0,0 +1 @@
+package response

+ 1 - 0
service/account/login.go

@@ -0,0 +1 @@
+package account