Index.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace app\index\controller;
  3. use app\index\logic\wxBizDataCrypt;
  4. use app\index\model\memodel;
  5. use think\Log;
  6. class Index
  7. {
  8. public function index()
  9. {
  10. 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>';
  11. }
  12. public function getOpenid()
  13. {
  14. $userinfo = request()->param();
  15. $code = $userinfo['code'];
  16. $appid = 'wxcacf6eb6e7478e29';
  17. $appSecret = '3722c3f95df0569498a0e32ae6e85153';
  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. $res_r['code'] = 200;
  26. $res_r['errmsg'] = '错误';
  27. return json($res_r);
  28. } else {
  29. $logfail = array_key_exists('errcode', $wxResult);
  30. if ($logfail) {
  31. var_dump($wxResult);
  32. Log::write(json_encode($wxResult));
  33. } else {
  34. $openid = $wxResult['openid'];
  35. // $unionid=null;
  36. $unionid = '';
  37. if (array_key_exists('unionid', $wxResult)) {
  38. $unionid = $wxResult['unionid'];
  39. } else {
  40. Log::write(json_encode($wxResult));
  41. }
  42. $res_r['code'] = 200;
  43. $res_r['resultData']['openId'] = $openid;
  44. $res_r['resultData']['openid'] = $openid;
  45. if (array_key_exists('unionid', $wxResult)) {
  46. $res_r['resultData']['unionid'] = $unionid;
  47. $t_m = new memodel();
  48. $urec = $t_m->getinfobyopenid($openid);
  49. if ($urec) {
  50. if (!$urec['union_id']) {
  51. $t_m->updunionidbyopenid($openid, $unionid);
  52. }
  53. }
  54. }
  55. return json($res_r);
  56. // return json($wxResult);
  57. }
  58. }
  59. }
  60. public function curl_get($url, &$httpCode = 0)
  61. {
  62. $ch = curl_init();
  63. curl_setopt($ch, CURLOPT_URL, $url);
  64. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  65. //不做证书校验,部署在linux环境下请改为true
  66. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  67. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  68. $file_contents = curl_exec($ch);
  69. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  70. curl_close($ch);
  71. return $file_contents;
  72. }
  73. public function getAccessToken()
  74. {
  75. $appid = 'wxcacf6eb6e7478e29';
  76. $appSecret = '3722c3f95df0569498a0e32ae6e85153';
  77. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appSecret;
  78. //echo $url;
  79. $result = $this->curl_get($url);
  80. //echo $result;
  81. $wxResult = json_decode($result, true);
  82. //echo $wxResult['access_token'];
  83. return $wxResult;
  84. }
  85. public function getAccessTokenWCGZH()
  86. {
  87. $appid = 'wxc77d58456db8082b'; //服务号
  88. $appSecret = '456c9c2a328b3bee5001f2326d862916';
  89. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appSecret;
  90. //echo $url;
  91. $result = $this->curl_get($url);
  92. //echo $result;
  93. $wxResult = json_decode($result, true);
  94. //echo $wxResult['access_token'];
  95. return $wxResult;
  96. }
  97. //获得二维码
  98. public function create_qrcode()
  99. {
  100. $sharepage = request()->param();
  101. $qr_path = "./Uploads/";
  102. if (!file_exists($qr_path . 'user/')) {
  103. mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录
  104. }
  105. $filename = 'user/' . time() . '.png';
  106. $file = $qr_path . $filename;
  107. $access = $this->getAccessToken(); //json_decode($this->getAccessToken(),true);
  108. $access_token = $access['access_token'];
  109. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
  110. $qrcode = array(
  111. 'scene' => $sharepage['scene'], //二维码所带参数
  112. 'width' => 200,
  113. 'page' => $sharepage['page'], //二维码跳转路径(要已发布小程序)
  114. 'auto_color' => true,
  115. );
  116. $result = $this->sendCmd($url, json_encode($qrcode)); //请求微信接口
  117. $errcode = json_decode($result, true)['errcode'];
  118. $errmsg = json_decode($result, true)['errmsg'];
  119. if ($errcode) {
  120. echo $errcode;
  121. echo $errmsg;
  122. //$this->render(0,$errmsg);
  123. }
  124. $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
  125. if ($res === false) {
  126. echo '生成二维码失败';
  127. // $this->render(0,'生成二维码失败');
  128. } else {
  129. $result_qrcode['resultData'] = '127.0.0.1:8094/Uploads/' . $filename;
  130. $result_qrcode['code'] = '200';
  131. return json_encode($result_qrcode);
  132. //$this->return['data'] = $this->config->item('base_url').'/Uploads/'.$filename;//返回图片地址链接给前端
  133. }
  134. }
  135. //开启curl post请求
  136. public function sendCmd($url, $data)
  137. {
  138. $curl = curl_init(); // 启动一个CURL会话
  139. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  140. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
  141. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  142. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
  143. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  144. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  145. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  146. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  147. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
  148. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  149. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  150. $tmpInfo = curl_exec($curl); // 执行操作
  151. if (curl_errno($curl)) {
  152. echo 'Errno' . curl_error($curl);
  153. }
  154. curl_close($curl); // 关键CURL会话
  155. return $tmpInfo; // 返回数据
  156. }
  157. public function getticket()
  158. {
  159. /*$access = $this->getAccessTokenWCGZH();
  160. $access['access_token'] = "123";
  161. $access_token = $access['access_token'];
  162. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=' . $access_token;
  163. $result = $this->sendCmd($url, []);
  164. $result_arr = json_decode($result, true);
  165. if ('ok' != $result_arr['errmsg']) {
  166. echo 'error:' . $result_arr['errmsg'];
  167. exit;
  168. }
  169. $ticket = $result_arr['ticket'];*/
  170. $ticket = "123";
  171. return $ticket;
  172. }
  173. //网上代骊,待修改
  174. public function WxDecode()
  175. {
  176. // 接收参数
  177. $data = request()->param();
  178. // 引入解密文件 在微信小程序开发文档下载
  179. // vendor('wx.WXBizDataCrypt');
  180. //vendor('wx.ErrorCode');
  181. //
  182. // $appid = config('APPID');
  183. // $appsecret = config('APPSECREET');
  184. $appid = 'wxcacf6eb6e7478e29';
  185. $appsecret = '3722c3f95df0569498a0e32ae6e85153';
  186. $grant_type = "authorization_code"; //授权(必填)
  187. $code = $data['code']; //有效期5分钟 登录会话
  188. $encryptedData = $data['encryptedData'];
  189. $iv = $data['iv'];
  190. // $signature = $data['signature'];
  191. // $rawData = $data['rawData'];
  192. // 拼接url
  193. $url = "https://api.weixin.qq.com/sns/jscode2session?" . "appid=" . $appid . "&secret=" . $appsecret . "&js_code=" . $code . "&grant_type=" . $grant_type;
  194. $res = json_decode($this->curl_get($url), true);
  195. // var_dump($res);
  196. $sessionKey = $res['session_key']; //取出json里对应的值
  197. // $signature2 = sha1(htmlspecialchars_decode($rawData).$sessionKey);
  198. // // 验证签名
  199. // if ($signature2 !== $signature){
  200. // return json("验签失败");
  201. // }
  202. $pc = new wxBizDataCrypt($appid, $sessionKey);
  203. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  204. // var_dump($data);
  205. //这里是按二手设备做的,以后应该单独变成逻辑,由逻辑层完成,以应对不同的需求
  206. if ($errCode == 0) {
  207. $result['code'] = 200;
  208. $resultData['msg'] = '解密成功';
  209. $resultData['userInfo'] = json_decode($data);
  210. $result['resultData'] = $resultData;
  211. echo json_encode($result);
  212. // exit(json_encode($data,JSON_UNESCAPED_UNICODE));
  213. } else {
  214. return json($errCode);
  215. }
  216. }
  217. //获得二维码
  218. public function download_qrcode()
  219. {
  220. $sharepage = request()->param();
  221. $qr_path = "./Uploads/";
  222. if (!file_exists($qr_path . 'user/')) {
  223. mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录
  224. }
  225. $filename = 'user/' . time() . '.png';
  226. $file = $qr_path . $filename;
  227. $access = $this->getAccessToken(); //json_decode($this->getAccessToken(),true);
  228. $access_token = $access['access_token'];
  229. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
  230. $qrcode = array(
  231. 'scene' => $sharepage['scene'], //二维码所带参数
  232. 'width' => 200,
  233. 'page' => $sharepage['page'], //二维码跳转路径(要已发布小程序)
  234. 'auto_color' => true,
  235. );
  236. $result = $this->sendCmd($url, json_encode($qrcode)); //请求微信接口
  237. $errcode = json_decode($result, true)['errcode'];
  238. $errmsg = json_decode($result, true)['errmsg'];
  239. if ($errcode) {
  240. echo $errcode;
  241. echo $errmsg;
  242. //$this->render(0,$errmsg);
  243. }
  244. $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
  245. $strfile = 'Uploads/' . $filename;
  246. //echo $strfile;
  247. $file1 = fopen($strfile, 'r');
  248. header("Content-Type: application/force-download");
  249. header("Content-Type: application/octet-stream");
  250. header("Content-Type: application/download");
  251. header('Content-Disposition:inline;filename="' . $strfile . '"');
  252. ob_clean();
  253. flush();
  254. echo fread($file1, filesize($file));
  255. fclose($file1);
  256. // $result->save('php://output');
  257. if ($res === false) {
  258. echo '生成二维码失败';
  259. // $this->render(0,'生成二维码失败');
  260. } else {
  261. $result_qrcode['resultData'] = 'https://app.gasgrid.cn::21443/Uploads/' . $filename;
  262. $result_qrcode['code'] = '200';
  263. return json_encode($result_qrcode);
  264. //$this->return['data'] = $this->config->item('base_url').'/Uploads/'.$filename;//返回图片地址链接给前端
  265. }
  266. }
  267. public function getsharedata()
  268. {
  269. $appid = 'wxc77d58456db8082b'; //服务号
  270. $ticket = $this->getticket();
  271. $timestamp = time();
  272. $nonceStr = getRandomStrings();
  273. $data = ['ticket' => $ticket, 'timestamp' => $timestamp, 'nonceStr' => $nonceStr, 'appid' => $appid];
  274. return $data;
  275. }
  276. }