123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- namespace app\index\controller;
- use app\index\logic\wxBizDataCrypt;
- use app\index\model\memodel;
- use think\Log;
- class Index
- {
- public function index()
- {
- return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">这是8116labourunion</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
- }
- public function getOpenid()
- {
- $userinfo = request()->param();
- $code = $userinfo['code'];
- $appid = 'wxcacf6eb6e7478e29';
- $appSecret = '3722c3f95df0569498a0e32ae6e85153';
- $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);
- // echo $getUrl;
- $result = $this->curl_get($getUrl);
- // echo $result;
- $wxResult = json_decode($result, true);
- if (empty($wxResult)) {
- $res_r['code'] = 200;
- $res_r['errmsg'] = '错误';
- return json($res_r);
- } else {
- $logfail = array_key_exists('errcode', $wxResult);
- if ($logfail) {
- var_dump($wxResult);
- Log::write(json_encode($wxResult));
- } else {
- $openid = $wxResult['openid'];
- // $unionid=null;
- $unionid = '';
- if (array_key_exists('unionid', $wxResult)) {
- $unionid = $wxResult['unionid'];
- } else {
- Log::write(json_encode($wxResult));
- }
- $res_r['code'] = 200;
- $res_r['resultData']['openId'] = $openid;
- $res_r['resultData']['openid'] = $openid;
- if (array_key_exists('unionid', $wxResult)) {
- $res_r['resultData']['unionid'] = $unionid;
- $t_m = new memodel();
- $urec = $t_m->getinfobyopenid($openid);
- if ($urec) {
- if (!$urec['union_id']) {
- $t_m->updunionidbyopenid($openid, $unionid);
- }
- }
- }
- return json($res_r);
- // return json($wxResult);
- }
- }
- }
- public 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;
- }
- public function getAccessToken()
- {
- $appid = 'wxcacf6eb6e7478e29';
- $appSecret = '3722c3f95df0569498a0e32ae6e85153';
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appSecret;
- //echo $url;
- $result = $this->curl_get($url);
- //echo $result;
- $wxResult = json_decode($result, true);
- //echo $wxResult['access_token'];
- return $wxResult;
- }
- public function getAccessTokenWCGZH()
- {
- $appid = 'wxc77d58456db8082b'; //服务号
- $appSecret = '456c9c2a328b3bee5001f2326d862916';
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appSecret;
- //echo $url;
- $result = $this->curl_get($url);
- //echo $result;
- $wxResult = json_decode($result, true);
- //echo $wxResult['access_token'];
- return $wxResult;
- }
- //获得二维码
- public function create_qrcode()
- {
- $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(); //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' => $sharepage['scene'], //二维码所带参数
- 'width' => 200,
- 'page' => $sharepage['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) {
- echo $errcode;
- echo $errmsg;
- //$this->render(0,$errmsg);
- }
- $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
- if ($res === false) {
- echo '生成二维码失败';
- // $this->render(0,'生成二维码失败');
- } else {
- $result_qrcode['resultData'] = '127.0.0.1:8094/Uploads/' . $filename;
- $result_qrcode['code'] = '200';
- return json_encode($result_qrcode);
- //$this->return['data'] = $this->config->item('base_url').'/Uploads/'.$filename;//返回图片地址链接给前端
- }
- }
- //开启curl post请求
- public 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; // 返回数据
- }
- public function getticket()
- {
- $access = $this->getAccessTokenWCGZH();
- $access_token = $access['access_token'];
- $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=' . $access_token;
- $result = $this->sendCmd($url, []);
- $result_arr = json_decode($result, true);
- if ('ok' != $result_arr['errmsg']) {
- echo 'error:' . $result_arr['errmsg'];
- exit;
- }
- $ticket = $result_arr['ticket'];
- return $ticket;
- }
- //网上代骊,待修改
- public function WxDecode()
- {
- // 接收参数
- $data = request()->param();
- // 引入解密文件 在微信小程序开发文档下载
- // vendor('wx.WXBizDataCrypt');
- //vendor('wx.ErrorCode');
- //
- // $appid = config('APPID');
- // $appsecret = config('APPSECREET');
- $appid = 'wxcacf6eb6e7478e29';
- $appsecret = '3722c3f95df0569498a0e32ae6e85153';
- $grant_type = "authorization_code"; //授权(必填)
- $code = $data['code']; //有效期5分钟 登录会话
- $encryptedData = $data['encryptedData'];
- $iv = $data['iv'];
- // $signature = $data['signature'];
- // $rawData = $data['rawData'];
- // 拼接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);
- // var_dump($res);
- $sessionKey = $res['session_key']; //取出json里对应的值
- // $signature2 = sha1(htmlspecialchars_decode($rawData).$sessionKey);
- // // 验证签名
- // if ($signature2 !== $signature){
- // return json("验签失败");
- // }
- $pc = new wxBizDataCrypt($appid, $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $data);
- // var_dump($data);
- //这里是按二手设备做的,以后应该单独变成逻辑,由逻辑层完成,以应对不同的需求
- if ($errCode == 0) {
- $result['code'] = 200;
- $resultData['msg'] = '解密成功';
- $resultData['userInfo'] = json_decode($data);
- $result['resultData'] = $resultData;
- echo json_encode($result);
- // exit(json_encode($data,JSON_UNESCAPED_UNICODE));
- } else {
- return json($errCode);
- }
- }
- //获得二维码
- public function download_qrcode()
- {
- $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(); //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' => $sharepage['scene'], //二维码所带参数
- 'width' => 200,
- 'page' => $sharepage['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) {
- echo $errcode;
- echo $errmsg;
- //$this->render(0,$errmsg);
- }
- $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
- $strfile = 'Uploads/' . $filename;
- //echo $strfile;
- $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);
- // $result->save('php://output');
- if ($res === false) {
- echo '生成二维码失败';
- // $this->render(0,'生成二维码失败');
- } else {
- $result_qrcode['resultData'] = 'https://app.gasgrid.cn::21443/Uploads/' . $filename;
- $result_qrcode['code'] = '200';
- return json_encode($result_qrcode);
- //$this->return['data'] = $this->config->item('base_url').'/Uploads/'.$filename;//返回图片地址链接给前端
- }
- }
- public function getsharedata()
- {
- $appid = 'wxc77d58456db8082b'; //服务号
- $ticket = $this->getticket();
- $timestamp = time();
- $nonceStr = getRandomStrings();
- $data = ['ticket' => $ticket, 'timestamp' => $timestamp, 'nonceStr' => $nonceStr, 'appid' => $appid];
- return $data;
- }
- }
|