Эх сурвалжийг харах

update:修改T_pid获取方式,路由优化

zoie 2 жил өмнө
parent
commit
8ed6d73119

+ 1 - 3
Nats/NatsServer/NatsDevice.go

@@ -24,9 +24,9 @@ func ReadDeviceByT_sn(T_sn string) (d Device.Device, err error) {
 
 	err = msgpack.Unmarshal(msg.Data, &t_R)
 	if err != nil {
+		fmt.Println("Cold_ReadDeviceByT_sn err : %+v\n", t_R)
 		return d, err
 	}
-	fmt.Printf("Cold_ReadDeviceByT_sn : %+v\n", t_R)
 
 	if t_R.Code != 200 {
 		return d, errors.New(t_R.Msg)
@@ -52,8 +52,6 @@ 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
 }
 

+ 2 - 2
Nats/NatsServer/NatsQiniu.go

@@ -25,7 +25,7 @@ func Qiniu_UploadToken(T_suffix string, T_MimeLimit string) (string, bool) {
 	// 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
 	msg, err := lib.Nats.Request("Qiniu_UploadToken", b, 3*time.Second)
 	if err != nil {
-		fmt.Println(err)
+		fmt.Println("Qiniu_UploadToken Err:", err)
 	} else {
 		fmt.Printf("Qiniu_UploadToken : %s\n", string(msg.Data))
 		return string(msg.Data), true
@@ -50,7 +50,7 @@ func Qiniu_UploadFile(localFile string, name string) (string, bool) {
 	// 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
 	msg, err := lib.Nats.Request("Qiniu_UploadFile", b, 3*time.Second)
 	if err != nil {
-		fmt.Println(err)
+		fmt.Println("Qiniu_UploadFile Err:", err)
 	} else {
 		fmt.Printf("Qiniu_UploadFile : %s\n", string(msg.Data))
 		return string(msg.Data), true

+ 2 - 4
Nats/NatsServer/NatsUser.go

@@ -3,7 +3,6 @@ package NatsServer
 import (
 	"Cold_GoodsOrder/lib"
 	"Cold_GoodsOrder/models/Account"
-	"fmt"
 	"github.com/vmihailenco/msgpack/v5"
 	"time"
 )
@@ -26,6 +25,7 @@ func Verification(GetCookie string, GetString string) (bool, Account.User) {
 	type T_R struct {
 		Code int16        `xml:"Code"`
 		Msg  string       `xml:"Msg"`
+		Pid  int          `xml:"Pid"`  // 公司id
 		Data Account.User `xml:"Data"` // 泛型
 	}
 
@@ -35,8 +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)
-
+	Account.T_pid = t_R.Pid
 	return true, t_R.Data
 
 }
@@ -72,7 +71,6 @@ 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
 }

+ 25 - 33
controllers/GoodsOrder.go

@@ -21,6 +21,21 @@ import (
 // Handle
 type GoodsOrderController struct {
 	beego.Controller
+	User_r Account.User // 登陆的用户
+	T_pid  int          // 公司id
+}
+
+func (c *GoodsOrderController) Prepare() {
+	GetCookie := c.Ctx.GetCookie("User_tokey")
+	GetString := c.GetString("User_tokey")
+
+	User_tokey := GetCookie
+	if len(User_tokey) == 0 {
+		User_tokey = GetString
+	}
+
+	c.User_r = Account.User_r
+	c.T_pid = Account.T_pid
 }
 
 func (c *GoodsOrderController) GoodsOrder_List() {
@@ -35,7 +50,6 @@ func (c *GoodsOrderController) GoodsOrder_List() {
 	var r_jsons R_JSONS
 
 	page, _ := c.GetInt("page")
-	println(page)
 	if page < 1 {
 		page = 1
 	}
@@ -44,15 +58,10 @@ func (c *GoodsOrderController) GoodsOrder_List() {
 		page_z = conf.Page_size
 	}
 
-	T_pid := Account.User_r.T_pid
-	if T_pid == 0 {
-		T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
-	}
-
 	Name := c.GetString("T_name")
 	c.Data["Name"] = Name
 
-	r_jsons.Data, r_jsons.Num = Function.Read_GoodsOrder_List(T_pid, page, page_z, Name)
+	r_jsons.Data, r_jsons.Num = Function.Read_GoodsOrder_List(c.T_pid, page, page_z, Name)
 	r_jsons.Page = page
 	r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
 
@@ -63,19 +72,15 @@ func (c *GoodsOrderController) GoodsOrder_List() {
 func (c *GoodsOrderController) GoodsOrder_Get() {
 
 	id, _ := c.GetInt("T_id")
-	T_pid := Account.User_r.T_pid
-	if T_pid == 0 {
-		T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
-	}
-
 	r := Function.Read_GoodsOrder_ById(id)
-
+	// 记录不存在
 	if r.Id == 0 {
-		c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id !"}
+		c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
 		c.ServeJSON()
 		return
 	}
-	if T_pid != r.T_pid {
+
+	if c.T_pid != r.T_pid {
 		c.Data["json"] = lib.JSONS{Code: 205, Msg: "没有权限!"}
 		c.ServeJSON()
 		return
@@ -101,10 +106,6 @@ func (c *GoodsOrderController) GoodsOrder_Add() {
 		c.ServeJSON()
 		return
 	}
-	T_pid := Account.User_r.T_pid
-	if T_pid == 0 {
-		T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
-	}
 
 	T_start_Ut_, err := time.ParseInLocation("2006-01-02 15:04:05", T_time_s[0], time.Local)
 	T_end_Ut_, err := time.ParseInLocation("2006-01-02 15:04:05", T_time_s[1], time.Local)
@@ -114,7 +115,7 @@ func (c *GoodsOrderController) GoodsOrder_Add() {
 		return
 	}
 	var_ := Function.GoodsOrder{
-		T_pid:        T_pid,
+		T_pid:        c.T_pid,
 		T_orderid:    T_orderid,
 		T_outorderid: T_outorderid,
 		T_sn:         T_sn,
@@ -143,11 +144,6 @@ func (c *GoodsOrderController) GoodsOrder_Edit() {
 
 	id, _ := c.GetInt("T_id")
 
-	T_pid := Account.User_r.T_pid
-	if T_pid == 0 {
-		T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
-	}
-
 	r := Function.Read_GoodsOrder_ById(id)
 
 	if r.Id == 0 {
@@ -155,7 +151,7 @@ func (c *GoodsOrderController) GoodsOrder_Edit() {
 		c.ServeJSON()
 		return
 	}
-	if T_pid != r.T_pid {
+	if c.T_pid != r.T_pid {
 		c.Data["json"] = lib.JSONS{Code: 205, Msg: "没有权限!"}
 		c.ServeJSON()
 		return
@@ -225,10 +221,6 @@ func (c *GoodsOrderController) GoodsOrder_Edit() {
 func (c *GoodsOrderController) GoodsOrder_Del() {
 
 	Id, _ := c.GetInt("T_id")
-	T_pid := Account.User_r.T_pid
-	if T_pid == 0 {
-		T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
-	}
 	r := Function.Read_GoodsOrder_ById(Id)
 
 	if r.Id == 0 {
@@ -236,7 +228,7 @@ func (c *GoodsOrderController) GoodsOrder_Del() {
 		c.ServeJSON()
 		return
 	}
-	if T_pid != r.T_pid {
+	if c.T_pid != r.T_pid {
 		c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"}
 		c.ServeJSON()
 		return
@@ -260,14 +252,14 @@ func (c *GoodsOrderController) GoodsOrder_PDF() {
 	var err error
 	Id, _ := c.GetInt("T_id")
 	if Id == 0 {
-		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id  e!"}
+		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
 		c.ServeJSON()
 		return
 	}
 
 	GoodsOrder_r := Function.Read_GoodsOrder_ById(Id)
 	if GoodsOrder_r.Id == 0 {
-		c.Data["json"] = lib.JSONS{Code: 207, Msg: "Id  e!"}
+		c.Data["json"] = lib.JSONS{Code: 207, Msg: "Id Err!"}
 		c.ServeJSON()
 		return
 	}

+ 1 - 1
main.go

@@ -42,7 +42,7 @@ func main() {
 		ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
 		// 如果设置,则允许共享身份验证凭据,例如cookie
 		AllowCredentials: true,
-	})).InsertFilter("/*", beego.BeforeRouter, routers.FilterRBAC)
+	})).InsertFilter("/*", beego.BeforeRouter, routers.RBACFilter)
 
 	beego.BConfig.AppName = conf.AppName                    // 项目名
 	beego.BConfig.ServerName = conf.AppName + conf.HTTPPort //server  名称

+ 1 - 0
models/Account/Admin.go

@@ -6,6 +6,7 @@ import (
 )
 
 var User_r User
+var T_pid int
 
 type User struct {
 	T_uuid  string `xml:"T_uuid"`

+ 12 - 8
routers/GoodsOrder.go

@@ -7,14 +7,18 @@ import (
 )
 
 func init() {
-	var version = conf.Version
-
 	// 订单管理
-	beego.Router(version+"/GoodsOrder/List", &controllers.GoodsOrderController{}, "*:GoodsOrder_List") // 订单列表
-	beego.Router(version+"/GoodsOrder/Get", &controllers.GoodsOrderController{}, "*:GoodsOrder_Get")   // 获取订单
-	beego.Router(version+"/GoodsOrder/Add", &controllers.GoodsOrderController{}, "*:GoodsOrder_Add")   // 添加订单
-	beego.Router(version+"/GoodsOrder/Edit", &controllers.GoodsOrderController{}, "*:GoodsOrder_Edit") // 编辑订单
-	beego.Router(version+"/GoodsOrder/Del", &controllers.GoodsOrderController{}, "*:GoodsOrder_Del")   // 删除订单
-	beego.Router(version+"/GoodsOrder/PDF", &controllers.GoodsOrderController{}, "*:GoodsOrder_PDF")   // 订单pdf
+	ns := beego.NewNamespace(conf.Version,
+		beego.NSNamespace("/GoodsOrder",
+			beego.NSRouter("List", &controllers.GoodsOrderController{}, "*:GoodsOrder_List"), // 订单列表
+			beego.NSRouter("Get", &controllers.GoodsOrderController{}, "*:GoodsOrder_Get"),   // 获取订单
+			beego.NSRouter("Add", &controllers.GoodsOrderController{}, "*:GoodsOrder_Add"),   // 添加订单
+			beego.NSRouter("Edit", &controllers.GoodsOrderController{}, "*:GoodsOrder_Edit"), // 编辑订单
+			beego.NSRouter("Del", &controllers.GoodsOrderController{}, "*:GoodsOrder_Del"),   // 删除订单
+			beego.NSRouter("PDF", &controllers.GoodsOrderController{}, "*:GoodsOrder_PDF"),   // 订单pdf
+		),
+	)
+
+	beego.AddNamespace(ns)
 
 }

+ 2 - 2
routers/filter.go

@@ -7,7 +7,7 @@ import (
 	"github.com/beego/beego/v2/server/web/context"
 )
 
-func FilterRBAC(ctx *context.Context) {
+func RBACFilter(ctx *context.Context) {
 	var b_ bool
 	b_, Account.User_r = NatsServer.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
 	if !b_ {
@@ -18,7 +18,7 @@ func FilterRBAC(ctx *context.Context) {
 	flag := NatsServer.CheckUserPermissions(Account.User_r.T_power, ctx.Request.URL.Path)
 
 	if !flag {
-		data := lib.JSONS{Code: 201, Msg: "无权访问!"}
+		data := lib.JSONS{Code: 202, Msg: "无权访问!"}
 		ctx.Output.JSON(data, true, false)
 		return
 	}