authorization.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <div id="authorization" class="plg-tab-container">
  2. <div class="layui-carousel" id="authorization-carousel1">
  3. <div carousel-item>
  4. <div id="authorization-grid-1" style="min-height:500px;"></div>
  5. <div style="min-height:500px;">
  6. <blockquote class="layui-elem-quote layui-text plg-elem-quote" style="background:#fff;">
  7. 给客户端<span id="clientId" class="plg-mark"></span>授权<a href="javascript:void(0)" style="margin-left: 15px;" id="authorization_cancel">关闭</a>
  8. </blockquote>
  9. <blockquote class="layui-elem-quote layui-text plg-elem-quote plg-authorization-style" style="background:#fff;">
  10. <div class="layui-form-item" style="margin-bottom: 0px;">
  11. <label class="layui-form-label" style="width:60px;padding:9px 0px;">授权类型</label>
  12. <!-- <div class="layui-form plg-authorization-list"> -->
  13. <form class="layui-form layui-input-block" id="plg-authorization-list" lay-filter="authorization" style="margin-left: 80px">
  14. <div class="layui-input-block layui-form">
  15. <input type="checkbox" name="client_credentials" lay-skin="primary" title="客户端模式">
  16. <div data-id="0406eec9-4c7c-4655-99ae-79d9d7813d3a" data-name="client_credentials" class="layui-unselect layui-form-checkbox layui-form-checked" lay-skin="primary"><span>客户端模式</span><i class="layui-icon layui-icon-ok"></i></div>
  17. <input type="checkbox" name="password" lay-skin="primary" title="密码模式" >
  18. <div data-id="0406eec9-4c7c-4655-99ae-79d9d7813d3a" data-name="password" class="layui-unselect layui-form-checkbox " lay-skin="primary"><span>密码模式</span><i class="layui-icon layui-icon-ok"></i></div>
  19. <input type="checkbox" name="authorization_code" lay-skin="primary" title="授权码模式" >
  20. <div data-id="0406eec9-4c7c-4655-99ae-79d9d7813d3a" data-name="authorization_code" class="layui-unselect layui-form-checkbox " lay-skin="primary"><span>授权码模式</span><i class="layui-icon layui-icon-ok"></i></div>
  21. <input type="checkbox" name="implicit" lay-skin="primary" title="授权码简化模式" >
  22. <div data-id="0406eec9-4c7c-4655-99ae-79d9d7813d3a" data-name="implicit" class="layui-unselect layui-form-checkbox layui-form-checked" lay-skin="primary"><span>授权码简化模式</span><i class="layui-icon layui-icon-ok"></i></div>
  23. </div>
  24. </form>
  25. </div>
  26. </blockquote>
  27. <div class="layui-row layui-col-space5" id="plg-main-zone">
  28. <div class="layui-col-xs6">
  29. <div class="layui-card">
  30. <div class="layui-card-header">权限组</div>
  31. <div class="layui-card-body" style="height:500px;">
  32. <div id="plg-authorization-group" class="layui-form"></div>
  33. </div>
  34. </div>
  35. </div>
  36. <div class="layui-col-xs6">
  37. <div class="layui-card">
  38. <div class="layui-card-header">权限</div>
  39. <div class="layui-card-body" style="height:500px;">
  40. <div id="plg-authorization" class="layui-form"></div>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. <script type="text/javascript">
  50. (function () {
  51. // cSpell:ignore layui api authmgr augrid microservice multiselect newcolumn prologui fdata cindex layero
  52. var BASE_URL = '/api/service-authorization/';
  53. var form = layui.form;
  54. var opts = {
  55. columns: [{
  56. id: "id",
  57. name: "id",
  58. type: 'txt',
  59. width: "50",
  60. hidden: true
  61. },
  62. {
  63. id:"op",
  64. name:"操作",
  65. type:"button",
  66. width:"80",
  67. align:"center",
  68. render:function(){return "授权^authorization_grant";}
  69. },
  70. {
  71. id: "clientId",
  72. name: "客户端",
  73. type: 'txt',
  74. width: "150"
  75. },
  76. {
  77. id: "clientSecret",
  78. name: "密码",
  79. type: 'txt',
  80. width: "150"
  81. },
  82. {
  83. id: "authorizedGrantTypes",
  84. name: "授权类型",
  85. type: 'txt',
  86. width: "100"
  87. },
  88. {
  89. id: "scopes",
  90. name: "作用域",
  91. type: 'txt',
  92. align:'center',
  93. width: "120"
  94. },
  95. {
  96. id: "resourceIds",
  97. name: "资源",
  98. type: 'txt',
  99. width: "*"
  100. },
  101. {
  102. id: "authorities",
  103. name: "权限",
  104. type: 'txt',
  105. hidden: true
  106. },
  107. {
  108. id: "authorityGroups",
  109. name: "权限组",
  110. type: 'txt',
  111. hidden: true
  112. }
  113. ],
  114. multiselect: false,
  115. toolbar: [ {
  116. type: "newcolumn"
  117. },
  118. {
  119. type: "input",
  120. name: 'clientId',
  121. label: '客户端:',
  122. width: 300,
  123. placeholder: '请输入客户端名称'
  124. },
  125. {
  126. type: "newcolumn"
  127. },
  128. {
  129. type: "input",
  130. name: "authorities",
  131. label: "权限:",
  132. width: 300,
  133. placeholder: '请输入权限名称',
  134. offsetLeft: 10
  135. },
  136. {
  137. type: "newcolumn"
  138. },
  139. {
  140. type: "button",
  141. name: "searchBtn",
  142. className: "",
  143. value: "查询"
  144. },
  145. {
  146. type: "newcolumn"
  147. },
  148. {
  149. type: "button",
  150. name: "all",
  151. className: "normal",
  152. value: "所有"
  153. },
  154. {
  155. type: "newcolumn"
  156. }
  157. ],
  158. imagePath: "../../prologui/assets/PlgGrid",
  159. url: BASE_URL + 'authmgr/like', //数据接口
  160. type: "get", //数据提交方式,默认为get
  161. params: {}, //查询提交参数,分页参数默认为pageNum,pageSize
  162. page: true, //是否启用分页
  163. pageNum: "pageNo",
  164. totalCount: "totalCount" //总记录数字段,默认totalCount
  165. };
  166. var augrid = new PlgGrid(opts);
  167. augrid.renderTo("authorization-grid-1");
  168. augrid.loadData();
  169. var carousel = layui.carousel;
  170. var car = carousel.render({
  171. elem: '#authorization-carousel1',
  172. autoplay: false,
  173. index: 0,
  174. arrow: 'none',
  175. indicator: 'none',
  176. anim: "updown",
  177. width: '100%', //设置容器宽度
  178. height: '100%'
  179. });
  180. // 用户点击授权中的
  181. var tt = "add";
  182. function changePanel() {
  183. car.slide(tt);
  184. tt = tt == "sub" ? "add" : "sub";
  185. }
  186. $("#authorization-carousel1").on("click", "#authorization_cancel", function () {
  187. // 点击关闭的时候需要重新刷新页面,在其他页面可能有操作,数据变动了
  188. augrid.reload();
  189. changePanel()
  190. });
  191. var operationGroup = {
  192. searchBtn: function () {
  193. var fdata = augrid.getToolBarForm().getFormData();
  194. augrid.setParams(fdata);
  195. augrid.reload();
  196. },
  197. all: function () {
  198. augrid.getToolBarForm().clear();
  199. augrid.setParams({});
  200. augrid.reload();
  201. },
  202. grant: function (id,currentData) {
  203. // //console.log('授权之前要先选定信息');
  204. authorizationInit(currentData);
  205. authorizationTypeInit(currentData)
  206. form.render();
  207. changePanel(); // 切换
  208. }
  209. };
  210. augrid.attachToolBarEvent("onButtonClick", function (name) {
  211. if (name && operationGroup.hasOwnProperty(name)) {
  212. operationGroup[name]();
  213. }
  214. });
  215. window.authorization_grant = operationGroup["grant"];
  216. /*
  217. 给用户授权初始化信息, 主要是渲染【权限组】【权限】的两个模块
  218. */
  219. function authorizationInit(data) {
  220. //console.log('进入到页面的初始化中来.....');
  221. var groupConfig = {
  222. //title:"资源管理",
  223. columns: [{
  224. id: "id",
  225. name: "id",
  226. type: 'ro',
  227. width: "50",
  228. hidden: true
  229. },
  230. {
  231. id: "op",
  232. name: "操作",
  233. align:"center",
  234. width: "80",
  235. type: "ro",
  236. render: function (v, record) {
  237. var checked = data["authorityGroups"].indexOf(record["name"]) > -1 ? 'checked=""' : '';
  238. return '<input type="checkbox" lay-filter="operatingAuthorizationGroup" ' + checked + ' value="' + record["name"] + '" lay-skin="switch" lay-text="ON|OFF">';
  239. }
  240. },
  241. {
  242. id: "name",
  243. name: "权限组名称",
  244. type: 'ed',
  245. width: "120"
  246. },
  247. {
  248. id: "description",
  249. name: "备注",
  250. type: 'ed',
  251. width: "250"
  252. }
  253. ],
  254. multiselect: false,
  255. toolbar: [{
  256. type: "settings",
  257. labelAlign: 'left',
  258. labelWidth: 65
  259. },
  260. {
  261. type: "block",
  262. id: "bk",
  263. blockOffset: 0,
  264. list: [
  265. {
  266. type: "input",
  267. name: "name",
  268. label: "权限组名称:",
  269. offsetLeft: 10,
  270. placeholder: '请输入权限组名称',
  271. width: 200
  272. }, {
  273. type: "newcolumn"
  274. },
  275. {
  276. type: "button",
  277. name: "searchBtn",
  278. value: "查询"
  279. }, {
  280. type: "newcolumn"
  281. }
  282. ]
  283. }
  284. ],
  285. imagePath: "../../prologui/assets/PlgGrid",
  286. url: "/api/service-authorization/service-resource/resourcegroup", //数据接口
  287. type: "GET", //数据提交方式,默认为get
  288. //filters:["#text_filter", "#select_filter","#numeric_filter","#text_filter","#text_filter"],
  289. pagebar: {
  290. type: 'full' //full simple
  291. },
  292. params: {
  293. pageSize: 20,
  294. pageNum: 1
  295. }, //查询提交参数,分页参数默认为pageNum,pageSize
  296. page: true, //是否启用分页
  297. pageNum: "pageNum", //设置分页参数页码key值,默认为pageNum
  298. totalCount: "totalCount" //总记录数字段,默认totalCount
  299. };
  300. var augridGroup = new PlgGrid(groupConfig);
  301. augridGroup.renderTo("plg-authorization-group");
  302. augridGroup.loadData(null, function(){
  303. form.render();
  304. });
  305. /*
  306. 操作权限
  307. */
  308. form.on('switch(operatingAuthorizationGroup)', function(data){
  309. // //console.log('进入操作权限组的阶段');
  310. // //console.log('op ....');
  311. // //console.log(data);
  312. // //console.log('op ....');
  313. var sid = augrid.getSelectedRowId(); // 此处的【id】应该是当前用户的选择进来的id
  314. // 进入【添加权限】的阶段
  315. if(data.elem.checked){
  316. var loading = Prolog.loading2();
  317. Prolog.ajax({
  318. url: BASE_URL + 'authmgr/authoritygroup/' + sid,
  319. type: "POST",
  320. data: {"authorityGroup": data.value},
  321. success:function(da){
  322. loading();
  323. if(!da.success){
  324. PlgDialog.alert("操作失败,"+da.message);
  325. } else {
  326. // data.elem.checked = false;
  327. }
  328. },
  329. error:function(){
  330. // data.elem.checked = false;
  331. loading();
  332. }
  333. });
  334. }
  335. // 进入【删除权限组】的阶段
  336. if(!data.elem.checked){
  337. var loading = Prolog.loading2();
  338. Prolog.ajax({
  339. url: BASE_URL + 'authmgr/authoritygroup/' + sid,
  340. type: "POST",
  341. data: {"authorityGroup": data.value, _method: 'delete' },
  342. success:function(da){
  343. loading();
  344. if(!da.success){
  345. PlgDialog.alert("操作失败,"+da.message);
  346. } else {
  347. }
  348. },
  349. error:function(){
  350. loading();
  351. }
  352. });
  353. }
  354. });
  355. augridGroup.attachToolBarEvent("onButtonClick", function (name) {
  356. if (name == "searchBtn") {
  357. var fdata = augridGroup.getToolBarForm().getFormData();
  358. augridGroup.setParams(fdata);
  359. augridGroup.reload();
  360. }
  361. });
  362. var cellConfig = {
  363. //title:"资源管理",
  364. columns: [{
  365. id: "id",
  366. name: "id",
  367. type: 'ro',
  368. width: "50",
  369. hidden: true
  370. },
  371. {
  372. id: "op",
  373. name: "操作",
  374. width: "80",
  375. align:"center",
  376. type: "ro",
  377. render: function (v, record) {
  378. var checked = '';
  379. if(data["authorities"]){
  380. var checked = data["authorities"].indexOf(record["serviceName"] + ":" + record["authNumber"]) >
  381. -1 ? 'checked=""' : '';
  382. }
  383. return '<input type="checkbox" lay-filter="operatingAuthorization" ' + checked + ' value="' + (record["serviceName"] +
  384. ":" + record["authNumber"]) + '" lay-skin="switch" lay-text="ON|OFF">';
  385. }
  386. },
  387. {
  388. id: "serviceName",
  389. name: "服务名",
  390. type: 'ed',
  391. width: "120"
  392. },
  393. {
  394. id: "authNumber",
  395. name: "权限编号",
  396. type: 'ed',
  397. width: "100"
  398. },
  399. {
  400. id: "notes",
  401. name: "备注",
  402. type: 'ed',
  403. width: "200"
  404. }
  405. ],
  406. multiselect: false,
  407. toolbar: [{
  408. type: "settings",
  409. labelAlign: 'left',
  410. labelWidth: 65
  411. },
  412. {
  413. type: "block",
  414. id: "bk",
  415. blockOffset: 0,
  416. list: [
  417. {
  418. type: "input",
  419. name: "serviceName",
  420. label: "服务名:",
  421. offsetLeft: 10,
  422. placeholder: '服务名',
  423. width: 200
  424. }, {
  425. type: "newcolumn"
  426. },{
  427. type: "input",
  428. name: "authNumber",
  429. label: "权限编号:",
  430. offsetLeft: 10,
  431. placeholder: '请输入权限编号',
  432. width: 200
  433. }, {
  434. type: "newcolumn"
  435. },
  436. {
  437. type: "button",
  438. name: "searchBtn",
  439. value: "查询"
  440. }, {
  441. type: "newcolumn"
  442. }
  443. ]
  444. }
  445. ],
  446. imagePath: "../../prologui/assets/PlgGrid",
  447. url: "/api/service-resource/resource", //数据接口
  448. type: "get", //数据提交方式,默认为get
  449. pagebar: {
  450. type: 'full' //full simple min
  451. },
  452. params: {
  453. pageSize: 10,
  454. pageNo: 1
  455. }, //查询提交参数,分页参数默认为pageNum,pageSize
  456. page: true, //是否启用分页
  457. pageNum: "pageNo", //设置分页参数页码key值,默认为pageNum
  458. totalCount: "totalCount" //总记录数字段,默认totalCount
  459. };
  460. var augridCell = new PlgGrid(cellConfig);
  461. augridCell.renderTo("plg-authorization");
  462. augridCell.loadData(null, function(){
  463. form.render();
  464. });
  465. augridCell.attachToolBarEvent("onButtonClick", function (name) {
  466. if (name == "searchBtn") {
  467. var fdata = augridCell.getToolBarForm().getFormData();
  468. augridCell.setParams(fdata);
  469. augridCell.reload();
  470. }
  471. });
  472. /*
  473. 操作权限
  474. */
  475. form.on('switch(operatingAuthorization)', function(data){
  476. // //console.log('进入添加权限操作');
  477. // //console.log('op ....');
  478. // //console.log(data);
  479. // //console.log(data.value);
  480. // //console.log('op ....');
  481. // 此处获取的id应该是当前选中的id进来的
  482. var sid = augrid.getSelectedRowId();
  483. // //console.log('sid::' + sid);
  484. // //console.log('data.elem.checked::' + data.elem.checked);
  485. // 进入【添加权限】的阶段
  486. if(data.elem.checked){
  487. var loading = Prolog.loading2();
  488. Prolog.ajax({
  489. url: BASE_URL + '/authmgr/authority/'+ sid,
  490. type: "POST",
  491. data: {"authority": data.value},
  492. success:function(da){
  493. loading();
  494. if(!da.success){
  495. PlgDialog.alert("操作失败,"+da.message);
  496. }
  497. },
  498. error:function(){
  499. loading();
  500. }
  501. });
  502. }
  503. // 进入【删除权限】的阶段
  504. if(!data.elem.checked){
  505. var loading = PlgDialog.loading2();
  506. Prolog.ajax({
  507. url: BASE_URL + '/authmgr/authority/'+ sid,
  508. type:"POST",
  509. data:{"authority": data.value, _method: 'delete'},
  510. success:function(da){
  511. loading();
  512. if(!da.success){
  513. PlgDialog.alert("操作失败,"+da.message);
  514. }
  515. },
  516. error:function(){
  517. loading();
  518. }
  519. });
  520. }
  521. });
  522. }
  523. /*
  524. 给用户授权类型初始化信息
  525. */
  526. function authorizationTypeInit(data) {
  527. // 显示用户信息
  528. $('#clientId').html(data.clientId);
  529. // 渲染授权类型
  530. var authorizationType = [
  531. {
  532. text: '客户端模式',
  533. name: 'client_credentials'
  534. },
  535. {
  536. text: '密码模式',
  537. name: 'password'
  538. },
  539. {
  540. text: '授权码模式',
  541. name: 'authorization_code'
  542. },
  543. {
  544. text: '授权码简化模式',
  545. name: 'implicit'
  546. }
  547. ];
  548. var authorizedGrantTypeArr =[];
  549. if(data.authorizedGrantTypes){
  550. authorizedGrantTypeArr = data.authorizedGrantTypes.split(',');
  551. }
  552. authorizationType = authorizationType.map(function (val) {
  553. if (authorizedGrantTypeArr.indexOf(val.name) > -1) {
  554. val.checked = true;
  555. } else {
  556. val.checked = false;
  557. }
  558. return val;
  559. });
  560. var temStr = '',
  561. formConfig = {}; // layui_form,初始化,配置参数
  562. authorizationType.map(function (val) {
  563. temStr +=
  564. `
  565. <input type="checkbox" name="${ val.name }" lay-skin="primary" title="${ val.text }" >
  566. <div class="layui-unselect layui-form-checkbox" lay-skin="primary"><span>${ val.text }</span><i class="layui-icon layui-icon-ok"></i></div>
  567. `;
  568. formConfig[val.name] = val.checked;
  569. })
  570. $('#plg-authorization-list').html(temStr);
  571. //表单初始赋值
  572. form.val('authorization', formConfig);
  573. // 监听用户选择授权类型的事件
  574. $('.plg-authorization-style').on('click', '.layui-form-checkbox', function () {
  575. var self = $(this);
  576. var currentCell = self.find('span').text();
  577. authorizationType.forEach(function(val){
  578. if(currentCell === val.text){
  579. if(val.checked){
  580. val.checked = false;
  581. updateauthorizationType();
  582. } else {
  583. val.checked = true;
  584. // //console.log('当前元素是未选中的,需要做添加操作');
  585. updateauthorizationType();
  586. }
  587. }
  588. });
  589. // 更新用户的授权类型
  590. function updateauthorizationType(){
  591. // //console.log('更新用户的授权类型');
  592. var temArr = [];
  593. authorizationType.forEach(function(val){
  594. if(val.checked){
  595. temArr.push(val.name);
  596. }
  597. });
  598. data.authorizedGrantTypes = temArr.join(',');
  599. var obj = data, // 请求更新API的数据
  600. loading = Prolog.loading2();
  601. Prolog.ajax({
  602. url: BASE_URL + 'authmgr/update',
  603. type: 'post',
  604. data: obj,
  605. dataType: 'json',
  606. success: function (data) {
  607. loading();
  608. if (data.success) {
  609. layer.closeAll();
  610. } else {
  611. PlgDialog.alert(data.message);
  612. }
  613. }
  614. });
  615. }
  616. });
  617. }
  618. }())
  619. </script>