Address.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class Address extends Auth
  5. {
  6. public function index(){
  7. if(request()->isAjax()){
  8. //分页参数
  9. $length = input('rows',10,'intval'); //每页条数
  10. $page = input('page',1,'intval'); //第几页
  11. $start = ($page - 1) * $length; //分页开始位置
  12. //排序
  13. $sortRow = input('sidx','id','trim'); //排序列
  14. $sort = input('sord','desc','trim'); //排序方式
  15. $order = $sortRow.' '.$sort;
  16. $title = input('title','','trim');
  17. if($title){
  18. $map[] = ['title','like','%'.$title.'%'];
  19. }
  20. $enable = input('enable','','trim');
  21. if($enable != ''){
  22. $map[] = ['enable','=',$enable];
  23. }
  24. $type = input('type','','trim');
  25. if($type != ''){
  26. $map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  27. }
  28. $map[] = ['del','=',0];
  29. $map[] = ['org_id','=',$this->orgId];
  30. $map= empty($map) ? true: $map;
  31. //数据查询
  32. $lists = db('address')->where($map)->limit($start,$length)->order($order)->select();
  33. foreach ($lists as $k=>$v){
  34. $ts = $v['types']?explode(',',$v['types']):[];
  35. $lists[$k]['types_text'] = model('Address')->getTypeStr($ts);
  36. }
  37. //数据返回
  38. $totalCount = db('address')->where($map)->count();
  39. $totalPage = ceil($totalCount/$length);
  40. $result['page'] = $page;
  41. $result['total'] = $totalPage;
  42. $result['records'] = $totalCount;
  43. $result['rows'] = $lists;
  44. return json($result);
  45. }else{
  46. $types = model('Address')->getTypes();
  47. $this->assign('types',$types);
  48. return $this->fetch();
  49. }
  50. }
  51. /**
  52. * 新增/编辑
  53. */
  54. public function add($id=0){
  55. if(request()->isPost()){
  56. $res = model('Address')->updates();
  57. if($res){
  58. $this->success('操作成功',url('index'));
  59. }else{
  60. $this->error(model('Address')->getError());
  61. }
  62. }else{
  63. if($id){
  64. $info = db('address')->where('id',$id)->find();
  65. if($info){
  66. $info['types'] = $info['types']?explode(',',$info['types']):[];
  67. }
  68. $this->assign('info',$info);
  69. }
  70. $ts = model('Address')->getTypes();
  71. $types = [];
  72. foreach ($ts as $k=>$v){
  73. $types[] = [
  74. 'id' => $k,
  75. 'title' => $v
  76. ];
  77. }
  78. $dep = model('dep')->getList();
  79. $this->assign('dep',$dep);
  80. $this->assign('types',$types);
  81. return $this->fetch();
  82. }
  83. }
  84. /**
  85. * 删除记录
  86. * @param int $id
  87. */
  88. public function del($id=0){
  89. if(!$id){
  90. $this->error('参数错误');
  91. }
  92. $res = db('address')->where('id',$id)->setField('del',1);
  93. if($res){
  94. $this->success('删除成功');
  95. }else{
  96. $this->error('删除失败');
  97. }
  98. }
  99. /**
  100. * 改变字段值
  101. * @param int $fv
  102. * @param string $fn
  103. * @param int $fv
  104. */
  105. public function changeField($id=0,$fn='',$fv=0){
  106. if(!$fn||!$id){
  107. $this->error('参数错误');
  108. }
  109. $res = db('address')->where('id',$id)->setField($fn,$fv);
  110. if($res){
  111. $this->success('操作成功');
  112. }else{
  113. $this->error('操作失败');
  114. }
  115. }
  116. public function qrcode($id=0){
  117. $info = Db::name('address')->where('id',$id)->find();
  118. $code = get_qrcode_str('address',$id);
  119. $config = config('app.addr_url');
  120. $code = $config.$code;
  121. $this->assign('code',$code);
  122. $this->assign('info',$info);
  123. return $this->fetch();
  124. }
  125. // 地址下载
  126. public function addrdown(){
  127. $orgid = $this->orgId;
  128. $map[] = ['del','=',0];
  129. $map[] = ['org_id','=',$this->orgId];
  130. $title = input('title','','trim');
  131. if($title){
  132. $map[] = ['title','like','%'.$title.'%'];
  133. }
  134. $enable = input('enable','','trim');
  135. if($enable != ''){
  136. $map[] = ['enable','=',$enable];
  137. }
  138. $type = input('type','','trim');
  139. if($type != ''){
  140. $map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
  141. }
  142. $ids = input('ids','','trim');
  143. if($ids !=''){
  144. $map[] = ['id','in',explode(',',$ids)];
  145. }
  146. $map= empty($map) ? true: $map;
  147. //数据查询
  148. $lists = db('address')->where($map)->select();
  149. $config = config('app.addr_url');
  150. foreach ($lists as $k=>$v){
  151. $lists[$k]['code'] = $config.get_qrcode_str('address',$v['id']);
  152. }
  153. $path = date('Ymd').'_'.$orgid.'_'.time().mt_rand(1000,9999);
  154. $dir = './uploads/qrcodeimg/'.$path.'/';
  155. foreach ($lists as $v1){
  156. $name = str_ireplace('/','_',$v1['title']).'('.$v1['id'].')'.'.jpg';
  157. $filepath = $dir.'/'.$name;
  158. create_qrcode($v1['code'],$filepath);
  159. }
  160. $zippath = './uploads/qrcodeimg/'.$path.'.zip';
  161. $spath = $dir;
  162. @unlink($zippath);
  163. $zip = new \ZipArchive();
  164. if($zip->open($zippath, \ZipArchive::CREATE)=== TRUE){
  165. add_file_to_zip($spath,$zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
  166. $zip->close(); //关闭处理的zip文件
  167. if(!file_exists($zippath)){
  168. $this->error("打包失败"); //即使创建,仍有可能失败。。。。
  169. }
  170. deldir($dir); // 删除生成的目录
  171. $downname = session('orgName').'地点二维码.zip';
  172. header("Cache-Control: public");
  173. header("Content-Description: File Transfer");
  174. header('Content-disposition: attachment; filename='.$downname); //文件名
  175. header("Content-Type: application/zip"); //zip格式的
  176. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  177. header('Content-Length: '. filesize($zippath)); //告诉浏览器,文件大小
  178. @readfile($zippath);
  179. }else{
  180. $this->error("打包失败");
  181. }
  182. }
  183. public function import(){
  184. return $this->fetch();
  185. }
  186. /**
  187. * 下载点模板
  188. */
  189. public function downloadtem(){
  190. set_time_limit(0);
  191. ini_set("memory_limit","512M");
  192. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  193. $fileName = '地点模板.xlsx';
  194. $excel = new \PHPExcel();
  195. $sheet = $excel->setActiveSheetIndex(0);
  196. $arr = array('A','B','C','D');
  197. $types = model('Address')->getTypes();
  198. ksort($types);
  199. foreach($arr as $k=>$v){
  200. $excel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(true);//换行
  201. $excel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);
  202. if($k==3){
  203. $excel->getActiveSheet()->getColumnDimension($v)->setWidth(100);
  204. }else{
  205. $excel->getActiveSheet()->getColumnDimension($v)->setWidth(18);
  206. }
  207. }
  208. $sheet->setCellValueByColumnAndRow(0,1,'名称');
  209. $sheet->setCellValueByColumnAndRow(1,1,'设备编号');
  210. $sheet->setCellValueByColumnAndRow(2,1,'备注');
  211. $sheet->setCellValueByColumnAndRow(3,1,'类型('.implode(',',$types).') 多个用英文逗号隔开');
  212. $excel->getActiveSheet()->setCellValue('A2', '示例地址');
  213. $excel->getActiveSheet()->setCellValue('B2', 'XXX');
  214. $excel->getActiveSheet()->setCellValue('C2', 'XXX');
  215. $excel->getActiveSheet()->setCellValue('D2', '报修,运送');
  216. ob_end_clean();//清除缓冲区,避免乱码
  217. header('Content-Type: application/vnd.ms-excel');
  218. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  219. header('Cache-Control: max-age=0');
  220. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
  221. $objWriter->save('php://output'); //文件通过浏览器下载
  222. }
  223. /**
  224. * 导入
  225. */
  226. public function importexcel_bak(){
  227. set_time_limit(0);
  228. ini_set("memory_limit", -1);
  229. ob_flush();//清空缓存
  230. flush();//刷新缓存
  231. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  232. $orgId = $this->orgId;
  233. if(request()->file()) {
  234. $file = request()->file('file');
  235. //获取文件后缀
  236. $e = explode('.',$_FILES['file']['name']);
  237. $ext = $e[count($e)-1];
  238. if($ext == 'xls'){
  239. \PHPExcel_IOFactory::createReader( 'Excel5');
  240. }else{
  241. \PHPExcel_IOFactory::createReader('Excel2007');
  242. }
  243. $newArr=['xls','xlsx'];
  244. if(!in_array($ext,$newArr)){
  245. exit('文件格式不正确');
  246. }
  247. // 移动到框架应用根目录/uploads/ 目录下
  248. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  249. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  250. if(!$info){
  251. exit('文件上传失败');
  252. }
  253. $img = './uploads/files/' . $info->getSaveName();
  254. $filePath = str_replace('\\', '/', $img);
  255. $newPath = \PHPExcel_IOFactory::load($filePath);
  256. $excelArray = $newPath->getsheet(0)->toArray(); //转换为数组格式
  257. array_shift($excelArray); //删除第一个数组(标题);
  258. unset($excelArray[0]);
  259. if(empty($excelArray)){
  260. exit('文件内容为空');
  261. }
  262. foreach ($excelArray as $k => $v) {
  263. if(!$v[1]){
  264. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称为空,未导入</font><br />";
  265. continue;
  266. }
  267. if(!$v[4]){
  268. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型为空,未导入</font><br />";
  269. continue;
  270. }
  271. $check = Db::name('address')
  272. ->where('org_id',$orgId)
  273. ->where('del',0)
  274. ->where('title',$v[1])
  275. ->find();
  276. if($check){
  277. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称已存在,未导入</font><br />";
  278. continue;
  279. }
  280. $typeId = [];
  281. $type = explode(',',$v[4]);
  282. $types = model('Address')->getTypes();
  283. $types = array_flip($types);
  284. foreach ($type as $k1=>$v1){
  285. if(isset($types[$v1])){
  286. $typeId[] = $types[$v1];
  287. }
  288. }
  289. if(count($type)!=count($typeId) || empty($typeId)){
  290. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型字段格式错误,未导入</font><br />";
  291. continue;
  292. }
  293. $rData = [
  294. 'org_id'=>$orgId,
  295. 'title'=>$v[1],
  296. 'sn'=>$v[0],
  297. 'remark'=>'',
  298. 'types'=>implode(',',$typeId),
  299. 'create_time'=>getTime()
  300. ];
  301. $ret=Db::name('address')->insert($rData);
  302. if(!$ret){
  303. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  304. }else{
  305. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  306. }
  307. }
  308. }else{
  309. exit('请上传文件');
  310. }
  311. }
  312. public function importexcel(){
  313. set_time_limit(0);
  314. ini_set("memory_limit", -1);
  315. ob_flush();//清空缓存
  316. flush();//刷新缓存
  317. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  318. $orgId = $this->orgId;
  319. if(request()->file()) {
  320. $file = request()->file('file');
  321. //获取文件后缀
  322. $e = explode('.',$_FILES['file']['name']);
  323. $ext = $e[count($e)-1];
  324. if($ext == 'xls'){
  325. \PHPExcel_IOFactory::createReader( 'Excel5');
  326. }else{
  327. \PHPExcel_IOFactory::createReader('Excel2007');
  328. }
  329. $newArr=['xls','xlsx'];
  330. if(!in_array($ext,$newArr)){
  331. exit('文件格式不正确');
  332. }
  333. // 移动到框架应用根目录/uploads/ 目录下
  334. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  335. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  336. if(!$info){
  337. exit('文件上传失败');
  338. }
  339. $img = './uploads/files/' . $info->getSaveName();
  340. $filePath = str_replace('\\', '/', $img);
  341. $newPath = \PHPExcel_IOFactory::load($filePath);
  342. $excelArray = $newPath->getsheet(0)->toArray(); //转换为数组格式
  343. array_shift($excelArray); //删除第一个数组(标题);
  344. if(empty($excelArray)){
  345. exit('文件内容为空');
  346. }
  347. foreach ($excelArray as $k => $v) {
  348. if(!$v[0]){
  349. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称为空,未导入</font><br />";
  350. continue;
  351. }
  352. if(!$v[3]){
  353. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型为空,未导入</font><br />";
  354. continue;
  355. }
  356. $check = Db::name('address')
  357. ->where('org_id',$orgId)
  358. ->where('del',0)
  359. ->where('title',$v[0])
  360. ->find();
  361. if($check){
  362. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称已存在,未导入</font><br />";
  363. continue;
  364. }
  365. $typeId = [];
  366. $type = explode(',',$v[3]);
  367. $types = model('Address')->getTypes();
  368. $types = array_flip($types);
  369. foreach ($type as $k1=>$v1){
  370. if(isset($types[$v1])){
  371. $typeId[] = $types[$v1];
  372. }
  373. }
  374. if(count($type)!=count($typeId) || empty($typeId)){
  375. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型字段格式错误,未导入</font><br />";
  376. continue;
  377. }
  378. $rData = [
  379. 'org_id'=>$orgId,
  380. 'title'=>$v[0],
  381. 'sn'=>$v[1],
  382. 'remark'=>$v[2],
  383. 'types'=>implode(',',$typeId),
  384. 'create_time'=>getTime()
  385. ];
  386. $ret=Db::name('address')->insert($rData);
  387. if(!$ret){
  388. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  389. }else{
  390. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  391. }
  392. }
  393. }else{
  394. exit('请上传文件');
  395. }
  396. }
  397. public function three($id=0){
  398. if(request()->isPost()){
  399. $id = input('id/d',0);
  400. $x = input('x','','trim');
  401. $y = input('y','','trim');
  402. $z = input('z','','trim');
  403. $xyz[] = $x;
  404. $xyz[] = $y;
  405. $xyz[] = $z;
  406. $ret = Db::name('address')->where('id',$id)->update(['xyz'=>implode(',',$xyz),'update_time' => date('Y-m-d H:i:s')]);
  407. if($ret){
  408. $this->success('操作成功');
  409. }else{
  410. $this->error('操作失败');
  411. }
  412. }else{
  413. $info = Db::name('address')->where('id',$id)->find();
  414. $arr = $info['xyz']?explode(',',$info['xyz']):[];
  415. $x = '';
  416. $y = '';
  417. $z = '';
  418. if(count($arr) == 3){
  419. $x = $arr[0];
  420. $y = $arr[1];
  421. $z = $arr[2];
  422. }
  423. $this->assign('x',$x);
  424. $this->assign('y',$y);
  425. $this->assign('z',$z);
  426. $this->assign('id',$id);
  427. return $this->fetch();
  428. }
  429. }
  430. public function downImg(){
  431. $id = input('id','');
  432. $config = config('app.addr_url');
  433. $code = $config.get_qrcode_str('address',$id);
  434. $info = db('address')->where('id',$id)->find();
  435. echo "<pre/>";
  436. print_r($code);
  437. die();
  438. }
  439. public function batchEditCate(){
  440. $ids = input('ids','');
  441. if(request()->isPost()){
  442. if(empty($ids)){
  443. $this->error('请选择地点');
  444. }
  445. $types = input('types');
  446. if(empty($types)){
  447. $this->error('请选择选择类型');
  448. }
  449. $res = Db::name('address')
  450. ->where('id','in',explode(',',$ids))
  451. ->update([
  452. 'types'=>$types,
  453. 'update_time'=>getTime()
  454. ]);
  455. if($res){
  456. $this->success('操作成功',url('index'));
  457. }else{
  458. $this->error(model('Address')->getError());
  459. }
  460. }else{
  461. $ts = model('Address')->getTypes();
  462. $types = [];
  463. foreach ($ts as $k=>$v){
  464. $types[] = [
  465. 'id' => $k,
  466. 'title' => $v
  467. ];
  468. }
  469. $this->assign('types',$types);
  470. $address =db('address')->where('id','in',explode(',',$ids))->column('title');
  471. $this->assign('address',implode(';',$address));
  472. $this->assign('ids',$ids);
  473. return $this->fetch();
  474. }
  475. }
  476. public function importexcel1(){
  477. set_time_limit(0);
  478. ini_set("memory_limit", -1);
  479. ob_flush();//清空缓存
  480. flush();//刷新缓存
  481. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  482. $orgId = $this->orgId;
  483. if(request()->file()) {
  484. $file = request()->file('file');
  485. //获取文件后缀
  486. $e = explode('.',$_FILES['file']['name']);
  487. $ext = $e[count($e)-1];
  488. if($ext == 'xls'){
  489. \PHPExcel_IOFactory::createReader( 'Excel5');
  490. }else{
  491. \PHPExcel_IOFactory::createReader('Excel2007');
  492. }
  493. $newArr=['xls','xlsx'];
  494. if(!in_array($ext,$newArr)){
  495. exit('文件格式不正确');
  496. }
  497. // 移动到框架应用根目录/uploads/ 目录下
  498. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  499. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  500. if(!$info){
  501. exit('文件上传失败');
  502. }
  503. $img = './uploads/files/' . $info->getSaveName();
  504. $filePath = str_replace('\\', '/', $img);
  505. $newPath = \PHPExcel_IOFactory::load($filePath);
  506. $excelArray = $newPath->getsheet(0)->toArray(); //转换为数组格式
  507. array_shift($excelArray); //删除第一个数组(标题);
  508. if(empty($excelArray)){
  509. exit('文件内容为空');
  510. }
  511. foreach ($excelArray as $k => $v) {
  512. if($k > 0){
  513. if(!$v[1]){
  514. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称为空,未导入</font><br />";
  515. continue;
  516. }
  517. if(!$v[4]){
  518. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型为空,未导入</font><br />";
  519. continue;
  520. }
  521. $check = Db::name('address')
  522. ->where('org_id',$orgId)
  523. ->where('del',0)
  524. ->where('title',$v[1])
  525. ->find();
  526. if($check){
  527. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称已存在,未导入</font><br />";
  528. continue;
  529. }
  530. $typeId = [];
  531. $type = explode(',',$v[4]);
  532. $types = model('Address')->getTypes();
  533. $types = array_flip($types);
  534. foreach ($type as $k1=>$v1){
  535. if(isset($types[$v1])){
  536. $typeId[] = $types[$v1];
  537. }
  538. }
  539. if(count($type)!=count($typeId) || empty($typeId)){
  540. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型字段格式错误,未导入</font><br />";
  541. continue;
  542. }
  543. $rData = [
  544. 'org_id'=>$orgId,
  545. 'title'=>$v[1],
  546. 'sn'=>'',
  547. 'remark'=>'',
  548. 'types'=>implode(',',$typeId),
  549. 'create_time'=>getTime()
  550. ];
  551. $ret=Db::name('address')->insert($rData);
  552. if(!$ret){
  553. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  554. }else{
  555. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  556. }
  557. }
  558. }
  559. }else{
  560. exit('请上传文件');
  561. }
  562. }
  563. }