• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

生成带参数的二维码(扫码跳到小程序)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
public function httpRequest($url, $data=\'\', $method=\'GET\'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER[\'HTTP_USER_AGENT\']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method==\'POST\')
{
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != \'\')
{
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}

curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}

//生成base64图片
public function base64_image($base64_image_content,$img_path,$img_name){
$ds = DIRECTORY_SEPARATOR;
//匹配出图片的格式
if (preg_match(\'/^(data:\s*image\/(\w+);base64,)/\', $base64_image_content, $result)){
$type = $result[2];
if(!file_exists($img_path)){
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($img_path, 0777);
}
$new_file = $img_path."/".$img_name.".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], \'\', $base64_image_content)))){
//return $new_file;
return "http://mpapi.ymt.kairende.com".$ds."upload".$ds."device".$ds."mp".$ds.$img_name.".{$type}";;
}else{
return false;
}
}else{
return false;
}
}



public function getAcess(){

header(\'content-type:text/html;charset=utf-8\');
//配置APPID、APPSECRET
$APPID = "";
$APPSECRET = "";
//获取access_token
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";

//缓存access_token
session_start();
// $_SESSION[\'access_token\'] = "";
// $_SESSION[\'expires_in\'] = 0;
$expires_in = Yii::$app->session->get(\'expires_in\');
$ACCESS_TOKEN = "";
//if(!isset($_SESSION[\'access_token\']) || (isset($_SESSION[\'expires_in\']) && time() > $_SESSION[\'expires_in\']))
if(time() > $expires_in)
{
$json = $this->httpRequest( $access_token );
$json = json_decode($json,true);


Yii::$app->session->set(\'access_token\', $json[\'access_token\']);
Yii::$app->session->set(\'expires_in\', time()+7200);

$ACCESS_TOKEN = $json["access_token"];
}else{
//$ACCESS_TOKEN = $_SESSION["access_token"];
$ACCESS_TOKEN = Yii::$app->session->get(\'access_token\');
}

return $ACCESS_TOKEN;
}



//构建请求二维码参数
//path是扫描二维码跳转的小程序路径,可以带参数?id=xxx
//width是二维码宽度
public function actionRwm(){
$ACCESS_TOKEN = $this->getAcess();

 

//$ACCESS_TOKEN = "45_8Qwze6rRDB2uAxoitcpWMjc77Mfziyk8SCPsPCVdnnhEC2Er6MX-fqYa9KdRWnxMOnc7962JjtBgk3ea7l3QOw_ypTOIiNckT_aCBuOOUqDrb9OiydF-3NKay-5XJQJ0PeU8PObt9Sw-kWGzORNjAGAWZV";
$qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN";
$param = json_encode(array("path"=>"pages/buy/index?device_id=203615615","width"=> 150));

//POST参数
$result = $this->httpRequest( $qcode, $param,"POST");
//生成二维码

$base64_image ="data:image/jpeg;base64,".base64_encode( $result );

//echo $base64_image;

//echo "<img src=".$base64_image.">"; //在postman中可打印中图片
$ds = DIRECTORY_SEPARATOR;

$baseDir = dirname(Yii::$app->basePath);
$baseUploadDirName = \'mpapi\'.$ds.\'web\';
$uploadDir = \'upload\'.$ds.\'device\'.$ds.\'mp\';

$completeUploadDir = $baseDir.$ds.$baseUploadDirName.$ds.$uploadDir;
$device_id = "203615615";


$res = $this->base64_image($base64_image, $completeUploadDir, $device_id); //
//http://mpapi.ymt.kairende.com/upload/device/mp/203615615.jpeg
return $res;

}

 

小程序调起扫码  去扫上面生成的二维码并跳转,直接微信里扫也OK 

nav_jump:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    if(index == 0){
      //调用扫码功能
      wx.scanCode({
        success: (res) => {
          console.log(res)
          var path = res.path;  //打印出:pages/buy/index?device_id=203615615
          wx.redirectTo({
            // url: \'/pages/buy/index?id=\'+device_id
            url: \'/\'+path
          })   
        },
        fail: (res) => {
          wx.showToast({
            title: \'扫码失败\',
            icon: \'none\',
            duration: 2000
          })
        },
      })
    }else if(index == 1){
      wx.navigateTo({
        url: \'/pages/member/index\',
      })
    }else if(index == 2){
      wx.navigateTo({
        url: \'/pages/buycart/index\',
      })
    }else if(index == 3){
      wx.navigateTo({
        url: \'/pages/appointment/index\',
      })
    }
  },

 

小程序接收参数:

 onLoad: function (options) {
    var that = this;
    console.log(options)
    var id = options.device_id;
    that.setData({
        device_id:id
    })
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
微信小程序基础(一)发布时间:2022-07-18
下一篇:
Charles抓取微信小程序数据 以及 其它应用网站数据发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap