123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package pubsub
- import (
- "gd_management/apis"
- "gd_management/common.in/cache"
- "gd_management/common.in/config"
- "gd_management/errors"
- "encoding/json"
- "fmt"
- "time"
- "github.com/astaxie/beego/orm"
- )
- func PublishApiExportInfoNotify() error {
- _, err := cache.Redis.Publish(apis.ApiExportInfoNotifyChannel, "-")
- if err != nil {
- return errors.RedisPublishFailed
- }
- return nil
- }
- func PublishProviderExportInfoNotify() error {
- _, err := cache.Redis.Publish(apis.ProviderExportInfoNotifyChannel, "-")
- if err != nil {
- return errors.RedisPublishFailed
- }
- return nil
- }
- func PublishApiNotify(apiId int64, router, method string) error {
- if router == "" || method == "" {
- o := orm.NewOrm()
- o.Raw("select router, method from t_gd_api where id=?", apiId).QueryRow(&router, &method)
- }
- if router == "" || method == "" {
- return nil
- }
- m := map[string]string{
- "router": router,
- "method": method,
- }
- bytes, _ := json.Marshal(m)
- _, err := cache.Redis.Publish(apis.ApiNotifyChannel, string(bytes))
- if err != nil {
- return errors.RedisPublishFailed
- }
- return nil
- }
- func PublishMerchantNotify(appKey string) error {
- _, err := cache.Redis.Publish(apis.MerchantNotifyChannel, appKey)
- if err != nil {
- return errors.RedisPublishFailed
- }
- return nil
- }
- type MerchantApiInfo struct {
- ApiId int64 `json:"api_id"`
- MerchantId int64 `json:"merchant_id"`
- }
- func PublishMerchantApiNotify(merchantDataApiId int64, queryTypeId int64, apiId int64, merchantId int64) error {
- if merchantDataApiId > 0 {
- tabs := []MerchantApiInfo{}
- o := orm.NewOrm()
- o.Raw("select t1.merchant_id, t2.api_id from t_gd_merchant_data_api as t1 left join t_gd_merchant_child_data_api as t2 on t1.id=t2.merchant_data_api_id where t2.merchant_data_api_id=?", merchantDataApiId).QueryRows(&tabs)
- for _, v := range tabs {
- bytes, _ := json.Marshal(v)
- _, err := cache.Redis.Publish(apis.MerchantApiNotifyChannel, string(bytes))
- if err != nil {
- return errors.RedisPublishFailed
- }
- }
- return nil
- }
- if queryTypeId > 0 {
- tabs := []MerchantApiInfo{}
- o := orm.NewOrm()
- o.Raw("select t1.merchant_id, t2.api_id from t_gd_merchant_data_api as t1 left join t_gd_merchant_child_data_api as t2 on t1.id=t2.merchant_data_api_id where t1.query_type_id=?", queryTypeId).QueryRows(&tabs)
- for _, v := range tabs {
- bytes, _ := json.Marshal(v)
- _, err := cache.Redis.Publish(apis.MerchantApiNotifyChannel, string(bytes))
- if err != nil {
- return errors.RedisPublishFailed
- }
- }
- return nil
- }
- tab := MerchantApiInfo{
- ApiId: apiId,
- MerchantId: merchantId,
- }
- bytes, _ := json.Marshal(tab)
- _, err := cache.Redis.Publish(apis.MerchantApiNotifyChannel, string(bytes))
- if err != nil {
- return errors.RedisPublishFailed
- }
- return nil
- }
- func PublishNameNotify(field string, id int64, providerId int64) error {
- if providerId == 0 {
- msg := fmt.Sprintf("\"%s\":%d", field, id)
- _, err := cache.Redis.Publish(apis.NameNotifyChannel, msg)
- if err != nil {
- return errors.RedisPublishFailed
- }
- return nil
- }
- o := orm.NewOrm()
- ids := []int64{}
- o.Raw("select id from t_gd_provider_api where provider_id=?", providerId).QueryRows(&ids)
- for _, v := range ids {
- msg := fmt.Sprintf("\"%s\":%d", "provider_api_id", v)
- _, err := cache.Redis.Publish(apis.NameNotifyChannel, msg)
- if err != nil {
- return errors.RedisPublishFailed
- }
- }
- return nil
- }
- func PublishRateLimit() {
- for i := 0; i < 3; i++ {
- if _, err := cache.Redis.Publish(config.Conf.RateLimit.RateLimitChannel, time.Now().UnixNano()); err == nil {
- break
- }
- }
- }
|