附近小店
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

167 рядки
5.3 KiB

  1. package db
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db/model"
  5. "applet/app/utils"
  6. "applet/app/utils/cache"
  7. "crypto/tls"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/tidwall/gjson"
  12. "io"
  13. "io/ioutil"
  14. "net/http"
  15. "strings"
  16. "time"
  17. "xorm.io/xorm"
  18. )
  19. func GetAppletKey(c *gin.Context, eg *xorm.Engine) map[string]string {
  20. appId := c.GetHeader("appid")
  21. isFilterTaobao := "0"
  22. appletName := ""
  23. appletLogo := ""
  24. originalAppId := ""
  25. bottomNavCssId := ""
  26. mpAuditVersion := ""
  27. MpAuditGoodsDetailCssId := ""
  28. orderCssId := ""
  29. if appId == "" {
  30. wxAppletCfg := SysCfgGetWithDb(eg, c.GetString("mid"), "wx_applet_key")
  31. isFilterTaobao = gjson.Get(wxAppletCfg, "taobaoGoodsOnOff").String()
  32. appletName = gjson.Get(wxAppletCfg, "appletName").String()
  33. originalAppId = gjson.Get(wxAppletCfg, "originalAppId").String()
  34. appletLogo = gjson.Get(wxAppletCfg, "appletIcon").String()
  35. mpAuditVersion = SysCfgGetWithDb(eg, c.GetString("mid"), "mp_audit_version")
  36. var tm model.SysTemplate
  37. if has, err := eg.Where("is_use = 1 AND type = 'bottom' AND platform = 4 ").
  38. Cols("id,uid,name,is_use,is_system").
  39. Get(&tm); err != nil || has == false {
  40. bottomNavCssId = ""
  41. } else {
  42. bottomNavCssId = utils.IntToStr(tm.Id)
  43. }
  44. if c.GetHeader("AppVersionName") == mpAuditVersion && c.GetHeader("AppVersionName") != "" {
  45. m := SysCfgGetWithDb(eg, c.GetString("mid"), "mp_audit_template")
  46. if m != "" {
  47. bottomNavCssId = utils.Int64ToStr(gjson.Get(m, "bottom").Int())
  48. }
  49. m1 := SysCfgGet(c, "mp_audit_template")
  50. if m1 != "" {
  51. MpAuditGoodsDetailCssId = utils.Int64ToStr(gjson.Get(m1, "product_detail").Int())
  52. }
  53. }
  54. appId = gjson.Get(wxAppletCfg, "appId").String()
  55. } else {
  56. var wxApplet model.WxAppletList
  57. has, err2 := eg.Where("app_id=?", appId).Get(&wxApplet)
  58. if has && err2 == nil {
  59. isFilterTaobao = utils.IntToStr(wxApplet.IsFilterTaobao)
  60. appletName = wxApplet.AppletName
  61. appletLogo = wxApplet.AppletLogo
  62. originalAppId = wxApplet.OriginalAppId
  63. orderCssId = utils.IntToStr(wxApplet.OrderCssId)
  64. mpAuditVersion = wxApplet.MpAuditVersion
  65. bottomNavCssId = utils.IntToStr(wxApplet.BottomNavCssId)
  66. MpAuditGoodsDetailCssId = ""
  67. if c.GetHeader("AppVersionName") == mpAuditVersion && c.GetHeader("AppVersionName") != "" {
  68. bottomNavCssId = utils.IntToStr(wxApplet.MpAuditCssId)
  69. MpAuditGoodsDetailCssId = utils.IntToStr(wxApplet.MpAuditGoodsDetailCssId)
  70. }
  71. }
  72. }
  73. r := map[string]string{
  74. "order_css_id": orderCssId,
  75. "app_id": appId,
  76. "is_filter_taobao": isFilterTaobao,
  77. "applet_name": appletName,
  78. "applet_logo": appletLogo,
  79. "original_app_id": originalAppId,
  80. "bottom_nav_css_id": bottomNavCssId,
  81. "mp_audit_version": mpAuditVersion,
  82. "mp_audit_goods_detail_css_id": MpAuditGoodsDetailCssId,
  83. }
  84. return r
  85. }
  86. func GetShareUse(c *gin.Context, eg *xorm.Engine) map[string]string {
  87. var wxApplet model.WxAppletList
  88. has, err2 := eg.Where("share_use=?", 1).Asc("id").Get(&wxApplet)
  89. wxAppletCfg := SysCfgGetWithDb(eg, c.GetString("mid"), "wx_applet_key")
  90. originalAppId := gjson.Get(wxAppletCfg, "originalAppId").String()
  91. appId := gjson.Get(wxAppletCfg, "appId").String()
  92. if has && err2 == nil {
  93. originalAppId = wxApplet.OriginalAppId
  94. appId = wxApplet.AppId
  95. }
  96. r := map[string]string{
  97. "app_id": appId,
  98. "original_app_id": originalAppId,
  99. }
  100. return r
  101. }
  102. func GetAppletToken(c *gin.Context, appId string, isMore, isReset string) (accessToken string) {
  103. key := fmt.Sprintf("%s:%s:%s", c.GetString("mid"), "wx_applet_access_token2", appId)
  104. token, err := cache.GetString(key)
  105. if err == nil && token != "" && strings.Contains(token, "{") == false && isReset == "0" {
  106. // 有缓存
  107. accessToken = token
  108. } else {
  109. ExpiresIn := 1800
  110. accessTokenStr := ApiToSuperAdminWx(c, appId, isMore)
  111. if accessTokenStr == "" {
  112. return ""
  113. }
  114. ExpiresIn = int(gjson.Get(accessTokenStr, "expires_in").Int()) - 60*60*1 //TODO::暂时只用1个小时
  115. accessToken = gjson.Get(accessTokenStr, "authorizer_access_token").String()
  116. if ExpiresIn > 0 {
  117. //fmt.Printf("返回结果: %#v", res)
  118. _, err = cache.SetEx(key, accessToken, ExpiresIn)
  119. if err != nil {
  120. fmt.Println("微信授权错误", err)
  121. return ""
  122. }
  123. }
  124. }
  125. return accessToken
  126. }
  127. // 总后台微信token isMore多小程序登录总后台要判断读另一个表
  128. func ApiToSuperAdminWx(c *gin.Context, appid string, isMore string) string {
  129. var req = make(map[string]string, 0)
  130. var host string
  131. host = cfg.WebsiteBackend.URL + "/Wx/getAuthorizerResult?appid=" + appid + "&is_more=" + isMore
  132. fmt.Println(host)
  133. tr := &http.Transport{
  134. MaxIdleConnsPerHost: 200,
  135. MaxIdleConns: 200,
  136. MaxConnsPerHost: 200,
  137. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  138. }
  139. client := &http.Client{
  140. Timeout: 5 * time.Second,
  141. Transport: tr,
  142. }
  143. byte1, _ := json.Marshal(req)
  144. req1, _ := http.NewRequest("POST", host, strings.NewReader(string(byte1)))
  145. req1.Header.Set("Content-Type", "application/json")
  146. resp, err := (client).Do(req1)
  147. if err != nil || resp == nil {
  148. if resp != nil {
  149. _, _ = io.Copy(ioutil.Discard, resp.Body)
  150. }
  151. return ""
  152. }
  153. defer resp.Body.Close()
  154. respByte, _ := ioutil.ReadAll(resp.Body)
  155. if len(respByte) == 0 {
  156. return ""
  157. }
  158. return string(respByte)
  159. }