拍卖小程序实战开发:生成微信二维码

有任何问题欢迎私信我。

一、官方HTTPS调用接口生成
(不推荐使用 前端无法将获得的图片二进制流文件转化为图片显示在页面且不安全)
1.请求地址
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
使用post请求方式 通过该接口生成的小程序码,永久有效,数量暂无限制 (该接口生成的二维码形状为菊花型二维码)
在这里插入图片描述
2.请求参数

	(1) access_token  必填
		GET请求接口获取接口调用凭证access_token
		https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
	(2)scene  必填  二维码携带的参数
		最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
	(3)Page  不必填 
		必须是已经发布的小程序存在的页面(如小程序未发布 page参数不为空则无法生成二维码),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
	(4)	width   auto_color   line_color   is_hyaline

在这里插入图片描述
3.返回值为图片二进制流文件 Buffer
4.获取扫描二维码进入的page页面携带scene参数

scene 字段的值会作为 query 参数传递给小程序/小游戏。用户扫描该码进入小程序/小游戏后,开发者可以获取到二维码中的 scene 值,再做处理逻辑。
调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 encodeURIComponent
onLoad (query) {
    // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
    const scene = decodeURIComponent(query.scene)
  }

二、云调用
1.接口方法
openapi.wxacode.getUnlimited

2.请求参数同HTTPS调用参数相比不需要access_token其他相同

3.示例

const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.wxacode.getUnlimited({
        scene: 'a=1'
      })
    console.log(result)
    return result
  } catch (err) {
    console.log(err)
    return err
  }}
返回参数: {
 "errcode": 0,
 "errmsg": "ok",
 "contentType": "image/jpeg",  数据类型 (MIME Type)
 "buffer": Buffer  数据 Buffer
}

三、后端开发接口调用 (推荐使用)
后端根据HTTPS调用接口转化获得的图片二进制流文件为图片格式
以下为PHP示例:

 public function Wx_share_code()
    {
        $data = $this->input->post();

        if (empty($data['uid']) ||empty($data['scene'])){
            die(json_encode(array('status'=>0,'msg'=>'缺少请求参数,请重试')));
        }
        //查询微信appid等信息
        $config = $this->wechat_config_model->get_all();

        $access_token = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$config[0]['app_id']}&secret={$config[0]['app_secret']}");
        $access_token = json_decode($access_token,true);
        if ($access_token['errcode']){
            die(json_encode(array('status'=>0,'msg'=>'获取access_token失败,请重试')));
        }

        $message = [
            'scene'=>$data['uid'].'k'.$data['scene'],                                       //	参数
            'page'=>$data['page']                                                           //跳转页面
        ];

        //转换成json
        $msg = json_encode($message);
        $options = [
            'http' => [
                'method'  => 'POST',
                'header'  => 'Content-type:application/json', // header 需要设置为 JSON
                'content' => $msg,
                'timeout' => 60 // 超时时间
            ]
        ];
        $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token['access_token'];
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        if (!$result){
            die(json_encode(array('status'=>0,'msg'=>'请重试')));
        }
      
        $time = 'C'.date('YmdHis',time()).mt_rand(1000,9999).'.png';
        $success = file_put_contents('./code/'.$time, $result);
        if (!$success){
            die(json_encode(array('status'=>0,'msg'=>'图片保存失败')));
        }
        $img = 'https://'.$_SERVER['SERVER_NAME'].'/api/code/'.$time;
        die(json_encode(array('status'=>1,'msg'=>'二维码生成成功','data'=>$img)));

四、注意事项
1.请求获取二维码接口时若page请求参数不为空则需保证小程序当前为已发布状态 否则接口请求不成功无法获取二维码
2.不推荐直接使用微信的HTTPS调用接口 因为前端无法转化接口返回的二进制流文件,需要后台进行转换且前端获取access_token参数不安全
3.页面内解析必须使用decodeURIComponent(scene) 才能获取到生成二维码时传入的scene参数

有任何问题欢迎私信我。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值