正在显示
17 个修改的文件
包含
528 行增加
和
25 行删除
@@ -12,3 +12,10 @@ cname ="https://media.goexample.live/" | @@ -12,3 +12,10 @@ cname ="https://media.goexample.live/" | ||
12 | 12 | ||
13 | #友盟推送 | 13 | #友盟推送 |
14 | UMENG_API_HOST = "http://msg.umeng.com" | 14 | UMENG_API_HOST = "http://msg.umeng.com" |
15 | + | ||
16 | +#数据库相关 | ||
17 | +MYSQL_USER = "${MYSQL_USER||root}" | ||
18 | +MYSQL_PASSWORD = "${MYSQL_PASSWORD||sutianxia2018}" | ||
19 | +MYSQL_HOST = "${MYSQL_HOST||101.37.68.23}" | ||
20 | +MYSQL_PORT = "${MYSQL_PORT||3306}" | ||
21 | +MYSQL_DB_NAME = "${MYSQL_DB_NAME||mmm_open_dev}" |
@@ -9,3 +9,10 @@ aliyun_logs_access ="${aliyun_logs_access||F:/log/app.log}" | @@ -9,3 +9,10 @@ aliyun_logs_access ="${aliyun_logs_access||F:/log/app.log}" | ||
9 | 9 | ||
10 | #阿里云 | 10 | #阿里云 |
11 | cname ="https://media.fjmaimaimai.com/" | 11 | cname ="https://media.fjmaimaimai.com/" |
12 | + | ||
13 | +#数据库相关 | ||
14 | +MYSQL_USER = "${MYSQL_USER||root}" | ||
15 | +MYSQL_PASSWORD = "${MYSQL_PASSWORD||sutianxia2018}" | ||
16 | +MYSQL_HOST = "${MYSQL_HOST||101.37.68.23}" | ||
17 | +MYSQL_PORT = "${MYSQL_PORT||3306}" | ||
18 | +MYSQL_DB_NAME = "${MYSQL_DB_NAME||mmm_open_dev}" |
@@ -9,3 +9,10 @@ AccessKeySecret ="aLZXwK8pgrs10Ws03qcN7NsrSXFVsg" | @@ -9,3 +9,10 @@ AccessKeySecret ="aLZXwK8pgrs10Ws03qcN7NsrSXFVsg" | ||
9 | 9 | ||
10 | #阿里云 | 10 | #阿里云 |
11 | cname ="https://media.fjmaimaimai.com/" | 11 | cname ="https://media.fjmaimaimai.com/" |
12 | + | ||
13 | +#数据库相关 | ||
14 | +MYSQL_USER = "${MYSQL_USER||root}" | ||
15 | +MYSQL_PASSWORD = "${MYSQL_PASSWORD||sutianxia2018}" | ||
16 | +MYSQL_HOST = "${MYSQL_HOST||101.37.68.23}" | ||
17 | +MYSQL_PORT = "${MYSQL_PORT||3306}" | ||
18 | +MYSQL_DB_NAME = "${MYSQL_DB_NAME||mmm_open}" |
@@ -9,3 +9,10 @@ AccessKeySecret ="aLZXwK8pgrs10Ws03qcN7NsrSXFVsg" | @@ -9,3 +9,10 @@ AccessKeySecret ="aLZXwK8pgrs10Ws03qcN7NsrSXFVsg" | ||
9 | 9 | ||
10 | #阿里云 | 10 | #阿里云 |
11 | cname ="https://media.fjmaimaimai.com/" | 11 | cname ="https://media.fjmaimaimai.com/" |
12 | + | ||
13 | +#数据库相关 | ||
14 | +MYSQL_USER = "${MYSQL_USER||root}" | ||
15 | +MYSQL_PASSWORD = "${MYSQL_PASSWORD||sutianxia2018}" | ||
16 | +MYSQL_HOST = "${MYSQL_HOST||101.37.68.23}" | ||
17 | +MYSQL_PORT = "${MYSQL_PORT||3306}" | ||
18 | +MYSQL_DB_NAME = "${MYSQL_DB_NAME||mmm_open_test}" |
@@ -9,6 +9,7 @@ require ( | @@ -9,6 +9,7 @@ require ( | ||
9 | github.com/astaxie/beego v1.10.0 | 9 | github.com/astaxie/beego v1.10.0 |
10 | github.com/fatih/structs v1.1.0 // indirect | 10 | github.com/fatih/structs v1.1.0 // indirect |
11 | github.com/gavv/httpexpect v2.0.0+incompatible | 11 | github.com/gavv/httpexpect v2.0.0+incompatible |
12 | + github.com/go-sql-driver/mysql v1.4.1 | ||
12 | github.com/google/go-querystring v1.0.0 // indirect | 13 | github.com/google/go-querystring v1.0.0 // indirect |
13 | github.com/imkira/go-interpol v1.1.0 // indirect | 14 | github.com/imkira/go-interpol v1.1.0 // indirect |
14 | github.com/klauspost/cpuid v1.2.3 // indirect | 15 | github.com/klauspost/cpuid v1.2.3 // indirect |
@@ -3,8 +3,7 @@ package main | @@ -3,8 +3,7 @@ package main | ||
3 | import ( | 3 | import ( |
4 | "github.com/astaxie/beego" | 4 | "github.com/astaxie/beego" |
5 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 5 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
6 | - "openapi/pkg/constant" | ||
7 | - | 6 | + _ "openapi/pkg/infrastructure/bgorm" |
8 | _ "openapi/pkg/log" | 7 | _ "openapi/pkg/log" |
9 | _ "openapi/pkg/port/beego" | 8 | _ "openapi/pkg/port/beego" |
10 | ) | 9 | ) |
@@ -14,6 +13,6 @@ func main() { | @@ -14,6 +13,6 @@ func main() { | ||
14 | log.Info("server on stop!") | 13 | log.Info("server on stop!") |
15 | }() | 14 | }() |
16 | log.Info("server on start!") | 15 | log.Info("server on start!") |
17 | - constant.DebugConfig() | 16 | + //constant.DebugConfig() |
18 | beego.Run() | 17 | beego.Run() |
19 | } | 18 | } |
1 | package push | 1 | package push |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "encoding/json" | 4 | + "fmt" |
5 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 5 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
6 | protocol "openapi/pkg/domain" | 6 | protocol "openapi/pkg/domain" |
7 | "openapi/pkg/infrastructure/push" | 7 | "openapi/pkg/infrastructure/push" |
8 | "openapi/pkg/infrastructure/push/getui" | 8 | "openapi/pkg/infrastructure/push/getui" |
9 | + "openapi/pkg/infrastructure/repository" | ||
9 | "openapi/pkg/infrastructure/utils" | 10 | "openapi/pkg/infrastructure/utils" |
10 | ) | 11 | ) |
11 | 12 | ||
12 | //推送信息 | 13 | //推送信息 |
13 | func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequest) (rsp *protocol.PushInfoResponse, err error) { | 14 | func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequest) (rsp *protocol.PushInfoResponse, err error) { |
14 | var ( | 15 | var ( |
15 | - sendData = make(map[string]interface{}) | ||
16 | - clientIds []string | ||
17 | - fc = func(m json.RawMessage) { | ||
18 | - if len(m) == 0 { | ||
19 | - return | 16 | + repApp, _ = repository.NewAppInfoRepository(nil) |
17 | + repDevice, _ = repository.NewPushDeviceRepository(nil) | ||
18 | + appInfo *protocol.AppInfo | ||
19 | + receivers []string | ||
20 | + deviceList []*protocol.Device | ||
21 | + requestOriginal *protocol.PushInfoOriginalRequest = &protocol.PushInfoOriginalRequest{ | ||
22 | + Type: request.Type, | ||
23 | + Title: request.Title, | ||
24 | + Content: request.Content, | ||
25 | + Ext: request.Ext, | ||
20 | } | 26 | } |
21 | - var ( | ||
22 | - id string | ||
23 | - ids []string | ||
24 | ) | 27 | ) |
25 | - if e := json.Unmarshal(m, &ids); e == nil { | ||
26 | - clientIds = append(clientIds, ids...) | 28 | + if appInfo, err = repApp.FindOne(map[string]interface{}{"project_key": request.ProjectKey}); err != nil { |
29 | + log.Error(err) | ||
30 | + err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey)) | ||
27 | return | 31 | return |
28 | } | 32 | } |
29 | - if e := json.Unmarshal(m, &id); e == nil { | ||
30 | - if len(id) == 0 { | 33 | + if deviceList, err = repDevice.Find(map[string]interface{}{"receivers": request.Receivers}); err != nil { |
34 | + log.Error(err) | ||
35 | + err = nil | ||
31 | return | 36 | return |
32 | } | 37 | } |
33 | - clientIds = append(clientIds, id) | 38 | + if len(deviceList) == 0 { |
39 | + err = protocol.NewSuccessWithMessage(fmt.Sprintf("接受者:%v 未查询到注册的设备信息!", request.Receivers)) | ||
34 | return | 40 | return |
35 | } | 41 | } |
36 | - return | 42 | + for i := range deviceList { |
43 | + receivers = append(receivers, deviceList[i].ClientId) | ||
37 | } | 44 | } |
45 | + requestOriginal.AppKey = appInfo.AppKey | ||
46 | + requestOriginal.AppId = appInfo.AppId | ||
47 | + requestOriginal.Secret = appInfo.AppMasterSecret | ||
48 | + requestOriginal.ClientIdList = receivers | ||
49 | + return NotificationOriginal(header, requestOriginal) | ||
50 | +} | ||
51 | + | ||
52 | +func NotificationOriginal(header *protocol.RequestHeader, request *protocol.PushInfoOriginalRequest) (rsp *protocol.PushInfoResponse, err error) { | ||
53 | + var ( | ||
54 | + sendData = make(map[string]interface{}) | ||
55 | + clientIds []string | ||
38 | options []push.Option = []push.Option{ | 56 | options []push.Option = []push.Option{ |
39 | push.DebugModule(true), | 57 | push.DebugModule(true), |
40 | push.AppId(request.AppId), | 58 | push.AppId(request.AppId), |
@@ -51,8 +69,7 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | @@ -51,8 +69,7 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | ||
51 | if v, ok := request.Ext["transData"]; ok { | 69 | if v, ok := request.Ext["transData"]; ok { |
52 | options = append(options, push.TransmissionContent(utils.JsonAssertString(v))) | 70 | options = append(options, push.TransmissionContent(utils.JsonAssertString(v))) |
53 | } | 71 | } |
54 | - fc(request.ClientId) | ||
55 | - fc(request.DeviceToken) | 72 | + clientIds = request.ClientIdList |
56 | switch len(clientIds) { | 73 | switch len(clientIds) { |
57 | case 0: | 74 | case 0: |
58 | err = protocol.NewCustomMessage(2, "clientId/deviceToken 不能同时为空.") | 75 | err = protocol.NewCustomMessage(2, "clientId/deviceToken 不能同时为空.") |
@@ -84,3 +101,22 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | @@ -84,3 +101,22 @@ func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequ | ||
84 | rsp = &protocol.PushInfoResponse{} | 101 | rsp = &protocol.PushInfoResponse{} |
85 | return | 102 | return |
86 | } | 103 | } |
104 | + | ||
105 | +//更新设备信息 | ||
106 | +func UpdateDevice(header *protocol.RequestHeader, request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) { | ||
107 | + var () | ||
108 | + rep, _ := repository.NewPushDeviceRepository(nil) | ||
109 | + if _, err = rep.FindOne(map[string]interface{}{"uid": request.Muid}); err != nil { | ||
110 | + if err == protocol.ERR_DB_NOT_FOUND { | ||
111 | + rep.Save(request) | ||
112 | + return | ||
113 | + } | ||
114 | + log.Error(err) | ||
115 | + return | ||
116 | + } | ||
117 | + if err = rep.UpdateDevice(request.Muid, request.ClientId, request.DeviceToken); err != nil { | ||
118 | + log.Error(err) | ||
119 | + } | ||
120 | + err = protocol.NewSuccessWithMessage("更新成功") | ||
121 | + return | ||
122 | +} |
pkg/constant/mysql.go
0 → 100644
1 | +package constant | ||
2 | + | ||
3 | +import "fmt" | ||
4 | + | ||
5 | +var MYSQL_DB_NAME = "mmm_open_dev" | ||
6 | +var MYSQL_USER = "root" | ||
7 | +var MYSQL_PASSWORD = "sutianxia2018" | ||
8 | +var MYSQL_HOST = "101.37.68.23" | ||
9 | +var MYSQL_PORT = "3306" | ||
10 | +var MYSQL_DATA_SOURCE = "" | ||
11 | +var MYSQL_MAX_IDLE = 100 | ||
12 | +var MYSQL_MAX_OPEN = 100 | ||
13 | + | ||
14 | +func init() { | ||
15 | + MYSQL_USER = config.StringDefault("MYSQL_USER", MYSQL_USER) | ||
16 | + MYSQL_PASSWORD = config.StringDefault("MYSQL_PASSWORD", MYSQL_PASSWORD) | ||
17 | + MYSQL_HOST = config.StringDefault("MYSQL_HOST", MYSQL_HOST) | ||
18 | + MYSQL_PORT = config.StringDefault("MYSQL_PORT", MYSQL_PORT) | ||
19 | + | ||
20 | + MYSQL_DB_NAME = config.StringDefault("MYSQL_DB_NAME", MYSQL_DB_NAME) | ||
21 | + | ||
22 | + MYSQL_DATA_SOURCE = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?loc=Asia%%2FShanghai&charset=utf8mb4", | ||
23 | + MYSQL_USER, | ||
24 | + MYSQL_PASSWORD, | ||
25 | + MYSQL_HOST, | ||
26 | + MYSQL_PORT, | ||
27 | + MYSQL_DB_NAME, | ||
28 | + ) | ||
29 | +} |
1 | package domain | 1 | package domain |
2 | 2 | ||
3 | -import "encoding/json" | ||
4 | - | ||
5 | /*PushInfo 推送信息*/ | 3 | /*PushInfo 推送信息*/ |
6 | -type PushInfoRequest struct { | 4 | +type PushInfoOriginalRequest struct { |
7 | Type int `json:"msgType"` | 5 | Type int `json:"msgType"` |
8 | - DeviceToken json.RawMessage `json:"deviceToken" ` | ||
9 | - ClientId json.RawMessage `json:"clientId"` | 6 | + ClientIdList []string `json:"clientId"` |
10 | AppKey string `json:"appKey" valid:"Required"` | 7 | AppKey string `json:"appKey" valid:"Required"` |
11 | Secret string `json:"secret" valid:"Required"` | 8 | Secret string `json:"secret" valid:"Required"` |
12 | AppId string `json:"appId" valid:"Required"` | 9 | AppId string `json:"appId" valid:"Required"` |
@@ -17,3 +14,38 @@ type PushInfoRequest struct { | @@ -17,3 +14,38 @@ type PushInfoRequest struct { | ||
17 | } | 14 | } |
18 | type PushInfoResponse struct { | 15 | type PushInfoResponse struct { |
19 | } | 16 | } |
17 | + | ||
18 | +/*PushInfo 推送信息*/ | ||
19 | +type PushInfoRequest struct { | ||
20 | + Type int `json:"msgType"` | ||
21 | + Receivers []int64 `json:"receivers"` //接受用户id列表 | ||
22 | + ProjectKey string `json:"project"` //ability | ||
23 | + | ||
24 | + Title string `json:"title" valid:"Required"` | ||
25 | + Content string `json:"content" valid:"Required"` | ||
26 | + Ext map[string]interface{} `json:"ext"` //key->transData:透传数据 | ||
27 | +} | ||
28 | + | ||
29 | +/*UpdateDevice 更新设备*/ | ||
30 | +type UpdateDeviceRequest struct { | ||
31 | + Muid int64 `json:"muid" valid:"Required;"` //企业平台中的用户 UID | ||
32 | + ClientId string `json:"clientId" valid:"Required"` | ||
33 | + DeviceToken string `json:"deviceToken"` | ||
34 | +} | ||
35 | +type UpdateDeviceResponse struct { | ||
36 | +} | ||
37 | + | ||
38 | +type Device struct { | ||
39 | + Uid int64 | ||
40 | + ClientId string | ||
41 | + DeviceToken string | ||
42 | +} | ||
43 | + | ||
44 | +type AppInfo struct { | ||
45 | + Id int | ||
46 | + AppKey string | ||
47 | + AppMasterSecret string | ||
48 | + AppId string | ||
49 | + ProjectName string | ||
50 | + ProjectKey string | ||
51 | +} |
pkg/infrastructure/bgorm/init.go
0 → 100644
1 | +package bgorm | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/astaxie/beego/orm" | ||
5 | + _ "github.com/go-sql-driver/mysql" | ||
6 | + "openapi/pkg/constant" | ||
7 | + _ "openapi/pkg/infrastructure/bgorm/model" | ||
8 | +) | ||
9 | + | ||
10 | +func init() { | ||
11 | + aliasName := "default" | ||
12 | + _ = orm.RegisterDataBase(aliasName, "mysql", constant.MYSQL_DATA_SOURCE) | ||
13 | + orm.SetMaxIdleConns(aliasName, constant.MYSQL_MAX_IDLE) | ||
14 | + orm.SetMaxOpenConns(aliasName, constant.MYSQL_MAX_OPEN) | ||
15 | +} |
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "reflect" | ||
7 | + "strings" | ||
8 | + | ||
9 | + "github.com/astaxie/beego/orm" | ||
10 | +) | ||
11 | + | ||
12 | +type PushAppInfo struct { | ||
13 | + Id int `orm:"column(id);auto" description:"编号"` | ||
14 | + AppKey string `orm:"column(app_key);size(255);null" description:"推送key "` | ||
15 | + AppMasterSecret string `orm:"column(app_master_secret);size(255);null" description:"推送服务端密钥"` | ||
16 | + AppId string `orm:"column(app_id);size(255);null" description:"推送应用编号"` | ||
17 | + ProjectName string `orm:"column(project_name);size(255);null" description:"项目名称 能力展示"` | ||
18 | + ProjectKey string `orm:"column(project_key);size(255);null" description:"项目唯一标识 ability"` | ||
19 | +} | ||
20 | + | ||
21 | +func (t *PushAppInfo) TableName() string { | ||
22 | + return "push_app_info" | ||
23 | +} | ||
24 | + | ||
25 | +func init() { | ||
26 | + orm.RegisterModel(new(PushAppInfo)) | ||
27 | +} | ||
28 | + | ||
29 | +// AddPushAppInfo insert a new PushAppInfo into database and returns | ||
30 | +// last inserted Id on success. | ||
31 | +func AddPushAppInfo(m *PushAppInfo) (id int64, err error) { | ||
32 | + o := orm.NewOrm() | ||
33 | + id, err = o.Insert(m) | ||
34 | + return | ||
35 | +} | ||
36 | + | ||
37 | +// GetPushAppInfoById retrieves PushAppInfo by Id. Returns error if | ||
38 | +// Id doesn't exist | ||
39 | +func GetPushAppInfoById(id int) (v *PushAppInfo, err error) { | ||
40 | + o := orm.NewOrm() | ||
41 | + v = &PushAppInfo{Id: id} | ||
42 | + if err = o.Read(v); err == nil { | ||
43 | + return v, nil | ||
44 | + } | ||
45 | + return nil, err | ||
46 | +} | ||
47 | + | ||
48 | +// GetAllPushAppInfo retrieves all PushAppInfo matches certain condition. Returns empty list if | ||
49 | +// no records exist | ||
50 | +func GetAllPushAppInfo(query map[string]string, fields []string, sortby []string, order []string, | ||
51 | + offset int64, limit int64) (ml []interface{}, err error) { | ||
52 | + o := orm.NewOrm() | ||
53 | + qs := o.QueryTable(new(PushAppInfo)) | ||
54 | + // query k=v | ||
55 | + for k, v := range query { | ||
56 | + // rewrite dot-notation to Object__Attribute | ||
57 | + k = strings.Replace(k, ".", "__", -1) | ||
58 | + if strings.Contains(k, "isnull") { | ||
59 | + qs = qs.Filter(k, (v == "true" || v == "1")) | ||
60 | + } else { | ||
61 | + qs = qs.Filter(k, v) | ||
62 | + } | ||
63 | + } | ||
64 | + // order by: | ||
65 | + var sortFields []string | ||
66 | + if len(sortby) != 0 { | ||
67 | + if len(sortby) == len(order) { | ||
68 | + // 1) for each sort field, there is an associated order | ||
69 | + for i, v := range sortby { | ||
70 | + orderby := "" | ||
71 | + if order[i] == "desc" { | ||
72 | + orderby = "-" + v | ||
73 | + } else if order[i] == "asc" { | ||
74 | + orderby = v | ||
75 | + } else { | ||
76 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
77 | + } | ||
78 | + sortFields = append(sortFields, orderby) | ||
79 | + } | ||
80 | + qs = qs.OrderBy(sortFields...) | ||
81 | + } else if len(sortby) != len(order) && len(order) == 1 { | ||
82 | + // 2) there is exactly one order, all the sorted fields will be sorted by this order | ||
83 | + for _, v := range sortby { | ||
84 | + orderby := "" | ||
85 | + if order[0] == "desc" { | ||
86 | + orderby = "-" + v | ||
87 | + } else if order[0] == "asc" { | ||
88 | + orderby = v | ||
89 | + } else { | ||
90 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
91 | + } | ||
92 | + sortFields = append(sortFields, orderby) | ||
93 | + } | ||
94 | + } else if len(sortby) != len(order) && len(order) != 1 { | ||
95 | + return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1") | ||
96 | + } | ||
97 | + } else { | ||
98 | + if len(order) != 0 { | ||
99 | + return nil, errors.New("Error: unused 'order' fields") | ||
100 | + } | ||
101 | + } | ||
102 | + | ||
103 | + var l []PushAppInfo | ||
104 | + qs = qs.OrderBy(sortFields...) | ||
105 | + if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { | ||
106 | + if len(fields) == 0 { | ||
107 | + for _, v := range l { | ||
108 | + ml = append(ml, v) | ||
109 | + } | ||
110 | + } else { | ||
111 | + // trim unused fields | ||
112 | + for _, v := range l { | ||
113 | + m := make(map[string]interface{}) | ||
114 | + val := reflect.ValueOf(v) | ||
115 | + for _, fname := range fields { | ||
116 | + m[fname] = val.FieldByName(fname).Interface() | ||
117 | + } | ||
118 | + ml = append(ml, m) | ||
119 | + } | ||
120 | + } | ||
121 | + return ml, nil | ||
122 | + } | ||
123 | + return nil, err | ||
124 | +} | ||
125 | + | ||
126 | +// UpdatePushAppInfo updates PushAppInfo by Id and returns error if | ||
127 | +// the record to be updated doesn't exist | ||
128 | +func UpdatePushAppInfoById(m *PushAppInfo) (err error) { | ||
129 | + o := orm.NewOrm() | ||
130 | + v := PushAppInfo{Id: m.Id} | ||
131 | + // ascertain id exists in the database | ||
132 | + if err = o.Read(&v); err == nil { | ||
133 | + var num int64 | ||
134 | + if num, err = o.Update(m); err == nil { | ||
135 | + fmt.Println("Number of records updated in database:", num) | ||
136 | + } | ||
137 | + } | ||
138 | + return | ||
139 | +} | ||
140 | + | ||
141 | +// DeletePushAppInfo deletes PushAppInfo by Id and returns error if | ||
142 | +// the record to be deleted doesn't exist | ||
143 | +func DeletePushAppInfo(id int) (err error) { | ||
144 | + o := orm.NewOrm() | ||
145 | + v := PushAppInfo{Id: id} | ||
146 | + // ascertain id exists in the database | ||
147 | + if err = o.Read(&v); err == nil { | ||
148 | + var num int64 | ||
149 | + if num, err = o.Delete(&PushAppInfo{Id: id}); err == nil { | ||
150 | + fmt.Println("Number of records deleted in database:", num) | ||
151 | + } | ||
152 | + } | ||
153 | + return | ||
154 | +} |
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "time" | ||
5 | + | ||
6 | + "github.com/astaxie/beego/orm" | ||
7 | +) | ||
8 | + | ||
9 | +type PushDeviceInfo struct { | ||
10 | + Id int `orm:"column(id);auto"` | ||
11 | + Uid int64 `orm:"column(uid);null" description:"企业平台用户id (muid)"` | ||
12 | + ClientId string `orm:"column(client_id);size(100);null" description:"设备识别码 推送标识"` | ||
13 | + DeviceToken string `orm:"column(device_token);size(100);null" description:"设备识别码 推送标识"` | ||
14 | + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"` | ||
15 | + UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"` | ||
16 | +} | ||
17 | + | ||
18 | +func (t *PushDeviceInfo) TableName() string { | ||
19 | + return "push_device_info" | ||
20 | +} | ||
21 | + | ||
22 | +func init() { | ||
23 | + orm.RegisterModel(new(PushDeviceInfo)) | ||
24 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/astaxie/beego/orm" | ||
5 | + "openapi/pkg/domain" | ||
6 | + "openapi/pkg/infrastructure/bgorm/model" | ||
7 | +) | ||
8 | + | ||
9 | +type AppInfoRepository struct { | ||
10 | +} | ||
11 | + | ||
12 | +func (repository *AppInfoRepository) FindOne(queryOptions map[string]interface{}) (*domain.AppInfo, error) { | ||
13 | + o := orm.NewOrm() | ||
14 | + model := new(models.PushAppInfo) | ||
15 | + qs := o.QueryTable(model).Filter("project_key", queryOptions["project_key"]) | ||
16 | + err := qs.One(model) | ||
17 | + if err != nil { | ||
18 | + return nil, err | ||
19 | + } | ||
20 | + return repository.transformBgormModelToDomainModel(model) | ||
21 | +} | ||
22 | + | ||
23 | +func (repository *AppInfoRepository) Find(queryOptions map[string]interface{}) (rsp []*domain.AppInfo, err error) { | ||
24 | + return | ||
25 | +} | ||
26 | + | ||
27 | +func (repository *AppInfoRepository) transformBgormModelToDomainModel(model *models.PushAppInfo) (*domain.AppInfo, error) { | ||
28 | + return &domain.AppInfo{ | ||
29 | + Id: model.Id, | ||
30 | + AppKey: model.AppKey, | ||
31 | + AppMasterSecret: model.AppMasterSecret, | ||
32 | + AppId: model.AppId, | ||
33 | + ProjectName: model.ProjectName, | ||
34 | + ProjectKey: model.ProjectKey, | ||
35 | + }, nil | ||
36 | +} | ||
37 | + | ||
38 | +func NewAppInfoRepository(transactionContext interface{}) (*AppInfoRepository, error) { | ||
39 | + if transactionContext == nil { | ||
40 | + return &AppInfoRepository{}, nil | ||
41 | + } else { | ||
42 | + return &AppInfoRepository{}, nil | ||
43 | + } | ||
44 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/astaxie/beego/orm" | ||
5 | + "openapi/pkg/domain" | ||
6 | + "openapi/pkg/infrastructure/bgorm/model" | ||
7 | + "strings" | ||
8 | + "time" | ||
9 | +) | ||
10 | + | ||
11 | +type PushDeviceRepository struct { | ||
12 | +} | ||
13 | + | ||
14 | +func (repository *PushDeviceRepository) Save(device *domain.UpdateDeviceRequest) error { | ||
15 | + o := orm.NewOrm() | ||
16 | + | ||
17 | + m := &models.PushDeviceInfo{ | ||
18 | + Uid: device.Muid, | ||
19 | + ClientId: strings.TrimSpace(device.ClientId), | ||
20 | + DeviceToken: strings.TrimSpace(device.DeviceToken), | ||
21 | + CreateAt: time.Now(), | ||
22 | + UpdateAt: time.Now(), | ||
23 | + } | ||
24 | + _, err := o.Insert(m) | ||
25 | + return err | ||
26 | +} | ||
27 | + | ||
28 | +func (repository *PushDeviceRepository) FindOne(queryOptions map[string]interface{}) (*domain.Device, error) { | ||
29 | + o := orm.NewOrm() | ||
30 | + model := new(models.PushDeviceInfo) | ||
31 | + qs := o.QueryTable(model.TableName()).Filter("uid", queryOptions["uid"]) | ||
32 | + err := qs.One(model) | ||
33 | + if err != nil { | ||
34 | + return nil, err | ||
35 | + } | ||
36 | + return repository.transformBgormModelToDomainModel(model) | ||
37 | +} | ||
38 | + | ||
39 | +func (repository *PushDeviceRepository) Find(queryOptions map[string]interface{}) (rsp []*domain.Device, err error) { | ||
40 | + o := orm.NewOrm() | ||
41 | + model := new(models.PushDeviceInfo) | ||
42 | + var ms []*models.PushDeviceInfo | ||
43 | + qs := o.QueryTable(model.TableName()).Filter("uid__in", queryOptions["receivers"]) | ||
44 | + qs.All(&ms) | ||
45 | + if len(ms) == 0 { | ||
46 | + return | ||
47 | + } | ||
48 | + for i := range ms { | ||
49 | + v, _ := repository.transformBgormModelToDomainModel(ms[i]) | ||
50 | + rsp = append(rsp, v) | ||
51 | + } | ||
52 | + return | ||
53 | +} | ||
54 | + | ||
55 | +func (repository *PushDeviceRepository) UpdateDevice(uid int64, clientId, deviceToken string) error { | ||
56 | + o := orm.NewOrm() | ||
57 | + _, err := o.Raw("UPDATE push_device_info SET client_id=?,device_token = ? where uid=?", clientId, deviceToken, uid).Exec() | ||
58 | + if err != nil { | ||
59 | + return err | ||
60 | + } | ||
61 | + return nil | ||
62 | +} | ||
63 | + | ||
64 | +func (repository *PushDeviceRepository) transformBgormModelToDomainModel(model *models.PushDeviceInfo) (*domain.Device, error) { | ||
65 | + return &domain.Device{ | ||
66 | + Uid: model.Uid, | ||
67 | + ClientId: model.ClientId, | ||
68 | + DeviceToken: model.DeviceToken, | ||
69 | + }, nil | ||
70 | +} | ||
71 | + | ||
72 | +func NewPushDeviceRepository(transactionContext interface{}) (*PushDeviceRepository, error) { | ||
73 | + if transactionContext == nil { | ||
74 | + return &PushDeviceRepository{}, nil | ||
75 | + } else { | ||
76 | + return &PushDeviceRepository{}, nil | ||
77 | + } | ||
78 | +} |
@@ -32,3 +32,45 @@ func (this *PushController) PushInfo() { | @@ -32,3 +32,45 @@ func (this *PushController) PushInfo() { | ||
32 | header := controllers.GetRequestHeader(this.Ctx) | 32 | header := controllers.GetRequestHeader(this.Ctx) |
33 | msg = protocol.NewReturnResponse(push.Notification(header, request)) | 33 | msg = protocol.NewReturnResponse(push.Notification(header, request)) |
34 | } | 34 | } |
35 | + | ||
36 | +//原生推送信息 PushInfoOriginal | ||
37 | +// @router /pushInfoOriginal [post] | ||
38 | +func (this *PushController) PushInfoOriginal() { | ||
39 | + var msg *protocol.ResponseMessage | ||
40 | + defer func() { | ||
41 | + this.Resp(msg) | ||
42 | + }() | ||
43 | + var request *protocol.PushInfoOriginalRequest | ||
44 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
45 | + log.Error(err) | ||
46 | + msg = protocol.BadRequestParam(1) | ||
47 | + return | ||
48 | + } | ||
49 | + if b, m := this.Valid(request); !b { | ||
50 | + msg = m | ||
51 | + return | ||
52 | + } | ||
53 | + header := controllers.GetRequestHeader(this.Ctx) | ||
54 | + msg = protocol.NewReturnResponse(push.NotificationOriginal(header, request)) | ||
55 | +} | ||
56 | + | ||
57 | +//UpdateDevice | ||
58 | +//@router /updateDevice [post] | ||
59 | +func (this *PushController) UpdateDevice() { | ||
60 | + var msg *protocol.ResponseMessage | ||
61 | + defer func() { | ||
62 | + this.Resp(msg) | ||
63 | + }() | ||
64 | + var request *protocol.UpdateDeviceRequest | ||
65 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { | ||
66 | + log.Error(err) | ||
67 | + msg = protocol.BadRequestParam(1) | ||
68 | + return | ||
69 | + } | ||
70 | + if b, m := this.Valid(request); !b { | ||
71 | + msg = m | ||
72 | + return | ||
73 | + } | ||
74 | + header := controllers.GetRequestHeader(this.Ctx) | ||
75 | + msg = protocol.NewReturnResponse(push.UpdateDevice(header, request)) | ||
76 | +} |
@@ -15,6 +15,22 @@ func init() { | @@ -15,6 +15,22 @@ func init() { | ||
15 | MethodParams: param.Make(), | 15 | MethodParams: param.Make(), |
16 | Params: nil}) | 16 | Params: nil}) |
17 | 17 | ||
18 | + beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:PushController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:PushController"], | ||
19 | + beego.ControllerComments{ | ||
20 | + Method: "PushInfoOriginal", | ||
21 | + Router: `/pushInfoOriginal`, | ||
22 | + AllowHTTPMethods: []string{"post"}, | ||
23 | + MethodParams: param.Make(), | ||
24 | + Params: nil}) | ||
25 | + | ||
26 | + beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:PushController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:PushController"], | ||
27 | + beego.ControllerComments{ | ||
28 | + Method: "UpdateDevice", | ||
29 | + Router: `/updateDevice`, | ||
30 | + AllowHTTPMethods: []string{"post"}, | ||
31 | + MethodParams: param.Make(), | ||
32 | + Params: nil}) | ||
33 | + | ||
18 | beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"], | 34 | beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"] = append(beego.GlobalControllerRouter["openapi/pkg/port/beego/controllers/v1:VodController"], |
19 | beego.ControllerComments{ | 35 | beego.ControllerComments{ |
20 | Method: "CreateUploadImage", | 36 | Method: "CreateUploadImage", |
-
请 注册 或 登录 后发表评论