[ ['name' => 'code', 'title' => 'code', 'require' => true, 'type' => 'string'], ], 'create_qrcode' => [ ['name' => 'scene', 'title' => 'scene', 'require' => true, 'type' => 'string'], ['name' => 'page', 'title' => 'page', 'require' => true, 'type' => 'string'], ], 'down_qrcode' => [ ['name' => 'scene', 'title' => 'scene', 'require' => true, 'type' => 'string'], ['name' => 'page', 'title' => 'page', 'require' => true, 'type' => 'string'], ], ]; return $list; } public function getselfwxconfig() { $config = [ 'appid' => 'wx8788dcdf3d075963', //'appid' => 'wx8240b08d4e0b8954', 'appSecret' => '0d1928572155d59f0c66c30378e42bfb', //'appSecret' => 'df07e160bbb1235e7b737cbb7c87a0ed', ]; return $config; } private function curl_get($url, &$httpCode = 0) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不做证书校验,部署在linux环境下请改为true curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $file_contents = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $file_contents; } private function getAccessToken() { $config = $this->getselfwxconfig(); $appid = $config['appid']; $appSecret = $config['appSecret']; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appSecret; $result = $this->curl_get($url); $wxResult = json_decode($result, true); return $wxResult; } //开启curl post请求 private function sendCmd($url, $data) { $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno' . curl_error($curl); } curl_close($curl); // 关键CURL会话 return $tmpInfo; // 返回数据 } /** * 获取openid * 20220118 * wj * code 必填 */ public function getOpenid($arr) { $result = $this->checkparam(__FUNCTION__, $arr); if (1 != $result['status']) { return $result; } $code = $arr['code']; $config = $this->getselfwxconfig(); $appid = $config['appid']; $appSecret = $config['appSecret']; $wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code'; $getUrl = sprintf($wxUrl, $appid, $appSecret, $code); $result = $this->curl_get($getUrl); $wxResult = json_decode($result, true); if (empty($wxResult)) { return backarr(0, "错误"); } else { $logfail = array_key_exists('errcode', $wxResult); if ($logfail) { return backarr(1, 'success', $logfail['errmsg']); trace(json_encode($wxResult)); } else { $openid = $wxResult['openid']; if (!isset($wxResult['unionid'])) { trace(json_encode($wxResult)); } $data = ['openid' => $openid]; if (isset($wxResult['unionid'])) { $unionid = $wxResult['unionid']; $data['unionid'] = $unionid; $t_m = new usermodel(); $urec = $t_m->getinfobyopenid($openid); if ($urec) { if (!$urec['union_id']) { $t_m->updateunionidbyopenid($openid, $unionid); } } } return backarr(1, 'success', $data); } } } //获得二维码 public function create_qrcode($arr) { $result = $this->checkparam(__FUNCTION__, $arr); if (1 != $result['status']) { return $result; } $sharepage = request()->param(); $qr_path = "./Uploads/"; if (!file_exists($qr_path . 'user/')) { mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录 } $filename = 'user/' . time() . '.png'; $file = $qr_path . $filename; $access = $this->getAccessToken(); $access_token = $access['access_token']; $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token; $qrcode = array( 'scene' => $arr['scene'], //二维码所带参数 'width' => 200, 'page' => $arr['page'], //二维码跳转路径(要已发布小程序) 'auto_color' => true, ); $result = $this->sendCmd($url, json_encode($qrcode)); //请求微信接口 $errcode = json_decode($result, true)['errcode']; $errmsg = json_decode($result, true)['errmsg']; if ($errcode) { trace(json_encode($result)); return backarr(0, $errmsg); } $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件 if ($res === false) { echo '生成二维码失败'; } else { $path = getselfurl('default') . "/Uploads/" . $filename; return backarr(1, 'success', ['path' => $path]); } } //网上代骊,待修改 public function WxDecode() { // 接收参数 $data = request()->param(); $config = $this->getselfwxconfig(); $appid = $config['appid']; $appSecret = $config['appSecret']; $grant_type = "authorization_code"; //授权(必填) $code = $data['code']; //有效期5分钟 登录会话 $encryptedData = $data['encryptedData']; $iv = $data['iv']; // 拼接url $url = "https://api.weixin.qq.com/sns/jscode2session?" . "appid=" . $appid . "&secret=" . $appsecret . "&js_code=" . $code . "&grant_type=" . $grant_type; $res = json_decode($this->curl_get($url), true); $sessionKey = $res['session_key']; //取出json里对应的值 $pc = new wxBizDataCrypt($appid, $sessionKey); $errCode = $pc->decryptData($encryptedData, $iv, $data); //这里是按二手设备做的,以后应该单独变成逻辑,由逻辑层完成,以应对不同的需求 if ($errCode == 0) { $result['code'] = 200; $resultData['msg'] = '解密成功'; $resultData['userInfo'] = json_decode($data); $result['resultData'] = $resultData; echo json_encode($result); } else { return json($errCode); } } //获得二维码 /** * 保存并下载二维码 * 20220118 * wj * scene page 必填 */ public function download_qrcode($arr) { $result = $this->checkparam(__FUNCTION__, $arr); if (1 != $result['status']) { return $result; } $qr_path = "./Uploads/"; if (!file_exists($qr_path . 'user/')) { mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录 } $filename = 'user/' . time() . '.png'; $file = $qr_path . $filename; $access = $this->getAccessToken(); //json_decode($this->getAccessToken(),true); $access_token = $access['access_token']; $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token; $qrcode = array( 'scene' => $arr['scene'], //二维码所带参数 'width' => 200, 'page' => $arr['page'], //二维码跳转路径(要已发布小程序) 'auto_color' => true, ); $result = $this->sendCmd($url, json_encode($qrcode)); //请求微信接口 $errcode = json_decode($result, true)['errcode']; $errmsg = json_decode($result, true)['errmsg']; if ($errcode) { trace(json_encode($result)); return backarr(0, $errmsg); } $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件 $strfile = 'Uploads/' . $filename; $file1 = fopen($strfile, 'r'); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Disposition:inline;filename="' . $strfile . '"'); ob_clean(); flush(); echo fread($file1, filesize($file)); fclose($file1); if ($res === false) { echo '生成二维码失败'; } else { $path = getselfurl('default') . "/Uploads/" . $filename; return backarr(1, 'success', ['path' => $path]); } } }