api.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. var app = angular.module('sentinelDashboardApp');
  2. app.controller('GatewayApiCtl', ['$scope', '$stateParams', 'GatewayApiService', 'ngDialog', 'MachineService',
  3. function ($scope, $stateParams, GatewayApiService, ngDialog, MachineService) {
  4. $scope.app = $stateParams.app;
  5. $scope.apisPageConfig = {
  6. pageSize: 10,
  7. currentPageIndex: 1,
  8. totalPage: 1,
  9. totalCount: 0,
  10. };
  11. $scope.macsInputConfig = {
  12. searchField: ['text', 'value'],
  13. persist: true,
  14. create: false,
  15. maxItems: 1,
  16. render: {
  17. item: function (data, escape) {
  18. return '<div>' + escape(data.text) + '</div>';
  19. }
  20. },
  21. onChange: function (value, oldValue) {
  22. $scope.macInputModel = value;
  23. }
  24. };
  25. getApis();
  26. function getApis() {
  27. if (!$scope.macInputModel) {
  28. return;
  29. }
  30. var mac = $scope.macInputModel.split(':');
  31. GatewayApiService.queryApis($scope.app, mac[0], mac[1]).success(
  32. function (data) {
  33. if (data.code == 0 && data.data) {
  34. // To merge rows for api who has more than one predicateItems, here we build data manually
  35. $scope.apis = [];
  36. data.data.forEach(function(api) {
  37. api["predicateItems"].forEach(function (item, index) {
  38. var newItem = {};
  39. newItem["id"] = api["id"];
  40. newItem["app"] = api["app"];
  41. newItem["ip"] = api["ip"];
  42. newItem["port"] = api["port"];
  43. newItem["apiName"] = api["apiName"];
  44. newItem["pattern"] = item["pattern"];
  45. newItem["matchStrategy"] = item["matchStrategy"];
  46. // The itemSize indicates how many rows to merge, by using rowspan="{{api.itemSize}}" in <td> tag
  47. newItem["itemSize"] = api["predicateItems"].length;
  48. // Mark the flag of first item to zero, indicates the start row to merge
  49. newItem["firstFlag"] = index == 0 ? 0 : 1;
  50. // Still hold the data of predicateItems, in order to bind data in edit dialog html
  51. newItem["predicateItems"] = api["predicateItems"];
  52. $scope.apis.push(newItem);
  53. });
  54. });
  55. $scope.apisPageConfig.totalCount = data.data.length;
  56. } else {
  57. $scope.apis = [];
  58. $scope.apisPageConfig.totalCount = 0;
  59. }
  60. });
  61. };
  62. $scope.getApis = getApis;
  63. var gatewayApiDialog;
  64. $scope.editApi = function (api) {
  65. $scope.currentApi = angular.copy(api);
  66. $scope.gatewayApiDialog = {
  67. title: '编辑自定义 API',
  68. type: 'edit',
  69. confirmBtnText: '保存'
  70. };
  71. gatewayApiDialog = ngDialog.open({
  72. template: '/app/views/dialog/gateway/api-dialog.html',
  73. width: 900,
  74. overlay: true,
  75. scope: $scope
  76. });
  77. };
  78. $scope.addNewApi = function () {
  79. var mac = $scope.macInputModel.split(':');
  80. $scope.currentApi = {
  81. grade: 0,
  82. app: $scope.app,
  83. ip: mac[0],
  84. port: mac[1],
  85. predicateItems: [{matchStrategy: 0, pattern: ''}]
  86. };
  87. $scope.gatewayApiDialog = {
  88. title: '新增自定义 API',
  89. type: 'add',
  90. confirmBtnText: '新增'
  91. };
  92. gatewayApiDialog = ngDialog.open({
  93. template: '/app/views/dialog/gateway/api-dialog.html',
  94. width: 900,
  95. overlay: true,
  96. scope: $scope
  97. });
  98. };
  99. $scope.saveApi = function () {
  100. var apiNames = [];
  101. if ($scope.gatewayApiDialog.type === 'add') {
  102. apiNames = $scope.apis.map(function (item, index, array) {
  103. return item["apiName"];
  104. }).filter(function (item, index, array) {
  105. return array.indexOf(item) === index;
  106. });
  107. }
  108. if (!GatewayApiService.checkApiValid($scope.currentApi, apiNames)) {
  109. return;
  110. }
  111. if ($scope.gatewayApiDialog.type === 'add') {
  112. addNewApi($scope.currentApi);
  113. } else if ($scope.gatewayApiDialog.type === 'edit') {
  114. saveApi($scope.currentApi, true);
  115. }
  116. };
  117. function addNewApi(api) {
  118. GatewayApiService.newApi(api).success(function (data) {
  119. if (data.code == 0) {
  120. getApis();
  121. gatewayApiDialog.close();
  122. } else {
  123. alert('新增自定义API失败!' + data.msg);
  124. }
  125. });
  126. };
  127. function saveApi(api, edit) {
  128. GatewayApiService.saveApi(api).success(function (data) {
  129. if (data.code == 0) {
  130. getApis();
  131. if (edit) {
  132. gatewayApiDialog.close();
  133. } else {
  134. confirmDialog.close();
  135. }
  136. } else {
  137. alert('修改自定义API失败!' + data.msg);
  138. }
  139. });
  140. };
  141. var confirmDialog;
  142. $scope.deleteApi = function (api) {
  143. $scope.currentApi = api;
  144. $scope.confirmDialog = {
  145. title: '删除自定义API',
  146. type: 'delete_api',
  147. attentionTitle: '请确认是否删除如下自定义API',
  148. attention: 'API名称: ' + api.apiName,
  149. confirmBtnText: '删除',
  150. };
  151. confirmDialog = ngDialog.open({
  152. template: '/app/views/dialog/confirm-dialog.html',
  153. scope: $scope,
  154. overlay: true
  155. });
  156. };
  157. $scope.confirm = function () {
  158. if ($scope.confirmDialog.type == 'delete_api') {
  159. deleteApi($scope.currentApi);
  160. } else {
  161. console.error('error');
  162. }
  163. };
  164. function deleteApi(api) {
  165. GatewayApiService.deleteApi(api).success(function (data) {
  166. if (data.code == 0) {
  167. getApis();
  168. confirmDialog.close();
  169. } else {
  170. alert('删除自定义API失败!' + data.msg);
  171. }
  172. });
  173. };
  174. $scope.addNewMatchPattern = function() {
  175. var total;
  176. if ($scope.currentApi.predicateItems == null) {
  177. $scope.currentApi.predicateItems = [];
  178. total = 0;
  179. } else {
  180. total = $scope.currentApi.predicateItems.length;
  181. }
  182. $scope.currentApi.predicateItems.splice(total + 1, 0, {matchStrategy: 0, pattern: ''});
  183. };
  184. $scope.removeMatchPattern = function($index) {
  185. if ($scope.currentApi.predicateItems.length <= 1) {
  186. // Should never happen since no remove button will display when only one predicateItem.
  187. alert('至少有一个匹配规则');
  188. return;
  189. }
  190. $scope.currentApi.predicateItems.splice($index, 1);
  191. };
  192. queryAppMachines();
  193. function queryAppMachines() {
  194. MachineService.getAppMachines($scope.app).success(
  195. function (data) {
  196. if (data.code == 0) {
  197. // $scope.machines = data.data;
  198. if (data.data) {
  199. $scope.machines = [];
  200. $scope.macsInputOptions = [];
  201. data.data.forEach(function (item) {
  202. if (item.healthy) {
  203. $scope.macsInputOptions.push({
  204. text: item.ip + ':' + item.port,
  205. value: item.ip + ':' + item.port
  206. });
  207. }
  208. });
  209. }
  210. if ($scope.macsInputOptions.length > 0) {
  211. $scope.macInputModel = $scope.macsInputOptions[0].value;
  212. }
  213. } else {
  214. $scope.macsInputOptions = [];
  215. }
  216. }
  217. );
  218. };
  219. $scope.$watch('macInputModel', function () {
  220. if ($scope.macInputModel) {
  221. getApis();
  222. }
  223. });
  224. }]
  225. );