GreenAddr.php 6.8 KB

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