123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace app\index\controller;
- use app\index\logic\wxBizDataCrypt;
- 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">这是8095</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 testurl(){
- echo 'urlok';
- }
- public function getOpenid()
- {
- $code=$_GET['code'];
- $appid='wxa4a7e04c78524a73';
- $appSecret='375c071b8a8edfc14d53b269a0b734eb';
- $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)){
- echo '错误';
- }else{
- $logfail=array_key_exists('errcode',$wxResult);
- if ($logfail){
- var_dump($wxResult);
- }else{
- $openid=$wxResult['openid'];
- echo "openid:",$openid;
- // return json($wxResult);
- }
- }
- }
- 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='wxa4a7e04c78524a73';
- $appSecret='375c071b8a8edfc14d53b269a0b734eb';
- $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 WxDecode()
- {
- // 接收参数
- $data = request() -> param();
- // 引入解密文件 在微信小程序开发文档下载
- // vendor('wx.WXBizDataCrypt');
- //vendor('wx.ErrorCode');
- //
- // $appid = config('APPID');
- // $appsecret = config('APPSECREET');
- $appid='wxa4a7e04c78524a73';
- $appsecret='375c071b8a8edfc14d53b269a0b734eb';
- $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);
- }
- }
- }
|