2 次代码提交 8fb86e61ad ... 5e02883729

作者 SHA1 备注 提交日期
  abiao 5e02883729 fix xcx haibao logging and merchant lookup noise 3 周之前
  abiao 678ec66a3d Fix legacy mall user lookup on prod schema 2 月之前

+ 45 - 31
go/gopath/src/fohow.com/apps/controllers/tool_controller/qrcode_controller.go

@@ -3,7 +3,7 @@ package tool_controller
 import (
 	"context"
 	"fmt"
-	"log"
+	"strings"
 	"time"
 
 	"github.com/chromedp/chromedp"
@@ -14,6 +14,7 @@ import (
 
 	"github.com/astaxie/beego"
 	// "github.com/astaxie/beego/context"
+	"fohow.com/apps"
 	"github.com/go-wkhtmltoimage"
 	"github.com/skip2/go-qrcode"
 	// "fohow.com/apps"
@@ -56,19 +57,19 @@ func (self *ToolController) GetHaibao() {
 
 func (self *ToolController) GetHaibaoWithGoogle() {
 	uri := self.GetString("url")
-	fmt.Println("url:", uri)
 
 	// URL 解码
 	decodedUri, err := url.QueryUnescape(uri)
 	if err != nil {
-		fmt.Println("Error in decoding:", err)
-		log.Fatal(err)
+		beego.BeeLogger.Warning("GetHaibaoWithGoogle decode url failed: err=[%s] raw_url=[%s]", err, maskURLForLog(uri))
+		self.ReturnError(400, apps.ParamsError, "", nil)
+		return
 	}
 
-	fmt.Println("Decoded URI:", decodedUri)
+	beego.BeeLogger.Info("GetHaibaoWithGoogle start url=[%s]", maskURLForLog(decodedUri))
 	//title := self.GetString("title")
 	// 创建上下文
-	ctx, cancel := chromedp.NewContext(context.Background())
+	ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithErrorf(chromedpErrorLogf))
 	defer cancel()
 
 	// 设置超时时间
@@ -87,18 +88,12 @@ func (self *ToolController) GetHaibaoWithGoogle() {
 		chromedp.FullScreenshot(&buf, 60), // 调整质量参数
 	)
 	if err != nil {
-		fmt.Println("error occured!")
-		log.Fatal(err)
+		beego.BeeLogger.Error("GetHaibaoWithGoogle screenshot failed: err=[%s] url=[%s]", err, maskURLForLog(decodedUri))
+		self.ReturnError(504, apps.NetworkBusy, "", nil)
+		return
 	}
 
-	//fileName := fmt.Sprintf("%s.png", "shot_screen")
-	//// 将截图保存到文件
-	//err = ioutil.WriteFile(fileName, buf, 0644)
-	//if err != nil {
-	//	log.Fatal(err)
-	//}
-
-	//fmt.Println("image buf:", buf)
+	beego.BeeLogger.Info("GetHaibaoWithGoogle success url=[%s] bytes=[%d]", maskURLForLog(decodedUri), len(buf))
 	self.Ctx.Output.Header("Content-Type", "image/png")
 	self.Ctx.Output.Body(buf)
 
@@ -107,19 +102,19 @@ func (self *ToolController) GetHaibaoWithGoogle() {
 
 func (self *ToolController) GetOtherHaibaoWithGoogle() {
 	uri := self.GetString("url")
-	fmt.Println("url:", uri)
 
 	// URL 解码
 	decodedUri, err := url.QueryUnescape(uri)
 	if err != nil {
-		fmt.Println("Error in decoding:", err)
-		log.Fatal(err)
+		beego.BeeLogger.Warning("GetOtherHaibaoWithGoogle decode url failed: err=[%s] raw_url=[%s]", err, maskURLForLog(uri))
+		self.ReturnError(400, apps.ParamsError, "", nil)
+		return
 	}
 
-	fmt.Println("Decoded URI:", decodedUri)
+	beego.BeeLogger.Info("GetOtherHaibaoWithGoogle start url=[%s]", maskURLForLog(decodedUri))
 	//title := self.GetString("title")
 	// 创建上下文
-	ctx, cancel := chromedp.NewContext(context.Background())
+	ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithErrorf(chromedpErrorLogf))
 	defer cancel()
 
 	// 设置超时时间
@@ -137,18 +132,12 @@ func (self *ToolController) GetOtherHaibaoWithGoogle() {
 		chromedp.FullScreenshot(&buf, 90), // 调整质量参数
 	)
 	if err != nil {
-		fmt.Println("error occured!")
-		log.Fatal(err)
+		beego.BeeLogger.Error("GetOtherHaibaoWithGoogle screenshot failed: err=[%s] url=[%s]", err, maskURLForLog(decodedUri))
+		self.ReturnError(504, apps.NetworkBusy, "", nil)
+		return
 	}
 
-	//fileName := fmt.Sprintf("%s.png", "shot_screen")
-	//// 将截图保存到文件
-	//err = ioutil.WriteFile(fileName, buf, 0644)
-	//if err != nil {
-	//	log.Fatal(err)
-	//}
-
-	//fmt.Println("image buf:", buf)
+	beego.BeeLogger.Info("GetOtherHaibaoWithGoogle success url=[%s] bytes=[%d]", maskURLForLog(decodedUri), len(buf))
 	self.Ctx.Output.Header("Content-Type", "image/png")
 	self.Ctx.Output.Body(buf)
 
@@ -197,3 +186,28 @@ func isWhiteUrl(uri string) bool {
 	}
 	return false
 }
+
+func maskURLForLog(rawURL string) string {
+	if rawURL == "" {
+		return ""
+	}
+	u, err := url.Parse(rawURL)
+	if err != nil {
+		return rawURL
+	}
+	q := u.Query()
+	if q.Get("token") != "" {
+		q.Set("token", "***")
+	}
+	u.RawQuery = q.Encode()
+	return u.String()
+}
+
+func chromedpErrorLogf(format string, args ...interface{}) {
+	msg := fmt.Sprintf(format, args...)
+	if strings.Contains(msg, "could not unmarshal event") {
+		beego.BeeLogger.Warning("chromedp ignored protocol event: %s", msg)
+		return
+	}
+	beego.BeeLogger.Error("chromedp error: %s", msg)
+}

+ 1 - 1
go/gopath/src/fohow.com/apps/init.go

@@ -254,7 +254,7 @@ func (self *BaseController) GetCurrentUser(useCache bool) *user_model.User {
 	}
 	u := user_model.GetUserById(id, useCache)
 	if u == nil {
-		beego.BeeLogger.Error("User Not Found, uid=%d", id)
+		beego.BeeLogger.Error("User Not Found, wx_uid=%d, uid=%d", wxUser.Id, id)
 	}
 	return u
 }

+ 6 - 4
go/gopath/src/fohow.com/apps/models/merchant_model/merchant_user.go

@@ -2,9 +2,9 @@ package merchant_model
 
 import (
 	"fmt"
+	"fohow.com/cache"
 	"github.com/astaxie/beego"
 	"github.com/astaxie/beego/orm"
-	"fohow.com/cache"
 	"time"
 )
 
@@ -27,7 +27,7 @@ func (self *MerchantUserRelation) TableName() string {
 	return merchant_user_relations_tablename
 }
 
-//根据用户查询记录
+// 根据用户查询记录
 func GetMerchantUserRelationByUserId(userId int64, useCache bool) *MerchantUserRelation {
 
 	k := fmt.Sprintf("merchant_model.GetMerchantUserRelationByUserId(%d)", userId)
@@ -39,7 +39,9 @@ func GetMerchantUserRelationByUserId(userId int64, useCache bool) *MerchantUserR
 
 	merchantUserRelation := new(MerchantUserRelation)
 	if err := orm.NewOrm().QueryTable(merchantUserRelation).Filter("user_id", userId).Filter("is_effect", 1).Limit(1).One(merchantUserRelation); err != nil {
-		beego.BeeLogger.Info("GetMerchantUserRelationByUserId(%d) err=[%s]", userId, err)
+		if err != orm.ErrNoRows {
+			beego.BeeLogger.Warning("GetMerchantUserRelationByUserId(%d) err=[%s]", userId, err)
+		}
 		return nil
 	}
 
@@ -47,7 +49,7 @@ func GetMerchantUserRelationByUserId(userId int64, useCache bool) *MerchantUserR
 	return merchantUserRelation
 }
 
-//根据商家查询记录
+// 根据商家查询记录
 func GetMerchantUserRelationListByMerchantId(merchantId int64, useCache bool) (list []*MerchantUserRelation) {
 
 	k := fmt.Sprintf("merchant_model.GetMerchantUserRelationListByMerchantId(%d)", merchantId)

+ 12 - 4
go/gopath/src/fohow.com/apps/models/user_model/user.go

@@ -37,8 +37,8 @@ type User struct {
 	Province    string    `orm:"column(province);null"                          json:"-"`        // varchar(20)
 	City        string    `orm:"column(city);null"                              json:"-"`        // varchar(20)
 	Sex         int64     `orm:"column(sex);null"                               json:"sex"`      // int(11)
-	Race        string    `orm:"column(race);null"                              json:"race"`
-	Occupation  string    `orm:"column(occupation);null"                        json:"occupation"`
+	Race        string    `orm:"-"                                              json:"race"`
+	Occupation  string    `orm:"-"                                              json:"occupation"`
 	Head        string    `orm:"column(head)"     json:"head"`
 	Birthday    time.Time `orm:"column(birthday);null;type(datetime)"          json:"birthday"`
 	InviteId    int64     `orm:"column(invite_id);null"                         json:"invite_id"` // int(11)
@@ -149,8 +149,16 @@ func GetUserById(id int64, useCache bool) *User {
 	o := orm.NewOrm()
 	err := o.QueryTable(usr).Filter("id", id).Limit(1).One(usr)
 	if err != nil {
-		beego.Debug("GetByUid is not found err=[%s], id=%d", err, id)
-		return nil
+		// Fall back to raw SQL when ORM metadata/query building drifts from the runtime schema.
+		rawUser := new(User)
+		rawErr := o.Raw("SELECT * FROM users WHERE id = ? LIMIT 1", id).QueryRow(rawUser)
+		if rawErr != nil {
+			beego.BeeLogger.Error("GetByUid failed, id=%d, query_err=[%s], raw_err=[%s]", id, err, rawErr)
+			return nil
+		}
+		cache.Cache.Put(k, rawUser, 5*time.Minute)
+		beego.BeeLogger.Warn("GetByUid recovered via raw SQL, id=%d, query_err=[%s]", id, err)
+		return rawUser
 	} else {
 		cache.Cache.Put(k, usr, 5*time.Minute)
 		return usr