error('亲,您还没有登录呢!', '/'); } } public function cardData() { $result = array(); // 管理员总人数 $result[0] = Db::table('bs_administrator')->count(); // 用户总人数 $result[1] = Db::table('bs_user')->count(); // 用户总登录次数 $result[2] = Db::table('bs_user')->sum('count_times'); // 总设备数 $result[3] = Db::table('bs_device_serial')->count(); return $result; } /*获取地域信息的接口*/ public function areaData() { $addr = ['南海诸岛', '台湾', '河北', '山西', '内蒙古', '辽宁', '吉林', '黑龙江', '江苏', '浙江', '安徽', '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '广西', '海南', '四川', '贵州', '云南', '西藏', '陕西', '甘肃', '青海', '宁夏', '新疆', '北京', '天津', '上海', '重庆', '香港', '澳门']; $list = []; $i = 0; $max = 0; $devName = Db::table('bs_device_map')->select(); foreach ($addr as $data) { $list[$i]['name'] = $data; $list[$i]['value'] = 0; for ($dev = 0; $dev < count($devName); $dev++) { $data1 = Db::table('bs_device_' . $devName[$dev]['function'])->select(); for ($j = 0; $j < count($data1); $j++) { $area = (explode('-', $data1[$j]['area']))[0]; $area = substr($area, 0, -3); if ($area == $data) { $list[$i]['value'] += 1; // dump($area); } } if ($list[$i]['value'] > $max) { $max = $list[$i]['value']; } } $i++; } $result[0] = json_encode($list, JSON_UNESCAPED_UNICODE);//中文不转为unicode $result[1] = $max; // dump($result); return $result; } /*获取设备类型信息的接口*/ public function devTypeData() { $type = Db::table('bs_device_map')->select(); // dump($type);die; // 生成前台需要的标识数据 $result = array(); for ($i = 0; $i < count($type); $i++) { $result[0][$i] = $type[$i]['chinese']; } // $type[0]['name'] = $type[0]['chinese']; // unset($type[0]['chinese']); // dump($type); // 生成前台需要的每个设备的数据 for ($i = 0; $i < count($type); $i++) { $result[1][$i]['name'] = $type[$i]['chinese']; $result[1][$i]['value'] = (Db::table('bs_device_serial')->where('type', $type[$i]['function'])->count()); } $result[1] = json_encode($result[1], JSON_UNESCAPED_UNICODE);//中文不转为unicode // dump($result);die; return $result; } /*获取用户信息的接口*/ public function userData() { $data = Db::table('bs_user')->field('create_time')->order('create_time desc')->select(); $result = array(); // 提取用户的创建日期 for ($i = 0; $i < count($data); $i++) { $time = explode(' ', $data[$i]['create_time']); $time = explode('-', $time[0]); $create_time = $time[1] . '-' . $time[2]; $result[0][$i] = $create_time; } // 数组去重 $result[0] = array_unique($result[0]); // 保留前12条数据 $result[0] = array_slice($result[0], 0, 12); // 数组逆序 $result[0] = array_reverse($result[0]); // 仅保留数组键值对中的值 $result[0] = array_values($result[0]); // dump($result[0]); // return $result; // 生成前台需要的每个时间点的数据 for ($i = 0; $i < count($result[0]); $i++) { $result[1][$i] = (Db::table('bs_user')->where('create_time', 'like', '%' . $result[0][$i] . '%')->count()); } // dump($result);die; return $result; } // 将所有固定表中的ID重新排序 public function resort() { // 重新排序bs_people表 Db::execute("ALTER TABLE bs_people DROP id;"); Db::execute("alter table bs_people add id int(11) not null auto_increment primary key first;"); // 重新排序bs_user表 Db::execute("ALTER TABLE bs_user DROP id;"); Db::execute("alter table bs_user add id int(11) not null auto_increment primary key first;"); // 重新排序bs_administrator表 Db::execute("ALTER TABLE bs_administrator DROP id;"); Db::execute("alter table bs_administrator add id int(11) not null auto_increment primary key first;"); // 重新排序bs_device_map表 Db::execute("ALTER TABLE bs_device_map DROP id;"); Db::execute("alter table bs_device_map add id int(11) not null auto_increment primary key first;"); // 重新排序bs_device_serial表 Db::execute("ALTER TABLE bs_device_serial DROP id;"); Db::execute("alter table bs_device_serial add id int(11) not null auto_increment primary key first;"); } ///////////////以下为管理员管理//////////////////////// ///////////////以下为管理员管理//////////////////////// public function admin_add() { return $this->fetch(); } //新增管理员 public function admin_insert(Request $request) { $receive = $request->post(); //判断该用户名是否可用 if (Db::table('bs_people')->where('name', $receive['account'])->find()) { return '该账号名已被占用,请重新输入'; } //将该管理员账号名录入总账户表中 $flag1 = Db::table('bs_people')->insert(['name' => $receive['account'], 'root' => 1]); //拼接要添加到管理员列表的数据 $createTime = date('Y-m-d H:i:s', time()); $allData = ['account' => $receive['account'], 'password' => $receive['passwd'], 'tel' => $receive['phone'], 'email' => $receive['email'], 'create_time' => $createTime]; $flag2 = Db::table('bs_administrator')->insert($allData); $this->resort(); if ($flag1 && $flag2) { $log = '新增管理员:' . $receive['account']; $this->logInsert($log); return 'ok'; } else { if (!$flag1) { return "ERROR1"; } if (!$flag1) { return "ERROR2"; } } } //编辑管理员信息页面 public function admin_edit() { $id = input('get.id'); $data = Db::table('bs_administrator')->where('id', $id)->find(); $this->assign('data', $data); // dump($data); return $this->fetch(); } //修改管理员信息 public function admin_update(Request $request) { $receive = $request->post(); //检查修改了哪些信息 $preData = Db::table('bs_administrator')->where('id', $receive['id'])->field('account,password,tel,email')->find(); $preData = array_values($preData); $receive1 = array_values(array_slice($receive, 1)); $map = ['账户名', '密码', '电话', '邮箱']; $statusArr = []; for ($i = 0; $i < 4; $i++) { if ($preData[$i] != $receive1[$i]) { $statusArr['option'][] = $map[$i]; $statusArr['content'][] = $receive1[$i]; } } if (!$statusArr) { return '该管理员信息未发生变化'; } $oldAccount = (Db::table('bs_administrator')->where('id', $receive['id'])->find())['account']; if ($oldAccount != $receive['username']) { //判断账号是否同名 if (Db::table('bs_people')->where('name', $receive['username'])->find()) { return '该账户名已被占用,请重新输入'; } $result2 = Db::table('bs_people')->where('name', $oldAccount)->update(['name' => $receive['username']]); if (!$result2) { return 'ERROR2'; } } //拼接要添加的数据 $allData = ['account' => $receive['username'], 'password' => $receive['passwd'], 'tel' => $receive['phone'], 'email' => $receive['email']]; $result1 = Db::table('bs_administrator')->where('id', $receive['id'])->update($allData); if ($result1) { $log = '修改管理员' . $receive['username'] . '的信息: '; for ($i = 0; $i < count($statusArr['option']); $i++) { $log = $log . $statusArr['option'][$i] . '变为:' . $statusArr['content'][$i] . ';'; } $this->logInsert($log); return 'ok'; } else { return '后台异常,请联系后台管理员'; } } ///////////////以下为用户管理//////////////////////// ///////////////以下为用户管理//////////////////////// public function user_add() { return $this->fetch(); } //新增用户 public function user_insert(Request $request) { $receive = $request->post(); //检查新加入的用户名是否已存在 if (Db::table('bs_people')->where('name', $receive['username'])->find()) { return '该用户名已存在,请重新输入'; } //拼接要添加的数据 $allData = [ 'uid' => $receive['username'], 'password' => $receive['passwd'], 'tel' => $receive['phone'], 'email' => $receive['email'], ]; $user = new Usermodel($allData); $result1 = $user->allowField(true)->save(); //在设备数量映射表中创建该用户 $devData = ['uid' => $receive['username']]; $result2 = Db::table('bs_device')->insert($devData); // 在总账户库中录入该用户 $allAccount = ['name' => $receive['username']]; $result2 = Db::table('bs_people')->insert($allAccount); $this->resort(); if ($result1 && $result2) { $this->logInsert('新增用户:' . $receive['username']); return 'ok'; } else { if (!$result1) { return '用户表添加出错,请联系后台管理员'; }; if (!$result2) { return '用户设备表添加出错,请联系后台管理员'; }; } } //编辑用户信息页面 public function user_edit() { $id = input('get.id'); $data = Db::table('bs_user')->where('id', $id)->find(); $this->assign('data', $data); // dump($data); return $this->fetch(); } //更新用户 public function user_update(Request $request) { $receive = $request->post(); //检查修改了哪些信息 $preData = Db::table('bs_user')->where('id', $receive['id'])->field('uid,password,tel,email')->find(); $preData = array_values($preData); $receive1 = array_values(array_slice($receive, 1)); $map = ['账户名', '密码', '电话', '邮箱']; $statusArr = []; for ($i = 0; $i < 4; $i++) { if ($preData[$i] != $receive1[$i]) { $statusArr['option'][] = $map[$i]; $statusArr['content'][] = $receive1[$i]; } } if (!$statusArr) { return '该用户信息未发生变化'; } // 判断更改的是否为用户名 $uid = (Db::table('bs_user')->where('id', $receive['id'])->find())['uid']; if ($receive['uid'] == $uid) { $flag1 = false; } else { // 判断账号是否同名 if (Db::table('bs_people')->where('name', $receive['uid'])->find()) { return '该账户名已被占用,请重新输入'; } $flag1 = true; }; // 获得要匹配的设备循环遍历信息 $deviceName = Db::table('bs_device_map')->field('function')->select(); for ($i = 0; $i < count($deviceName); $i++) { $devArr[] = $deviceName[$i]['function']; }; //拼接要添加的数据并更新用户表中的数据 $allData = ['uid' => $receive['uid'], 'password' => $receive['passwd'], 'tel' => $receive['phone'], 'email' => $receive['email']]; $result1 = Db::table('bs_user')->where('id', $receive['id'])->update($allData); // 如果更改了用户名则需要修改其他表中对应的数据 if ($flag1) { //修改用户设备表 $result2 = Db::table('bs_device')->where('uid', $uid)->update(['uid' => $receive['uid']]); //修改总账号表 $result3 = Db::table('bs_people')->where('name', $uid)->update(['name' => $receive['uid']]); //修改各设备表中所有该用户名的uid信息 for ($i = 0; $i < count($devArr); $i++) { Db::table('bs_device_' . $devArr[$i])->where('uid', $uid)->update(['uid' => $receive['uid']]); }; if ($result1 && $result2 && $result3) { $log = '修改用户' . $receive['uid'] . '的信息: '; for ($i = 0; $i < count($statusArr['option']); $i++) { $log = $log . $statusArr['option'][$i] . '变为:' . $statusArr['content'][$i] . ';'; } $this->logInsert($log); return 'ok'; } else { if ($result1) { return 'ERROR1'; } if ($result1) { return 'ERROR2'; } if ($result1) { return 'ERROR3'; } } } if ($result1) { $log = '修改用户' . $receive['uid'] . '的信息: '; for ($i = 0; $i < count($statusArr['option']); $i++) { $log = $log . $statusArr['option'][$i] . '变为:' . $statusArr['content'][$i] . ';'; } $this->logInsert($log); return 'ok'; } else { return '后台数据异常,请联系管理员'; } } ///////////////以下为设备管理///////////////////////// ///////////////以下为设备管理//////////////////////// ///设备ID入库界面 public function device_id_add_page() { $data = Db::table('bs_device_map')->select(); $this->assign('data', $data); return $this->fetch(); } //设备ID入库 public function device_id_add(Request $request) { $receive = $request->post(); // dump($receive); // 判读该序列号是否存在,是则返回提示 if (Db::table('bs_device_serial')->where('serial_num', $receive['serial_num'])->find()) { return '该序列号已存在,请重新输入'; } $data = ['serial_num' => $receive['serial_num'], 'type' => $receive['type']]; $flag1 = Db::table('bs_device_serial')->insert($data); $this->resort(); if ($flag1) { $this->logInsert('设备ID入库,类型为:' . $receive['type'] . ' ' . '设备ID:' . $receive['serial_num']); return 'ok'; } else { return '数据库异常,请联系后台管理员'; } } //增删设备界面 public function device_operation() { $data = Db::table('bs_device_map')->select(); $this->assign('data', $data); $icons = Db::table('bs_icon_data')->select(); $this->assign('icons', $icons); return $this->fetch(); } //删除设备 public function device_delete(Request $request) { $receive = $request->post(); // dump($data['function']) ; // die; //将数据表bs_device中该设备字段删除 Db::execute("ALTER TABLE bs_device DROP " . $receive['function']); //在data数据库中删除该表,包括其中存放的所有新加入的设备中所有的用户uid对应的传感器型号以及该传感器添加时间 Db::execute("DROP TABLE bs_device_" . $receive['function']); // 删除data数据库中用来存放该传感器详细数据的表 Db::execute("DROP TABLE bs_device_data_" . $receive['function']); //将bs_device_map表中的该设备删除 $flag1 = Db::table('bs_device_map')->where('function', $receive['function'])->delete(); // 删除该类型设备对应的设备ID Db::table("bs_device_serial")->where('type', $receive['function'])->delete(); // 重置bs_device_map表排序ID Db::execute("ALTER TABLE bs_device_map DROP id;"); Db::execute("alter table bs_device_map add id int(11) not null auto_increment primary key first;"); // 重置排序ID $this->resort(); if ($flag1) { $this->logInsert('删除' . $receive['deviceName'] . '类型设备'); return 'ok'; } else { return 'ERROR'; } } //添加新的设备类型 public function device_insert(Request $request) { $receive = $request->post(); //判断字段,不能与数据库中的序列号和映射表同名 if ($receive['function'] == 'map' || $receive['function'] == 'serial') { return '该字段已被占用,请重新输入'; }; //判断新加入的名称和标识符是否重复,重复则报错提示 $flag0 = Db::table('bs_device_map')->where('function', $receive['function'])->find(); $flag1 = Db::table('bs_device_map')->where('chinese', $receive['chinese'])->find(); if ($flag0 || $flag1) { if ($flag0) { return '该设备标识名已存在,请重新输入'; }; if ($flag1) { return '该设备已存在,请重新输入'; }; } else { //在图标库中查找接收到的英文字符对应的该图标的数字符号 $icon = Db::table('bs_icon_data')->field('icon_string')->where('icon_sym', $receive['icon'])->select(); // dump($icon[0]['icon_string']); //给数据表bs_device添加新的字段用来存放每个用户的该设备的数目 Db::execute("ALTER TABLE bs_device ADD " . $receive['function'] . " INT(255) NOT NULL DEFAULT 0 "); //拼接bs_device_map表所需要的数据并写入数据库 $deviceMap = ['chinese' => $receive['chinese'], 'function' => $receive['function'], 'unit' => $receive['unit'], 'icon' => $icon[0]['icon_string']]; $flag = Db::table('bs_device_map')->insert($deviceMap); //在data数据库中增加新表,存放新加入的设备中所有的用户uid对应的传感器型号以及该传感器添加时间 Db::execute("CREATE TABLE bs_device_" . $receive['function'] . "(uid VARCHAR(255) not null, serial_num VARCHAR(255) not null,status BOOLEAN NOT NULL DEFAULT TRUE,create_time DATETIME not null,area VARCHAR(255) not null,UNIQUE(serial_num)) ENGINE = MyISAM CHARSET=utf8 COLLATE utf8_general_ci "); //在data数据库中添加数据存放新加入设备的详细数据 Db::execute("CREATE TABLE bs_device_data_" . $receive['function'] . "(time INT(255) NOT NULL, serial_num VARCHAR(255) not null, data FLOAT NOT NULL, id INT(255) NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) ) ENGINE = MyISAM CHARSET=utf8 COLLATE utf8_general_ci "); $this->resort(); if ($flag) { $this->logInsert('新增:' . $receive['chinese'] . '类型设备,' . '设备标识符为:' . $receive['function']); return 'ok'; } else { return '后台数据异常,请联系管理员'; } } } //设备ID添加绑定界面 public function device_add_page() { $devName = input('get.devName'); $this->assign('devName', $devName); return $this->fetch(); } //设备ID绑定函数 public function device_add(Request $request) { $receive = $request->post(); //获得该设备名所对应的设备标识符名称 $deviceName = (Db::table('bs_device_map')->where('chinese', $receive['deviceName'])->field('function')->find())['function']; //验证该用户是否在用户表中,如果没有则返回提示 if (!(Db::table('bs_user')->where('uid', $receive['uid'])->find())) { return '该用户不存在,请重新输入!'; }; //验证该设备序列号是否在设备序列号表中,如果没有则返回提示 $devSerial = Db::table('bs_device_serial')->where('type', $deviceName)->where('serial_num', $receive['serialNum'])->find(); if (!$devSerial) { return '设备序列号错误,请重新输入!'; }; //验证该设备序列号是否已绑定,如果有则返回提示 if (Db::table('bs_device_' . $deviceName)->where('serial_num', $receive['serialNum'])->find()) { return '该序列号已被绑定,请绑定其他设备!'; }; //获得当前系统时间 $dateTime = date("Y-m-d H:i:s"); //拼接要添加的数据 $allData = ['uid' => $receive['uid'], 'serial_num' => $receive['serialNum'], 'create_time' => $dateTime, 'area' => $receive['area']]; $flag = Db::table('bs_device_' . $deviceName)->insert($allData); //添加初始化测试数据 $min = 60; $max = 80; for ($i = 0; $i < 7; $i++) { $num = $min + mt_rand() / mt_getrandmax() * ($max - $min); $num = floatval(number_format($num, 2)); $timeStamp = time(); $testDate = ['time' => $timeStamp,'serial_num'=>$receive['serialNum'], 'data' => $num]; Db::table('bs_device_data_' . $deviceName)->insert($testDate); } //更新用户设备表中该用户拥有该设备的数量 $num = Db::table('bs_device_' . $deviceName)->where('uid', $receive['uid'])->count(); Db::table('bs_device')->where('uid', $receive['uid'])->update([$deviceName => $num]); if ($flag) { $this->logInsert('在' . $receive['deviceName'] . '类型设备中将设备ID:' . $receive['serialNum'] . '与用户:' . $receive['uid'] . '绑定'); return 'ok'; } else { return '数据库异常,请联系后台管理员'; } } //图标预览 public function icon() { return $this->fetch(); } //显示每个设备ID的基本信息 public function show_detail() { $devName = input('get.devName'); $serial_num = input('get.serial_num'); $unit = (Db::table('bs_device_map')->where('chinese', $devName)->field('unit')->find())['unit']; $this->assign('devName', $devName); $this->assign('serial_num', $serial_num); $this->assign('unit', $unit); return $this->fetch(); } // 单个设备ID的可视化数据传值 public function echarts(Request $request) { $receive = $request->post(); //将收到的设备名转换成对应的设备识别符 $deviceName = (Db::table('bs_device_map')->where('chinese', $receive['deviceName'])->field('function')->find())['function']; // //获得前端所要查询的时间戳 // $timeStamp = intval($receive['time']); $timeStamp = time(); // echo $timeStamp; /////////////////////////////////////// //以下代码为后端模拟设备数据生成,实际生产环境请删除并匹配自己的数据库 //下面4行代码用来生产60-80之间的随机float类型小数 $min = 60; $max = 80; $num = $min + mt_rand() / mt_getrandmax() * ($max - $min); $num = floatval(number_format($num, 2)); //////////$num为生成的随机float类型小数/////////////////// ///接下来将生成的随机小数插入到该设备对应的数据存储表中的对应时间上 $data = ['time' => $timeStamp, 'serial_num' => $receive['serial_num'], 'data' => $num]; Db::table('bs_device_data_' . $deviceName)->insert($data); //模拟设备数据生成完成///////////////////////////// /////////////////////////////////////// // 接下来根据所取得的设备型号以及设备ID去对应的数据库中查询最近的7条数据 $result = Db::table('bs_device_data_' . $deviceName)->where('serial_num', $receive['serial_num'])->order('time desc')->field('time, data')->limit(7)->select(); return $result; } //显示该设备ID对应的历史数据 public function device_detailMsg() { $request = Request::instance(); $receive = $request->get(); // dump($receive); // die; //获得该设备名所对应的设备标识符名称 $deviceName = (Db::table('bs_device_map')->where('chinese', $receive['deviceName'])->field('function')->find())['function']; // 判断请求类型是初始化数据表(true)还是搜索数据表(false) $flag = count($receive) > 4 ? false : true; //dump($flag); if ($flag) { $num = Db::table('bs_device_data_' . $deviceName)->where('serial_num', $receive['serial_num'])->count(); $page = input("get.page") ? input("get.page") : 1; $page = intval($page); $limit = input("get.limit") ? input("get.limit") : 1; $limit = intval($limit); $start = $limit * ($page - 1); //分页查询 $cate_list = Db::table("bs_device_data_" . $deviceName)->where('serial_num', $receive['serial_num'])->order('time desc')->limit($start, $limit)->select(); if (empty($cate_list)) { $list["code"] = 1; $list["msg"] = "暂无历史数据"; $list["count"] = $num; $list["data"] = $cate_list; } else { for ($i = 0; $i < count($cate_list); $i++) { $cate_list[$i]['time'] = date('Y-m-d H:i:s', $cate_list[$i]['time']); } $list["msg"] = ""; $list["code"] = 0; $list["count"] = $num; $list["data"] = $cate_list; } return json($list); } else { // dump($receive); // die; //将接收到的日期转化成时间戳 $startTime = strtotime($receive['startDate']); $endTime = strtotime($receive['endDate']); $page = input("get.page") ? input("get.page") : 1; $page = intval($page); $limit = input("get.limit") ? input("get.limit") : 1; $limit = intval($limit); $start = $limit * ($page - 1); //分页按照时间范围查询 $result = Db::table("bs_device_data_" . $deviceName)->where('serial_num', $receive['serial_num'])->where('time', '>', $startTime)->where('time', '<', $endTime)->order('time desc')->limit($start, $limit)->select(); $num = Db::table("bs_device_data_" . $deviceName)->where('serial_num', $receive['serial_num'])->where('time', '>', $startTime)->where('time', '<', $endTime)->order('time desc')->count(); for ($i = 0; $i < count($result); $i++) { $result[$i]['time'] = date('Y-m-d H:i:s', $result[$i]['time']); } // dump($result); if (empty($result)) { $list["code"] = 1; $list["msg"] = "未查询到" . $receive['startDate'] . ' ~ ' . $receive['endDate'] . "的数据"; $list["count"] = $num; $list["data"] = $result; } else { $list["msg"] = ""; $list["code"] = 0; $list["count"] = $num; $list["data"] = $result; } return json($list); } } // // //图标入库 // public function getdata() // { // return $this->fetch(); // } // public function iconsql(Request $request) // { // $data = $request->post(); //// dump($data); // for ($i=0; $i<168; $i++) // { // $allData = [ // 'icon_name' => $data['datas'][$i]['icon_name'], // 'icon_string' => $data['datas'][$i]['icon_string'], // 'icon_sym' => $data['datas'][$i]['icon_sym'], // ]; //// dump ($data['datas'][$i]['icon_string']) ; // Db::table('bs_icon_data')->insert($allData); // } // // } }