package mw import ( "applet/app/svc" "errors" "fmt" "github.com/gin-gonic/gin" "strings" "applet/app/db" "applet/app/e" "applet/app/md" ) // DB is 中间件 用来检查master_id是否有对应的数据库engine func DB(c *gin.Context) { fmt.Println(c.Request.Header) masterID := c.GetHeader("master_id") fmt.Println("master_id", masterID) if masterID == "" { fmt.Println("not found master_id found MasterId start") masterID = c.GetHeader("MasterId") fmt.Println("MasterId", masterID) // if masterID still emtpy if masterID == "" { platform := c.GetHeader("Platform") if platform == md.PLATFORM_WAP { // H5 要根据域名去获取mid hostList := strings.Split(c.Request.Host, ".") if len(hostList) == 4 && (hostList[2]+"."+hostList[3] == "zhiyingos.com" || hostList[2]+"."+hostList[3] == "izhyin.com") { // 官方域名 masterID = hostList[1] } else { // 自定义域名 masterID = svc.GetWebSiteDomainMasterId(md.PLATFORM_WAP, c.Request.Host) } //requestURL := cfg.Official.URL + "/api/user/check" //fmt.Println(c.Request.Host) //client := &http.Client{ // Timeout: time.Duration(time.Second * 2), //} //data := []byte(fmt.Sprintf(`{"domain":"%s"}`, c.Request.Host)) //body := bytes.NewReader(data) //request, err := http.NewRequest("POST", requestURL, body) //if err != nil { // e.OutErr(c, e.ERR_MASTER_ID, errors.New("not found master_id in DBs")) // return //} //request.Header.Set("Content-Type", "application/json;charset=UTF-8") //resp, err := client.Do(request.WithContext(context.TODO())) //if err != nil { // e.OutErr(c, e.ERR_MASTER_ID, err) // return //} //defer resp.Body.Close() //respBytes, err := ioutil.ReadAll(resp.Body) //if err != nil { // e.OutErr(c, e.ERR_MASTER_ID, err) // return //} //mid := gjson.GetBytes(respBytes, "data.db_master_id").String() //if mid == "" { // e.OutErr(c, e.ERR_MASTER_ID, errors.New("not found master_id in DBs")) // return //} //masterID = mid } } } _, ok := db.DBs[masterID] if !ok { e.OutErr(c, e.ERR_MASTER_ID, errors.New("not found master_id in DBs")) return } fmt.Println("master_id", masterID) c.Set("mid", masterID) //判断是否有独立域名 domain_wap_base := svc.GetWebSiteDomainInfo(c, "wap") httpStr := "http://" 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 { httpStr = "https://" domain_wap_base = strings.Replace(domain_wap_base, "http://", httpStr, 1) } c.Set("domain_wap_base", domain_wap_base) c.Set("http_host", httpStr) c.Set("h5_api_secret_key", svc.SysCfgGet(c, "h5_api_secret_key")) c.Set("app_api_secret_key", svc.SysCfgGet(c, "app_api_secret_key")) c.Set("applet_api_secret_key", svc.SysCfgGet(c, "applet_api_secret_key")) c.Next() }