面包店
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

94 rindas
3.0 KiB

  1. package mw
  2. import (
  3. "applet/app/svc"
  4. "errors"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "strings"
  8. "applet/app/db"
  9. "applet/app/e"
  10. "applet/app/md"
  11. )
  12. // DB is 中间件 用来检查master_id是否有对应的数据库engine
  13. func DB(c *gin.Context) {
  14. fmt.Println(c.Request.Header)
  15. masterID := c.GetHeader("master_id")
  16. fmt.Println("master_id", masterID)
  17. if masterID == "" {
  18. fmt.Println("not found master_id found MasterId start")
  19. masterID = c.GetHeader("MasterId")
  20. fmt.Println("MasterId", masterID)
  21. // if masterID still emtpy
  22. if masterID == "" {
  23. platform := c.GetHeader("Platform")
  24. if platform == md.PLATFORM_WAP {
  25. // H5 要根据域名去获取mid
  26. hostList := strings.Split(c.Request.Host, ".")
  27. if len(hostList) == 4 && (hostList[2]+"."+hostList[3] == "zhiyingos.com" || hostList[2]+"."+hostList[3] == "izhyin.com") {
  28. // 官方域名
  29. masterID = hostList[1]
  30. } else {
  31. // 自定义域名
  32. masterID = svc.GetWebSiteDomainMasterId(md.PLATFORM_WAP, c.Request.Host)
  33. }
  34. //requestURL := cfg.Official.URL + "/api/user/check"
  35. //fmt.Println(c.Request.Host)
  36. //client := &http.Client{
  37. // Timeout: time.Duration(time.Second * 2),
  38. //}
  39. //data := []byte(fmt.Sprintf(`{"domain":"%s"}`, c.Request.Host))
  40. //body := bytes.NewReader(data)
  41. //request, err := http.NewRequest("POST", requestURL, body)
  42. //if err != nil {
  43. // e.OutErr(c, e.ERR_MASTER_ID, errors.New("not found master_id in DBs"))
  44. // return
  45. //}
  46. //request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  47. //resp, err := client.Do(request.WithContext(context.TODO()))
  48. //if err != nil {
  49. // e.OutErr(c, e.ERR_MASTER_ID, err)
  50. // return
  51. //}
  52. //defer resp.Body.Close()
  53. //respBytes, err := ioutil.ReadAll(resp.Body)
  54. //if err != nil {
  55. // e.OutErr(c, e.ERR_MASTER_ID, err)
  56. // return
  57. //}
  58. //mid := gjson.GetBytes(respBytes, "data.db_master_id").String()
  59. //if mid == "" {
  60. // e.OutErr(c, e.ERR_MASTER_ID, errors.New("not found master_id in DBs"))
  61. // return
  62. //}
  63. //masterID = mid
  64. }
  65. }
  66. }
  67. _, ok := db.DBs[masterID]
  68. if !ok {
  69. e.OutErr(c, e.ERR_MASTER_ID, errors.New("not found master_id in DBs"))
  70. return
  71. }
  72. fmt.Println("master_id", masterID)
  73. c.Set("mid", masterID)
  74. //判断是否有独立域名
  75. domain_wap_base := svc.GetWebSiteDomainInfo(c, "wap")
  76. httpStr := "http://"
  77. if c.GetHeader("Platform") == md.PLATFORM_WX_APPLET || c.GetHeader("Platform") == md.PLATFORM_ALIPAY_APPLET || c.GetHeader("Platform") == md.PLATFORM_BAIDU_APPLET || c.GetHeader("Platform") == md.PLATFORM_TOUTIAO_APPLET || c.GetHeader("Platform") == md.PLATFORM_TIKTOK_APPLET {
  78. httpStr = "https://"
  79. domain_wap_base = strings.Replace(domain_wap_base, "http://", httpStr, 1)
  80. }
  81. c.Set("domain_wap_base", domain_wap_base)
  82. c.Set("http_host", httpStr)
  83. c.Set("h5_api_secret_key", svc.SysCfgGet(c, "h5_api_secret_key"))
  84. c.Set("app_api_secret_key", svc.SysCfgGet(c, "app_api_secret_key"))
  85. c.Set("applet_api_secret_key", svc.SysCfgGet(c, "applet_api_secret_key"))
  86. c.Next()
  87. }