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; }
小程序接收参数:
onLoad: function (options) {
var that = this;
console.log(options)
var id = options.device_id;
that.setData({
device_id:id
})
}