123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- package h5
- import (
- "context"
- "gd_management/apis"
- "gd_management/common.in/config"
- "gd_management/common.in/utils"
- "gd_management/errors"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/orm"
- "time"
- )
- var (
- CRYPTOKEY = "a95cbb574bdc905f9bf457820f1fa604"
- )
- func checkMerchant(appKey string, mangementH5AuthCheck *apis.MangementH5AuthCheck) error {
- // 1 检查商户是否存在
- //var merchant_id int64
- sql := `select id from t_gd_merchants where app_key = ?`
- err := orm.NewOrm().Raw(sql, appKey).QueryRow(&mangementH5AuthCheck.MerchantId)
- if err != nil {
- if err != orm.ErrNoRows {
- return errors.DataBaseError
- } else {
- return errors.CheckMerchantNotExist
- }
- }
- return nil
- }
- func checkH5Api(h5Router string, mangementH5AuthCheck *apis.MangementH5AuthCheck) error {
- sql := `select id from t_gd_h5_api where router = ?`
- err := orm.NewOrm().Raw(sql, h5Router).QueryRow(&mangementH5AuthCheck.H5ApiId)
- if err != nil {
- if err != orm.ErrNoRows {
- return errors.DataBaseError
- } else {
- return errors.H5ApiNotExist
- }
- }
- return nil
- }
- func checkAccessCount(H5ServiceMerchantId int64, CountType int, count int64) error {
- o := orm.NewOrm()
- acessCount := &apis.TGdH5ServiceAccessCount{}
- lockStr := fmt.Sprintf("t_gd_h5_service_access_count_%d", H5ServiceMerchantId)
- utils.Lock(lockStr)
- defer utils.UnLock(lockStr)
- if CountType == 2 {
- timeNow := time.Now()
- var timeLayout = "2006-01-02"
- Now := timeNow.Format(timeLayout)
- err := o.QueryTable("t_gd_h5_service_access_count").Filter("h5_service_merchant_id", H5ServiceMerchantId).Filter("create_time", Now).One(acessCount)
- if err != nil && err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- if acessCount.Count >= count {
- return errors.CheckApiDayCountLimit
- }
- /*acessCount.H5ServiceMerchantId = H5ServiceMerchantId
- acessCount.Count++
- if err != nil && err == orm.ErrNoRows {
- acessCount.CreateTime = Now
- o.Insert(acessCount)
- } else {
- o.Update(acessCount)
- }*/
- } else if CountType == 1 {
- err := o.QueryTable("t_gd_h5_service_access_count").Filter("h5_service_merchant_id", H5ServiceMerchantId).One(acessCount)
- if err != nil && err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- if acessCount.Count >= count {
- return errors.CheckApiTotalCountLimit
- }
- /*acessCount.H5ServiceMerchantId = H5ServiceMerchantId
- acessCount.Count++
- if err != nil && err == orm.ErrNoRows {
- o.Insert(acessCount)
- } else {
- o.Update(acessCount)
- }*/
- }
- return nil
- }
- func ManagementH5AuthCheck(ctx context.Context, req *apis.ManagementH5AuthCheckReq, reply *apis.ManagementH5AuthCheckReply) error {
- o := orm.NewOrm()
- mangementH5AuthCheck := apis.MangementH5AuthCheck{}
- defer func() {
- data, _ := json.Marshal(mangementH5AuthCheck)
- enData, _ := config.AesEncrypt(string(data), CRYPTOKEY)
- reply.Data = hex.EncodeToString(enData)
- }()
- // 1 检查商户是否存在
- /*var merchant_id int64
- var app_secret string
- sql := `select id,app_secret from t_gd_merchants where appkey = ?`
- err := o.Raw(sql, req.AppKey).QueryRow(&merchant_id, &app_secret)
- if err != nil {
- if err != orm.ErrNoRows {
- return errors.DataBaseError
- } else {
- }
- }*/
- // 2 检查h5 api是否存在
- /*mangementH5AuthCheck.MerchantId = merchant_id
- var h5_api_id int64
- sql := `select id from t_gd_h5_api where h5_router = ?`
- err := o.Raw(sql, req.H5Router).QueryRow(&h5_api_id)
- if err != nil {
- if err != orm.ErrNoRows {
- return errors.DataBaseError
- } else {
- }
- }
- mangementH5AuthCheck.H5ApiId = h5_api_id
- */
- // 3 检查h5 service 是否和h5 api关联
- // e1 := make(chan error)
- // e2 := make(chan error)
- err := checkMerchant(req.AppKey, &mangementH5AuthCheck)
- if err != nil {
- return err
- }
- err = checkH5Api(req.H5Router, &mangementH5AuthCheck)
- if err != nil {
- return err
- }
- //var h5_service_id int64
- sql := `select h5_service_id from t_gd_h5_service_api_relation where h5_api_id = ?`
- err = o.Raw(sql, mangementH5AuthCheck.H5ApiId).QueryRow(&mangementH5AuthCheck.H5ServiceId)
- if err != nil {
- if err != orm.ErrNoRows {
- return errors.DataBaseError
- } else {
- }
- }
- //mangementH5AuthCheck.H5ServiceId = h5_service_id
- // 4 检查商户是否购买h5 服务
- nowTimeStamp := time.Now().Unix()
- var endTimeStamp int64
- var count int64
- sql = `select id,count,theme_color,end_time from t_gd_h5_service_merchant where merchant_id = ? and h5_service_id = ?`
- err = o.Raw(sql, mangementH5AuthCheck.MerchantId, mangementH5AuthCheck.H5ServiceId).QueryRow(&mangementH5AuthCheck.H5ServiceMerchantId, &count, &reply.ThemeColor, &endTimeStamp)
- if err != nil {
- if err != orm.ErrNoRows {
- return errors.DataBaseError
- } else {
- return errors.CheckApiInfoNotExist
- }
- }
- if endTimeStamp != 0 && nowTimeStamp > endTimeStamp {
- return errors.CheckApiDaysLimit
- }
- if count != 0 {
- // 获取调用次数
- /*var accessCount int64
- timeNow := time.Now()
- var timeLayout = "2006-01-02"*/
- mangementH5AuthCheck.IsFree = false
- if endTimeStamp != 0 {
- // 按天
- mangementH5AuthCheck.CountType = 2
- err := checkAccessCount(mangementH5AuthCheck.H5ServiceMerchantId, mangementH5AuthCheck.CountType, count)
- if err != nil {
- return err
- }
- } else {
- // 按总量
- mangementH5AuthCheck.CountType = 1
- err := checkAccessCount(mangementH5AuthCheck.H5ServiceMerchantId, mangementH5AuthCheck.CountType, count)
- if err != nil {
- return err
- }
- }
- } else {
- mangementH5AuthCheck.IsFree = true
- }
- return nil
- }
|