Index.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\index\controller;
  3. use app\index\logic\wxBizDataCrypt;
  4. class Index
  5. {
  6. public function index()
  7. {
  8. 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>';
  9. }
  10. public function testurl(){
  11. echo 'urlok';
  12. }
  13. public function getOpenid()
  14. {
  15. $code=$_GET['code'];
  16. $appid='wxa4a7e04c78524a73';
  17. $appSecret='375c071b8a8edfc14d53b269a0b734eb';
  18. $wxUrl='https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
  19. $getUrl=sprintf($wxUrl,$appid,$appSecret,$code);
  20. // echo $getUrl;
  21. $result=$this->curl_get($getUrl);
  22. // echo $result;
  23. $wxResult=json_decode($result,true);
  24. if (empty($wxResult)){
  25. echo '错误';
  26. }else{
  27. $logfail=array_key_exists('errcode',$wxResult);
  28. if ($logfail){
  29. var_dump($wxResult);
  30. }else{
  31. $openid=$wxResult['openid'];
  32. echo "openid:",$openid;
  33. // return json($wxResult);
  34. }
  35. }
  36. }
  37. function curl_get($url, &$httpCode = 0) {
  38. $ch = curl_init();
  39. curl_setopt($ch, CURLOPT_URL, $url);
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  41. //不做证书校验,部署在linux环境下请改为true
  42. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  43. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  44. $file_contents = curl_exec($ch);
  45. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  46. curl_close($ch);
  47. return $file_contents;
  48. }
  49. public function getAccessToken(){
  50. $appid='wxa4a7e04c78524a73';
  51. $appSecret='375c071b8a8edfc14d53b269a0b734eb';
  52. $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appSecret;
  53. //echo $url;
  54. $result=$this->curl_get($url);
  55. //echo $result;
  56. $wxResult=json_decode($result,true);
  57. //echo $wxResult['access_token'];
  58. return $wxResult;
  59. }
  60. //获得二维码
  61. public function create_qrcode(){
  62. $sharepage = request() -> param();
  63. $qr_path = "./Uploads/";
  64. if(!file_exists($qr_path.'user/')){
  65. mkdir($qr_path.'user/', 0700,true);//判断保存目录是否存在,不存在自动生成文件目录
  66. }
  67. $filename = 'user/'.time().'.png';
  68. $file = $qr_path.$filename;
  69. $access =$this->getAccessToken(); //json_decode($this->getAccessToken(),true);
  70. $access_token= $access['access_token'];
  71. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
  72. $qrcode = array(
  73. 'scene' => $sharepage['scene'],//二维码所带参数
  74. 'width' => 200,
  75. 'page' => $sharepage['page'],//二维码跳转路径(要已发布小程序)
  76. 'auto_color' => true
  77. );
  78. $result = $this->sendCmd($url,json_encode($qrcode));//请求微信接口
  79. $errcode = json_decode($result,true)['errcode'];
  80. $errmsg = json_decode($result,true)['errmsg'];
  81. if($errcode) {
  82. echo $errcode;
  83. echo $errmsg;
  84. //$this->render(0,$errmsg);
  85. }
  86. $res = file_put_contents($file,$result);//将微信返回的图片数据流写入文件
  87. if($res===false){
  88. echo '生成二维码失败';
  89. // $this->render(0,'生成二维码失败');
  90. }else{
  91. $result_qrcode['resultData']='127.0.0.1:8094/Uploads/'.$filename;
  92. $result_qrcode['code']='200';
  93. return json_encode($result_qrcode);
  94. //$this->return['data'] = $this->config->item('base_url').'/Uploads/'.$filename;//返回图片地址链接给前端
  95. }
  96. }
  97. //开启curl post请求
  98. public function sendCmd($url,$data)
  99. {
  100. $curl = curl_init(); // 启动一个CURL会话
  101. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  102. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
  103. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  104. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
  105. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  106. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  107. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  108. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  109. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
  110. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  111. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  112. $tmpInfo = curl_exec($curl); // 执行操作
  113. if (curl_errno($curl)) {
  114. echo 'Errno'.curl_error($curl);
  115. }
  116. curl_close($curl); // 关键CURL会话
  117. return $tmpInfo; // 返回数据
  118. }
  119. //网上代骊,待修改
  120. public function WxDecode()
  121. {
  122. // 接收参数
  123. $data = request() -> param();
  124. // 引入解密文件 在微信小程序开发文档下载
  125. // vendor('wx.WXBizDataCrypt');
  126. //vendor('wx.ErrorCode');
  127. //
  128. // $appid = config('APPID');
  129. // $appsecret = config('APPSECREET');
  130. $appid='wxa4a7e04c78524a73';
  131. $appsecret='375c071b8a8edfc14d53b269a0b734eb';
  132. $grant_type = "authorization_code"; //授权(必填)
  133. $code = $data['code']; //有效期5分钟 登录会话
  134. $encryptedData=$data['encryptedData'];
  135. $iv = $data['iv'];
  136. // $signature = $data['signature'];
  137. // $rawData = $data['rawData'];
  138. // 拼接url
  139. $url = "https://api.weixin.qq.com/sns/jscode2session?"."appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=".$grant_type;
  140. $res = json_decode($this->curl_get($url),true);
  141. // var_dump($res);
  142. $sessionKey = $res['session_key']; //取出json里对应的值
  143. // $signature2 = sha1(htmlspecialchars_decode($rawData).$sessionKey);
  144. // // 验证签名
  145. // if ($signature2 !== $signature){
  146. // return json("验签失败");
  147. // }
  148. $pc = new wxBizDataCrypt($appid, $sessionKey);
  149. $errCode = $pc->decryptData($encryptedData, $iv, $data );
  150. // var_dump($data);
  151. //这里是按二手设备做的,以后应该单独变成逻辑,由逻辑层完成,以应对不同的需求
  152. if ($errCode == 0) {
  153. $result['code']=200;
  154. $resultData['msg']='解密成功';
  155. $resultData['userInfo']=json_decode($data);
  156. $result['resultData']=$resultData;
  157. echo json_encode($result);
  158. // exit(json_encode($data,JSON_UNESCAPED_UNICODE));
  159. } else {
  160. return json($errCode);
  161. }
  162. }
  163. }