Upload.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\controller\v1;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-08-31 15:05
  12. // +----------------------------------------------------------------------
  13. use app\BaseController;
  14. use app\Request;
  15. use library\services\UtilService;
  16. use library\utils\Qiniu;
  17. use think\Image;
  18. class Upload extends BaseController {
  19. public function index(Request $request){
  20. $file =$request->file('file');
  21. $isZ = $request->post('isz',1);
  22. if(empty($file)) {
  23. return app('json')->fail("未上传文件");
  24. }
  25. $rootTmp = config('filesystem.disks.local.root') .'/' . \think\facade\Filesystem::putFile( 'tmp', $file);
  26. if($isZ == 1) {
  27. $image_size = @getimagesize($rootTmp);
  28. if ($image_size[0] > 1000) {
  29. $imgS = Image::open($rootTmp);
  30. $imgS->thumb(1000, $image_size[1]);
  31. $imgS->save($rootTmp);
  32. } else {
  33. if ($image_size[1] > 1000) {
  34. $imgS = Image::open($rootTmp);
  35. $imgS->thumb($image_size[0], 1000);
  36. $imgS->save($rootTmp);
  37. }
  38. }
  39. }
  40. $qiniu = new Qiniu;
  41. $img_url = $qiniu->updateFile('img', $rootTmp, $rootTmp);
  42. if(empty($img_url['url'])){
  43. return app('json')->fail( $qiniu->getError());
  44. }
  45. return app('json')->success(['img'=>$img_url['url']]);
  46. }
  47. /**
  48. * 读取数据
  49. */
  50. public function xls(Request $request){
  51. $post = UtilService::getMore([
  52. ['platform_id','0','empty','请选择平台在上传']
  53. ],$request);
  54. $file = $request->file('file');
  55. if(empty($file)) {
  56. $this->ajaxReturn(['code'=>-1,'msg'=>'没有上传数据']);
  57. }
  58. $info = $file->getFileInfo();
  59. $extension = strtolower( pathinfo($file->getOriginalName(), PATHINFO_EXTENSION) );
  60. if ($extension =='xlsx') {
  61. $objReader = new \PHPExcel_Reader_Excel2007();
  62. $objPHPExcel = $objReader ->load($info->getPathname());
  63. }
  64. if ($extension =='xls') {
  65. $objReader = new \PHPExcel_Reader_Excel5();
  66. $objPHPExcel = $objReader ->load($info->getPathname());
  67. }
  68. if ($extension =='csv') {
  69. $objReader = new \PHPExcel_Reader_CSV();
  70. //默认输入字符集
  71. $objReader->setInputEncoding('GBK');
  72. //默认的分隔符
  73. $objReader->setDelimiter(',');
  74. //载入文件
  75. $objPHPExcel = $objReader->load($info->getPathname());
  76. }
  77. $sheet = $objPHPExcel->getSheet(0);
  78. $highestRow = $sheet->getHighestRow(); // 取得总行数
  79. $highestColumn = $sheet->getHighestColumn(); // 取得总列数
  80. //读取第一行信息
  81. $headerAr = [];
  82. for($i = 0;$i <= \PHPExcel_Cell::columnIndexFromString($highestColumn);$i++) {
  83. $column = \PHPExcel_Cell::stringFromColumnIndex($i);
  84. $value = $objPHPExcel
  85. ->getActiveSheet()
  86. ->getCell($column.'1')
  87. ->getValue();
  88. if(!empty($value)) $value = trim($value);
  89. $headerAr[$value] = $column;
  90. }
  91. $tAr = [];
  92. for ($i = 2; $i <= $highestRow ; $i++) {
  93. //外部订单号
  94. $d['out_order_id'] = empty($headerAr['订单编号']) ? '' : $objPHPExcel
  95. ->getActiveSheet()
  96. ->getCell($headerAr['订单编号'].$i)
  97. ->getValue();
  98. $d['out_order_id'] = preg_replace('/\="(.*?)"/','$1',$d['out_order_id']);
  99. //收货人
  100. $d['name'] = empty($headerAr['收货人姓名']) ? '' : $objPHPExcel
  101. ->getActiveSheet()
  102. ->getCell($headerAr['收货人姓名'].$i)
  103. ->getValue();
  104. //收货地址
  105. $d['address'] = empty($headerAr['收货地址']) ? '' : $objPHPExcel
  106. ->getActiveSheet()
  107. ->getCell($headerAr['收货地址'].$i)
  108. ->getValue();
  109. //收货地址
  110. $d['mobile'] = empty($headerAr['联系手机']) ? '' : $objPHPExcel
  111. ->getActiveSheet()
  112. ->getCell($headerAr['联系手机'].$i)
  113. ->getValue();
  114. $d['mobile'] = str_replace("'","",$d['mobile']);
  115. if(!empty($d['mobile']) && !empty($d['name'])) $tAr[] = $d;
  116. }
  117. return app('json')->success($tAr);
  118. }
  119. }