作者 yangfu

基础路由修改

... ... @@ -3,8 +3,10 @@ package controllers
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/astaxie/beego/plugins/cors"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
"strconv"
"strings"
... ... @@ -25,6 +27,7 @@ var (
)
type BaseController struct {
Header *protocol.RequestHeader
mybeego.BaseController
}
... ... @@ -91,7 +94,6 @@ func GetRequestHeader(ctx *context.Context) *protocol.RequestHeader {
}
h.CompanyId, _ = strconv.ParseInt(ctx.Input.Header("x-mmm-cid"), 10, 64)
h.UserId, _ = strconv.ParseInt(ctx.Input.Header("x-mmm-id"), 10, 64)
log.Debug(common.AssertJson(h))
return h
}
... ... @@ -258,3 +260,40 @@ var LogRequestData = func(ctx *context.Context) {
log.Info("====>Recv data from client:\nHeadData: %v ", hmap)
}
}
func (this *BaseController) Prepare() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
if this.Ctx.Input.Method() == "OPTIONS" {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
return
}
this.Query = map[string]string{}
input := this.Input()
for k := range input {
this.Query[k] = input.Get(k)
}
if this.Ctx.Input.RequestBody != nil {
this.ByteBody = this.Ctx.Input.RequestBody[:]
if len(this.ByteBody) < 1 {
this.ByteBody = []byte("{}")
}
this.RequestHead = this.GetRequestHead()
this.Header = GetRequestHeader(this.Ctx)
this.Header.SetRequestId(fmt.Sprintf("%v.%v.%s", this.Header.Uid, time.GetTimeByYyyymmddhhmmss(), this.Ctx.Request.URL))
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)))
}
}
func (this *BaseController) Finish() {
if this.Ctx.Input.Method() == "OPTIONS" {
return
}
strByte, _ := json.Marshal(this.Data["json"])
length := len(strByte)
if length > 5000 {
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])))
} else {
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)))
}
}
... ...
package protocol
import "opp/models"
import (
"fmt"
"opp/models"
"sync/atomic"
)
const (
LoginTypePassPord = "signInPassword"
... ... @@ -27,6 +31,20 @@ type RequestHeader struct {
Uid int64 //用户基本信息Id
CompanyId int64
UserId int64 //UserId 唯一标识,唯一关联所有用户信息(=user_company.id)
requestId string //请求编号 md5
reqIndex int64 //请求链序号
}
func (reqHead *RequestHeader) SetRequestId(addString ...string) {
if len(addString) == 0 {
return
}
reqHead.requestId = addString[0]
}
func (reqHead *RequestHeader) GetRequestId() string {
atomic.AddInt64(&reqHead.reqIndex, 1)
return fmt.Sprintf("%s.%d", reqHead.requestId, reqHead.reqIndex)
}
/*Login */
... ...
... ... @@ -52,16 +52,11 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp
return
}
//获取最后一次公司编号给统一用户中心
if userAuth, err = models.GetUserAuthByUserId(user.Id, protocol.DeviceType); err == nil {
if company, err = models.GetCompanyById(userAuth.CurrentCompanyId); err == nil {
if u, e := models.GetUserAuthByUserId(user.Id, protocol.DeviceType); e == nil && user.UserCenterId == id {
if company, e = models.GetCompanyById(u.CurrentCompanyId); e == nil {
getUserRequest.CompanyId = company.UserCenterId
}
}
//else{
// log.Error(err)
// err = protocol.NewErrWithMessage(4139) //authCode无效或过期
// return
//}
//TODO:验证模块权限
//从用户中心获取用户信息
... ...