Address.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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(){
  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. if(empty($excelArray)){
  259. exit('文件内容为空');
  260. }
  261. foreach ($excelArray as $k => $v) {
  262. if(!$v[0]){
  263. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称为空,未导入</font><br />";
  264. continue;
  265. }
  266. if(!$v[3]){
  267. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型为空,未导入</font><br />";
  268. continue;
  269. }
  270. $check = Db::name('address')
  271. ->where('org_id',$orgId)
  272. ->where('del',0)
  273. ->where('title',$v[0])
  274. ->find();
  275. if($check){
  276. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称已存在,未导入</font><br />";
  277. continue;
  278. }
  279. $typeId = [];
  280. $type = explode(',',$v[3]);
  281. $types = model('Address')->getTypes();
  282. $types = array_flip($types);
  283. foreach ($type as $k1=>$v1){
  284. if(isset($types[$v1])){
  285. $typeId[] = $types[$v1];
  286. }
  287. }
  288. if(count($type)!=count($typeId) || empty($typeId)){
  289. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型字段格式错误,未导入</font><br />";
  290. continue;
  291. }
  292. $rData = [
  293. 'org_id'=>$orgId,
  294. 'title'=>$v[0],
  295. 'sn'=>$v[1],
  296. 'remark'=>$v[2],
  297. 'types'=>implode(',',$typeId),
  298. 'create_time'=>getTime()
  299. ];
  300. $ret=Db::name('address')->insert($rData);
  301. if(!$ret){
  302. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  303. }else{
  304. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  305. }
  306. }
  307. }else{
  308. exit('请上传文件');
  309. }
  310. }
  311. public function three($id=0){
  312. if(request()->isPost()){
  313. $id = input('id/d',0);
  314. $x = input('x','','trim');
  315. $y = input('y','','trim');
  316. $z = input('z','','trim');
  317. $xyz[] = $x;
  318. $xyz[] = $y;
  319. $xyz[] = $z;
  320. $ret = Db::name('address')->where('id',$id)->update(['xyz'=>implode(',',$xyz),'update_time' => date('Y-m-d H:i:s')]);
  321. if($ret){
  322. $this->success('操作成功');
  323. }else{
  324. $this->error('操作失败');
  325. }
  326. }else{
  327. $info = Db::name('address')->where('id',$id)->find();
  328. $arr = $info['xyz']?explode(',',$info['xyz']):[];
  329. $x = '';
  330. $y = '';
  331. $z = '';
  332. if(count($arr) == 3){
  333. $x = $arr[0];
  334. $y = $arr[1];
  335. $z = $arr[2];
  336. }
  337. $this->assign('x',$x);
  338. $this->assign('y',$y);
  339. $this->assign('z',$z);
  340. $this->assign('id',$id);
  341. return $this->fetch();
  342. }
  343. }
  344. }