This is a creation in Article, where the information may have evolved or changed.
Today we finally solved the problem of invalid payment URL address mentioned earlier. It is very happy to write the QR code.
Two-dimensional code document writing simple and clear, but did not write why did actual display, I tried for a long time, even Base64 decoding are considered ... Finally found, or simple rough method for me.
The business scenario is as follows: Enter the page and display the user's QR code according to the user OpenID obtained.
I'm going to put the code in:
/** Display 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 () {...///here UID is OpenID wxuser: = Models. Wxuser{Wxid:uid} If ticket, err: = Getqrfromwx (&wxuser, &at)Err = = Nil {Here rudely directly spell out the user's QR code address, and then display it 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": {"scene_id":123}} ')) bytes. Newreader(jsonbytes)) If err! = Nil {FMT. Println("failed to send QR Setup request", err) logutils. GetLog(). Error("failed to send QR Setup request", 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 Build request", err) logutils. GetLog(). Error("client failed to send QR Build request", err) return ticker, err} else {//fmt. Printf("A successful request to send a QR----ready to read resp%+v\n", resp) body, err: = Ioutil. ReadAll(RESP. Body) If err! = Nil {FMT. Println("Send GET request get Wxuserinfo Read return body error", err) logutils. GetLog(). Error("Send GET request get Wxuserinfo Read return body error", err) return ticker, err} else {//fmt. Println("Parsing 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.