h5_auth_check.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package h5
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/common.in/config"
  6. "gd_management/common.in/utils"
  7. "gd_management/errors"
  8. "encoding/hex"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/astaxie/beego/orm"
  12. "time"
  13. )
  14. var (
  15. CRYPTOKEY = "a95cbb574bdc905f9bf457820f1fa604"
  16. )
  17. func checkMerchant(appKey string, mangementH5AuthCheck *apis.MangementH5AuthCheck) error {
  18. // 1 检查商户是否存在
  19. //var merchant_id int64
  20. sql := `select id from t_gd_merchants where app_key = ?`
  21. err := orm.NewOrm().Raw(sql, appKey).QueryRow(&mangementH5AuthCheck.MerchantId)
  22. if err != nil {
  23. if err != orm.ErrNoRows {
  24. return errors.DataBaseError
  25. } else {
  26. return errors.CheckMerchantNotExist
  27. }
  28. }
  29. return nil
  30. }
  31. func checkH5Api(h5Router string, mangementH5AuthCheck *apis.MangementH5AuthCheck) error {
  32. sql := `select id from t_gd_h5_api where router = ?`
  33. err := orm.NewOrm().Raw(sql, h5Router).QueryRow(&mangementH5AuthCheck.H5ApiId)
  34. if err != nil {
  35. if err != orm.ErrNoRows {
  36. return errors.DataBaseError
  37. } else {
  38. return errors.H5ApiNotExist
  39. }
  40. }
  41. return nil
  42. }
  43. func checkAccessCount(H5ServiceMerchantId int64, CountType int, count int64) error {
  44. o := orm.NewOrm()
  45. acessCount := &apis.TGdH5ServiceAccessCount{}
  46. lockStr := fmt.Sprintf("t_gd_h5_service_access_count_%d", H5ServiceMerchantId)
  47. utils.Lock(lockStr)
  48. defer utils.UnLock(lockStr)
  49. if CountType == 2 {
  50. timeNow := time.Now()
  51. var timeLayout = "2006-01-02"
  52. Now := timeNow.Format(timeLayout)
  53. err := o.QueryTable("t_gd_h5_service_access_count").Filter("h5_service_merchant_id", H5ServiceMerchantId).Filter("create_time", Now).One(acessCount)
  54. if err != nil && err != orm.ErrNoRows {
  55. return errors.DataBaseError
  56. }
  57. if acessCount.Count >= count {
  58. return errors.CheckApiDayCountLimit
  59. }
  60. /*acessCount.H5ServiceMerchantId = H5ServiceMerchantId
  61. acessCount.Count++
  62. if err != nil && err == orm.ErrNoRows {
  63. acessCount.CreateTime = Now
  64. o.Insert(acessCount)
  65. } else {
  66. o.Update(acessCount)
  67. }*/
  68. } else if CountType == 1 {
  69. err := o.QueryTable("t_gd_h5_service_access_count").Filter("h5_service_merchant_id", H5ServiceMerchantId).One(acessCount)
  70. if err != nil && err != orm.ErrNoRows {
  71. return errors.DataBaseError
  72. }
  73. if acessCount.Count >= count {
  74. return errors.CheckApiTotalCountLimit
  75. }
  76. /*acessCount.H5ServiceMerchantId = H5ServiceMerchantId
  77. acessCount.Count++
  78. if err != nil && err == orm.ErrNoRows {
  79. o.Insert(acessCount)
  80. } else {
  81. o.Update(acessCount)
  82. }*/
  83. }
  84. return nil
  85. }
  86. func ManagementH5AuthCheck(ctx context.Context, req *apis.ManagementH5AuthCheckReq, reply *apis.ManagementH5AuthCheckReply) error {
  87. o := orm.NewOrm()
  88. mangementH5AuthCheck := apis.MangementH5AuthCheck{}
  89. defer func() {
  90. data, _ := json.Marshal(mangementH5AuthCheck)
  91. enData, _ := config.AesEncrypt(string(data), CRYPTOKEY)
  92. reply.Data = hex.EncodeToString(enData)
  93. }()
  94. // 1 检查商户是否存在
  95. /*var merchant_id int64
  96. var app_secret string
  97. sql := `select id,app_secret from t_gd_merchants where appkey = ?`
  98. err := o.Raw(sql, req.AppKey).QueryRow(&merchant_id, &app_secret)
  99. if err != nil {
  100. if err != orm.ErrNoRows {
  101. return errors.DataBaseError
  102. } else {
  103. }
  104. }*/
  105. // 2 检查h5 api是否存在
  106. /*mangementH5AuthCheck.MerchantId = merchant_id
  107. var h5_api_id int64
  108. sql := `select id from t_gd_h5_api where h5_router = ?`
  109. err := o.Raw(sql, req.H5Router).QueryRow(&h5_api_id)
  110. if err != nil {
  111. if err != orm.ErrNoRows {
  112. return errors.DataBaseError
  113. } else {
  114. }
  115. }
  116. mangementH5AuthCheck.H5ApiId = h5_api_id
  117. */
  118. // 3 检查h5 service 是否和h5 api关联
  119. // e1 := make(chan error)
  120. // e2 := make(chan error)
  121. err := checkMerchant(req.AppKey, &mangementH5AuthCheck)
  122. if err != nil {
  123. return err
  124. }
  125. err = checkH5Api(req.H5Router, &mangementH5AuthCheck)
  126. if err != nil {
  127. return err
  128. }
  129. //var h5_service_id int64
  130. sql := `select h5_service_id from t_gd_h5_service_api_relation where h5_api_id = ?`
  131. err = o.Raw(sql, mangementH5AuthCheck.H5ApiId).QueryRow(&mangementH5AuthCheck.H5ServiceId)
  132. if err != nil {
  133. if err != orm.ErrNoRows {
  134. return errors.DataBaseError
  135. } else {
  136. }
  137. }
  138. //mangementH5AuthCheck.H5ServiceId = h5_service_id
  139. // 4 检查商户是否购买h5 服务
  140. nowTimeStamp := time.Now().Unix()
  141. var endTimeStamp int64
  142. var count int64
  143. sql = `select id,count,theme_color,end_time from t_gd_h5_service_merchant where merchant_id = ? and h5_service_id = ?`
  144. err = o.Raw(sql, mangementH5AuthCheck.MerchantId, mangementH5AuthCheck.H5ServiceId).QueryRow(&mangementH5AuthCheck.H5ServiceMerchantId, &count, &reply.ThemeColor, &endTimeStamp)
  145. if err != nil {
  146. if err != orm.ErrNoRows {
  147. return errors.DataBaseError
  148. } else {
  149. return errors.CheckApiInfoNotExist
  150. }
  151. }
  152. if endTimeStamp != 0 && nowTimeStamp > endTimeStamp {
  153. return errors.CheckApiDaysLimit
  154. }
  155. if count != 0 {
  156. // 获取调用次数
  157. /*var accessCount int64
  158. timeNow := time.Now()
  159. var timeLayout = "2006-01-02"*/
  160. mangementH5AuthCheck.IsFree = false
  161. if endTimeStamp != 0 {
  162. // 按天
  163. mangementH5AuthCheck.CountType = 2
  164. err := checkAccessCount(mangementH5AuthCheck.H5ServiceMerchantId, mangementH5AuthCheck.CountType, count)
  165. if err != nil {
  166. return err
  167. }
  168. } else {
  169. // 按总量
  170. mangementH5AuthCheck.CountType = 1
  171. err := checkAccessCount(mangementH5AuthCheck.H5ServiceMerchantId, mangementH5AuthCheck.CountType, count)
  172. if err != nil {
  173. return err
  174. }
  175. }
  176. } else {
  177. mangementH5AuthCheck.IsFree = true
  178. }
  179. return nil
  180. }