作者 yangfu

1.推送支持其他项目编码

... ... @@ -12,7 +12,7 @@ cname ="https://media.fjmaimaimai.com/"
#数据库相关
MYSQL_USER = "${MYSQL_USER||root1}"
MYSQL_PASSWORD = "${MYSQL_PASSWORD||sutianxia20181}"
MYSQL_HOST = "${MYSQL_HOST||101.37.68.231}"
MYSQL_PORT = "${MYSQL_PORT||33061}"
MYSQL_DB_NAME = "${MYSQL_DB_NAME||mmm_open_test1}"
\ No newline at end of file
MYSQL_PASSWORD = "${MYSQL_PASSWORD||sutianxia2018}"
MYSQL_HOST = "${MYSQL_HOST||101.37.68.23}"
MYSQL_PORT = "${MYSQL_PORT||3306}"
MYSQL_DB_NAME = "${MYSQL_DB_NAME||mmm_open_test}"
\ No newline at end of file
... ...
package main
import _ "openapi/pkg/log"
import (
"github.com/astaxie/beego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"openapi/pkg/constant"
_ "openapi/pkg/infrastructure/bgorm"
_ "openapi/pkg/log"
_ "openapi/pkg/port/beego"
)
... ... @@ -14,6 +15,6 @@ func main() {
log.Info("server on stop!")
}()
log.Info("server on start!")
constant.DebugConfig()
//constant.DebugConfig()
beego.Run()
}
... ...
... ... @@ -32,7 +32,10 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ
err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey))
return
}
if deviceList, err = repDevice.Find(map[string]interface{}{"receivers": request.Receivers}); err != nil {
if deviceList, err = repDevice.Find(
map[string]interface{}{
"receivers": request.Receivers, "project_master_key": appInfo.ProjectMasterKey},
); err != nil {
log.Error(err)
err = nil
return
... ... @@ -117,20 +120,22 @@ func UpdateDevice(header *protocol.RequestHeader, request *protocol.UpdateDevice
)
rsp = &protocol.UpdateDeviceResponse{}
rep, _ := repository.NewPushDeviceRepository(nil)
if _, err = rep.FindOne(map[string]interface{}{"uid": request.Muid}); err != nil {
if _, err = rep.FindOne(map[string]interface{}{
"uid": request.Muid,
"project_master_key": request.ProjectKey,
}); err != nil {
if err == protocol.ERR_DB_NOT_FOUND {
err = nil
rep.Save(request)
err = rep.Save(request)
if err != nil {
log.Error(err)
}
return
}
log.Error(err)
return
}
//if device.ClientId == strings.TrimSpace(request.ClientId) && device.DeviceToken == strings.TrimSpace(request.DeviceToken) && device.IsActive==1{
// err = protocol.NewSuccessWithMessage("更新成功")
// return
//}
if err = rep.UpdateDevice(request.Muid, request.ClientId, request.DeviceToken); err != nil {
if err = rep.UpdateDevice(request.Muid, request.ClientId, request.DeviceToken, request.ProjectKey); err != nil {
log.Error(err)
}
err = protocol.NewSuccessWithMessage("更新成功")
... ...
... ... @@ -31,6 +31,7 @@ type PushInfoRequest struct {
/*UpdateDevice 更新设备*/
type UpdateDeviceRequest struct {
ProjectKey string `json:"projectKey"` //项目编号
Muid int64 `json:"muid" valid:"Required;"` //企业平台中的用户 UID
ClientId string `json:"clientId" valid:"Required"`
DeviceToken string `json:"deviceToken"`
... ... @@ -52,6 +53,7 @@ type AppInfo struct {
AppKey string
AppMasterSecret string
AppId string
ProjectName string
ProjectKey string
ProjectName string //子项目名称
ProjectKey string //包含在主项目内-子项目编码 worth
ProjectMasterKey string //主项目编码 ability
}
... ...
... ... @@ -12,7 +12,7 @@ import (
func init() {
aliasName := "default"
if err := orm.RegisterDataBase(aliasName, "mysql", constant.MYSQL_DATA_SOURCE); err != nil {
log.Error(fmt.Sprintf("【MYSQL】注册数据库失败:%v source:%v", err, constant.MYSQL_DATA_SOURCE))
log.Error(fmt.Sprintf("【MYSQL】注册数据库失败:%v", err))
return
}
orm.SetMaxIdleConns(aliasName, constant.MYSQL_MAX_IDLE)
... ...
... ... @@ -16,6 +16,7 @@ type PushAppInfo struct {
AppId string `orm:"column(app_id);size(255);null" description:"推送应用编号"`
ProjectName string `orm:"column(project_name);size(255);null" description:"项目名称 能力展示"`
ProjectKey string `orm:"column(project_key);size(255);null" description:"项目唯一标识 ability"`
ProjectMasterKey string `orm:"column(project_master_key);size(50);null" description:"主项目唯一标识 ability"`
}
func (t *PushAppInfo) TableName() string {
... ...
... ... @@ -14,6 +14,7 @@ type PushDeviceInfo struct {
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
IsActive int `orm:"column(is_active);null" description:"是否在活动 1:在线 0:离线(解绑,切换账号)"`
ProjectMasterKey string `orm:"column(project_master_key);size(50);null" description:"主项目唯一标识 ability"`
}
func (t *PushDeviceInfo) TableName() string {
... ...
... ... @@ -35,7 +35,7 @@ func TestGetui(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = notification.Send(param)
_, err = notification.Send(param)
if err != nil {
t.Fatal(err)
}
... ... @@ -72,7 +72,7 @@ func TestGetuiPrd(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = notification.Send(param)
_, err = notification.Send(param)
if err != nil {
t.Fatal(err)
}
... ...
... ... @@ -4,8 +4,11 @@ import (
"github.com/astaxie/beego/orm"
"openapi/pkg/domain"
"openapi/pkg/infrastructure/bgorm/model"
"sync"
)
var Cache sync.Map
type AppInfoRepository struct {
}
... ... @@ -17,7 +20,14 @@ func (repository *AppInfoRepository) FindOne(queryOptions map[string]interface{}
if err != nil {
return nil, err
}
return repository.transformBgormModelToDomainModel(model)
var appInfo *domain.AppInfo
var ok bool
if appInfo, ok = getCache(queryOptions["project_key"]); ok {
return appInfo, nil
}
appInfo, err = repository.transformBgormModelToDomainModel(model)
setCache(queryOptions["project_key"], appInfo)
return appInfo, err
}
func (repository *AppInfoRepository) Find(queryOptions map[string]interface{}) (rsp []*domain.AppInfo, err error) {
... ... @@ -32,6 +42,7 @@ func (repository *AppInfoRepository) transformBgormModelToDomainModel(model *mod
AppId: model.AppId,
ProjectName: model.ProjectName,
ProjectKey: model.ProjectKey,
ProjectMasterKey: model.ProjectMasterKey,
}, nil
}
... ... @@ -42,3 +53,20 @@ func NewAppInfoRepository(transactionContext interface{}) (*AppInfoRepository, e
return &AppInfoRepository{}, nil
}
}
//设置缓存
func setCache(key interface{}, value interface{}) {
Cache.Store(key, value)
}
//设置缓存
func getCache(key interface{}) (*domain.AppInfo, bool) {
v, ok := Cache.Load(key)
if !ok {
return nil, false
}
if appInfo, ok := v.(*domain.AppInfo); ok {
return appInfo, true
}
return nil, false
}
... ...
... ... @@ -21,6 +21,7 @@ func (repository *PushDeviceRepository) Save(device *domain.UpdateDeviceRequest)
DeviceToken: strings.TrimSpace(device.DeviceToken),
CreateAt: time.Now(),
UpdateAt: time.Now(),
ProjectMasterKey: device.ProjectKey,
}
_, err := o.Insert(m)
return err
... ... @@ -29,7 +30,9 @@ func (repository *PushDeviceRepository) Save(device *domain.UpdateDeviceRequest)
func (repository *PushDeviceRepository) FindOne(queryOptions map[string]interface{}) (*domain.Device, error) {
o := orm.NewOrm()
model := new(models.PushDeviceInfo)
qs := o.QueryTable(model.TableName()).Filter("uid", queryOptions["uid"])
qs := o.QueryTable(model.TableName()).
Filter("uid", queryOptions["uid"]).
Filter("project_master_key", queryOptions["project_master_key"])
err := qs.One(model)
if err != nil {
if err == orm.ErrNoRows {
... ... @@ -44,7 +47,7 @@ func (repository *PushDeviceRepository) Find(queryOptions map[string]interface{}
o := orm.NewOrm()
model := new(models.PushDeviceInfo)
var ms []*models.PushDeviceInfo
qs := o.QueryTable(model.TableName()).Filter("uid__in", queryOptions["receivers"]).Filter("is_active", 1)
qs := o.QueryTable(model.TableName()).Filter("uid__in", queryOptions["receivers"]).Filter("is_active", 1).Filter("project_master_key", queryOptions["project_master_key"])
qs.All(&ms)
if len(ms) == 0 {
return
... ... @@ -56,17 +59,17 @@ func (repository *PushDeviceRepository) Find(queryOptions map[string]interface{}
return
}
func (repository *PushDeviceRepository) UpdateDevice(uid int64, clientId, deviceToken string) error {
func (repository *PushDeviceRepository) UpdateDevice(uid int64, clientId, deviceToken string, projectKey string) error {
o := orm.NewOrm()
o.Begin()
//更新其他绑定这个client_id的设备 is_active=0
_, err := o.Raw("UPDATE push_device_info SET update_at=now(),is_active=0 where client_id=? and is_active=1", clientId).Exec()
_, err := o.Raw("UPDATE push_device_info SET update_at=now(),is_active=0 where client_id=? and is_active=1 and project_master_key=?", clientId, projectKey).Exec()
if err != nil {
o.Rollback()
return err
}
_, err = o.Raw("UPDATE push_device_info SET client_id=?,device_token = ?,update_at=now(),is_active=1 where uid=?", clientId, deviceToken, uid).Exec()
_, err = o.Raw("UPDATE push_device_info SET client_id=?,device_token = ?,update_at=now(),is_active=1 where uid=? and project_master_key=?", clientId, deviceToken, uid, projectKey).Exec()
if err != nil {
o.Rollback()
return err
... ...
... ... @@ -74,6 +74,9 @@ func (this *PushController) UpdateDevice() {
msg = m
return
}
if request.ProjectKey == "" {
request.ProjectKey = "ability" //默认能力展示项目
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(push.UpdateDevice(header, request))
}
... ...
... ... @@ -14,6 +14,6 @@ func init() {
beego.NSNamespace("vod", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.VodController{})),
beego.NSNamespace("push", beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.PushController{})),
)
beego.SetStaticPath("/log", constant.LogFilePath)
beego.SetStaticPath("/log/NIONkenfieldon", constant.LogFilePath)
beego.AddNamespace(nsV1)
}
... ...