You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 line
3.6KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. )
  7. //QRLimitScene 生成永久二维码的信息
  8. type QRLimitScene struct {
  9. Name string `json:"action_name"`
  10. Info struct {
  11. Scene struct {
  12. ID int32 `json:"scene_id"`
  13. IDstr string `json:"scene_str"` //对于永久二维码,我们使用 字符串, 用来区分 数字为主的 临时二维码
  14. } `json:"scene"`
  15. } `json:"action_info"`
  16. }
  17. //QRTmpScene 生成临时二维码的信息
  18. type QRTmpScene struct { //only valid for 30 days
  19. Name string `json:"action_name"` // 'QR_SCENE'
  20. Expire int32 `json:"expire_seconds"` //max 2592000 = 30 days
  21. Info struct {
  22. Scene struct {
  23. ID int32 `json:"scene_id"`
  24. } `json:"scene"`
  25. } `json:"action_info"`
  26. }
  27. //QRSrcInfo QrCode Information
  28. type QRSrcInfo struct {
  29. Ticket string `json:"ticket"`
  30. URL string `json:"url"`
  31. Expire int64 `json:"expire_seconds"`
  32. }
  33. //CreateProfileEditQR give user a temp password to login
  34. //for editing one's own profile
  35. func CreateProfileEditQR() (result QRSrcInfo, err error) {
  36. // http请求方式: POST
  37. // URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json
  38. // POST数据例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}
  39. // 或者也可以使用以下POST数据创建字符串形式的二维码参数:
  40. // {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "123"}}}
  41. u := getURL4CreateQR()
  42. s := QRLimitScene{}
  43. //s.Name = "QR_LIMIT_SCENE" //use integer
  44. s.Name = "QR_LIMIT_STR_SCENE" //use string
  45. s.Info.Scene.IDstr = "edit_profile"
  46. j, _ := json.Marshal(s)
  47. log.Println(string(j))
  48. resp, err := postJSON(j, u)
  49. if err != nil {
  50. return
  51. } //edit_profile gQFR8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZnNRMVJmZ3VjU2gxMDAwME0wN28AAgTQaSVZAwQAAAAA
  52. //{"ticket":"gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/02K_TnQ9gucSh10000M03N"}
  53. //image: https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA
  54. err = json.Unmarshal([]byte(resp), &result)
  55. return
  56. }
  57. func getURL4CreateQR() (URL string) {
  58. atk, _ := GetAccessToken()
  59. u := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s", atk)
  60. log.Println(u)
  61. return u
  62. }
  63. //CreateTempQr Create a temp QR code
  64. func CreateTempQr(sceneID int32, expire int32) (result QRSrcInfo, err error) {
  65. // http请求方式: POST
  66. // URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json
  67. // POST数据例子:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
  68. u := getURL4CreateQR()
  69. s := QRTmpScene{}
  70. s.Name = "QR_SCENE" //
  71. s.Info.Scene.ID = sceneID
  72. if expire <= 0 || expire >= 259200 {
  73. log.Println("invalid expire time for Temp QR code, set to 30days instead")
  74. s.Expire = 2592000
  75. } else {
  76. s.Expire = expire
  77. }
  78. j, _ := json.Marshal(s)
  79. log.Println(string(j))
  80. resp, err := postJSON(j, u)
  81. if err != nil {
  82. return
  83. }
  84. //{"ticket":"gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/02K_TnQ9gucSh10000M03N"}
  85. //image: https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA
  86. err = json.Unmarshal([]byte(resp), &result)
  87. return
  88. }
  89. func qrImgByTicket(ticket string) (url string) {
  90. return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket
  91. }