Device.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\User;
  4. use think\App;
  5. use think\Db;
  6. class Device extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->model = new \app\common\model\Device();
  11. }
  12. public function index(){
  13. if(request()->isAjax()){
  14. //分页参数
  15. $length = input('rows',10,'intval'); //每页条数
  16. $page = input('page',1,'intval'); //第几页
  17. $start = ($page - 1) * $length; //分页开始位置
  18. //排序
  19. $sortRow = input('sidx','sort','trim'); //排序列
  20. $sort = input('sord','asc','trim'); //排序方式
  21. $order = $sortRow.' '.$sort.' ,id desc';
  22. $title = input('title','','trim');
  23. if($title){
  24. $map[] = ['title','like','%'.$title.'%'];
  25. }
  26. $enable = input('enable','','trim');
  27. if($enable != ''){
  28. $map[] = ['enable','=',$enable];
  29. }
  30. $cate_id = input('cate_id','','trim');
  31. if($cate_id != ''){
  32. $map[] = ['cate_id','=',$cate_id];
  33. }
  34. $map[] = ['org_id','=',$this->orgId];
  35. $map[] = ['del','=',0];
  36. $map= empty($map) ? true: $map;
  37. //数据查询
  38. $lists = Db::name('device')
  39. ->where($map)->limit($start,$length)
  40. ->order($order)->select();
  41. foreach ($lists as $k=>$v){
  42. $lists[$k]['user_name']=implode(',',$this->model->getByIdUserName($v['id']));
  43. $info = Db::name('device_cate')
  44. ->where('id',$v['cate_id'])
  45. ->find();
  46. $lists[$k]['cate_name'] = $info?$info['title']:'';
  47. }
  48. //数据返回
  49. $totalCount = db('device')->where($map)->count();
  50. $totalPage = ceil($totalCount/$length);
  51. $result['page'] = $page;
  52. $result['total'] = $totalPage;
  53. $result['records'] = $totalCount;
  54. $result['rows'] = $lists;
  55. return json($result);
  56. }else{
  57. $cate = model('DeviceCate')->list();
  58. $this->assign('cate',$cate);
  59. $this->assign('meta_title','设备管理列表');
  60. return $this->fetch();
  61. }
  62. }
  63. /**
  64. * 新增/编辑
  65. */
  66. public function add($id=0){
  67. $model = $this->model;
  68. if(request()->isPost()){
  69. $res = $model->updates();
  70. if($res){
  71. $this->success('操作成功',url('index'));
  72. }else{
  73. $this->error($model->getError());
  74. }
  75. }else{
  76. $meta_title = '新增设备';
  77. $formModel = new \app\common\model\DeviceForm();
  78. if($id){
  79. $info = Db::name('device')->where('id',$id)->find();
  80. $info['device_form']=explode(',',$info['device_form']);
  81. $info['user']=$model->getDeviceUserList($info['id']);
  82. $this->assign('info',$info);
  83. $meta_title = '编辑设备';
  84. $deviceForm= $formModel->byCateList($info['cate_id']);
  85. $this->assign('device_form',$deviceForm);
  86. }
  87. //用户
  88. $user= (new User())->getDeviceWorker();
  89. $this->assign('device_user',$user);
  90. $this->assign('meta_title',$meta_title);
  91. $cate = model('DeviceCate')->list();
  92. $this->assign('cate',$cate);
  93. return $this->fetch();
  94. }
  95. }
  96. /**
  97. * 删除记录
  98. * @param int $id
  99. */
  100. public function del($id=0){
  101. if(!$id){
  102. $this->error('参数错误');
  103. }
  104. $res = Db::name('device')->where('id',$id)->update(['del'=>1]);
  105. if($res){
  106. $this->success('删除成功');
  107. }else{
  108. $this->error('删除失败');
  109. }
  110. }
  111. /**
  112. * 改变字段值
  113. * @param int $fv
  114. * @param string $fn
  115. * @param int $fv
  116. */
  117. public function changeField($id=0,$fn='',$fv=0){
  118. if(!$fn||!$id){
  119. $this->error('参数错误');
  120. }
  121. $res = Db::name('device')->where('id',$id)->update([$fn => $fv]);
  122. if($res){
  123. $this->success('操作成功');
  124. }else{
  125. $this->error('操作失败');
  126. }
  127. }
  128. //二维码
  129. public function qrcode($id=0)
  130. {
  131. $info=Db::name('device')->where('id',$id)->find();
  132. if (!$info) {
  133. exit('数据不存在');
  134. }
  135. $code = get_qrcode_str('device', $id);
  136. $this->assign('code',$code);
  137. $this->assign('info',$info);
  138. return $this->fetch();
  139. }
  140. public function allPrint(){
  141. $ids = input('ids/array');
  142. $colNum = input('colNum',4);
  143. $deviceList = Db::name('device')
  144. ->where('org_id',cur_org_id())
  145. ->where('enable',1)
  146. ->where('del',0)
  147. ->select();
  148. if($ids){
  149. $list = Db::name('device')
  150. ->where('org_id',cur_org_id())
  151. ->where('enable',1)
  152. ->where('id','in',$ids)
  153. ->where('del',0)
  154. ->select();
  155. }else{
  156. $list = $deviceList;
  157. }
  158. foreach ($list as $k=>$v){
  159. $list[$k]['qCode'] = get_qrcode_str('device',$v['id']);
  160. }
  161. $this->assign('deviceList',$deviceList);
  162. $this->assign('list',$list);
  163. $this->assign('ids',$ids);
  164. $this->assign('colNum',$colNum);
  165. return $this->fetch();
  166. }
  167. public function addrdown(){
  168. $orgid = $this->orgId;
  169. $map[] = ['del','=',0];
  170. $map[] = ['org_id','=',$this->orgId];
  171. $map= empty($map) ? true: $map;
  172. //数据查询
  173. $lists = db('device')->where($map)->select();
  174. foreach ($lists as $k=>$v){
  175. $lists[$k]['code'] = get_qrcode_str('device',$v['id']);
  176. }
  177. $path = date('Ymd').'_'.$orgid.'_'.time().mt_rand(1000,9999);
  178. $dir = './uploads/qrcodeimg/'.$path.'/';
  179. foreach ($lists as $v1){
  180. $name = $v1['title'].'('.$v1['id'].')'.'.jpg';
  181. $filepath = $dir.'/'.$name;
  182. create_qrcode($v1['code'],$filepath);
  183. }
  184. $zippath = './uploads/qrcodeimg/'.$path.'.zip';
  185. $spath = $dir;
  186. @unlink($zippath);
  187. $zip = new \ZipArchive();
  188. if($zip->open($zippath, \ZipArchive::CREATE)=== TRUE){
  189. add_file_to_zip($spath,$zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
  190. $zip->close(); //关闭处理的zip文件
  191. if(!file_exists($zippath)){
  192. $this->error("打包失败"); //即使创建,仍有可能失败。。。。
  193. }
  194. deldir($dir); // 删除生成的目录
  195. $downname = session('orgName').'设备维保二维码.zip';
  196. header("Cache-Control: public");
  197. header("Content-Description: File Transfer");
  198. header('Content-disposition: attachment; filename='.$downname); //文件名
  199. header("Content-Type: application/zip"); //zip格式的
  200. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  201. header('Content-Length: '. filesize($zippath)); //告诉浏览器,文件大小
  202. @readfile($zippath);
  203. }else{
  204. $this->error("打包失败");
  205. }
  206. }
  207. public function getByCate(){
  208. $cate_id = input('id/d',0);
  209. if($cate_id==0){
  210. $this->success('操作成功','',[]);
  211. }
  212. $list = Db::name('device_form')
  213. ->where('org_id',$this->orgId)
  214. ->where('del',0)
  215. ->where('enable',1)
  216. ->where('cate_id',$cate_id)
  217. ->select();
  218. $this->success('操作成功','',$list);
  219. }
  220. }