DeviceForm.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\adopt\models\client;
  8. use app\librarys\YsOpen\YsClient;
  9. use app\plugins\adopt\models\AdoptGoodsDevice;
  10. use app\models\Goods;
  11. use app\models\GoodsCat;
  12. use app\plugins\adopt\models\AdoptCat;
  13. use app\plugins\adopt\models\AdoptSetting;
  14. use app\plugins\adopt\models\Monitor;
  15. use yii\base\Model;
  16. use yii\data\Pagination;
  17. class DeviceForm extends Model
  18. {
  19. public $cat_id;// 分类ID
  20. public $page;
  21. public $limit = 10;
  22. public $store_id;
  23. public $device_id;
  24. /**
  25. * 获取监控列表
  26. * @return array
  27. */
  28. public function search(){
  29. // 先获取所有的分类
  30. $catList = AdoptCat::find()->where([
  31. 'store_id' => $this->store_id,
  32. 'is_show'=> 1,
  33. 'is_delete' => 0
  34. ])->orderBy('sort DESC')->all();
  35. if($this->cat_id == 0){
  36. $goodsIdArray = Goods::find()->where([
  37. 'product_type' => Goods::GOODS_TYPE_ADOPT,
  38. 'status' => 1,
  39. 'is_delete' => 0,
  40. 'store_id' => $this->store_id
  41. ])->select('id')->orderBy('sort ASC')->column();
  42. }else{
  43. $goodsIdArray = GoodsCat::find()->where([
  44. 'is_delete' => 0,
  45. 'cat_id' => $this->cat_id,
  46. 'store_id' => $this->store_id
  47. ])->select('goods_id')->column();
  48. }
  49. // 获取设备ID列表
  50. $deviceId = AdoptGoodsDevice::find()->where(['in','goods_id',$goodsIdArray])->andWhere(['is_delete'=>0,'store_id'=>$this->store_id])->select(['device_id'])->column();
  51. $queryList = Monitor::find()->where(['in','id',$deviceId]);
  52. $count = $queryList->count();
  53. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  54. $list = $queryList->orderBy('status DESC')->limit($pagination->limit)
  55. ->offset($pagination->offset)
  56. ->asArray()->all();
  57. foreach($list as &$item){
  58. if(empty($item['cover_pic'])){
  59. $item['cover_pic'] = \Yii::$app->request->hostInfo .'/web/v1/static/images/device.png';
  60. }
  61. }
  62. return ['code'=>0,'data'=>[
  63. 'cat_list' => $catList,
  64. 'list' => $list
  65. ]];
  66. }
  67. public function getPlayUrl(){
  68. if(!$this->device_id || $this->device_id == 'undefined'){
  69. return [
  70. 'code'=>1,
  71. 'msg'=>'请选择要播放的设备'
  72. ];
  73. }
  74. // 获取萤石配置
  75. $setting = AdoptSetting::findOne(['store_id'=>$this->store_id]);
  76. if(!$setting->ys_app_key || !$setting->ys_secret){
  77. return ['code'=>1,'msg'=>'未设置萤石信息'];
  78. }
  79. $ysOpen = new YsClient($setting->ys_app_key,$setting->ys_secret);
  80. $getAccessTokenResult = $ysOpen->getAccessToken();
  81. if(false == $getAccessTokenResult){
  82. return ['code'=>1,'msg'=>"萤石账号有误"];
  83. }
  84. $monitorInfo = Monitor::findOne($this->device_id);
  85. $source = $monitorInfo->serial;//.":1"
  86. $result = $ysOpen->getAddress($source);
  87. // if (!$result['data'][0]['hlsHd']) {
  88. // debug_log($result['data']);
  89. // return [
  90. // 'code'=>1,
  91. // 'msg'=>'获取播放地址失败'
  92. // ];
  93. // }
  94. if ($result['code'] != 200) {
  95. return [
  96. 'code'=>1,
  97. 'msg'=>'获取播放地址失败'
  98. ];
  99. }
  100. return ['code'=>0,'data'=>['url' => $result['data']['url'],'token' => $getAccessTokenResult]];
  101. }
  102. }