I am very happy that the problem of invalid payment url address mentioned before was finally solved today. Let’s write down the QR code as well.
The QR code document is simple and clear, but it does not include any actual display. I tried for a long time and even considered base64 decoding. . . In the end, I found that the simple and crude method suited me.
The business scenario is as follows: Enter the page and display the user’s QR code based on the obtained user’s openId.
Get out of the way, I’m going to put the code:
/**
WeChat displays user QR code
*/
type WXShowUserQrController struct {
beego.Controller
}
type WxQrGet struct {
ActionName string `json:"action_name"`
AInfo *WxQrActionInfo `json:"action_info"`
}
type WxQrActionInfo struct {
Sc *Scene `json:"scene"`
}
type Scene struct {
SceneId int `json:"scene_id"`
}
type QrBody struct {
Ticket string `json:"ticket"`
ExpireSeconds string `json:"expire_seconds"`
Url string `json: "url"`
}
func (c *WXShowUserQrController) Get() {
...
//The uid here is openID
wxUser := models.WxUser{WxId: uid}
if ticket, err := getQrFromWx(&wxUser , &at); err == nil {
//Here, the user's QR code address is spelled out directly and displayed on the page
c.Data["ticket"] = "https: //mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket
}
c.TplName = "wx_show_user_qr.html"
}
func getQrFromWx(wxUser *models.WxUser , at *models.WxAccessToken) (string, error) {
ticker := ""
scence := Scene{SceneId: wxUser.Id}
wxQrActionInfo := WxQrActionInfo{Sc: &scence}
wxQrGet := WxQrGet{ActionName: "QR_LIMIT_SCENE", AInfo: &wxQrActionInfo}
if jsonBytes, err := json.Marshal(wxQrGet); err == nil {
//fmt.Println("at.AccessToken---------", at. AccessToken)
//fmt.Println("jsonBytes--------- ", string(jsonBytes))
postReq, err := http.NewRequest("POST" ,
strings.Join([]string{`https://api.weixin.qq.com/cgi-bin/qrcode/create`, "?access_token=", at.AccessToken}, "") ,
//bytes.NewReader([]byte(`{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene" span>: {"scene_id": 123}}}`)))
bytes.NewReader(jsonBytes))
if err != nil {
fmt.Println ("Sending qr establishment request to WeChat failed", err)
logUtils.GetLog().Error("Failed to send qr establishment request to WeChat", err)
return ticker, err
}
postReq.Header.Set("Content-Type", "application/json; encoding=utf-8")
client := &http.Client{}
resp, err := client.Do(postReq)
if err != nil {
fmt.Println("Client failed to send qr establishment request to WeChat", err)
logUtils.GetLog().Error("client sends qr to WeChat Creation request failed", err)
return ticker, err
} else {
//fmt.Printf("Sending qr establishment request to WeChat successfully----ready to read resp%+v\n", resp)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Send a get request to obtain wxUserInfo and read the body error", err)
logUtils.GetLog().Error("Send a get request to obtain wxUserInfo and read the body error" , err)
return ticker, err
} else {
//fmt.Println("Parse body----->", string(body))
qrBody := new(QrBody)
if err := json.Unmarshal(body, qrBody); err == nil {
fmt. Printf("qrbody-----%+v\n", qrBody)
ticker = qrBody.Ticket
} else {
fmt.Println("json Conversion error", err)
logUtils.GetLog().Error("json conversion error", err)
return ticker, err
}
}
}
defer resp.Body.Close()
} else {
fmt.Println("json conversion error", err)
logUtils.GetLog ().Error("json conversion error", err)
return ticker , err
}
return ticker, nil
}
Get it done