You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 line
5.1 KiB

3 年之前
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class RunapiController extends BaseController {
  5. //添加环境
  6. public function addEnv(){
  7. $login_user = $this->checkLogin();
  8. $env_id = I("env_id/d");
  9. $env_name = I("env_name");
  10. $item_id = I("item_id/d");
  11. $uid = $login_user['uid'] ;
  12. if(!$this->checkItemPermn($uid , $item_id)){
  13. $this->sendError(10303);
  14. return ;
  15. }
  16. $res = false;
  17. if($env_id){
  18. $res = D("RunapiEnv")->where("id = {$env_id} and item_id = {$item_id} ")->save(array(
  19. "env_name" => $env_name ,
  20. "uid" => $uid ,
  21. "last_update_time" => date("Y-m-d H:i:s") ,
  22. ));
  23. $this->sendResult(array("env_id"=>$env_id));
  24. }else{
  25. $env_id = D("RunapiEnv")->add(array(
  26. "env_name" => $env_name ,
  27. "item_id" => $item_id ,
  28. "uid" => $uid ,
  29. "addtime" => date("Y-m-d H:i:s") ,
  30. "last_update_time" => date("Y-m-d H:i:s") ,
  31. ));
  32. $this->sendResult(array("env_id"=>$env_id));
  33. }
  34. }
  35. //更新环境
  36. public function updateEnv(){
  37. $this->addEnv();
  38. }
  39. //获取环境列表
  40. public function getEnvList(){
  41. $item_id = I("item_id/d");
  42. $login_user = $this->checkLogin();
  43. $uid = $login_user['uid'] ;
  44. if(!$this->checkItemPermn($uid , $item_id)){
  45. $this->sendError(10303);
  46. return ;
  47. }
  48. $res = D("RunapiEnv")->where("item_id = {$item_id} ")->select();
  49. if($res){
  50. $this->sendResult($res);
  51. }else{
  52. //如果尚未有环境,则帮其创建一个默认环境
  53. $env_id = D("RunapiEnv")->add(array(
  54. "env_name" => '默认环境' ,
  55. "item_id" => $item_id ,
  56. "uid" => $uid ,
  57. "addtime" => date("Y-m-d H:i:s") ,
  58. "last_update_time" => date("Y-m-d H:i:s") ,
  59. ));
  60. //并且把项目变量都绑定到该默认环境中
  61. D("ItemVariable")->where(" item_id = '$item_id'")->save(array(
  62. "env_id"=>$env_id
  63. ));
  64. sleep(1);
  65. $this->getEnvList();
  66. }
  67. }
  68. //删除环境
  69. public function delEnv(){
  70. $env_id = I("env_id/d");
  71. $login_user = $this->checkLogin();
  72. $uid = $login_user['uid'] ;
  73. $res = D("RunapiEnv")->where("id = {$env_id}")->find();
  74. $item_id = $res['item_id'] ;
  75. if(!$this->checkItemPermn($uid , $item_id)){
  76. $this->sendError(10303);
  77. return ;
  78. }
  79. $res = D("RunapiEnvSelectd")->where("id = {$env_id} ")->delete();
  80. $res = D("RunapiEnv")->where("id = {$env_id} ")->delete();
  81. $res = D("ItemVariable")->where("env_id = {$env_id}")->delete();
  82. if($res){
  83. $this->sendResult($res);
  84. }else{
  85. $this->sendResult(array());
  86. }
  87. }
  88. //设置某个环境变量为选中
  89. public function selectEnv(){
  90. $env_id = I("env_id/d");
  91. $login_user = $this->checkLogin();
  92. $uid = $login_user['uid'] ;
  93. $res = D("RunapiEnv")->where("id = {$env_id}")->find();
  94. $item_id = $res['item_id'] ;
  95. if(!$this->checkItemPermn($uid , $item_id)){
  96. $this->sendError(10303);
  97. return ;
  98. }
  99. D("RunapiEnvSelectd")->where("item_id = {$item_id} and uid = '$uid' ")->delete();
  100. $res = D("RunapiEnvSelectd")->add(array(
  101. "item_id" => $item_id ,
  102. "uid" => $uid ,
  103. "env_id" => $env_id ,
  104. ));
  105. if($res){
  106. $this->sendResult($res);
  107. }else{
  108. $this->sendResult(array());
  109. }
  110. }
  111. //获取用户选中的环境
  112. public function getSelectEnv(){
  113. $item_id = I("item_id/d");
  114. $login_user = $this->checkLogin();
  115. $uid = $login_user['uid'] ;
  116. if(!$this->checkItemPermn($uid , $item_id)){
  117. $this->sendError(10303);
  118. return ;
  119. }
  120. $res = D("RunapiEnvSelectd")->where("item_id = {$item_id} and uid = '$uid' ")->find();
  121. if($res){
  122. $this->sendResult($res);
  123. }else{
  124. $this->sendResult(array(
  125. "env_id" => 0 ,
  126. ));
  127. }
  128. }
  129. //获取全局参数
  130. public function getGlobalParam(){
  131. $item_id = I("item_id/d");
  132. $login_user = $this->checkLogin();
  133. $uid = $login_user['uid'] ;
  134. if(!$this->checkItemPermn($uid , $item_id)){
  135. $this->sendError(10303);
  136. return ;
  137. }
  138. $return = D("Runapi")->getGlobalParam($item_id);
  139. $this->sendResult($return);
  140. }
  141. //修改全局参数
  142. public function updateGlobalParam(){
  143. $item_id = I("item_id/d");
  144. $param_type = I("param_type");
  145. $content_json_str = I("content_json_str");
  146. $login_user = $this->checkLogin();
  147. $uid = $login_user['uid'] ;
  148. if(!$this->checkItemPermn($uid , $item_id)){
  149. $this->sendError(10303);
  150. return ;
  151. }
  152. $res = D("RunapiGlobalParam")->where("param_type = '%s' and item_id = {$item_id} ",array($param_type))->save(array(
  153. "content_json_str" => $content_json_str ,
  154. "last_update_time" => date("Y-m-d H:i:s") ,
  155. ));
  156. if($res){
  157. $this->sendResult($res);
  158. }else{
  159. $this->sendResult(array());
  160. }
  161. }
  162. }