123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 场景
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use think\Hook;
  9. use think\Session;
  10. use think\Request;
  11. use think\Db;
  12. class Scene extends ApiCommon
  13. {
  14. /**
  15. * 用于判断权限
  16. * @permission 无限制
  17. * @allow 登录用户可访问
  18. * @other 其他根据系统设置
  19. **/
  20. public function _initialize()
  21. {
  22. $action = [
  23. 'permission'=>[''],
  24. 'allow'=>['index','save','read','update','delete','sort','defaults']
  25. ];
  26. Hook::listen('check_auth',$action);
  27. $request = Request::instance();
  28. $a = strtolower($request->action());
  29. if (!in_array($a, $action['permission'])) {
  30. parent::_initialize();
  31. }
  32. }
  33. /**
  34. * 场景列表
  35. * @return
  36. */
  37. public function index()
  38. {
  39. $param = $this->param;
  40. $userInfo = $this->userInfo;
  41. $sceneModel = model('Scene');
  42. $data = $sceneModel->getDataList($param['types'], $userInfo['id']);
  43. if (!$data) {
  44. return resultArray(['error' => $sceneModel->getError()]);
  45. }
  46. return resultArray(['data' => $data]);
  47. }
  48. /**
  49. * 场景创建
  50. * @param
  51. * @return
  52. */
  53. public function save()
  54. {
  55. $sceneModel = model('Scene');
  56. $param = $this->param;
  57. $userInfo = $this->userInfo;
  58. $param['user_id'] = $userInfo['id'];
  59. $data = $sceneModel->createData($param, $param['types']);
  60. if (!$data) {
  61. return resultArray(['error' => $sceneModel->getError()]);
  62. }
  63. return resultArray(['data' => '添加成功']);
  64. }
  65. /**
  66. * 场景详情
  67. * @param int $id
  68. * @return
  69. */
  70. public function read()
  71. {
  72. $sceneModel = model('Scene');
  73. $param = $this->param;
  74. $userInfo = $this->userInfo;
  75. $data = $sceneModel->getDataById($param['id'], $userInfo['id']);
  76. if (!$data) {
  77. return resultArray(['error' => $sceneModel->getError()]);
  78. }
  79. return resultArray(['data' => $data]);
  80. }
  81. /**
  82. * 编辑场景
  83. * @param int $id
  84. * @return
  85. */
  86. public function update()
  87. {
  88. $sceneModel = model('Scene');
  89. $param = $this->param;
  90. $userInfo = $this->userInfo;
  91. $param['user_id'] = $userInfo['id'];
  92. $data = $sceneModel->updateDataById($param, $param['id']);
  93. if (!$data) {
  94. return resultArray(['error' => $sceneModel->getError()]);
  95. }
  96. return resultArray(['data' => '编辑成功']);
  97. }
  98. /**
  99. * 删除场景
  100. * @param int $id
  101. * @return
  102. */
  103. public function delete($id)
  104. {
  105. $sceneModel = model('Scene');
  106. $param = $this->param;
  107. $userInfo = $this->userInfo;
  108. //权限判断
  109. if (!$sceneModel->getDataById($param['id'], $userInfo['id'])) {
  110. return resultArray(['error' => '数据不存在或已删除']);
  111. }
  112. $dataInfo = db('admin_scene')->where(['scene_id' => $param['id']])->find();
  113. $resData = $sceneModel->delDataById($param['id']);
  114. if ($resData) {
  115. //重新设置默认
  116. $default = db('admin_scene')->where(['types' => $dataInfo['types'],'bydata' => 'all'])->find();
  117. $sceneModel->defaultDataById(['types' => $dataInfo['types'],'user_id' => $userInfo['id']], $default['scene_id']);
  118. return resultArray(['data' => '删除成功']);
  119. } else {
  120. return resultArray(['error' => $sceneModel->getError()]);
  121. }
  122. }
  123. /**
  124. * 场景排序
  125. * @param
  126. * @return
  127. */
  128. public function sort()
  129. {
  130. $sceneModel = model('Scene');
  131. $param = $this->param;
  132. $userInfo = $this->userInfo;
  133. $param['ids'] = $param['ids'] ? : [];
  134. $param['hide_ids'] = $param['hide_ids'] ? : [];
  135. $resData = $sceneModel->listOrder($param, $userInfo['id']);
  136. if (!$resData) {
  137. return resultArray(['error' => $sceneModel->getError()]);
  138. }
  139. return resultArray(['data' => '设置成功']);
  140. }
  141. /**
  142. * 场景默认
  143. * @param scene_id 场景ID
  144. * @return
  145. */
  146. public function defaults()
  147. {
  148. $sceneModel = model('Scene');
  149. $param = $this->param;
  150. $userInfo = $this->userInfo;
  151. $scene_id = $param['id'];
  152. $param['user_id'] = $userInfo['id'];
  153. $resData = $sceneModel->defaultDataById($param, $scene_id);
  154. if (!$resData) {
  155. return resultArray(['error' => $sceneModel->getError()]);
  156. }
  157. return resultArray(['data' => '设置成功']);
  158. }
  159. }