作者 yangfu

基础路由修改

@@ -3,8 +3,10 @@ package controllers @@ -3,8 +3,10 @@ package controllers
3 import ( 3 import (
4 "crypto/sha256" 4 "crypto/sha256"
5 "encoding/hex" 5 "encoding/hex"
  6 + "encoding/json"
6 "fmt" 7 "fmt"
7 "github.com/astaxie/beego/plugins/cors" 8 "github.com/astaxie/beego/plugins/cors"
  9 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
8 "strconv" 10 "strconv"
9 "strings" 11 "strings"
10 12
@@ -25,6 +27,7 @@ var ( @@ -25,6 +27,7 @@ var (
25 ) 27 )
26 28
27 type BaseController struct { 29 type BaseController struct {
  30 + Header *protocol.RequestHeader
28 mybeego.BaseController 31 mybeego.BaseController
29 } 32 }
30 33
@@ -91,7 +94,6 @@ func GetRequestHeader(ctx *context.Context) *protocol.RequestHeader { @@ -91,7 +94,6 @@ func GetRequestHeader(ctx *context.Context) *protocol.RequestHeader {
91 } 94 }
92 h.CompanyId, _ = strconv.ParseInt(ctx.Input.Header("x-mmm-cid"), 10, 64) 95 h.CompanyId, _ = strconv.ParseInt(ctx.Input.Header("x-mmm-cid"), 10, 64)
93 h.UserId, _ = strconv.ParseInt(ctx.Input.Header("x-mmm-id"), 10, 64) 96 h.UserId, _ = strconv.ParseInt(ctx.Input.Header("x-mmm-id"), 10, 64)
94 - log.Debug(common.AssertJson(h))  
95 return h 97 return h
96 } 98 }
97 99
@@ -258,3 +260,40 @@ var LogRequestData = func(ctx *context.Context) { @@ -258,3 +260,40 @@ var LogRequestData = func(ctx *context.Context) {
258 log.Info("====>Recv data from client:\nHeadData: %v ", hmap) 260 log.Info("====>Recv data from client:\nHeadData: %v ", hmap)
259 } 261 }
260 } 262 }
  263 +
  264 +func (this *BaseController) Prepare() {
  265 + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
  266 + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
  267 + if this.Ctx.Input.Method() == "OPTIONS" {
  268 + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
  269 + return
  270 + }
  271 + this.Query = map[string]string{}
  272 + input := this.Input()
  273 + for k := range input {
  274 + this.Query[k] = input.Get(k)
  275 + }
  276 + if this.Ctx.Input.RequestBody != nil {
  277 + this.ByteBody = this.Ctx.Input.RequestBody[:]
  278 + if len(this.ByteBody) < 1 {
  279 + this.ByteBody = []byte("{}")
  280 + }
  281 + this.RequestHead = this.GetRequestHead()
  282 + this.Header = GetRequestHeader(this.Ctx)
  283 + this.Header.SetRequestId(fmt.Sprintf("%v.%v.%s", this.Header.Uid, time.GetTimeByYyyymmddhhmmss(), this.Ctx.Request.URL))
  284 + log.Debug(fmt.Sprintf("====>Recv data from uid(%d) ucid(%v) client:\nHeadData: %s\nRequestId:%s BodyData: %s", this.Header.Uid, this.Header.UserId, common.AssertJson(this.Header), this.Header.GetRequestId(), string(this.ByteBody)))
  285 + }
  286 +}
  287 +
  288 +func (this *BaseController) Finish() {
  289 + if this.Ctx.Input.Method() == "OPTIONS" {
  290 + return
  291 + }
  292 + strByte, _ := json.Marshal(this.Data["json"])
  293 + length := len(strByte)
  294 + if length > 5000 {
  295 + log.Debug(fmt.Sprintf("<====Send to uid(%d) ucid(%v) client: %d byte\nRequestId:%s RspBodyData: %s......", this.Header.Uid, this.Header.UserId, length, this.Header.GetRequestId(), string(strByte[:5000])))
  296 + } else {
  297 + log.Debug(fmt.Sprintf("<====Send to uid(%d) ucid(%v) client: %d byte\nRequestId:%s RspBodyData: %s", this.Header.Uid, this.Header.UserId, length, this.Header.GetRequestId(), string(strByte)))
  298 + }
  299 +}
1 package protocol 1 package protocol
2 2
3 -import "opp/models" 3 +import (
  4 + "fmt"
  5 + "opp/models"
  6 + "sync/atomic"
  7 +)
4 8
5 const ( 9 const (
6 LoginTypePassPord = "signInPassword" 10 LoginTypePassPord = "signInPassword"
@@ -27,6 +31,20 @@ type RequestHeader struct { @@ -27,6 +31,20 @@ type RequestHeader struct {
27 Uid int64 //用户基本信息Id 31 Uid int64 //用户基本信息Id
28 CompanyId int64 32 CompanyId int64
29 UserId int64 //UserId 唯一标识,唯一关联所有用户信息(=user_company.id) 33 UserId int64 //UserId 唯一标识,唯一关联所有用户信息(=user_company.id)
  34 +
  35 + requestId string //请求编号 md5
  36 + reqIndex int64 //请求链序号
  37 +}
  38 +
  39 +func (reqHead *RequestHeader) SetRequestId(addString ...string) {
  40 + if len(addString) == 0 {
  41 + return
  42 + }
  43 + reqHead.requestId = addString[0]
  44 +}
  45 +func (reqHead *RequestHeader) GetRequestId() string {
  46 + atomic.AddInt64(&reqHead.reqIndex, 1)
  47 + return fmt.Sprintf("%s.%d", reqHead.requestId, reqHead.reqIndex)
30 } 48 }
31 49
32 /*Login */ 50 /*Login */
@@ -52,16 +52,11 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp @@ -52,16 +52,11 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp
52 return 52 return
53 } 53 }
54 //获取最后一次公司编号给统一用户中心 54 //获取最后一次公司编号给统一用户中心
55 - if userAuth, err = models.GetUserAuthByUserId(user.Id, protocol.DeviceType); err == nil {  
56 - if company, err = models.GetCompanyById(userAuth.CurrentCompanyId); err == nil { 55 + if u, e := models.GetUserAuthByUserId(user.Id, protocol.DeviceType); e == nil && user.UserCenterId == id {
  56 + if company, e = models.GetCompanyById(u.CurrentCompanyId); e == nil {
57 getUserRequest.CompanyId = company.UserCenterId 57 getUserRequest.CompanyId = company.UserCenterId
58 } 58 }
59 } 59 }
60 - //else{  
61 - // log.Error(err)  
62 - // err = protocol.NewErrWithMessage(4139) //authCode无效或过期  
63 - // return  
64 - //}  
65 //TODO:验证模块权限 60 //TODO:验证模块权限
66 61
67 //从用户中心获取用户信息 62 //从用户中心获取用户信息