在开发微信小程序时会,可能会需要做一个用户推广的功能,一种方式是直接分享小程序给好友或朋友圈,另一种方式就是生成专属推广的小程序二维码,那么如何实现呢?下面模板兔给出一个简单的示例代码供大家参考。
服务端代码:
$appid = get_option("erphpapp_appid");
$appsecret = get_option("erphpapp_appsecret");
$userid = $_POST['userid'];
$json = erphpapp_http("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret");
$json = json_decode($json,true);
$ACCESS_TOKEN = $json['access_token'];
$param = json_encode(array("page"=>"pages/index/index", "scene"=>$userid, "env_version"=>"develop"));
$result = erphpapp_http("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$ACCESS_TOKEN", $param, "POST");
echo $result;
小程序端代码:
wx.request({
url: app.globalData.appUrl,
data: {
userid: wx.getStorageSync('UserId')
},
method: 'POST',
responseType:'arraybuffer',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function(res){
wx.hideToast();
//console.log(res.data);
let bufferImg = "data:image/png;base64," + wx.arrayBufferToBase64(res.data);
that.setData({ qrcode: bufferImg });
},
fail: function(){
wx.showToast({
title: '获取失败',
icon: 'none',
duration: 3000
});
}
})


0 个评论