github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/qrcode/create.go (about)

     1  package qrcode
     2  
     3  import (
     4  	"github.com/chanxuehong/wechat/mp/core"
     5  )
     6  
     7  type TempQrcode struct {
     8  	ExpireSeconds int `json:"expire_seconds,omitempty"`
     9  	PermQrcode
    10  }
    11  
    12  type PermQrcode struct {
    13  	Ticket string `json:"ticket"`
    14  	URL    string `json:"url"`
    15  }
    16  
    17  // CreateTempQrcode 创建临时二维码.
    18  //
    19  //	sceneId:       场景值ID, 为32位非0整型
    20  //	expireSeconds: 二维码有效时间, 以秒为单位
    21  func CreateTempQrcode(clt *core.Client, sceneId int32, expireSeconds int) (qrcode *TempQrcode, err error) {
    22  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="
    23  
    24  	var request struct {
    25  		ExpireSeconds int    `json:"expire_seconds"`
    26  		ActionName    string `json:"action_name"`
    27  		ActionInfo    struct {
    28  			Scene struct {
    29  				SceneId int32 `json:"scene_id"`
    30  			} `json:"scene"`
    31  		} `json:"action_info"`
    32  	}
    33  	request.ExpireSeconds = expireSeconds
    34  	request.ActionName = "QR_SCENE"
    35  	request.ActionInfo.Scene.SceneId = sceneId
    36  
    37  	var result struct {
    38  		core.Error
    39  		TempQrcode
    40  	}
    41  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    42  		return
    43  	}
    44  	if result.ErrCode != core.ErrCodeOK {
    45  		err = &result.Error
    46  		return
    47  	}
    48  	qrcode = &result.TempQrcode
    49  	return
    50  }
    51  
    52  // CreateStrSceneTempQrcode 创建临时二维码.
    53  //
    54  //	sceneStr:      场景值ID(字符串形式的ID), 字符串类型, 长度限制为1到64
    55  //	expireSeconds: 二维码有效时间, 以秒为单位
    56  func CreateStrSceneTempQrcode(clt *core.Client, sceneStr string, expireSeconds int) (qrcode *TempQrcode, err error) {
    57  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="
    58  
    59  	var request struct {
    60  		ExpireSeconds int    `json:"expire_seconds"`
    61  		ActionName    string `json:"action_name"`
    62  		ActionInfo    struct {
    63  			Scene struct {
    64  				SceneStr string `json:"scene_str"`
    65  			} `json:"scene"`
    66  		} `json:"action_info"`
    67  	}
    68  	request.ExpireSeconds = expireSeconds
    69  	request.ActionName = "QR_STR_SCENE"
    70  	request.ActionInfo.Scene.SceneStr = sceneStr
    71  
    72  	var result struct {
    73  		core.Error
    74  		TempQrcode
    75  	}
    76  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    77  		return
    78  	}
    79  	if result.ErrCode != core.ErrCodeOK {
    80  		err = &result.Error
    81  		return
    82  	}
    83  	qrcode = &result.TempQrcode
    84  	return
    85  }
    86  
    87  // CreatePermQrcode 创建永久二维码
    88  //
    89  //	sceneId: 场景值ID
    90  func CreatePermQrcode(clt *core.Client, sceneId int32) (qrcode *PermQrcode, err error) {
    91  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="
    92  
    93  	var request struct {
    94  		ActionName string `json:"action_name"`
    95  		ActionInfo struct {
    96  			Scene struct {
    97  				SceneId int32 `json:"scene_id"`
    98  			} `json:"scene"`
    99  		} `json:"action_info"`
   100  	}
   101  	request.ActionName = "QR_LIMIT_SCENE"
   102  	request.ActionInfo.Scene.SceneId = sceneId
   103  
   104  	var result struct {
   105  		core.Error
   106  		PermQrcode
   107  	}
   108  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
   109  		return
   110  	}
   111  	if result.ErrCode != core.ErrCodeOK {
   112  		err = &result.Error
   113  		return
   114  	}
   115  	qrcode = &result.PermQrcode
   116  	return
   117  }
   118  
   119  // CreateStrScenePermQrcode 创建永久二维码
   120  //
   121  //	sceneStr: 场景值ID(字符串形式的ID), 字符串类型, 长度限制为1到64
   122  func CreateStrScenePermQrcode(clt *core.Client, sceneStr string) (qrcode *PermQrcode, err error) {
   123  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="
   124  
   125  	var request struct {
   126  		ActionName string `json:"action_name"`
   127  		ActionInfo struct {
   128  			Scene struct {
   129  				SceneStr string `json:"scene_str"`
   130  			} `json:"scene"`
   131  		} `json:"action_info"`
   132  	}
   133  	request.ActionName = "QR_LIMIT_STR_SCENE"
   134  	request.ActionInfo.Scene.SceneStr = sceneStr
   135  
   136  	var result struct {
   137  		core.Error
   138  		PermQrcode
   139  	}
   140  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
   141  		return
   142  	}
   143  	if result.ErrCode != core.ErrCodeOK {
   144  		err = &result.Error
   145  		return
   146  	}
   147  	qrcode = &result.PermQrcode
   148  	return
   149  }