文件
mashangyoujiu/Application/SmallProgramApi/Controller/ShareController.class.php

217 行
7.1 KiB
PHP
可执行文件

<?php
namespace SmallProgramApi\Controller;
//[1.7.90]
/**
* 小程序分享
* Class ShareController
* @package SmallProgramApi\Controller
*/
class ShareController extends BaseController
{
/**
* 分享海报获取连接二维码
* @param string $path
* @return array
*/
public function shareQrCode()
{
// 接收参数
$path = I('post.path');
// 判断参数
if (empty($path)) {
$res = array(
'status' => 0,
'msg' => '参数缺失'
);
$this->ajaxReturn($res);
}
// 获取access_token
$access_token = get_authorizer_access_tokens(self::$merchant_id);
if (empty($access_token)) {
$res = array(
'status' => 0,
'msg' => 'access_token获取失败'
);
$this->ajaxReturn($res);
}
// 请求微信
$post_data = array(
'path' => $path,
'width' => 430
);
$url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" . $access_token;
$qr_code = curl_get($url, json_encode($post_data));
if (empty($qr_code)) {
$res = array(
'status' => 0,
'msg' => '二维码生成失败'
);
} else {
$res = array(
'status' => 1,
'qr_code' => base64_encode($qr_code),
'msg' => '二维码生成成功'
);
}
$this->ajaxReturn($res);
}
public function shareQrCode_v1()
{
// 接收参数
$path = I('post.path');
$image_url = I('post.image_url');//商品图片(非必传)
$avatar_url = I('post.avatar_url');//头像(非必传)
// 判断参数
if (empty($path)) {
$res = array(
'status' => 0,
'msg' => '参数缺失'
);
$this->ajaxReturn($res);
}
// 获取access_token
$access_token = get_authorizer_access_tokens(self::$merchant_id);
if (empty($access_token)) {
$res = array(
'status' => 0,
'msg' => 'access_token获取失败'
);
$this->ajaxReturn($res);
}
// 请求微信
$post_data = array(
'path' => $path,
'width' => 430
);
$url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" . $access_token;
$qr_code = curl_get($url, json_encode($post_data));
if (!empty($qr_code)) {
$res = array(
'status' => 1,
'qr_code' => base64_encode($qr_code),
'msg' => '二维码生成成功'
);
if($image_url){
//商品图片转base64
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$file = curl_exec($ch);
$file_content = chunk_split(base64_encode($file));//base64编码
if($file_content){
$res['goods_img'] = $file_content;
}
}
if($avatar_url){
//头像转base64
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $avatar_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$avatar_url = curl_exec($ch);
$avatar_url_content = chunk_split(base64_encode($avatar_url));//base64编码
if($avatar_url_content){
$res['avatar_url'] = $avatar_url_content;
}
}
} else {
$res = array(
'status' => 0,
'msg' => '二维码生成失败'
);
}
$this->ajaxReturn($res);
}
/**
* 小程序二维码生成大量5000/每份
*/
public function shareQrCode_v2()
{
// 接收参数
$path = I('post.path');
$image_url = I('post.image_url');//商品图片(非必传)
$avatar_url = I('post.avatar_url');//商品图片(非必传)
// 判断参数
if (empty($path)) {
$res = array(
'status' => 0,
'msg' => '参数缺失'
);
$this->ajaxReturn($res);
}
// 获取access_token
$access_token = get_authorizer_access_tokens(self::$merchant_id);
if (empty($access_token)) {
$res = array(
'status' => 0,
'msg' => 'access_token获取失败'
);
$this->ajaxReturn($res);
}
// 请求微信
$scene = I('post.goods_id').'&'.I('post.store_id').'&'.I('post.share_guide_id');
$post_data = array(
'scene' => $scene,
'page' => substr($path, 1),
'width' => 430
);
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
$qr_code = curl_get($url, json_encode($post_data));
if (!empty($qr_code)) {
$res = array(
'status' => 1,
'qr_code' => base64_encode($qr_code),
'msg' => '二维码生成成功'
);
if($image_url){
//商品图片转base64
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$file = curl_exec($ch);
$file_content = chunk_split(base64_encode($file));//base64编码
if($file_content){
$res['goods_img'] = $file_content;
}
}
if($avatar_url){
//头像转base64
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $avatar_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$avatar_url = curl_exec($ch);
$avatar_url_content = chunk_split(base64_encode($avatar_url));//base64编码
if($avatar_url_content){
$res['avatar_url'] = $avatar_url_content;
}
}
} else {
$res = array(
'status' => 0,
'msg' => '二维码生成失败'
);
}
$this->ajaxReturn($res);
}
}