Controller.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  4. use Illuminate\Foundation\Bus\DispatchesJobs;
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. use Illuminate\Routing\Controller as BaseController;
  7. use Excel;
  8. class Controller extends BaseController
  9. {
  10. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  11. /**
  12. * @Author woann <304550409@qq.com>
  13. * @param int $code
  14. * @param string $msg
  15. * @param array $data
  16. * @return mixed
  17. * @description 接口返回数据格式
  18. */
  19. protected function json($code = 200, $msg = '', $data = [])
  20. {
  21. if ($data == []) {
  22. $res = [
  23. 'code' => $code,
  24. 'msg' => $msg,
  25. ];
  26. } else {
  27. $res = [
  28. 'code' => $code,
  29. 'msg' => $msg,
  30. 'data' => $data,
  31. ];
  32. }
  33. return response()->json($res)->header('Content-Type', 'application/json; charset=UTF-8');
  34. }
  35. protected function http_request($url,$method = false,$data = null,$foll = 0){
  36. //初始化
  37. $curl = curl_init();
  38. curl_setopt($curl, CURLOPT_URL, $url); //访问的url
  39. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //忽略https
  40. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //忽略https
  41. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //完全静默
  42. if($method == true){
  43. //post请求
  44. if (!empty($data)){
  45. curl_setopt($curl, CURLOPT_POST, 1);
  46. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  47. }
  48. }else{
  49. //get请求
  50. curl_setopt($curl, CURLOPT_HEADER, false);
  51. curl_setopt($curl, CURLOPT_HTTPHEADER, ["user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"]); //UA
  52. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $foll); //默认为$foll=0,大概意思就是对照模块网页访问的禁止301 302 跳转。
  53. }
  54. $output = curl_exec($curl);
  55. curl_close($curl);
  56. return $output;
  57. }
  58. //二维数组排序
  59. protected function arraySort($array, $keys, $sort = SORT_DESC) {
  60. $keysValue = [];
  61. foreach ($array as $k => $v) {
  62. $keysValue[$k] = $v[$keys];
  63. }
  64. array_multisort($keysValue, $sort, $array);
  65. return $array;
  66. }
  67. public function addexcel($request)
  68. {
  69. if( $request->isMethod('post') && $_FILES['file'] ){
  70. //> 获取上传文件路径 $_FILES
  71. if( $_FILES['file']['error'] == 0 ){
  72. //> 获取上传文件名称(已便于后面判断是否上传需要后缀文件)
  73. $name = $_FILES['file']['name'];
  74. //> 获取上传文件后缀 如(xls exe xlsx 等)
  75. $ext = strtolower(trim(substr($name,(strpos($name,'.')+1))));
  76. //> 判断文件是否为指定的上传文件后缀
  77. if( ! in_array($ext,array('xls','xlsx')) ){
  78. //> 返回上一次请求位置,并携带错误消息
  79. return redirect()->back()->withErrors('请输入xls或xlsx后缀文件')->withInput();
  80. }
  81. //> 获取文件上传路径
  82. $fileName = $_FILES['file']['tmp_name'];
  83. $list = [];
  84. //> excel文件导入 上传文件
  85. Excel::load($fileName, function ($reader) use (&$list){
  86. $list = $reader->getSheet(0)->toArray();
  87. });
  88. return $list;
  89. }
  90. return $list = [];
  91. }
  92. }
  93. }