FlowController.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class FlowController extends BaseController {
  5. private $pages ;
  6. //保存
  7. public function save(){
  8. $login_user = $this->checkLogin();
  9. $id = I("id/d");
  10. $flow_name = I("flow_name");
  11. $item_id = I("item_id/d");
  12. $env_id = I("env_id/d");
  13. $times = isset($_REQUEST['times']) ? I("times/d") : 1 ;
  14. $time_interval = isset($_REQUEST['time_interval']) ? I("time_interval/d") : 0 ;
  15. $error_continue =isset($_REQUEST['error_continue']) ? I("error_continue/d") : 1;
  16. $save_change = isset($_REQUEST['save_change']) ? I("save_change/d") : 1 ;
  17. $date_time = date("Y-m-d H:i:s");
  18. if($id){
  19. $res = D("RunapiFlow")->where(" id = '{$id}' ")->find();
  20. if(!$this->checkItemEdit($login_user['uid'] , $res['item_id'])){
  21. $this->sendError(10303);
  22. return ;
  23. }
  24. $data = array() ;
  25. $data['last_update_time'] = $date_time ;
  26. if($flow_name){
  27. $data['flow_name'] = $flow_name ;
  28. }
  29. if(isset($_REQUEST['env_id'])){
  30. $data['env_id'] = $env_id ;
  31. }
  32. if(isset($_REQUEST['times'])){
  33. $data['times'] = $times ;
  34. }
  35. if(isset($_REQUEST['time_interval'])){
  36. $data['time_interval'] = $time_interval ;
  37. }
  38. if(isset($_REQUEST['error_continue'])){
  39. $data['error_continue'] = $error_continue ;
  40. }
  41. if(isset($_REQUEST['save_change'])){
  42. $data['save_change'] = $save_change ;
  43. }
  44. D("RunapiFlow")->where(" id = '{$id}' ")->save($data);
  45. }else{
  46. if(!$this->checkItemEdit($login_user['uid'] , $item_id)){
  47. $this->sendError(10303);
  48. return ;
  49. }
  50. $data = array() ;
  51. $data['username'] = $login_user['username'] ;
  52. $data['uid'] = $login_user['uid'] ;
  53. $data['flow_name'] = $flow_name ;
  54. $data['env_id'] = $env_id ;
  55. $data['item_id'] = $item_id ;
  56. $data['times'] = $times ;
  57. $data['time_interval'] = $time_interval ;
  58. $data['error_continue'] = $error_continue ;
  59. $data['save_change'] = $save_change ;
  60. $data['addtime'] = $date_time ;
  61. $data['last_update_time'] = $date_time ;
  62. // 如果环境小于等于0,尝试获取项目的第一个环境变量赋值
  63. if($env_id <= 0){
  64. $res = D("RunapiEnv")->where(" item_id = '{$item_id}' ")->find() ;
  65. if($res && $res['id']){
  66. $data['env_id'] = $res['id'] ;
  67. }
  68. }
  69. $id = D("RunapiFlow")->add($data);
  70. }
  71. usleep(300000);
  72. $res = D("RunapiFlow")->where(" id = '{$id}' ")->find();
  73. $this->sendResult($res);
  74. }
  75. //获取列表
  76. public function getList(){
  77. $login_user = $this->checkLogin();
  78. $item_id = I("item_id/d");
  79. if(!$this->checkItemEdit($login_user['uid'] , $item_id)){
  80. $this->sendError(10303);
  81. return ;
  82. }
  83. $ret = D("RunapiFlow")->where(" item_id = '{$item_id}' ")->order(" id desc ")->select();
  84. if ($ret) {
  85. $this->sendResult($ret);
  86. }else{
  87. $this->sendResult(array());
  88. }
  89. }
  90. //删除
  91. public function delete(){
  92. $id = I("id/d")? I("id/d") : 0;
  93. $login_user = $this->checkLogin();
  94. $res = D("RunapiFlow")->where(" id = '{$id}' ")->find();
  95. if(!$this->checkItemEdit($login_user['uid'] , $res['item_id'])){
  96. $this->sendError(10303);
  97. return ;
  98. }
  99. $ret = D("RunapiFlow")->where(" id = '$id'")->delete();
  100. if ($ret) {
  101. $this->sendResult($ret);
  102. }else{
  103. $return['error_code'] = 10103 ;
  104. $return['error_message'] = 'request fail' ;
  105. $this->sendResult($return);
  106. }
  107. }
  108. // 新增接口到flow中
  109. public function addFlowPage(){
  110. $login_user = $this->checkLogin();
  111. $flow_id = I("flow_id/d");
  112. $page_id = I("page_id/d");
  113. $flow_res = D("RunapiFlow")->where(" id = '{$flow_id}' ")->find();
  114. if(!$this->checkItemEdit($login_user['uid'] , $flow_res['item_id'])){
  115. $this->sendError(10303);
  116. return ;
  117. }
  118. $page_res = $page = M("Page")->where(" page_id = '$page_id' ")->find();
  119. if(!$this->checkItemEdit($login_user['uid'] , $page_res['item_id'])){
  120. $this->sendError(10303);
  121. return ;
  122. }
  123. // 获取该flow的最后一个页面的顺序号
  124. $s_number1 = D("RunapiFlowPage")->where("flow_id = '{$flow_id}'")->order("s_number desc")->getField("s_number");
  125. $s_number = $s_number1 + 1 ;
  126. $id = D("RunapiFlowPage")->add(array(
  127. "flow_id" => $flow_id ,
  128. "page_id" => $page_id ,
  129. "s_number" => $s_number ,
  130. "addtime" => date("Y-m-d H:i:s") ,
  131. ));
  132. if($id){
  133. $this->sendResult($id);
  134. }else{
  135. $this->sendError(10101);
  136. }
  137. }
  138. // 从flow中删除接口
  139. public function deleteFlowPage(){
  140. $login_user = $this->checkLogin();
  141. $id = I("id/d");
  142. $flow_page_res = D("RunapiFlowPage")->where(" id = '{$id}' ")->find();
  143. $page_id = $flow_page_res['page_id'] ;
  144. $page_res = $page = M("Page")->where(" page_id = '$page_id' ")->find();
  145. if(!$this->checkItemEdit($login_user['uid'] , $page_res['item_id'])){
  146. $this->sendError(10303);
  147. return ;
  148. }
  149. $res = D("RunapiFlowPage")->where(" id = '{$id}' ")->delete();
  150. if($res){
  151. $this->sendResult($res);
  152. }else{
  153. $this->sendError(10101);
  154. }
  155. }
  156. // 获取某个流程里的接口列表
  157. public function getFlowPageList(){
  158. $login_user = $this->checkLogin();
  159. $flow_id = I("flow_id/d");
  160. $flow_res = D("RunapiFlow")->where(" id = '{$flow_id}' ")->find();
  161. if(!$this->checkItemEdit($login_user['uid'] , $flow_res['item_id'])){
  162. $this->sendError(10303);
  163. return ;
  164. }
  165. $res = D("RunapiFlowPage")->where(array(
  166. "flow_id" => $flow_id ,
  167. ))->order("s_number asc ")->select();
  168. if($res){
  169. foreach ($res as $key => $value) {
  170. $res[$key]['page_title'] = $this->_get_page_title($flow_res['item_id'],$value['page_id']);
  171. }
  172. $this->sendResult($res);
  173. }else{
  174. $this->sendResult(array());
  175. }
  176. }
  177. private function _get_page_title($item_id,$page_id){
  178. if(!$this->pages){
  179. $ret = D("Page")->where(" item_id = '%d' " , array($item_id))->select();
  180. if($ret){
  181. $this->pages = $ret ;
  182. }else{
  183. return false ;
  184. }
  185. }
  186. foreach ( $this->pages as $key => $value) {
  187. if($value['page_id'] == $page_id){
  188. return $value['page_title'] ;
  189. }
  190. }
  191. return false ;
  192. }
  193. // 保存顺序关系
  194. public function saveSort(){
  195. $login_user = $this->checkLogin();
  196. $flow_id = I("flow_id/d");
  197. $orders = I("orders");
  198. $res = D("RunapiFlow")->where(" id = '{$flow_id}' ")->find();
  199. if(!$this->checkItemEdit($login_user['uid'] , $res['item_id'])){
  200. $this->sendError(10303);
  201. return ;
  202. }
  203. $data_array = json_decode(htmlspecialchars_decode($orders) , true) ;
  204. if($data_array){
  205. foreach ($data_array as $key => $value) {
  206. if($value['id']){
  207. D("RunapiFlowPage")->where(" flow_id = '%d' and id = '%d' ",array($flow_id , $value['id']))->save(array(
  208. "s_number"=>$value['s_number']
  209. ));
  210. }
  211. }
  212. }
  213. $this->sendResult(array());
  214. }
  215. // 保存启用关系
  216. public function setFlowPageEnabled(){
  217. $login_user = $this->checkLogin();
  218. $flow_id = I("flow_id/d");
  219. $ids = I("ids");
  220. $res = D("RunapiFlow")->where(" id = '{$flow_id}' ")->find();
  221. if(!$this->checkItemEdit($login_user['uid'] , $res['item_id'])){
  222. $this->sendError(10303);
  223. return ;
  224. }
  225. $data_array = json_decode(htmlspecialchars_decode($ids) , true) ;
  226. if($data_array){
  227. D("RunapiFlowPage")->where(" flow_id = '%d'",array($flow_id))->save(array(
  228. "enabled"=>0
  229. ));
  230. foreach ($data_array as $key => $value) {
  231. if($value){
  232. D("RunapiFlowPage")->where(" flow_id = '%d' and id = '%d' ",array($flow_id , $value))->save(array(
  233. "enabled"=>1
  234. ));
  235. }
  236. }
  237. }
  238. $this->sendResult(array());
  239. }
  240. }