diff --git a/qrcode.go b/qrcode.go index 5031e3e..28a2cbb 100644 --- a/qrcode.go +++ b/qrcode.go @@ -6,39 +6,95 @@ import ( "log" ) +//QRLimitScene 生成永久二维码的信息 type QRLimitScene struct { Name string `json:"action_name"` Info struct { Scene struct { ID int32 `json:"scene_id"` - IDstr string `json:"scene_str"` + IDstr string `json:"scene_str"` //对于永久二维码,我们使用 字符串, 用来区分 数字为主的 临时二维码 } `json:"scene"` } `json:"action_info"` } -//CreateProfileEditQR, give user a temp password to login +//QRTmpScene 生成临时二维码的信息 +type QRTmpScene struct { //only valid for 30 days + Name string `json:"action_name"` // 'QR_SCENE' + Expire int32 `json:"expire_seconds"` //max 2592000 = 30 days + Info struct { + Scene struct { + ID int32 `json:"scene_id"` + } `json:"scene"` + } `json:"action_info"` +} + +//QRSrcInfo QrCode Information +type QRSrcInfo struct { + Ticket string `json:"ticket"` + URL string `json:"url"` + Expire int64 `json:"expire_seconds"` +} + +//CreateProfileEditQR give user a temp password to login //for editing one's own profile -func CreateProfileEditQR() { +func CreateProfileEditQR() (result QRSrcInfo, err error) { // http请求方式: POST // URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json // POST数据例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}} // 或者也可以使用以下POST数据创建字符串形式的二维码参数: // {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "123"}}} - u := getURL4QRPerm() + u := getURL4CreateQR() s := QRLimitScene{} - s.Name = "QR_LIMIT_SCENE" + //s.Name = "QR_LIMIT_SCENE" //use integer + s.Name = "QR_LIMIT_STR_SCENE" //use string s.Info.Scene.IDstr = "edit_profile" j, _ := json.Marshal(s) log.Println(string(j)) - postJSON(j, u) - + resp, err := postJSON(j, u) + if err != nil { + return + } //{"ticket":"gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/02K_TnQ9gucSh10000M03N"} //image: https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA + err = json.Unmarshal([]byte(resp), &result) + return } -func getURL4QRPerm() (URL string) { +func getURL4CreateQR() (URL string) { atk, _ := GetAccessToken() u := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s", atk) log.Println(u) return u } + +//CreateTempQr Create a temp QR code +func CreateTempQr(sceneID int32, expire int32) (result QRSrcInfo, err error) { + // http请求方式: POST + // URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json + // POST数据例子:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}} + u := getURL4CreateQR() + s := QRTmpScene{} + s.Name = "QR_SCENE" // + s.Info.Scene.ID = sceneID + if expire <= 0 || expire >= 259200 { + log.Println("invalid expire time for Temp QR code, set to 30days instead") + s.Expire = 2592000 + } else { + s.Expire = expire + } + + j, _ := json.Marshal(s) + log.Println(string(j)) + resp, err := postJSON(j, u) + if err != nil { + return + } + //{"ticket":"gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/02K_TnQ9gucSh10000M03N"} + //image: https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA + err = json.Unmarshal([]byte(resp), &result) + return +} + +func qrImgByTicket(ticket string) (url string) { + return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket +} diff --git a/qrcode_test.go b/qrcode_test.go index 7e835a5..3189cc1 100644 --- a/qrcode_test.go +++ b/qrcode_test.go @@ -1,8 +1,27 @@ package main -import "testing" +import ( + "fmt" + "log" + "testing" +) func TestCreatePermQR(t *testing.T) { SetupConfig() - CreateProfileEditQR() + r, e := CreateProfileEditQR() + AssertEqual(t, e, nil, "Create QR code should not have error") + log.Println("qr ticket = " + r.Ticket) + log.Println("qr URL = " + r.URL) + log.Println(fmt.Sprintf("qr expire = %d", r.Expire)) + +} + +func TestCreateTempQr(t *testing.T) { + SetupConfig() + r, e := CreateTempQr(000, 2592001) + AssertEqual(t, e, nil, "Create Temp QR should be success") + log.Println("qr ticket = " + r.Ticket) + log.Println("qr URL = " + r.URL) + log.Println(qrImgByTicket(r.Ticket)) + log.Println(fmt.Sprintf("qr expire = %d", r.Expire)) }