SchoolPhysicalExamination/application/app/controller/Device.php

228 lines
8.7 KiB
PHP

<?php
namespace app\app\controller;
use think\Db;
use think\Log;
class Device extends Base{
protected $color = ['#FF5656','#FFAB00','#5AD06D','#6492F6','#3967D6'];
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
// 获取系统设备列表
public function device_data_list($data = ['token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data)){
return $this->msg(10001);
}
// cache($data['token'],time());
if($this->token_time_validate($data['token']) === false){
return $this->msg(20001);
}
unset($data['token']);
return $this->device_data_list_action($data);
}
// 绑定系统设备
public function device_binding($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','device_id'=>'3','device_mac'=>'54654654']){
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data) || !array_key_exists('device_id', $data) || !array_key_exists('device_mac', $data)){
return $this->msg(10001);
}
// cache($data['token'],time());
if($this->token_time_validate($data['token']) === false){
return $this->msg(20001);
}
// unset($data['token']);
return $this->device_binding_action($data);
}
// 获取用户设备列表
public function device_user_data_list($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a']){
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data)){
return $this->msg(10001);
}
// cache($data['token'],time());
if($this->token_time_validate($data['token']) === false){
return $this->msg(20001);
}
// unset($data['token']);
return $this->device_user_data_list_action($data);
}
// 解绑系统设备
public function device_unbinding($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','id'=>'3']){
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data) || !array_key_exists('id', $data)){
return $this->msg(10001);
}
// cache($data['token'],time());
if($this->token_time_validate($data['token']) === false){
return $this->msg(20001);
}
// unset($data['token']);
return $this->device_unbinding_action($data);
}
################################################################业务接口################################################################
################################################################业务接口################################################################
################################################device_data_list
public function device_data_list_action($data){
$result = Db::table('app_device_data')->where(['is_del'=>0])->field('id,name,pic,content,page_measure,bluetooth_type,device_model')->select();
foreach ($result as $key => $value) {
unset($result[$key]['ROW_NUMBER']);
}
if(empty($result)){
return $this->msg(10004);
}else{
return $this->msg(['list'=>$result]);
}
}
################################################device_binding
public function device_binding_action($data){
$result_user = Db::table('app_account_number')->where(['token'=>$data['token']])->field('id,token')->find();
if(!$result_user){
return $this->msg(10003);
}
$device_code_data = Db::table('app_device_code_data')->where(['machine_code'=>$data['device_mac']])->find();
if($device_code_data){
if($device_code_data['bind_account_id']){
return $this->msg(10003,'设备已被绑定');
}else{
$device_binding = Db::table('app_device_code_data')->where(['machine_code'=>$data['device_mac']])->update([
'bind_account_id'=>$result_user['id'],
]);
if($device_binding){
return $this->msg([]);
}else{
return $this->msg(10002,'更新失败');
}
}
}
$device_binding = Db::table('app_device_code_data')->insert([
'add_id'=>$data['device_id'],
'machine_code'=>$data['device_mac'],
'create_time'=>date('Y-m-d H:i:s'),
'bind_account_id'=>$result_user['id'],
]);
if($device_binding){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
################################################device_user_data_list
public function device_user_data_list_action($data){
$result_user = Db::table('app_account_number')->where(['token'=>$data['token']])->field('id,token')->find();
if(!$result_user){
return $this->msg(10003);
}
$device_code_data = Db::table('app_device_code_data')
->alias('adcd')
->join('app_device_data add','adcd.add_id = add.id','LEFT')
->where(['adcd.bind_account_id'=>$result_user['id']])
->field('adcd.id,adcd.add_id,adcd.machine_code,adcd.create_time,add.name,add.pic,add.content,add.page_measure,add.bluetooth_type,add.device_model')
->select();
if(count($device_code_data) <= 0){
return $this->msg([]);
}
$device_code_data1 = [];
$result = [];
// 新版格式
// $result['list'] = [];
foreach ($device_code_data as $key => $value) {
array_push($result,[
'add_id'=>$value['add_id'],
'name'=>$value['name'],
'pic'=>$value['pic'],
'content'=>$value['content'],
'page_measure'=>$value['page_measure'],
'bluetooth_type'=>$value['bluetooth_type'],
'device_model'=>$value['device_model'],
'bind_time'=>$value['create_time'],
'id'=>$value['id'],
'mac'=>$value['machine_code']
]);
}
// dump($device_code_data);
// die;
// 旧版格式
// foreach ($device_code_data as $key => $value) {
// if(!array_key_exists($value['add_id'], $device_code_data1)){
// $device_code_data1[$value['add_id']] = [
// 'add_id'=>$value['add_id'],
// 'name'=>$value['name'],
// 'pic'=>$value['pic'],
// 'content'=>$value['content'],
// 'page_measure'=>$value['page_measure'],
// 'bluetooth_type'=>$value['bluetooth_type'],
// 'device_model'=>$value['device_model'],
// 'list'=>[[
// 'id'=>$value['id'],
// 'mac'=>$value['machine_code']
// ]]
// ];
// }else{
// array_push($device_code_data1[$value['add_id']]['list'],['id'=>$value['id'],'mac'=>$value['machine_code']]);
// }
// }
// foreach ($device_code_data1 as $key => $value) {
// array_push($result,$value);
// }
return $this->msg(['list'=>$result]);
}
################################################device_binding
public function device_unbinding_action($data){
$result_user = Db::table('app_account_number')->where(['token'=>$data['token']])->field('id,token')->find();
if(!$result_user){
return $this->msg(10003);
}
$device_binding = Db::table('app_device_code_data')->where(['id'=>$data['id'],'bind_account_id'=>$result_user['id']])->update([
'bind_account_id'=>'',
]);
if($device_binding){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
################################################################其他接口################################################################
################################################################其他接口################################################################
}