首先到微信小程序平台配置服务器信息
小程序必须是验证的,而且已经发布审核过的页面路径

创建Wxcode类
生成小程序码业务类
- declare (strict_types = 1);
-
- namespace app\controller\Index;
-
-
- class Wxcode
- {
- public function getAccesstoken(){
-
- $appid = ''; /*字符串小程序appid*/
- $srcret = ''; /*字符串小程序秘钥*/
- $tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$srcret;
- $getArr=array();
- $tokenArr=json_decode($this->send_post($tokenUrl,$getArr,"GET"));
- $access_token=$tokenArr->access_token;
- return $access_token;
- }
-
- public function send_post($url, $post_data,$method='POST') {
-
- $postdata = http_build_query($post_data);
- $options = array(
- 'http' => array(
- 'method' => $method, //or GET
- 'header' => 'Content-type:application/x-www-form-urlencoded',
- 'content' => $postdata,
- 'timeout' => 15 * 60 // 超时时间(单位:s)
- )
- );
- $context = stream_context_create($options);
- $result = file_get_contents($url, false, $context);
- return $result;
- }
- public function api_notice_increment($url, $data){
- $ch = curl_init();
- $header=array('Accept-Language:zh-CN','x-appkey:114816004000028','x-apsignature:933931F9124593865313864503D477035C0F6A0C551804320036A2A1C5DF38297C9A4D30BB1714EC53214BD92112FB31B4A6FAB466EEF245710CC83D840D410A7592D262B09D0A5D0FE3A2295A81F32D4C75EBD65FA846004A42248B096EDE2FEE84EDEBEBEC321C237D99483AB51235FCB900AD501C07A9CAD2F415C36DED82','x-apversion:1.0','Content-Type:application/x-www-form-urlencoded','Accept-Charset: utf-8','Accept:application/json','X-APFormat:json');
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $tmpInfo = curl_exec($ch);
- if (curl_errno($ch)) {
- return false;
- }else{
- return $tmpInfo;
- }
- }
- /*上面生成的是数量限制10万的二维码,下面重写数量不限制的码*/
- /*getWXACodeUnlimit*/
- public function mpcode($page,$cardid){
- //传过来的参数
- $postdata['scene']=$cardid;
- // 宽度
- $postdata['width']=430;
- // 传过来的页面
- $postdata['page']=$page;
- // 线条颜色
- $postdata['auto_color']=false;
- //auto_color 为 false 时生效
- $postdata['line_color']=['r'=>'0','g'=>'0','b'=>'0'];
- // 是否有底色为true时是透明的
- $postdata['is_hyaline']=true;
- $post_data = json_encode($postdata);
- $access_token=$this->getAccesstoken();
- $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
- $result=$this->api_notice_increment($url,$post_data);
- $data='data:image/jpg;base64,'.base64_encode($result);
-
- //需要上传文件图片的
- //$data='image/png;base64,'.base64_encode($result);
- return $data;
- }
- }
调用的方法
- declare (strict_types = 1);
-
- namespace app\controller\Index;
-
- use app\BaseController;
-
-
- class Order_Details_Controller extends BaseController
- {
-
- // 生成小程码
- public function hexiao_number()
- {
-
- $wxcode = new Wxcode();
- $page='pages/index/index'; //页面地址
- $cardid= '123-abc'; //用-不用&区分,因为二次刷新&会转换%27,容易出错 注意:微信官方规定参数长度不能超过32
- $mpcodeimg=$wxcode->mpcode($page,$cardid);
- if (!$mpcodeimg) {
- return json(['code' => 201, 'msg' => '图片生成失败']);
- }else {
- return json(['code' => 200, 'imginfo' => $mpcodeimg, 'msg' => '图片生成成功']);
- }
-
- // 上传文件(需要上传图片的 mpcode函数中'data:image/jpg;base64,'
- 改为 'image/png;base64,')
- // $dataret['mpcodeimg']=$mpcodeimg;
- // $imageName = "25220_".date("His",time())."_".rand(1111,9999).'.png';
- // if (strstr($dataret['mpcodeimg'],",")){
- // $image = explode(',',$dataret['mpcodeimg']);
- // $image = $image[1];
- // }
- // $path = "uploads/".date("Ymd",time());
- // if (!is_dir($path)){ //判断目录是否存在 不存在就创建
- // mkdir($path,0777,true);
- // }
- // $imageSrc= $path."/". $imageName; //图片名字
- // $r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
- // if (!$r) {
- // return json(['code' => 201, 'data' => '', 'msg' => '图片生成失败']);
- // }else {
- // $imginfo = 'https://***.***.***/' . $imageSrc; //自己的服务器域名/地址
- // return json(['code' => 200, 'imginfo' => $mpcodeimg, 'msg' => '图片生成成功']);
- // }
- }
- /* 图片没显示,就先下载图片看下报错了什么信息
- 40169 scene参数不能超过32
- 45009 调用分钟频率受限(目前5000次/分钟,会调整),如需大量小程序码,建议预生成。
- 41030 所传page页面不存在,或者小程序没有发布*/
- }
uniapp 获取参数
- onLoad(e) {
- let scene = decodeURIComponent(e.scene);
- //&是我们定义的参数链接方式
- let userId = scene.split("-")[0];
- let recommendId = scene.split('-')[1];
- console.log(userId)
- console.log(recommendId)
- }