Userdevice.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * 获取我的设备
  44. */
  45. public function my_device_list() {
  46. $mylist = Db::name('user_device')->field('user_device.*,device.name,device.info,device.image')
  47. ->join('device','user_device.device_id = device.id','LEFT')
  48. ->where(["user_id"=>$this->auth->id])->select();
  49. $this->success("获取成功!",$mylist);
  50. }
  51. //健康档案
  52. public function my_device_data(){
  53. $sn_arr = Db::name('user_device')->where(["user_id"=>$this->auth->id])->column('sn');
  54. $list = Db::name('device_sanheyi')->field('id,createtime,type,type_text,value')->where('sn','IN',$sn_arr)->autopage()->select();
  55. $result = [
  56. 'number' => count($sn_arr),
  57. 'list' => $list,
  58. ];
  59. $this->success(1,$result);
  60. }
  61. }