后台代码:php
实现功能
1.从后台获取小程序码
2.微信扫码进入小程序并获取对应参数
对应代码
1.从后台获取小程序码
//获取小程序码
public function get_share_code(){
$img=$user=$goods=array();$res='';
$user=db('user')->where('id',input('uid'))->find();
$goods=db('goods')->where('id',input('gid'))->find();
$img=db('my_share_goods')->where('uid='.input('uid').' and gid='.input('gid'))->find();//halt($img);
//判断是否获取过,没获取过前往获取
if (!$img) {
$res=$this->xcxcode(input('gid'),input('uid'),input('fid'));
if ($res)
db('my_share_goods')->insert(['uid'=>input('uid'),'gid'=>input('gid'),'img'=>$res]);
else
die('获取小程序码出错,请检查接口后重试');
}else{
$res=$img['img'];
}
$user['img']='data:image/jpg;base64,'.fileToBase64($user['img']);//用户图像转base64,没特殊要求无需处理
return view('',['img'=>$img,'user'=>$user,'goods'=>$goods,'res'=>$res]);
}
//获取调用API接口的accesstoken
public function getaccesstoken(){
$path="./json/accesstoken.json";
$info=file_get_contents($path);//获取本地存储accesstoken,格式自定义,不想存本地直接跳过判断用else获取即可
$res=json_decode($info,true);
if ($res) {
$time=$res['time']+$res['expires_in'];
if ($time>time()) {
$access_token=$res['access_token'];
}else{
file_put_contents($path,'');
return $this->getaccesstoken();
}
}else{
$file=fopen($path, 'w');
$config=config('xcx.');
$appid = $config['appid'];
$secret = $config['secret'];
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$ret = curl_request($url,'GET');
$ret=json_decode($ret,true);
if (isset($ret['access_token']) && $ret['access_token']!='') {
$access_token=$ret['access_token'];
$data['access_token']=$ret['access_token'];
$data['expires_in']=$ret['expires_in'];
$data['time']=time();
fwrite($file,json_encode($data));
}
}
return $access_token;
}
//请求微信获取小程序码
public function xcxcode($id=3,$uid='',$fid='') {
$access_token = $this->getaccesstoken();
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
$data['scene'] = 'id=' . $id;//scene参数一定要拼接成字符串
if ($uid)
$data['scene'] .='&fid='.$uid;
if ($fid)
$data['scene'] .='&pid='.$fid;
$data['page'] = "pages/details/details";//扫码后跳转的小程序页面(在小程序中必须注册)
$data['is_hyaline']=true;
$data['width']=840;
$res = curl_request($url,'POST',$data);//post请求
$path = './uploads/share_qrcode/h_' . $id . '.jpg';//保存小程序码文件
@unlink($path);
$ret=file_put_contents($path, $res);//将微信返回二进制文本写入图片
db('goods')->where('id',$id)->update(['share_qrcode'=>mb_substr($path, 1)]);
return mb_substr($path, 1);
}
post请求微信接口方法
//请求第三方接口
function curl_request($url, $type = "GET", $data=[]){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_HEADER,0);
$type = strtolower($type);
switch ($type){
case 'get':
break;
case 'post':
//post请求配置
curl_setopt($ch, CURLOPT_POST,1);
if(!empty($data)){
$postdata=json_encode($data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json;charset=utf-8','Content-Length:'.strlen($postdata)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
break;
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
2.微信扫码进入小程序并获取对应参数
1.二维码测试。生成二维码后可在开发者工具测试。通过二维码编译,如下图
参数处理,使用decodeURIComponent()函数处理参数
2.也可直接自定义参数测试。需将参数通过encodeURIComponent函数处理
如:parm="a=1&b=2c=3" 页面初始化参数应为:scene=encodeURIComponent(parm)