Index.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_token = $access['access_token'];
  161. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=' . $access_token;
  162. $result = $this->sendCmd($url, []);
  163. $result_arr = json_decode($result, true);
  164. if ('ok' != $result_arr['errmsg']) {
  165. echo 'error:' . $result_arr['errmsg'];
  166. exit;
  167. }
  168. $ticket = $result_arr['ticket'];
  169. return $ticket;
  170. }
  171. //网上代骊,待修改
  172. public function WxDecode()
  173. {
  174. // 接收参数
  175. $data = request()->param();
  176. // 引入解密文件 在微信小程序开发文档下载
  177. // vendor('wx.WXBizDataCrypt');
  178. //vendor('wx.ErrorCode');
  179. //
  180. // $appid = config('APPID');
  181. // $appsecret = config('APPSECREET');
  182. $appid = 'wxcacf6eb6e7478e29';
  183. $appsecret = '3722c3f95df0569498a0e32ae6e85153';
  184. $grant_type = "authorization_code"; //授权(必填)
  185. $code = $data['code']; //有效期5分钟 登录会话
  186. $encryptedData = $data['encryptedData'];
  187. $iv = $data['iv'];
  188. // $signature = $data['signature'];
  189. // $rawData = $data['rawData'];
  190. // 拼接url
  191. $url = "https://api.weixin.qq.com/sns/jscode2session?" . "appid=" . $appid . "&secret=" . $appsecret . "&js_code=" . $code . "&grant_type=" . $grant_type;
  192. $res = json_decode($this->curl_get($url), true);
  193. // var_dump($res);
  194. $sessionKey = $res['session_key']; //取出json里对应的值
  195. // $signature2 = sha1(htmlspecialchars_decode($rawData).$sessionKey);
  196. // // 验证签名
  197. // if ($signature2 !== $signature){
  198. // return json("验签失败");
  199. // }
  200. $pc = new wxBizDataCrypt($appid, $sessionKey);
  201. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  202. // var_dump($data);
  203. //这里是按二手设备做的,以后应该单独变成逻辑,由逻辑层完成,以应对不同的需求
  204. if ($errCode == 0) {
  205. $result['code'] = 200;
  206. $resultData['msg'] = '解密成功';
  207. $resultData['userInfo'] = json_decode($data);
  208. $result['resultData'] = $resultData;
  209. echo json_encode($result);
  210. // exit(json_encode($data,JSON_UNESCAPED_UNICODE));
  211. } else {
  212. return json($errCode);
  213. }
  214. }
  215. //获得二维码
  216. public function download_qrcode()
  217. {
  218. $sharepage = request()->param();
  219. $qr_path = "./Uploads/";
  220. if (!file_exists($qr_path . 'user/')) {
  221. mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录
  222. }
  223. $filename = 'user/' . time() . '.png';
  224. $file = $qr_path . $filename;
  225. $access = $this->getAccessToken(); //json_decode($this->getAccessToken(),true);
  226. $access_token = $access['access_token'];
  227. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
  228. $qrcode = array(
  229. 'scene' => $sharepage['scene'], //二维码所带参数
  230. 'width' => 200,
  231. 'page' => $sharepage['page'], //二维码跳转路径(要已发布小程序)
  232. 'auto_color' => true,
  233. );
  234. $result = $this->sendCmd($url, json_encode($qrcode)); //请求微信接口
  235. $errcode = json_decode($result, true)['errcode'];
  236. $errmsg = json_decode($result, true)['errmsg'];
  237. if ($errcode) {
  238. echo $errcode;
  239. echo $errmsg;
  240. //$this->render(0,$errmsg);
  241. }
  242. $res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
  243. $strfile = 'Uploads/' . $filename;
  244. //echo $strfile;
  245. $file1 = fopen($strfile, 'r');
  246. header("Content-Type: application/force-download");
  247. header("Content-Type: application/octet-stream");
  248. header("Content-Type: application/download");
  249. header('Content-Disposition:inline;filename="' . $strfile . '"');
  250. ob_clean();
  251. flush();
  252. echo fread($file1, filesize($file));
  253. fclose($file1);
  254. // $result->save('php://output');
  255. if ($res === false) {
  256. echo '生成二维码失败';
  257. // $this->render(0,'生成二维码失败');
  258. } else {
  259. $result_qrcode['resultData'] = 'https://app.gasgrid.cn::21443/Uploads/' . $filename;
  260. $result_qrcode['code'] = '200';
  261. return json_encode($result_qrcode);
  262. //$this->return['data'] = $this->config->item('base_url').'/Uploads/'.$filename;//返回图片地址链接给前端
  263. }
  264. }
  265. public function getsharedata()
  266. {
  267. $appid = 'wxc77d58456db8082b'; //服务号
  268. $ticket = $this->getticket();
  269. $timestamp = time();
  270. $nonceStr = getRandomStrings();
  271. $data = ['ticket' => $ticket, 'timestamp' => $timestamp, 'nonceStr' => $nonceStr, 'appid' => $appid];
  272. return $data;
  273. }
  274. }