作者 yangfu

1.推送修改,推送设备信息存储

... ... @@ -12,3 +12,10 @@ cname ="https://media.goexample.live/"
#友盟推送
UMENG_API_HOST = "http://msg.umeng.com"
#数据库相关
MYSQL_USER = "${MYSQL_USER||root}"
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_dev}"
\ No newline at end of file
... ...
... ... @@ -9,3 +9,10 @@ aliyun_logs_access ="${aliyun_logs_access||F:/log/app.log}"
#阿里云
cname ="https://media.fjmaimaimai.com/"
#数据库相关
MYSQL_USER = "${MYSQL_USER||root}"
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_dev}"
... ...
... ... @@ -9,3 +9,10 @@ AccessKeySecret ="aLZXwK8pgrs10Ws03qcN7NsrSXFVsg"
#阿里云
cname ="https://media.fjmaimaimai.com/"
#数据库相关
MYSQL_USER = "${MYSQL_USER||root}"
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}"
\ No newline at end of file
... ...
... ... @@ -9,3 +9,10 @@ AccessKeySecret ="aLZXwK8pgrs10Ws03qcN7NsrSXFVsg"
#阿里云
cname ="https://media.fjmaimaimai.com/"
#数据库相关
MYSQL_USER = "${MYSQL_USER||root}"
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
... ...
... ... @@ -9,6 +9,7 @@ require (
github.com/astaxie/beego v1.10.0
github.com/fatih/structs v1.1.0 // indirect
github.com/gavv/httpexpect v2.0.0+incompatible
github.com/go-sql-driver/mysql v1.4.1
github.com/google/go-querystring v1.0.0 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/klauspost/cpuid v1.2.3 // indirect
... ...
... ... @@ -3,8 +3,7 @@ package main
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 +13,6 @@ func main() {
log.Info("server on stop!")
}()
log.Info("server on start!")
constant.DebugConfig()
//constant.DebugConfig()
beego.Run()
}
... ...
package push
import (
"encoding/json"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
protocol "openapi/pkg/domain"
"openapi/pkg/infrastructure/push"
"openapi/pkg/infrastructure/push/getui"
"openapi/pkg/infrastructure/repository"
"openapi/pkg/infrastructure/utils"
)
//推送信息
func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequest) (rsp *protocol.PushInfoResponse, err error) {
var (
sendData = make(map[string]interface{})
clientIds []string
fc = func(m json.RawMessage) {
if len(m) == 0 {
return
repApp, _ = repository.NewAppInfoRepository(nil)
repDevice, _ = repository.NewPushDeviceRepository(nil)
appInfo *protocol.AppInfo
receivers []string
deviceList []*protocol.Device
requestOriginal *protocol.PushInfoOriginalRequest = &protocol.PushInfoOriginalRequest{
Type: request.Type,
Title: request.Title,
Content: request.Content,
Ext: request.Ext,
}
var (
id string
ids []string
)
if e := json.Unmarshal(m, &ids); e == nil {
clientIds = append(clientIds, ids...)
if appInfo, err = repApp.FindOne(map[string]interface{}{"project_key": request.ProjectKey}); err != nil {
log.Error(err)
err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey))
return
}
if e := json.Unmarshal(m, &id); e == nil {
if len(id) == 0 {
if deviceList, err = repDevice.Find(map[string]interface{}{"receivers": request.Receivers}); err != nil {
log.Error(err)
err = nil
return
}
clientIds = append(clientIds, id)
if len(deviceList) == 0 {
err = protocol.NewSuccessWithMessage(fmt.Sprintf("接受者:%v 未查询到注册的设备信息!", request.Receivers))
return
}
return
for i := range deviceList {
receivers = append(receivers, deviceList[i].ClientId)
}
requestOriginal.AppKey = appInfo.AppKey
requestOriginal.AppId = appInfo.AppId
requestOriginal.Secret = appInfo.AppMasterSecret
requestOriginal.ClientIdList = receivers
return NotificationOriginal(header, requestOriginal)
}
func NotificationOriginal(header *protocol.RequestHeader, request *protocol.PushInfoOriginalRequest) (rsp *protocol.PushInfoResponse, err error) {
var (
sendData = make(map[string]interface{})
clientIds []string
options []push.Option = []push.Option{
push.DebugModule(true),
push.AppId(request.AppId),
... ... @@ -51,8 +69,7 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ
if v, ok := request.Ext["transData"]; ok {
options = append(options, push.TransmissionContent(utils.JsonAssertString(v)))
}
fc(request.ClientId)
fc(request.DeviceToken)
clientIds = request.ClientIdList
switch len(clientIds) {
case 0:
err = protocol.NewCustomMessage(2, "clientId/deviceToken 不能同时为空.")
... ... @@ -84,3 +101,22 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ
rsp = &protocol.PushInfoResponse{}
return
}
//更新设备信息
func UpdateDevice(header *protocol.RequestHeader, request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) {
var ()
rep, _ := repository.NewPushDeviceRepository(nil)
if _, err = rep.FindOne(map[string]interface{}{"uid": request.Muid}); err != nil {
if err == protocol.ERR_DB_NOT_FOUND {
rep.Save(request)
return
}
log.Error(err)
return
}
if err = rep.UpdateDevice(request.Muid, request.ClientId, request.DeviceToken); err != nil {
log.Error(err)
}
err = protocol.NewSuccessWithMessage("更新成功")
return
}
... ...
package constant
import "fmt"
var MYSQL_DB_NAME = "mmm_open_dev"
var MYSQL_USER = "root"
var MYSQL_PASSWORD = "sutianxia2018"
var MYSQL_HOST = "101.37.68.23"
var MYSQL_PORT = "3306"
var MYSQL_DATA_SOURCE = ""
var MYSQL_MAX_IDLE = 100
var MYSQL_MAX_OPEN = 100
func init() {
MYSQL_USER = config.StringDefault("MYSQL_USER", MYSQL_USER)
MYSQL_PASSWORD = config.StringDefault("MYSQL_PASSWORD", MYSQL_PASSWORD)
MYSQL_HOST = config.StringDefault("MYSQL_HOST", MYSQL_HOST)
MYSQL_PORT = config.StringDefault("MYSQL_PORT", MYSQL_PORT)
MYSQL_DB_NAME = config.StringDefault("MYSQL_DB_NAME", MYSQL_DB_NAME)
MYSQL_DATA_SOURCE = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?loc=Asia%%2FShanghai&charset=utf8mb4",
MYSQL_USER,
MYSQL_PASSWORD,
MYSQL_HOST,
MYSQL_PORT,
MYSQL_DB_NAME,
)
}
... ...
... ... @@ -2,6 +2,11 @@ package domain
import (
"encoding/json"
"fmt"
)
var (
ERR_DB_NOT_FOUND = fmt.Errorf("db:not found")
)
//CustomErrParse 解析自定义错误结构体
... ...
package domain
import "encoding/json"
/*PushInfo 推送信息*/
type PushInfoRequest struct {
type PushInfoOriginalRequest struct {
Type int `json:"msgType"`
DeviceToken json.RawMessage `json:"deviceToken" `
ClientId json.RawMessage `json:"clientId"`
ClientIdList []string `json:"clientId"`
AppKey string `json:"appKey" valid:"Required"`
Secret string `json:"secret" valid:"Required"`
AppId string `json:"appId" valid:"Required"`
... ... @@ -17,3 +14,38 @@ type PushInfoRequest struct {
}
type PushInfoResponse struct {
}
/*PushInfo 推送信息*/
type PushInfoRequest struct {
Type int `json:"msgType"`
Receivers []int64 `json:"receivers"` //接受用户id列表
ProjectKey string `json:"project"` //ability
Title string `json:"title" valid:"Required"`
Content string `json:"content" valid:"Required"`
Ext map[string]interface{} `json:"ext"` //key->transData:透传数据
}
/*UpdateDevice 更新设备*/
type UpdateDeviceRequest struct {
Muid int64 `json:"muid" valid:"Required;"` //企业平台中的用户 UID
ClientId string `json:"clientId" valid:"Required"`
DeviceToken string `json:"deviceToken"`
}
type UpdateDeviceResponse struct {
}
type Device struct {
Uid int64
ClientId string
DeviceToken string
}
type AppInfo struct {
Id int
AppKey string
AppMasterSecret string
AppId string
ProjectName string
ProjectKey string
}
... ...
package bgorm
import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"openapi/pkg/constant"
_ "openapi/pkg/infrastructure/bgorm/model"
)
func init() {
aliasName := "default"
_ = orm.RegisterDataBase(aliasName, "mysql", constant.MYSQL_DATA_SOURCE)
orm.SetMaxIdleConns(aliasName, constant.MYSQL_MAX_IDLE)
orm.SetMaxOpenConns(aliasName, constant.MYSQL_MAX_OPEN)
}
... ...
package models
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/astaxie/beego/orm"
)
type PushAppInfo struct {
Id int `orm:"column(id);auto" description:"编号"`
AppKey string `orm:"column(app_key);size(255);null" description:"推送key "`
AppMasterSecret string `orm:"column(app_master_secret);size(255);null" description:"推送服务端密钥"`
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"`
}
func (t *PushAppInfo) TableName() string {
return "push_app_info"
}
func init() {
orm.RegisterModel(new(PushAppInfo))
}
// AddPushAppInfo insert a new PushAppInfo into database and returns
// last inserted Id on success.
func AddPushAppInfo(m *PushAppInfo) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetPushAppInfoById retrieves PushAppInfo by Id. Returns error if
// Id doesn't exist
func GetPushAppInfoById(id int) (v *PushAppInfo, err error) {
o := orm.NewOrm()
v = &PushAppInfo{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// GetAllPushAppInfo retrieves all PushAppInfo matches certain condition. Returns empty list if
// no records exist
func GetAllPushAppInfo(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(PushAppInfo))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []PushAppInfo
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
}
ml = append(ml, m)
}
}
return ml, nil
}
return nil, err
}
// UpdatePushAppInfo updates PushAppInfo by Id and returns error if
// the record to be updated doesn't exist
func UpdatePushAppInfoById(m *PushAppInfo) (err error) {
o := orm.NewOrm()
v := PushAppInfo{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeletePushAppInfo deletes PushAppInfo by Id and returns error if
// the record to be deleted doesn't exist
func DeletePushAppInfo(id int) (err error) {
o := orm.NewOrm()
v := PushAppInfo{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&PushAppInfo{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
package models
import (
"time"
"github.com/astaxie/beego/orm"
)
type PushDeviceInfo struct {
Id int `orm:"column(id);auto"`
Uid int64 `orm:"column(uid);null" description:"企业平台用户id (muid)"`
ClientId string `orm:"column(client_id);size(100);null" description:"设备识别码 推送标识"`
DeviceToken string `orm:"column(device_token);size(100);null" description:"设备识别码 推送标识"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
}
func (t *PushDeviceInfo) TableName() string {
return "push_device_info"
}
func init() {
orm.RegisterModel(new(PushDeviceInfo))
}
... ...
package repository
import (
"github.com/astaxie/beego/orm"
"openapi/pkg/domain"
"openapi/pkg/infrastructure/bgorm/model"
)
type AppInfoRepository struct {
}
func (repository *AppInfoRepository) FindOne(queryOptions map[string]interface{}) (*domain.AppInfo, error) {
o := orm.NewOrm()
model := new(models.PushAppInfo)
qs := o.QueryTable(model).Filter("project_key", queryOptions["project_key"])
err := qs.One(model)
if err != nil {
return nil, err
}
return repository.transformBgormModelToDomainModel(model)
}
func (repository *AppInfoRepository) Find(queryOptions map[string]interface{}) (rsp []*domain.AppInfo, err error) {
return
}
func (repository *AppInfoRepository) transformBgormModelToDomainModel(model *models.PushAppInfo) (*domain.AppInfo, error) {
return &domain.AppInfo{
Id: model.Id,
AppKey: model.AppKey,
AppMasterSecret: model.AppMasterSecret,
AppId: model.AppId,
ProjectName: model.ProjectName,
ProjectKey: model.ProjectKey,
}, nil
}
func NewAppInfoRepository(transactionContext interface{}) (*AppInfoRepository, error) {
if transactionContext == nil {
return &AppInfoRepository{}, nil
} else {
return &AppInfoRepository{}, nil
}
}
... ...
package repository
import (
"github.com/astaxie/beego/orm"
"openapi/pkg/domain"
"openapi/pkg/infrastructure/bgorm/model"
"strings"
"time"
)
type PushDeviceRepository struct {
}
func (repository *PushDeviceRepository) Save(device *domain.UpdateDeviceRequest) error {
o := orm.NewOrm()
m := &models.PushDeviceInfo{
Uid: device.Muid,
ClientId: strings.TrimSpace(device.ClientId),
DeviceToken: strings.TrimSpace(device.DeviceToken),
CreateAt: time.Now(),
UpdateAt: time.Now(),
}
_, err := o.Insert(m)
return err
}
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"])
err := qs.One(model)
if err != nil {
return nil, err
}
return repository.transformBgormModelToDomainModel(model)
}
func (repository *PushDeviceRepository) Find(queryOptions map[string]interface{}) (rsp []*domain.Device, err error) {
o := orm.NewOrm()
model := new(models.PushDeviceInfo)
var ms []*models.PushDeviceInfo
qs := o.QueryTable(model.TableName()).Filter("uid__in", queryOptions["receivers"])
qs.All(&ms)
if len(ms) == 0 {
return
}
for i := range ms {
v, _ := repository.transformBgormModelToDomainModel(ms[i])
rsp = append(rsp, v)
}
return
}
func (repository *PushDeviceRepository) UpdateDevice(uid int64, clientId, deviceToken string) error {
o := orm.NewOrm()
_, err := o.Raw("UPDATE push_device_info SET client_id=?,device_token = ? where uid=?", clientId, deviceToken, uid).Exec()
if err != nil {
return err
}
return nil
}
func (repository *PushDeviceRepository) transformBgormModelToDomainModel(model *models.PushDeviceInfo) (*domain.Device, error) {
return &domain.Device{
Uid: model.Uid,
ClientId: model.ClientId,
DeviceToken: model.DeviceToken,
}, nil
}
func NewPushDeviceRepository(transactionContext interface{}) (*PushDeviceRepository, error) {
if transactionContext == nil {
return &PushDeviceRepository{}, nil
} else {
return &PushDeviceRepository{}, nil
}
}
... ...
... ... @@ -32,3 +32,45 @@ func (this *PushController) PushInfo() {
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(push.Notification(header, request))
}
//原生推送信息 PushInfoOriginal
// @router /pushInfoOriginal [post]
func (this *PushController) PushInfoOriginal() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.PushInfoOriginalRequest
if err := json.Unmarshal(this.ByteBody, &request); err != nil {
log.Error(err)
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(push.NotificationOriginal(header, request))
}
//UpdateDevice
//@router /updateDevice [post]
func (this *PushController) UpdateDevice() {
var msg *protocol.ResponseMessage
defer func() {
this.Resp(msg)
}()
var request *protocol.UpdateDeviceRequest
if err := json.Unmarshal(this.ByteBody, &request); err != nil {
log.Error(err)
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
header := controllers.GetRequestHeader(this.Ctx)
msg = protocol.NewReturnResponse(push.UpdateDevice(header, request))
}
... ...
... ... @@ -15,6 +15,22 @@ func init() {
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:PushController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:PushController"],
beego.ControllerComments{
Method: "PushInfoOriginal",
Router: `/pushInfoOriginal`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:PushController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:PushController"],
beego.ControllerComments{
Method: "UpdateDevice",
Router: `/updateDevice`,
AllowHTTPMethods: []string{"post"},
MethodParams: param.Make(),
Params: nil})
beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"],
beego.ControllerComments{
Method: "CreateUploadImage",
... ...