Browse Source

update:优化登录验证

zoie 2 years ago
parent
commit
9cb41f97f4

+ 3 - 1
.gitignore

@@ -32,4 +32,6 @@ _testmain.go
 /logs/logx
 lastupdate.tmp
 Cold_GoodsOrder
-Makefile
+Cold_GoodsOrder6250
+Makefile
+main

+ 6 - 0
Makefile

@@ -0,0 +1,6 @@
+build:
+	@echo "+ build"
+	go build -o Cold_GoodsOrder6250 main.go
+build-linux:
+	@echo "+ build linux"
+	GOOS=linux GOARCH=amd64 go build -o Cold_GoodsOrder6250 main.go

+ 8 - 3
Nats/NatsServer/NatsDevice.go

@@ -14,7 +14,7 @@ func ReadDeviceByT_sn(T_sn string) (d Device.Device, err error) {
 	if err != nil {
 		return d, err
 	}
-	fmt.Printf("Cold_ReadDeviceSensorALLByT_sn: %s\n", string(msg.Data))
+	//fmt.Printf("Cold_ReadDeviceByT_sn: %s\n", string(msg.Data))
 	type T_R struct {
 		Code int16         `xml:"Code"`
 		Msg  string        `xml:"Msg"`
@@ -26,6 +26,8 @@ func ReadDeviceByT_sn(T_sn string) (d Device.Device, err error) {
 	if err != nil {
 		return d, err
 	}
+	fmt.Printf("Cold_ReadDeviceByT_sn : %+v\n", t_R)
+
 	if t_R.Code != 200 {
 		return d, errors.New(t_R.Msg)
 	}
@@ -38,7 +40,7 @@ func ReadDeviceSensorALLByT_sn(T_sn string) (d []Device.DeviceSensor) {
 	if err != nil {
 		return d
 	}
-	fmt.Printf("Cold_ReadDeviceSensorALLByT_sn: %s\n", string(msg.Data))
+	//fmt.Printf("Cold_ReadDeviceSensorALLByT_sn: %s\n", string(msg.Data))
 	type T_R struct {
 		Code int16                 `xml:"Code"`
 		Msg  string                `xml:"Msg"`
@@ -50,6 +52,7 @@ func ReadDeviceSensorALLByT_sn(T_sn string) (d []Device.DeviceSensor) {
 	if err != nil {
 		return d
 	}
+	fmt.Printf("Cold_ReadDeviceSensorALLByT_sn : %+v\n", t_R)
 
 	return t_R.Data
 }
@@ -73,7 +76,7 @@ func ReadDeviceDataListBy_T_snid(T_snid string, Time_start string, Time_end stri
 
 	// 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
 	msg, err := lib.Nats.Request("Cold_ReadDeviceDataListBy_T_snid", b, 3*time.Second)
-	fmt.Printf("Cold_ReadDeviceDataListBy_T_snid : %s\n", string(msg.Data))
+	//fmt.Printf("Cold_ReadDeviceDataListBy_T_snid : %s\n", string(msg.Data))
 
 	if err != nil {
 		return r, cnt
@@ -93,6 +96,8 @@ func ReadDeviceDataListBy_T_snid(T_snid string, Time_start string, Time_end stri
 		return r, cnt
 	}
 
+	fmt.Printf("Cold_ReadDeviceDataListBy_T_snid : %+v\n", t_R)
+
 	return t_R.Data, t_R.Count
 
 }

+ 3 - 2
Nats/NatsServer/NatsUser.go

@@ -23,7 +23,6 @@ func Verification(GetCookie string, GetString string) (bool, Account.User) {
 	if err != nil {
 		return false, Account.User{}
 	}
-	fmt.Printf("Cold_User_verification : %s\n", string(msg.Data))
 	type T_R struct {
 		Code int16        `xml:"Code"`
 		Msg  string       `xml:"Msg"`
@@ -36,6 +35,7 @@ func Verification(GetCookie string, GetString string) (bool, Account.User) {
 	if err != nil || t_R.Code != 200 {
 		return false, Account.User{}
 	}
+	fmt.Printf("Cold_User_verification : %+v\n", t_R)
 
 	return true, t_R.Data
 
@@ -54,7 +54,7 @@ func CheckUserPermissions(Power_Id int, Req_Url string) bool {
 
 	// 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
 	msg, err := lib.Nats.Request("Cold_User_CheckUserPermissions", b, 3*time.Second)
-	fmt.Printf("Cold_User_CheckUserPermissions : %s\n", string(msg.Data))
+	//fmt.Printf("Cold_User_CheckUserPermissions : %s\n", string(msg.Data))
 
 	if err != nil {
 		return false
@@ -72,6 +72,7 @@ func CheckUserPermissions(Power_Id int, Req_Url string) bool {
 	if err != nil {
 		return false
 	}
+	fmt.Printf("Cold_User_CheckUserPermissions : %+v\n", t_R)
 
 	return t_R.Pass
 }

+ 11 - 51
controllers/GoodsOrder.go

@@ -4,6 +4,7 @@ import (
 	"Cold_GoodsOrder/Nats/NatsServer"
 	"Cold_GoodsOrder/conf"
 	"Cold_GoodsOrder/lib"
+	"Cold_GoodsOrder/models/Account"
 	"Cold_GoodsOrder/models/Device"
 	"Cold_GoodsOrder/models/Function"
 	"fmt"
@@ -23,13 +24,6 @@ type GoodsOrderController struct {
 }
 
 func (c *GoodsOrderController) GoodsOrder_List() {
-	// 验证登录
-	b_, admin_r := NatsServer.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
-	if !b_ {
-		c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
-		c.ServeJSON()
-		return
-	}
 	type R_JSONS struct {
 		//必须的大写开头
 		Data      []Function.GoodsOrderR
@@ -50,7 +44,7 @@ func (c *GoodsOrderController) GoodsOrder_List() {
 		page_z = conf.Page_size
 	}
 
-	T_pid := admin_r.T_pid
+	T_pid := Account.User_r.T_pid
 	if T_pid == 0 {
 		T_pid, _ = c.GetInt("T_pid")
 	}
@@ -67,15 +61,9 @@ func (c *GoodsOrderController) GoodsOrder_List() {
 	return
 }
 func (c *GoodsOrderController) GoodsOrder_Get() {
-	// 验证登录
-	b_, admin_r := NatsServer.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
-	if !b_ {
-		c.Ctx.Redirect(302, "Login")
-		return
-	}
 
 	id, _ := c.GetInt("T_id")
-	T_pid := admin_r.T_pid
+	T_pid := Account.User_r.T_pid
 	if T_pid == 0 {
 		T_pid, _ = c.GetInt("T_pid")
 	}
@@ -98,13 +86,6 @@ func (c *GoodsOrderController) GoodsOrder_Get() {
 	return
 }
 func (c *GoodsOrderController) GoodsOrder_Add() {
-	// 验证登录
-	b_, admin_r := NatsServer.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
-	if !b_ {
-		c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
-		c.ServeJSON()
-		return
-	}
 
 	T_orderid := c.GetString("T_orderid")
 	T_outorderid := c.GetString("T_outorderid")
@@ -120,7 +101,7 @@ func (c *GoodsOrderController) GoodsOrder_Add() {
 		c.ServeJSON()
 		return
 	}
-	T_pid := admin_r.T_pid
+	T_pid := Account.User_r.T_pid
 	if T_pid == 0 {
 		T_pid, _ = c.GetInt("T_pid")
 	}
@@ -152,24 +133,17 @@ func (c *GoodsOrderController) GoodsOrder_Add() {
 		return
 	}
 
-	NatsServer.AddUserLogs(admin_r.T_uuid, "订单系统", "添加", var_)
+	NatsServer.AddUserLogs(Account.User_r.T_uuid, "订单系统", "添加", var_)
 	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
 	c.ServeJSON()
 	return
 
 }
 func (c *GoodsOrderController) GoodsOrder_Edit() {
-	// 验证登录
-	b_, admin_r := NatsServer.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
-	if !b_ {
-		c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
-		c.ServeJSON()
-		return
-	}
 
 	id, _ := c.GetInt("T_id")
 
-	T_pid := admin_r.T_pid
+	T_pid := Account.User_r.T_pid
 	if T_pid == 0 {
 		T_pid, _ = c.GetInt("T_pid")
 	}
@@ -240,7 +214,7 @@ func (c *GoodsOrderController) GoodsOrder_Edit() {
 		return
 	}
 
-	NatsServer.AddUserLogs(admin_r.T_uuid, "订单系统", "修改", r)
+	NatsServer.AddUserLogs(Account.User_r.T_uuid, "订单系统", "修改", r)
 
 	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
 	c.ServeJSON()
@@ -249,16 +223,9 @@ func (c *GoodsOrderController) GoodsOrder_Edit() {
 }
 
 func (c *GoodsOrderController) GoodsOrder_Del() {
-	// 验证登录
-	b_, admin_r := NatsServer.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
-	if !b_ {
-		c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
-		c.ServeJSON()
-		return
-	}
 
 	Id, _ := c.GetInt("T_id")
-	T_pid := admin_r.T_pid
+	T_pid := Account.User_r.T_pid
 	if T_pid == 0 {
 		T_pid, _ = c.GetInt("T_pid")
 	}
@@ -280,7 +247,7 @@ func (c *GoodsOrderController) GoodsOrder_Del() {
 		c.ServeJSON()
 		return
 	}
-	NatsServer.AddUserLogs(admin_r.T_uuid, "订单系统", "删除", r)
+	NatsServer.AddUserLogs(Account.User_r.T_uuid, "订单系统", "删除", r)
 
 	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
 	c.ServeJSON()
@@ -291,13 +258,6 @@ func (c *GoodsOrderController) GoodsOrder_Del() {
 // 列表 - 接口
 func (c *GoodsOrderController) GoodsOrder_PDF() {
 	var err error
-	// 验证登录
-	b_, user_r := NatsServer.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
-	if !b_ {
-		c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
-		c.ServeJSON()
-		return
-	}
 	Id, _ := c.GetInt("T_id")
 	if Id == 0 {
 		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id  e!"}
@@ -360,10 +320,10 @@ func (c *GoodsOrderController) GoodsOrder_PDF() {
 	//use path
 	//pdf.Image("logo.png", 100, 50, &gopdf.Rect{W: 50, H: 50})
 
-	textw, _ := pdf.MeasureTextWidth(user_r.T_name)
+	textw, _ := pdf.MeasureTextWidth(Account.User_r.T_name)
 	pdf.SetX((595 / 2) - (textw / 2))
 	pdf.SetY(40)
-	pdf.Text(user_r.T_name)
+	pdf.Text(Account.User_r.T_name)
 
 	// 线
 	pdf.SetLineWidth(2)

+ 2 - 8
models/Account/Admin.go

@@ -5,6 +5,8 @@ import (
 	_ "github.com/go-sql-driver/mysql"
 )
 
+var User_r User
+
 type User struct {
 	T_uuid  string `xml:"T_uuid"`
 	T_pid   int    `xml:"T_pid"`
@@ -15,11 +17,3 @@ type User struct {
 	T_mail  string `xml:"T_mail"`
 	T_wx    string `xml:"T_wx"`
 }
-
-func AdminListToMap(A []User) map[string]string {
-	maps := make(map[string]string, len(A))
-	for _, v := range A {
-		maps[v.T_uuid] = v.T_name
-	}
-	return maps
-}

+ 0 - 8
models/Account/Company.go

@@ -6,11 +6,3 @@ type Company struct {
 	T_name string `xml:"T_name"` // 公司名称
 	T_uids string `xml:"T_uids"` // 绑定用户 Uid|  如:U1|U2|
 }
-
-func CompanyListToMap(list []Company) map[int]string {
-	maps := make(map[int]string, len(list))
-	for _, v := range list {
-		maps[v.Id] = v.T_name
-	}
-	return maps
-}

+ 2 - 2
models/Function/GoodsOrder.go

@@ -198,10 +198,10 @@ func Read_GoodsOrder_List(T_pid int, page int, page_z int, Name string) (t []Goo
 	cond1 := cond.And("T_pid", T_pid).And("T_State", 1)
 
 	if len(Name) > 0 {
-		cond1 = cond.AndCond(cond1).AndCond(cond.Or("T_sn__icontains", Name).Or("T_orderid__icontains", Name).Or("T_outorderid__icontains", Name))
+		cond1 = cond1.AndCond(cond.Or("T_sn__icontains", Name).Or("T_orderid__icontains", Name).Or("T_outorderid__icontains", Name))
 	}
 
-	qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&r)
+	qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
 	cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
 
 	for _, v := range r {

+ 4 - 3
routers/filter.go

@@ -3,18 +3,19 @@ package routers
 import (
 	"Cold_GoodsOrder/Nats/NatsServer"
 	"Cold_GoodsOrder/lib"
+	"Cold_GoodsOrder/models/Account"
 	"github.com/beego/beego/v2/server/web/context"
 )
 
 func FilterRBAC(ctx *context.Context) {
-
-	b_, admin := NatsServer.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
+	var b_ bool
+	b_, Account.User_r = NatsServer.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
 	if !b_ {
 		ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
 		return
 	}
 
-	flag := NatsServer.CheckUserPermissions(admin.T_power, ctx.Request.URL.Path)
+	flag := NatsServer.CheckUserPermissions(Account.User_r.T_power, ctx.Request.URL.Path)
 
 	if !flag {
 		data := lib.JSONS{Code: 201, Msg: "无权访问!"}