Userdevice.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 用户设备
  7. */
  8. class Userdevice extends Api
  9. {
  10. protected $noNeedLogin = ['device_list'];
  11. protected $noNeedRight = ['*'];
  12. //所有设备
  13. public function device_list(){
  14. $list = Db::name('device')->where('status',1)->select();
  15. $list = list_domain_image($list,['image']);
  16. $this->success(1,$list);
  17. }
  18. /**
  19. * 绑定新的
  20. */
  21. public function bind_device() {
  22. $sn = input('sn');
  23. $device_id = input('device_id');
  24. if (!$sn) {
  25. $this->error(__('Invalid parameters'));
  26. }
  27. if(mb_strlen($sn) != 15){
  28. $this->error('请输入15位长度正确编号');
  29. }
  30. $data = [
  31. 'user_id' => $this->auth->id,
  32. 'device_id' => $device_id,
  33. 'sn' => $sn,
  34. ];
  35. $check = Db::name('user_device')->where($data)->find();
  36. if($check){
  37. $this->error('您已经绑定了该设备:'.$sn);
  38. }
  39. $res = Db::name('user_device')->insertGetId($data);
  40. $this->success('绑定成功');
  41. }
  42. //解绑
  43. public function unbind(){
  44. $id = input('id');
  45. Db::name('user_device')->where('id',$id)->delete();
  46. $this->success('解绑成功');
  47. }
  48. /**
  49. * 获取我的设备
  50. */
  51. public function my_device_list() {
  52. $mylist = Db::name('user_device')->field('user_device.*,device.name,device.info,device.image')
  53. ->join('device','user_device.device_id = device.id','LEFT')
  54. ->where(["user_id"=>$this->auth->id])->select();
  55. $mylist = list_domain_image($mylist,['image']);
  56. $this->success("获取成功!",$mylist);
  57. }
  58. //健康档案
  59. public function my_device_data(){
  60. $sn_arr = Db::name('user_device')->where(["user_id"=>$this->auth->id])->column('sn');
  61. $list = Db::name('device_sanheyi')->field('id,createtime,type,type_text,value')->where('sn','IN',$sn_arr)->autopage()->select();
  62. $result = [
  63. 'number' => count($sn_arr),
  64. 'list' => $list,
  65. ];
  66. $this->success(1,$result);
  67. }
  68. }