作者 yangfu

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/mmm-go/oppmg into dev

# Conflicts:
#	models/user_company.go
#	services/auth/auth.go
... ... @@ -48,12 +48,20 @@ func (this *BaseController) ResposeJson(msg *protocol.ResponseMessage) {
func (this *BaseController) GetCompanyId() int64 {
v := this.Ctx.Input.GetData(protocol.HeaderCompanyid)
companyid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
if beego.BConfig.RunMode != "prod" && companyid == 0 {
companyid, _ = strconv.ParseInt(this.Ctx.Input.Header("x-mmm-cid"), 10, 64)
}
return companyid
}
func (this *BaseController) GetUserId() int64 {
v := this.Ctx.Input.GetData(protocol.HeaderUserid)
userid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
if beego.BConfig.RunMode != "prod" && userid == 0 {
userid, _ = strconv.ParseInt(this.Ctx.Input.Header("x-mmm-uid"), 10, 64)
}
return userid
}
... ...
... ... @@ -31,9 +31,9 @@ func (this *BulletinController) BulletinRelease() {
msg = protocol.BadRequestParam("1")
return
}
if request.Type != 1 || request.Type != 2 {
if !(request.Type == 1 || request.Type == 2) {
msg = protocol.BadRequestParam("1")
log.Error("type error :", request.Type)
log.Error("type error :%v", request.Type)
return
}
if b, m := this.Valid(request); !b {
... ... @@ -79,15 +79,15 @@ func (this *BulletinController) GetBulletin() {
this.ResposeJson(msg)
}()
var request *protocol.GetBulletinRequest
if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
//if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
// log.Error("json 解析失败", err)
// msg = protocol.BadRequestParam("1")
// return
//}
//if b, m := this.Valid(request); !b {
// msg = m
// return
//}
param := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(param, 10, 64)
if id == 0 {
... ...
... ... @@ -2,6 +2,7 @@ package middleware
import (
"fmt"
"github.com/astaxie/beego"
"oppmg/common/log"
"oppmg/protocol"
serveauth "oppmg/services/auth"
... ... @@ -53,6 +54,9 @@ var LogRequestData = func(ctx *context.Context) {
//AuthToken Before Router
var AuthToken = func(ctx *context.Context) {
log.Debug("执行中间件AuthToken")
if beego.BConfig.RunMode != "prod" {
return
}
var (
storetoken redisdata.RedisLoginToken
msg *protocol.ResponseMessage
... ...
... ... @@ -83,14 +83,14 @@ func DeleteBulletin(id int) (err error) {
}
func GetBulletins(companyId int64, status int8, page, pageSize int) (v []*Bulletin, total int, err error) {
sql := "select * from bulletin where (status=? or ?==0) and company_id =? order by create_at desc limit ?,?"
sql := "select * from bulletin where (status=? or ?=0) and company_id =? order by create_at desc limit ?,?"
o := orm.NewOrm()
if _, err = o.Raw(sql, status, companyId, page-1, pageSize).QueryRows(&v); err != nil {
if _, err = o.Raw(sql, status, status, companyId, page-1, pageSize).QueryRows(&v); err != nil {
return
}
sqlTotal := "select count(1) from bulletin where (status=? or ?==0) and company_id =?"
if _, err = o.Raw(sqlTotal, status, companyId).QueryRows(&total); err != nil {
sqlTotal := "select count(1) from bulletin where (status=? or ?=0) and company_id =?"
if err = o.Raw(sqlTotal, status, status, companyId).QueryRow(&total); err != nil {
return
}
return
... ...
... ... @@ -15,8 +15,6 @@ type BulletinQuestion struct {
Content string `orm:"column(content);size(2000);null" description:"内容"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
MustRead int8 `orm:"column(must_read);null" description:"公告内容看完才可以关闭 1:是 0:否"`
MustAnswer int8 `orm:"column(must_answer);null" description:"回答完问题才可以关闭 1:是 0:否"`
}
func (t *BulletinQuestion) TableName() string {
... ...
... ... @@ -110,7 +110,7 @@ func getUserNameByIds(ids []int64) ([]User, error) {
o := orm.NewOrm()
_, err = o.QueryTable(&User{}).
Filter("id__in", ids).
Filter("delete_at", time.Unix(0, 0)).All(&users)
Filter("delete_at", 0).All(&users)
if err == orm.ErrNoRows {
return users, nil
... ...
package protocol
const (
BulletinUnRelease = 1 //下架
BulletinRelease = 2 //上架
)
const (
QuestionSingleSelect = 0 //单选
QuestionMultiSelect = 1 //多选
)
/*BulletinRelease */
type BulletinReleaseRequest struct {
Id int `json:"id"`
... ... @@ -20,11 +30,11 @@ type Question struct {
Content []QuestionContent `json:"content" valid:"Required"`
}
type QuestionContent struct {
Id int
Content string
Id int `json:"id" valid:"Required"`
Content string `json:"content" valid:"Required"`
}
type Cover struct {
Path string `json:"path"`
Path string `json:"path" valid:"Required"`
H int `json:"h"`
W int `json:"w"`
}
... ... @@ -43,28 +53,33 @@ type BulletinListResponse struct {
}
type BulletinItem struct {
Id int `json:"id"`
Type int8 `json:"type"`
Title string `json:"title"`
Status int8 `json:"status"`
Receiver []string `json:"receiver" valid:"Required"`
CreateAt string `json:"time"`
Id int `json:"id"`
Type int8 `json:"type"`
Title string `json:"title"`
Status int8 `json:"status"`
Receiver []Receiver `json:"receiver" valid:"Required"`
CreateAt string `json:"time"`
}
/*GetBulletin */
type GetBulletinRequest struct {
}
type GetBulletinResponse struct {
Id int `json:"id"`
Type int `json:"type" valid:"Required"`
Title string `json:"title" valid:"Required"`
Content string `json:"content" valid:"Required"`
AllowClose int `json:"allow_close"`
AllowCondition int `json:"allow_condition"`
QuestionSwitch int `json:"question_switch"`
Receiver []string `json:"receiver" valid:"Required"`
Question Question `json:"question"`
Cover Cover `json:"cover" valid:"Required"`
Id int `json:"id"`
Type int `json:"type" valid:"Required"`
Title string `json:"title" valid:"Required"`
Content string `json:"content" valid:"Required"`
AllowClose int `json:"allow_close"`
AllowCondition int `json:"allow_condition"`
QuestionSwitch int `json:"question_switch"`
Receiver []Receiver `json:"receiver" valid:"Required"`
Question Question `json:"question"`
Cover Cover `json:"cover" valid:"Required"`
}
type Receiver struct {
Id int64 `json:"id"`
NickName string `json:"name"`
}
/*UpdateBulletin */
... ...
... ... @@ -38,10 +38,10 @@ func init() {
beego.NSRouter("/refresh_token", &controllers.AuthController{}, "get:RefreshToken"),
),
beego.NSNamespace("/bulletin",
beego.NSRouter("/release", &controllers.BulletinController{}, "post:BulletinRelease"),
beego.NSRouter("/add", &controllers.BulletinController{}, "post:BulletinRelease"),
beego.NSRouter("/list", &controllers.BulletinController{}, "post:BulletinList"),
beego.NSRouter("/:id([0-9]+)", &controllers.BulletinController{}, "get:GetBulletin"),
beego.NSRouter("/update", &controllers.BulletinController{}, "get:UpdateBulletin"),
beego.NSRouter("/update", &controllers.BulletinController{}, "post:UpdateBulletin"),
),
)
... ...
... ... @@ -37,6 +37,7 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ
CompanyId: companyId,
CreateAt: time.Now(),
UpdateAt: time.Now(),
Status: protocol.BulletinUnRelease,
}
orm := orm2.NewOrm()
... ... @@ -55,6 +56,11 @@ func BulletinRelease(uid, companyId int64, request *protocol.BulletinReleaseRequ
log.Error(err.Error())
return
}
if !(request.Question.Type == protocol.QuestionSingleSelect || request.Question.Type == protocol.QuestionMultiSelect) {
err = protocol.NewErrWithMessage("1")
log.Error("BulletinRelease:Question.Type error:%v", request.Question.Type)
return
}
bulletinQuestion = &models.BulletinQuestion{
BulletinId: int(id),
Type: int8(request.Question.Type),
... ... @@ -86,6 +92,7 @@ func BulletinList(uid, companyId int64, request *protocol.BulletinListRequest) (
if request.PageSize == 0 {
request.PageSize = 20
}
rsp = &protocol.BulletinListResponse{}
if list, total, err = models.GetBulletins(companyId, request.Status, request.Page, request.PageSize); err != nil {
log.Error(err.Error())
return
... ... @@ -93,16 +100,14 @@ func BulletinList(uid, companyId int64, request *protocol.BulletinListRequest) (
if len(list) == 0 {
return
}
rsp = &protocol.BulletinListResponse{}
rsp.Total = total
for i := range list {
bulletin := list[i]
item := &protocol.BulletinItem{
Id: bulletin.Id,
Type: bulletin.Type,
Title: bulletin.Title,
Status: int8(bulletin.Status),
//TODO:user
Receiver: []string{},
Id: bulletin.Id,
Type: bulletin.Type,
Title: bulletin.Title,
Status: int8(bulletin.Status),
CreateAt: bulletin.CreateAt.Format("2006-01-02 15:04:05"),
}
if item.Receiver, err = getUsersName(bulletin.Receiver); err != nil {
... ... @@ -111,7 +116,7 @@ func BulletinList(uid, companyId int64, request *protocol.BulletinListRequest) (
}
rsp.List = append(rsp.List, item)
}
rsp.Total = total
return
}
... ... @@ -131,13 +136,16 @@ func getUsers(idsstr string) (v []models.User, err error) {
return models.GetUserNameByIds(ids)
}
func getUsersName(idsStr string) (v []string, err error) {
func getUsersName(idsStr string) (v []protocol.Receiver, err error) {
var users []models.User
if users, err = getUsers(idsStr); err != nil {
return
}
for i := range users {
v = append(v, users[i].NickName)
v = append(v, protocol.Receiver{
Id: users[i].Id,
NickName: users[i].NickName,
})
}
return
}
... ... @@ -178,6 +186,7 @@ func GetBulletin(id int, companyId int64, request *protocol.GetBulletinRequest)
}
rsp.QuestionSwitch = int(bulletin.QuestionSwitch)
rsp.Question = protocol.Question{
Id: question.Id,
Type: int(question.Type),
Title: question.Title,
}
... ... @@ -238,12 +247,18 @@ func UpdateBulletin(companyId int64, request *protocol.UpdateBulletinRequest) (r
log.Error(err.Error())
return
}
if bulletinQuestion.BulletinId != bulletin.Id {
err = protocol.NewErrWithMessage("1")
log.Error("UpdateBulletin:BulletinId not equal:(%v!=%v)", bulletinQuestion.BulletinId, bulletin.Id)
return
}
if questionContent, err = json.Marshal(request.Question.Content); err != nil {
log.Error(err.Error())
return
}
bulletinQuestion.Content = string(questionContent)
bulletinQuestion.Type = int8(request.Question.Type)
bulletinQuestion.UpdateAt = time.Now()
if err = models.UpdateBulletinQuestionById(bulletinQuestion); err != nil {
log.Error(err.Error())
return
... ...
... ... @@ -122,3 +122,237 @@
- 备注:切换公司实际是变更 token 信息
---
## 公告
### 添加公告
- 请求路径 :/v1/bulletin/add
- 请求方式 :post
- 请求 json:
```json
{
"type":2,
"title":"标题",
"content":"公告内容",
"allow_close":0,
"allow_condition":3,
"question_switch":1,
"receiver":["1","2"],
"question":
{
"id":1,
"type":1,
"title":"今天星期几?",
"content":[
{
"id":1,
"content":"星期1"
},
{
"id":2,
"content":"星期2"
}
]
},
"cover":{
"path":"/xx/xx.img",
"w":50,
"h":70
}
}
```
```
obj.type 公告类型(1图+跳转链接、2纯文本)
obj.title 标题
obj.content 公告内容
obj.allow_close 允许关闭公告(0允许,1禁止)
obj.allow_condition 关闭条件 (1(bit 0):公告内容查看完 2(bit 1):回答完问题)
obj.question_switch 设置问题开关 (是否有问题) 1:是 0:否
obj.receiver 接收者
obj.question.type 类型:0-单选,1-多选
obj.question.title 标题
obj.question.content 问题选项
obj.cover 封面
```
- 响应 json
```json
{
"code": "00000",
"msg": "成功",
"data": {}
}
```
---
### 公告列表
- 请求路径 :/v1/bulletin/list
- 请求方式 :post
- 请求 json:
```json
{
"page":1,
"page_size":20,
"status":0
}
```
```
obj.status 状态 0-所有 1-下架 2-上架
```
- 响应 json
```json
{
"code": "00000",
"msg": "成功",
"data": {
"list": [
{
"id": 5,
"type": 2,
"title": "测试公告",
"status": 1,
"receiver": [
{
"id": 1,
"name": "Jennifer Clark"
},
{
"id": 2,
"name": "邓娱婷1208"
}
],
"time": "2019-12-11 17:11:43"
}
],
"Total": 2
}
}
```
### 公告详情
- 请求路径 :/v1/bulletin/:id
- 请求方式 :get
- 请求 json:
```json
{
}
```
```
obj.status 状态 0-所有 1-下架 2-上架
```
- 响应 json
```json
{
"code": "00000",
"msg": "成功",
"data": {
"id": 5,
"type": 2,
"title": "测试公告",
"content": "今天发布了一则公告",
"allow_close": 0,
"allow_condition": 3,
"question_switch": 1,
"receiver": [
{
"id": 1,
"name": "Jennifer Clark"
},
{
"id": 2,
"name": "邓娱婷1208"
}
],
"question": {
"id": 4,
"type": 1,
"title": "今天星期几?",
"content": [
{
"id": 1,
"content": "星期1"
},
{
"id": 2,
"content": "星期2"
}
]
},
"cover": {
"path": "/xx/xx.img",
"h": 70,
"w": 50
}
}
}
```
### 公告更新
- 请求路径 :/v1/bulletin/update
- 请求方式 :post
- 请求 json:
```json
{
"id":3,
"type":2,
"title":"测试公告3",
"content":"今天发布了一则公告3",
"allow_close":0,
"allow_condition":2,
"question_switch":1,
"receiver":["1","2","3"],
"question":
{
"id":2,
"type":1,
"title":"今天星期几?",
"content":[
{
"id":1,
"content":"星期2"
},
{
"id":2,
"content":"星期3"
}
]
},
"cover":{
"path":"/xx/xx.img",
"w":50,
"h":70
}
}
```
- 响应 json
```json
{
"code": "00000",
"msg": "成功",
"data": null
}
```
... ...