index.js 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. var CloudAdmin = CloudAdmin || {};
  2. CloudAdmin.router = function () {
  3. this.currentUrl = '';
  4. this.printer = {
  5. pageNotFound: function () {
  6. let body = document.querySelector('body');
  7. mdui.JQ(body).empty();
  8. mdui.JQ(body).append(`
  9. <h1>404 Page Not Found :(</h1>
  10. <a href="#/index">返回首页</a>
  11. `);
  12. },
  13. /**
  14. * 内联样式初始化
  15. *
  16. * @param {any} next 初始化完成后执行的回调函数
  17. * @param {any} arg 回调函数参数
  18. */
  19. indexInit: function (next, arg) {
  20. this.permissionCheck.signStatus.call(this, function () {
  21. let body = document.querySelector('body');
  22. body.className = 'mdui-appbar-with-toolbar mdui-theme-primary-blue mdui-theme-accent-light-blue mdui-drawer-body-left mdui-loaded';
  23. mdui.JQ(body).empty();
  24. mdui.JQ(body).append(`
  25. <header class="mdui-appbar mdui-appbar-fixed">
  26. <div class="mdui-toolbar mdui-color-theme">
  27. <span class="mdui-btn mdui-btn-icon mdui-ripple mdui-ripple-white" mdui-drawer="{target: '#main-drawer', swipe: true}">
  28. <i class="mdui-icon material-icons">menu</i>
  29. </span>
  30. <a href="#/index" class="mdui-typo-headline mdui-hidden-xs">CloudSQL Admin Panel</a>
  31. <div class="mdui-toolbar-spacer"></div>
  32. <span class="mdui-btn mdui-btn-icon mdui-ripple mdui-ripple-white">
  33. <i class="mdui-icon material-icons">exit_to_app</i>
  34. </span>
  35. </div>
  36. </header>
  37. <div class="mdui-drawer mdui-drawer-open" id="main-drawer">
  38. <div class="mdui-list" mdui-collapse="{accordion: true}" style="margin-bottom: 76px;">
  39. <li class="mdui-list-item mdui-ripple">
  40. <i class="mdui-list-item-icon mdui-icon material-icons">view_list</i>
  41. <div class="mdui-list-item-content">数据库列表</div>
  42. </li>
  43. <li class="mdui-list-item mdui-ripple">
  44. <i class="mdui-list-item-icon mdui-icon material-icons">person</i>
  45. <div class="mdui-list-item-content">用户列表</div>
  46. </li>
  47. <li class="mdui-list-item mdui-ripple">
  48. <i class="mdui-list-item-icon mdui-icon material-icons">info</i>
  49. <div class="mdui-list-item-content">关于</div>
  50. </li>
  51. </div>
  52. </div>
  53. <div class="mdui-container main-container"></div>
  54. `);
  55. //左侧菜单栏
  56. let leftMenuButton = document.querySelector('span:nth-child(1).mdui-btn.mdui-btn-icon.mdui-ripple.mdui-ripple-white'),
  57. mainDrawer = new mdui.Drawer('#main-drawer');
  58. /* leftMenuButton.addEventListener('click', function () {
  59. mainDrawer.toggle();
  60. }, false); */
  61. this.handleEvent('click', {
  62. onElement: leftMenuButton,
  63. withCallback: function () {
  64. mainDrawer.toggle();
  65. }
  66. })
  67. let databaseListButton = document.querySelector('#main-drawer li:nth-child(1)');
  68. /* databaseListButton.addEventListener('click', function () {
  69. window.location.hash = '/index/databaseList';
  70. }, false); */
  71. this.handleEvent('click', {
  72. onElement: databaseListButton,
  73. withCallback: function () {
  74. window.location.hash = '/index/databaseList';
  75. }
  76. })
  77. let userListButton = document.querySelector('#main-drawer li:nth-child(2)');
  78. /* userListButton.addEventListener('click', function () {
  79. window.location.hash = '/index/userList';
  80. }, false); */
  81. this.handleEvent('click', {
  82. onElement: userListButton,
  83. withCallback: function () {
  84. window.location.hash = '/index/userList';
  85. }
  86. })
  87. //退出登录按钮
  88. let signoutButton = document.querySelector('span:nth-child(4).mdui-btn.mdui-btn-icon.mdui-ripple.mdui-ripple-white i.mdui-icon.material-icons');
  89. /* signoutButton.addEventListener('click', function () {
  90. this.ajax.get('http://admin.cloudsql.1473.cn/v1/signout', function (response) {
  91. if (response.status == 'successed') {
  92. mdui.snackbar({
  93. message: response.info
  94. });
  95. window.location.hash = response.url;
  96. } else {
  97. console.log(response.info);
  98. console.log(response.error);
  99. mdui.snackbar({
  100. message: response.info
  101. });
  102. }
  103. })
  104. }.bind(this), false); */
  105. this.handleEvent('click', {
  106. onElement: signoutButton,
  107. withCallback: function () {
  108. this.ajax.get('http://admin.cloudsql.1473.cn/v1/signout', function (response) {
  109. if (response.status == 'successed') {
  110. mdui.snackbar({
  111. message: response.info
  112. });
  113. window.location.hash = response.url;
  114. } else {
  115. console.log(response.info);
  116. console.log(response.error);
  117. mdui.snackbar({
  118. message: response.info
  119. });
  120. }
  121. });
  122. }
  123. }, this)
  124. next.call(this, arg);
  125. });
  126. },
  127. /**
  128. * 表格生成
  129. *
  130. * @param {any} data 表格数据
  131. * @returns DOM
  132. */
  133. tableGenerator: function (data) {
  134. //Generate tag tr
  135. let tr = document.createElement('tr');
  136. data.forEach(function (item, index, array) {
  137. let td = document.createElement('td');
  138. if (index + 1 === array.length) {
  139. item.forEach(function (_item, _index, _array) {
  140. td.appendChild(_item);
  141. tr.appendChild(td);
  142. }, this);
  143. } else {
  144. td.innerHTML = item;
  145. tr.appendChild(td);
  146. }
  147. }, this);
  148. return tr;
  149. },
  150. signin: function () {
  151. let html = document.querySelector('html'),
  152. body = document.querySelector('body');
  153. html.className = 'index';
  154. body.className = 'mdui-theme-primary-blue mdui-theme-accent-light-blue index';
  155. mdui.JQ(body).empty();
  156. mdui.JQ(body).append(`
  157. <header class="mdui-appbar mdui-shadow-0">
  158. <div class="mdui-toolbar mdui-color-theme">
  159. <a class="mdui-typo-headline" href="#">CloudSQL Admin Panel</a>
  160. <div class="mdui-toolbar-spacer"></div>
  161. <a class="mdui-btn mdui-ripple mdui-ripple-white" href="#/signin">登陆</a>
  162. <a class="mdui-btn mdui-ripple mdui-ripple-white" href="#/signup">注册</a>
  163. </div>
  164. </header>
  165. <main style="height: 85%;">
  166. <div class="mdui-container main-section">
  167. <h2 class="mdui-typo-display-1 main-section-tittle">用户登录</h2>
  168. <div class="mdui-container mdui-col-md-8 mdui-center main-sub-section">
  169. <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
  170. <i class="mdui-icon material-icons">person</i>
  171. <input class="mdui-textfield-input" name="userid" type="text" placeholder="用户名/邮箱" />
  172. </div>
  173. <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
  174. <i class="mdui-icon material-icons">lock</i>
  175. <input class="mdui-textfield-input" name="password" type="password" placeholder="密码" />
  176. </div>
  177. <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row captcha-row">
  178. <img src="http://admin.cloudsql.1473.cn/v1/captcha?` + new Date().getTime() + `">
  179. <input class="mdui-textfield-input" name="captcha" type="text" placeholder="验证码" />
  180. </div>
  181. <div class="mdui-col-md-6 mdui-center mdui-typo main-a-row">
  182. <a href="#/signup">还没有账户,点击注册</a>
  183. </div>
  184. <div class="mdui-col-md-6 mdui-center main-button-row">
  185. <button class="mdui-btn mdui-btn-block mdui-color-theme-accent">登陆</button>
  186. </div>
  187. </div>
  188. </div>
  189. </main>
  190. <footer>
  191. <div class="footer-copyright">Copyright © 2016 - 2017 <a href="/">CloudSQL</a> All rights reserved.</div>
  192. </footer>
  193. `)
  194. //验证码刷新逻辑
  195. let captchaImg = document.querySelector('.captcha-row img');
  196. captchaImg.addEventListener('click', function () {
  197. this.src = 'http://admin.cloudsql.1473.cn/v1/captcha?' + new Date().getTime(); //加入当前时间以避免缓存
  198. }, false);
  199. //登陆逻辑
  200. let submitButton = document.querySelector('.main-button-row button');
  201. submitButton.addEventListener('click', function () {
  202. let signInUrl = 'http://admin.cloudsql.1473.cn/v1/signin',
  203. userId = document.querySelector('input[name=userid]').value,
  204. password = document.querySelector('input[name=password]').value,
  205. captchaCode = document.querySelector('input[name=captcha]').value,
  206. postData = 'userId=' + userId + '&password=' + password + '&captcha=' + captchaCode;
  207. this.ajax.post(signInUrl, postData, function (response) {
  208. if (response.status == 'signedin') {
  209. window.location.hash = response.url;
  210. } else {
  211. mdui.snackbar({
  212. message: response.info
  213. });
  214. }
  215. })
  216. }.bind(this), false)
  217. },
  218. signup: function () {
  219. let html = document.querySelector('html'),
  220. body = document.querySelector('body');
  221. html.className = 'index';
  222. body.className = 'mdui-theme-primary-blue mdui-theme-accent-light-blue index';
  223. mdui.JQ(body).empty();
  224. mdui.JQ(body).append(`
  225. <header class="mdui-appbar mdui-shadow-0">
  226. <div class="mdui-toolbar mdui-color-theme">
  227. <a class="mdui-typo-headline" href="#">CloudSQL Admin Panel</a>
  228. <div class="mdui-toolbar-spacer"></div>
  229. <a class="mdui-btn mdui-ripple mdui-ripple-white" href="#/signin">登陆</a>
  230. <a class="mdui-btn mdui-ripple mdui-ripple-white" href='#/signup'>注册</a>
  231. </div>
  232. </header>
  233. <main style="height: 85%;">
  234. <div class="mdui-container main-section">
  235. <h2 class="mdui-typo-display-1 main-section-tittle">用户注册</h2>
  236. <div class="mdui-container mdui-col-md-8 mdui-center main-sub-section">
  237. <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
  238. <i class="mdui-icon material-icons">person</i>
  239. <input class="mdui-textfield-input" name="userid" type="text" placeholder="用户名/邮箱" />
  240. </div>
  241. <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
  242. <i class="mdui-icon material-icons">lock</i>
  243. <input class="mdui-textfield-input" name="password" type="password" placeholder="密码" />
  244. </div>
  245. <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
  246. <i class="mdui-icon material-icons">code</i>
  247. <input class="mdui-textfield-input" name="invitecode" type="text" placeholder="邀请码" />
  248. </div>
  249. <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row captcha-row">
  250. <img src="http://admin.cloudsql.1473.cn/v1/captcha?` + new Date().getTime() + `">
  251. <input class="mdui-textfield-input" name="captcha" type="text" placeholder="验证码" />
  252. </div>
  253. <div class="mdui-col-md-6 mdui-center mdui-typo main-a-row">
  254. <a href="#/signin">已有账户,点击登陆</a>
  255. </div>
  256. <div class="mdui-col-md-6 mdui-center main-button-row">
  257. <button class="mdui-btn mdui-btn-block mdui-color-theme-accent">注册</button>
  258. </div>
  259. </div>
  260. </div>
  261. </main>
  262. <footer>
  263. <div class="footer-copyright">Copyright © 2016 - 2017 <a href="/">CloudSQL</a> All rights reserved.</div>
  264. </footer>
  265. `)
  266. //验证码刷新逻辑
  267. let captchaImg = document.querySelector('.captcha-row img');
  268. captchaImg.addEventListener('click', function () {
  269. this.src = 'http://admin.cloudsql.1473.cn/v1/captcha' + '?' + new Date().getTime(); //加入当前时间以避免缓存
  270. }, false);
  271. let submitButton = document.querySelector('.main-button-row button');
  272. submitButton.addEventListener('click', function () {
  273. let signupUrl = 'http://admin.cloudsql.1473.cn/v1/signup',
  274. userId = document.querySelector('input[name=userid]').value,
  275. password = document.querySelector('input[name=password]').value,
  276. captchaCode = document.querySelector('input[name=captcha]').value,
  277. postData = 'userId=' + userId + '&password=' + password + '&captcha=' + captchaCode;
  278. this.ajax.post(signupUrl, postData, function (response) {
  279. if (response.status == 'successed') {
  280. mdui.snackbar({
  281. message: response.info
  282. });
  283. window.location.hash = response.url;
  284. } else {
  285. mdui.snackbar({
  286. message: response.info
  287. });
  288. }
  289. })
  290. }.bind(this), false)
  291. },
  292. index: function () {
  293. let contentAera = document.querySelector('.mdui-container.main-container'),
  294. getUserCountUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/getUserCount',
  295. getDatabaseCountUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/getDatabaseCount',
  296. content = `
  297. <div class="mdui-row-xs-1 mdui-row-sm-1 mdui-row-md-3 mdui-row-xl-3 mdui-m-t-3 main-status-row">
  298. <div class="mdui-col mdui-clearfix main-status-col">
  299. <div class="mdui-float-left icon-row">
  300. <i class="mdui-list-item-icon mdui-icon material-icons">person</i>
  301. </div>
  302. <div class="mdui-float-left info-row">
  303. <div class="name-col">用户数量</div>
  304. <div id="numberOfUser" class="value-col">1024</div>
  305. </div>
  306. </div>
  307. <div class="mdui-col mdui-clearfix main-status-col">
  308. <div class="mdui-float-left icon-row">
  309. <i class="mdui-list-item-icon mdui-icon material-icons">view_list</i>
  310. </div>
  311. <div class="mdui-float-left info-row">
  312. <div class="name-col">数据库数量</div>
  313. <div id="numberOfDatabse" class="value-col">1024</div>
  314. </div>
  315. </div>
  316. <div class="mdui-col mdui-clearfix main-status-col">
  317. <div class="mdui-float-left icon-row">
  318. <i class="mdui-list-item-icon mdui-icon material-icons">cloud</i>
  319. </div>
  320. <div class="mdui-float-left info-row">
  321. <div class="name-col">服务状态</div>
  322. <div class="value-col">正常</div>
  323. </div>
  324. </div>
  325. </div>
  326. `;
  327. mdui.JQ(contentAera).append(content);
  328. this.ajax.get(getUserCountUrl, function (response) {
  329. document.querySelector('#numberOfUser').innerText = response.data.results;
  330. });
  331. this.ajax.get(getDatabaseCountUrl, function (response) {
  332. document.querySelector('#numberOfDatabse').innerText = response.data.results;
  333. });
  334. },
  335. databaseList: function (arg) {
  336. let contentAera = document.querySelector('.mdui-container.main-container'),
  337. getDatabaseListUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList';
  338. if (!this.arguments.arg.argStr) {
  339. this.arguments.arg = {
  340. argStr: 'pageIndex=1&orderBy=databaseName',
  341. argObj: {
  342. 'pageIndex': 1,
  343. 'orderBy': 'databaseName',
  344. 'search': ''
  345. }
  346. }
  347. }
  348. getDatabaseListUrl += '?' + this.arguments.arg.argStr;
  349. mdui.JQ(contentAera).empty();
  350. mdui.JQ(contentAera).append(`
  351. <h2 class="mdui-text-color-theme">用户数据库列表</h2>
  352. <div class="mdui-col-xs-12 mdui-table-fluid">
  353. <div class="mdui-toolbar table-tools-bar">
  354. <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-menu="{target: '#tools-bar-attr'}">
  355. <i class="mdui-icon material-icons">menu</i>
  356. </a>
  357. <span class="mdui-typo-title">工具栏</span>
  358. <div class="mdui-toolbar-spacer"></div>
  359. <div class="mdui-textfield mdui-textfield-expandable table-search-row">
  360. <button id="serach" class="mdui-textfield-icon mdui-btn mdui-btn-icon">
  361. <i class="mdui-icon material-icons">search</i>
  362. </button>
  363. <input id="search-inner" class="mdui-textfield-input" type="text" placeholder="数据库名/用户ID/创建时间" />
  364. <button id="serach-done" class="mdui-textfield-close mdui-btn mdui-btn-icon">
  365. <i class="mdui-icon material-icons">check</i>
  366. </button>
  367. </div>
  368. <a id="refresh" class="mdui-btn mdui-btn-icon">
  369. <i class="mdui-icon material-icons">refresh</i>
  370. </a>
  371. <ul class="mdui-menu" id="tools-bar-attr">
  372. <li class="mdui-menu-item" disabled>
  373. <a href="javascript:;">排序方式</a>
  374. </li>
  375. <li id="userId" class="mdui-menu-item">
  376. <a href="javascript:;" class="mdui-ripple">用户ID</a>
  377. </li>
  378. <li id="databaseName" class="mdui-menu-item">
  379. <a href="javascript:;" class="mdui-ripple">数据库名称</a>
  380. </li>
  381. <li id="createDate" class="mdui-menu-item">
  382. <a href="javascript:;" class="mdui-ripple">数据库创建时间</a>
  383. </li>
  384. <li class="mdui-divider"></li>
  385. <li class="mdui-menu-item" disabled>
  386. <a href="javascript:;">操作</a>
  387. </li>
  388. <li class="mdui-menu-item">
  389. <a href="javascript:;" class="mdui-ripple">删除数据库</a>
  390. </li>
  391. <li class="mdui-menu-item">
  392. <a href="javascript:;" class="mdui-ripple">清空数据库</a>
  393. </li>
  394. </ul>
  395. </div>
  396. <table class="mdui-table mdui-table-selectable">
  397. <thead>
  398. <tr>
  399. <th>数据库名</th>
  400. <th>用户ID</th>
  401. <th>用户名称</th>
  402. <th>创建时间</th>
  403. <th class="mdui-table-col-numeric">操作</th>
  404. </tr>
  405. </thead>
  406. </table>
  407. <div class="mdui-col-xs-12 mdui-clearfix page-button-row">
  408. <div class="mdui-float-right">
  409. <span class="mdui-float-left">共</span>
  410. <b class="mdui-float-left"></b>
  411. <span class="mdui-float-left">页,当前为</span>
  412. <input class="mdui-float-left" name="pageNum" type="text" />
  413. <span class="mdui-float-left">页</span>
  414. <button class="mdui-float-left mdui-btn mdui-btn-raised mdui-ripple">跳转</button>
  415. </div>
  416. </div>
  417. </div>
  418. `);
  419. let table = document.querySelector('table'),
  420. searchInner = document.querySelector('#search-inner'),
  421. searchDoneButton = document.querySelector('#serach-done'),
  422. refreshButton = document.querySelector('#refresh'),
  423. orderByUserIdButton = document.querySelector('#tools-bar-attr > li:nth-child(2)'),
  424. orderByDbNameButton = document.querySelector('#tools-bar-attr > li:nth-child(3)'),
  425. orderByCreateDateButton = document.querySelector('#tools-bar-attr > li:nth-child(4)'),
  426. dropActionButton = document.querySelector('#tools-bar-attr > li:nth-child(7)'),
  427. emptyActionButton = document.querySelector('#tools-bar-attr > li:nth-child(8)'),
  428. menuOrderBut = [orderByUserIdButton, orderByDbNameButton, orderByCreateDateButton];
  429. searchDoneButton.addEventListener('click', (function () {
  430. this.arguments.arg.argObj.search = searchInner.value;
  431. let argStr = this.arguments.computed();
  432. window.location.hash = '/index/databaseList?' + argStr;
  433. }).bind(this), false);
  434. refreshButton.addEventListener('click', function () {
  435. window.location.reload();
  436. }, false);
  437. menuOrderBut.forEach(function (item, index) {
  438. item.addEventListener('click', (function () {
  439. this.arguments.arg.argObj.orderBy = item.id;
  440. let argStr = this.arguments.computed();
  441. window.location.hash = '/index/databaseList?' + argStr;
  442. }).bind(this), false);
  443. }, this);
  444. dropActionButton.addEventListener('click', function () {
  445. let selectedRow = document.querySelectorAll('tbody tr.mdui-table-row-selected');
  446. if (selectedRow.length <= 0) {
  447. mdui.dialog({
  448. title: '错误',
  449. content: '您未选择操作对象,无法删除数据库。',
  450. history: false,
  451. buttons: [{
  452. text: '关闭'
  453. }]
  454. });
  455. } else {
  456. mdui.dialog({
  457. title: '警告',
  458. content: '请确保您清楚您当前正在执行一个危险操作操作',
  459. history: false,
  460. buttons: [{
  461. text: '确定',
  462. onClick: function (inst) {
  463. let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/dropDatabase',
  464. successAry = [],
  465. faildAry = [];
  466. selectedRow.forEach(function (item, index, array) {
  467. let data = 'dbName=' + item.children[1].innerText;
  468. this.ajax.post(dropDatabaseUrl, data, function (response) {
  469. if (response.status == 'success') {
  470. successAry.push(item.children[1].innerText);
  471. } else {
  472. faildAry.push(item.children[1].innerText);
  473. }
  474. if (successAry.length + faildAry.length == array.length) {
  475. let dialogContent = `
  476. <ul class="mdui-list">
  477. <li class="mdui-list-item mdui-ripple">
  478. <div class="mdui-list-item-content">
  479. <div class="mdui-list-item-title mdui-list-item-one-line">执行成功</div>
  480. <div class="mdui-list-item-text mdui-list-item-two-line">` + successAry.join() + `</div>
  481. </div>
  482. </li>
  483. <li class="mdui-list-item mdui-ripple">
  484. <div class="mdui-list-item-content">
  485. <div class="mdui-list-item-title mdui-list-item-one-line">执行失败</div>
  486. <div class="mdui-list-item-text mdui-list-item-two-line">` + faildAry.join() + `</div>
  487. </div>
  488. </li>
  489. </ul>
  490. `;
  491. mdui.dialog({
  492. title: '完成',
  493. content: dialogContent,
  494. history: false,
  495. buttons: [{
  496. text: '关闭',
  497. onClick: function (inst) {
  498. window.location.reload();
  499. }
  500. }]
  501. });
  502. }
  503. });
  504. }, this);
  505. }
  506. },
  507. {
  508. text: '取消'
  509. }
  510. ]
  511. });
  512. }
  513. }, false)
  514. emptyActionButton.addEventListener('click', function () {
  515. let selectedRow = document.querySelectorAll('tbody tr.mdui-table-row-selected');
  516. if (selectedRow.length <= 0) {
  517. mdui.dialog({
  518. title: '错误',
  519. content: '您未选择操作对象,无法删除数据库。',
  520. history: false,
  521. buttons: [{
  522. text: '关闭'
  523. }]
  524. });
  525. } else {
  526. mdui.dialog({
  527. title: '警告',
  528. content: '请确保您清楚您当前正在执行一个危险操作操作',
  529. history: false,
  530. buttons: [{
  531. text: '确定',
  532. onClick: function (inst) {
  533. let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/emptyDatabase',
  534. successAry = [],
  535. faildAry = [];
  536. selectedRow.forEach(function (item, index, array) {
  537. let data = 'dbName=' + item.children[1].innerText;
  538. this.ajax.post(dropDatabaseUrl, data, function (response) {
  539. if (response.status == 'success') {
  540. successAry.push(item.children[1].innerText);
  541. } else {
  542. faildAry.push(item.children[1].innerText);
  543. }
  544. if (successAry.length + faildAry.length == array.length) {
  545. let dialogContent = `
  546. <ul class="mdui-list">
  547. <li class="mdui-list-item mdui-ripple">
  548. <div class="mdui-list-item-content">
  549. <div class="mdui-list-item-title mdui-list-item-one-line">执行成功</div>
  550. <div class="mdui-list-item-text mdui-list-item-two-line">` + successAry.join() + `</div>
  551. </div>
  552. </li>
  553. <li class="mdui-list-item mdui-ripple">
  554. <div class="mdui-list-item-content">
  555. <div class="mdui-list-item-title mdui-list-item-one-line">执行失败</div>
  556. <div class="mdui-list-item-text mdui-list-item-two-line">` + faildAry.join() + `</div>
  557. </div>
  558. </li>
  559. </ul>
  560. `;
  561. mdui.dialog({
  562. title: '完成',
  563. content: dialogContent,
  564. history: false,
  565. buttons: [{
  566. text: '关闭',
  567. onClick: function (inst) {
  568. window.location.reload();
  569. }
  570. }]
  571. });
  572. }
  573. });
  574. }, this);
  575. }
  576. },
  577. {
  578. text: '取消'
  579. }
  580. ]
  581. });
  582. }
  583. }, false)
  584. //AJAX向后台数据接口获取数据后执行回调函数
  585. this.ajax.get(getDatabaseListUrl, function (response) {
  586. if (response.status == 'success') {
  587. let tableBody = document.createElement('tbody'),
  588. results = response.data.results,
  589. resultsCount = response.data.count;
  590. if (resultsCount === 0) {
  591. let subContentAera = document.querySelector('.mdui-col-xs-12.mdui-table-fluid'),
  592. pageButRow = document.querySelector('.mdui-col-xs-12.mdui-clearfix.page-button-row');
  593. mdui.JQ(table).remove();
  594. mdui.JQ(pageButRow).remove();
  595. mdui.JQ(subContentAera).append(`<p style="text-align: center;">暂无数据</p>`);
  596. } else {
  597. results.forEach(function (item, index, array) {
  598. let resultsAry = [],
  599. detailBut = document.createElement('button'),
  600. removeBut = document.createElement('button');
  601. detailBut.className = 'mdui-btn';
  602. removeBut.className = 'mdui-btn mdui-color-theme-accent';
  603. detailBut.innerText = '查看详情';
  604. removeBut.innerText = '删除';
  605. detailBut.addEventListener('click', function () {
  606. let getDatabaseTablesUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/getDatabaseAllTables?dbName=' + item.dbName;
  607. this.ajax.get(getDatabaseTablesUrl, function (response) {
  608. let tablesAry = [];
  609. response.data.results.forEach(function (_item) {
  610. tablesAry.push(_item.TABLE_NAME);
  611. });
  612. if (tablesAry.length === 0) {
  613. tablesAry.push('该数据库无数据表信息');
  614. }
  615. let content = `
  616. <ul class="mdui-list">
  617. <li class="mdui-list-item mdui-ripple">
  618. <div class="mdui-list-item-content">
  619. <div class="mdui-list-item-title mdui-list-item-one-line">数据库名称</div>
  620. <div class="mdui-list-item-text mdui-list-item-two-line">` + item.dbName + `</div>
  621. </div>
  622. </li>
  623. <li class="mdui-list-item mdui-ripple">
  624. <div class="mdui-list-item-content">
  625. <div class="mdui-list-item-title mdui-list-item-one-line">数据库拥有者ID</div>
  626. <div class="mdui-list-item-text mdui-list-item-two-line">` + item.dbOwnUuid + `</div>
  627. </div>
  628. </li>
  629. <li class="mdui-list-item mdui-ripple">
  630. <div class="mdui-list-item-content">
  631. <div class="mdui-list-item-title mdui-list-item-one-line">数据库创建时间</div>
  632. <div class="mdui-list-item-text mdui-list-item-two-line">` + item.createDate + `</div>
  633. </div>
  634. </li>
  635. <li class="mdui-list-item mdui-ripple">
  636. <div class="mdui-list-item-content">
  637. <div class="mdui-list-item-title mdui-list-item-one-line">数据库数据表列表</div>
  638. <div class="mdui-list-item-text mdui-list-item-two-line">` + tablesAry.join(' ') + `</div>
  639. </div>
  640. </li>
  641. </ul>
  642. `;
  643. mdui.dialog({
  644. title: '数据库详情',
  645. content: content,
  646. history: false,
  647. buttons: [{
  648. text: '清空数据库',
  649. onClick: function (inst) {
  650. mdui.dialog({
  651. title: '警告',
  652. content: '请确保您清楚您当前正在执行一个危险操作操作',
  653. history: false,
  654. buttons: [{
  655. text: '确定',
  656. onClick: function (inst) {
  657. let emptyDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/emptyDatabase',
  658. data = 'dbName=' + item.dbName;
  659. this.ajax.post(emptyDatabaseUrl, data, function (response) {
  660. if (response.status == 'success') {
  661. mdui.dialog({
  662. content: '执行成功',
  663. history: false,
  664. buttons: [{
  665. text: '确定',
  666. onClick: function (inst) {
  667. window.location.reload();
  668. }
  669. }]
  670. });
  671. } else {
  672. mdui.dialog({
  673. content: '执行失败',
  674. history: false,
  675. buttons: [{
  676. text: '确定',
  677. onClick: function (inst) {
  678. window.location.reload();
  679. }
  680. }]
  681. });
  682. }
  683. });
  684. }
  685. },
  686. {
  687. text: '取消'
  688. }
  689. ]
  690. });
  691. }
  692. },
  693. {
  694. text: '删除数据库',
  695. onClick: function (inst) {
  696. mdui.dialog({
  697. title: '警告',
  698. content: '请确保您清楚您当前正在执行一个危险操作操作',
  699. history: false,
  700. buttons: [{
  701. text: '确定',
  702. onClick: function (inst) {
  703. let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/dropDatabase',
  704. data = 'dbName=' + item.dbName;
  705. this.ajax.post(dropDatabaseUrl, data, function (response) {
  706. if (response.status == 'success') {
  707. mdui.dialog({
  708. content: '执行成功',
  709. history: false,
  710. buttons: [{
  711. text: '确定',
  712. onClick: function (inst) {
  713. window.location.reload();
  714. }
  715. }]
  716. });
  717. } else {
  718. mdui.dialog({
  719. content: '执行失败',
  720. history: false,
  721. buttons: [{
  722. text: '确定',
  723. onClick: function (inst) {
  724. window.location.reload();
  725. }
  726. }]
  727. });
  728. }
  729. });
  730. }
  731. },
  732. {
  733. text: '取消'
  734. }
  735. ]
  736. });
  737. }
  738. },
  739. {
  740. text: '关闭'
  741. }
  742. ]
  743. });
  744. });
  745. }.bind(this), false);
  746. removeBut.addEventListener('click', function () {
  747. mdui.dialog({
  748. title: '警告',
  749. content: `
  750. <p>请确保您清楚您当前正在执行一个危险操作操作</p>
  751. <p>您确定要删除DB: <b>` + item.dbName + `</b> 吗?</p>
  752. `,
  753. history: false,
  754. buttons: [{
  755. text: '确定',
  756. onClick: function (inst) {
  757. let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/dropDatabase',
  758. data = 'dbName=' + item.dbName;
  759. this.ajax.post(dropDatabaseUrl, data, function (response) {
  760. if (response.status == 'success') {
  761. mdui.dialog({
  762. content: '执行成功',
  763. history: false,
  764. buttons: [{
  765. text: '确定',
  766. onClick: function (inst) {
  767. window.location.reload();
  768. }
  769. }]
  770. });
  771. } else {
  772. mdui.dialog({
  773. content: '执行失败',
  774. history: false,
  775. buttons: [{
  776. text: '确定',
  777. onClick: function (inst) {
  778. window.location.reload();
  779. }
  780. }]
  781. });
  782. }
  783. });
  784. }
  785. },
  786. {
  787. text: '取消'
  788. }
  789. ]
  790. });
  791. }, false);
  792. let actionColContent = [detailBut, removeBut];
  793. resultsAry.push(item.dbName, item.dbOwnUuid, item.account, item.createDate, actionColContent);
  794. let tr = this.printer.tableGenerator(resultsAry);
  795. tableBody.appendChild(tr);
  796. }, this);
  797. table.appendChild(tableBody);
  798. mdui.updateTables(table);
  799. let pageShowCol = document.querySelector('.mdui-col-xs-12.page-button-row b'),
  800. pageNumOfNow = document.querySelector('.mdui-col-xs-12.page-button-row input'),
  801. jumpButton = document.querySelector('.mdui-col-xs-12.page-button-row button'),
  802. pageNum = Math.ceil(resultsCount / 20);
  803. pageShowCol.innerHTML = pageNum;
  804. pageNumOfNow.value = this.arguments.arg.argObj.pageIndex;
  805. jumpButton.addEventListener('click', (function () {
  806. this.arguments.arg.argObj.pageIndex = pageNumOfNow.value;
  807. let argStr = this.arguments.computed();
  808. window.location.hash = '/index/databaseList?' + argStr;
  809. }).bind(this), false);
  810. }
  811. } else {
  812. mdui.snackbar({
  813. message: response.info
  814. });
  815. }
  816. }.bind(this));
  817. },
  818. userList: function () {
  819. let contentAera = document.querySelector('.mdui-container.main-container'),
  820. getUserListUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList';
  821. if (!this.arguments.arg.argStr) {
  822. this.arguments.arg = {
  823. argStr: 'pageIndex=1&orderBy=userName',
  824. argObj: {
  825. 'pageIndex': 1,
  826. 'orderBy': 'userName',
  827. 'search': ''
  828. }
  829. }
  830. }
  831. getUserListUrl += '?' + this.arguments.arg.argStr;
  832. mdui.JQ(contentAera).empty();
  833. mdui.JQ(contentAera).append(`
  834. <h2 class="mdui-text-color-theme">用户数据库列表</h2>
  835. <div class="mdui-col-xs-12 mdui-table-fluid">
  836. <div class="mdui-toolbar table-tools-bar">
  837. <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-menu="{target: '#tools-bar-attr'}">
  838. <i class="mdui-icon material-icons">menu</i>
  839. </a>
  840. <span class="mdui-typo-title">工具栏</span>
  841. <div class="mdui-toolbar-spacer"></div>
  842. <div class="mdui-textfield mdui-textfield-expandable table-search-row">
  843. <button id="serach" class="mdui-textfield-icon mdui-btn mdui-btn-icon">
  844. <i class="mdui-icon material-icons">search</i>
  845. </button>
  846. <input id="search-inner" class="mdui-textfield-input" type="text" placeholder="用户ID/用户昵称/数据库密码" />
  847. <button id="serach-done" class="mdui-textfield-close mdui-btn mdui-btn-icon">
  848. <i class="mdui-icon material-icons">check</i>
  849. </button>
  850. </div>
  851. <a id="refresh" class="mdui-btn mdui-btn-icon">
  852. <i class="mdui-icon material-icons">refresh</i>
  853. </a>
  854. <ul class="mdui-menu" id="tools-bar-attr">
  855. <li class="mdui-menu-item" disabled>
  856. <a href="javascript:;">排序方式</a>
  857. </li>
  858. <li id="userId" class="mdui-menu-item">
  859. <a href="javascript:;" class="mdui-ripple">用户ID</a>
  860. </li>
  861. <li id="userNickName" class="mdui-menu-item">
  862. <a href="javascript:;" class="mdui-ripple">用户昵称</a>
  863. </li>
  864. <li id="createDate" class="mdui-menu-item">
  865. <a href="javascript:;" class="mdui-ripple">用户创建时间</a>
  866. </li>
  867. <li class="mdui-divider"></li>
  868. <li class="mdui-menu-item" disabled>
  869. <a href="javascript:;">操作</a>
  870. </li>
  871. <li class="mdui-menu-item">
  872. <a href="javascript:;" class="mdui-ripple">删除用户</a>
  873. </li>
  874. </ul>
  875. </div>
  876. <table class="mdui-table mdui-table-selectable">
  877. <thead>
  878. <tr>
  879. <th>用户昵称</th>
  880. <th>用户ID</th>
  881. <th>数据库密码</th>
  882. <th>用户注册时间</th>
  883. <th class="mdui-table-col-numeric ">操作</th>
  884. </tr>
  885. </thead>
  886. </table>
  887. <div class="mdui-col-xs-12 mdui-clearfix page-button-row">
  888. <div class="mdui-float-right">
  889. <span class="mdui-float-left">共</span>
  890. <b class="mdui-float-left"></b>
  891. <span class="mdui-float-left">页,当前为</span>
  892. <input class="mdui-float-left" name="pageNum" type="text" />
  893. <span class="mdui-float-left">页</span>
  894. <button class="mdui-float-left mdui-btn mdui-btn-raised mdui-ripple">跳转</button>
  895. </div>
  896. </div>
  897. </div>
  898. `);
  899. //生成表格
  900. let table = document.querySelector('table'),
  901. searchInner = document.querySelector('#search-inner'),
  902. searchDoneButton = document.querySelector('#serach-done'),
  903. refreshButton = document.querySelector('#refresh'),
  904. orderByUserIdButton = document.querySelector('#tools-bar-attr > li:nth-child(2)'),
  905. orderByUserNameButton = document.querySelector('#tools-bar-attr > li:nth-child(3)'),
  906. orderByUserCreateButton = document.querySelector('#tools-bar-attr > li:nth-child(4)')
  907. removeActionButton = document.querySelector('#tools-bar-attr > li:nth-child(7)'),
  908. menuOrderBut = [orderByUserIdButton, orderByUserNameButton, orderByUserCreateButton];
  909. searchDoneButton.addEventListener('click', (function () {
  910. this.arguments.arg.argObj.search = searchInner.value;
  911. let argStr = this.arguments.computed();
  912. window.location.hash = '/index/userList?' + argStr;
  913. }).bind(this), false);
  914. refreshButton.addEventListener('click', function () {
  915. window.location.reload();
  916. }, false);
  917. removeActionButton.addEventListener('click', function () {
  918. let selectedRow = document.querySelectorAll('tbody tr.mdui-table-row-selected');
  919. if (selectedRow.length <= 0) {
  920. mdui.dialog({
  921. title: '错误',
  922. content: '您未选择操作对象,无法删除数据库。',
  923. history: false,
  924. buttons: [{
  925. text: '关闭'
  926. }]
  927. });
  928. } else {
  929. mdui.dialog({
  930. title: '警告',
  931. content: '请确保您清楚您当前正在执行一个危险操作操作',
  932. history: false,
  933. buttons: [{
  934. text: '确定',
  935. onClick: function (inst) {
  936. let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/removeUser',
  937. successAry = [],
  938. faildAry = [];
  939. selectedRow.forEach(function (item, index, array) {
  940. let data = 'userId=' + item.children[2].innerText + '&userNickName=' + item.children[1].innerText;
  941. this.ajax.post(dropDatabaseUrl, data, function (response) {
  942. if (response.status == 'success') {
  943. successAry.push(item.children[1].innerText);
  944. } else {
  945. faildAry.push(item.children[1].innerText);
  946. }
  947. if (successAry.length + faildAry.length == array.length) {
  948. let dialogContent = `
  949. <ul class="mdui-list">
  950. <li class="mdui-list-item mdui-ripple">
  951. <div class="mdui-list-item-content">
  952. <div class="mdui-list-item-title mdui-list-item-one-line">执行成功</div>
  953. <div class="mdui-list-item-text mdui-list-item-two-line">` + successAry.join() + `</div>
  954. </div>
  955. </li>
  956. <li class="mdui-list-item mdui-ripple">
  957. <div class="mdui-list-item-content">
  958. <div class="mdui-list-item-title mdui-list-item-one-line">执行失败</div>
  959. <div class="mdui-list-item-text mdui-list-item-two-line">` + faildAry.join() + `</div>
  960. </div>
  961. </li>
  962. </ul>
  963. `;
  964. mdui.dialog({
  965. title: '完成',
  966. content: dialogContent,
  967. history: false,
  968. buttons: [{
  969. text: '关闭',
  970. onClick: function (inst) {
  971. window.location.reload();
  972. }
  973. }]
  974. });
  975. }
  976. });
  977. }, this);
  978. }
  979. },
  980. {
  981. text: '取消'
  982. }
  983. ]
  984. });
  985. }
  986. }, false)
  987. menuOrderBut.forEach(function (item, index) {
  988. item.addEventListener('click', (function () {
  989. this.arguments.arg.argObj.orderBy = item.id;
  990. let argStr = this.arguments.computed();
  991. window.location.hash = '/index/userList?' + argStr;
  992. }).bind(this), false);
  993. }, this);
  994. //AJAX向后台数据接口获取数据后执行回调函数
  995. this.ajax.get(getUserListUrl, function (response) {
  996. if (response.status == 'success') {
  997. let tableBody = document.createElement('tbody'),
  998. results = response.data.results,
  999. resultsCount = response.data.count;
  1000. if (resultsCount === 0) {
  1001. let subContentAera = document.querySelector('.mdui-col-xs-12.mdui-table-fluid'),
  1002. pageButRow = document.querySelector('.mdui-col-xs-12.mdui-clearfix.page-button-row');
  1003. mdui.JQ(table).remove();
  1004. mdui.JQ(pageButRow).remove();
  1005. mdui.JQ(subContentAera).append(`<p style="text-align: center;">暂无数据</p>`);
  1006. } else {
  1007. }
  1008. results.forEach(function (item, index) {
  1009. let resultsAry = [],
  1010. detailBut = document.createElement('button'),
  1011. removeBut = document.createElement('button');
  1012. detailBut.className = 'mdui-btn';
  1013. removeBut.className = 'mdui-btn mdui-color-theme-accent';
  1014. detailBut.innerText = '查看详情';
  1015. removeBut.innerText = '删除';
  1016. detailBut.addEventListener('click', function () {
  1017. let getUserOwnDbUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/getUserOwnDb?userId=' + item.uuid;
  1018. this.ajax.get(getUserOwnDbUrl, function (response) {
  1019. let dbAry = [];
  1020. response.data.results.forEach(function (_item) {
  1021. dbAry.push(_item.dbName);
  1022. });
  1023. if (dbAry.length === 0) {
  1024. dbAry.push('无');
  1025. }
  1026. let content = `
  1027. <ul class="mdui-list">
  1028. <li class="mdui-list-item mdui-ripple">
  1029. <div class="mdui-list-item-content">
  1030. <div class="mdui-list-item-title mdui-list-item-one-line">用户昵称</div>
  1031. <div class="mdui-list-item-text mdui-list-item-two-line">` + item.account + `</div>
  1032. </div>
  1033. </li>
  1034. <li class="mdui-list-item mdui-ripple">
  1035. <div class="mdui-list-item-content">
  1036. <div class="mdui-list-item-title mdui-list-item-one-line">用户UUID</div>
  1037. <div class="mdui-list-item-text mdui-list-item-two-line">` + item.uuid + `</div>
  1038. </div>
  1039. </li>
  1040. <li class="mdui-list-item mdui-ripple">
  1041. <div class="mdui-list-item-content">
  1042. <div class="mdui-list-item-title mdui-list-item-one-line">数据库密码</div>
  1043. <div class="mdui-list-item-text mdui-list-item-two-line">` + item.dbpassword + `</div>
  1044. </div>
  1045. </li>
  1046. <li class="mdui-list-item mdui-ripple">
  1047. <div class="mdui-list-item-content">
  1048. <div class="mdui-list-item-title mdui-list-item-one-line">数据库列表</div>
  1049. <div class="mdui-list-item-text mdui-list-item-two-line">` + dbAry.join(' ') + `</div>
  1050. </div>
  1051. </li>
  1052. </ul>
  1053. `;
  1054. mdui.dialog({
  1055. title: '用户详情',
  1056. content: content,
  1057. history: false,
  1058. buttons: [{
  1059. text: '查看数据库',
  1060. onClick: function (inst) {
  1061. window.location.hash = '/index/databaseList?search=' + item.uuid;
  1062. }
  1063. },
  1064. {
  1065. text: '删除用户',
  1066. onClick: function (inst) {
  1067. mdui.dialog({
  1068. title: '警告',
  1069. content: '请确保您清楚您当前正在执行一个危险操作操作',
  1070. history: false,
  1071. buttons: [{
  1072. text: '确定',
  1073. onClick: function (inst) {
  1074. let removeUserUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/removeUser',
  1075. data = 'userId=' + item.uuid + '&userNickName=' + item.account;
  1076. this.ajax.post(removeUserUrl, data, function (response) {
  1077. if (response.status == 'success') {
  1078. mdui.dialog({
  1079. content: '执行成功',
  1080. history: false,
  1081. buttons: [{
  1082. text: '确定',
  1083. onClick: function (inst) {
  1084. window.location.reload();
  1085. }
  1086. }]
  1087. });
  1088. } else {
  1089. mdui.dialog({
  1090. content: '执行失败',
  1091. history: false,
  1092. buttons: [{
  1093. text: '确定',
  1094. onClick: function (inst) {
  1095. window.location.reload();
  1096. }
  1097. }]
  1098. });
  1099. }
  1100. });
  1101. }
  1102. },
  1103. {
  1104. text: '取消'
  1105. }
  1106. ]
  1107. });
  1108. }
  1109. },
  1110. {
  1111. text: '关闭'
  1112. }
  1113. ]
  1114. });
  1115. });
  1116. }.bind(this), false);
  1117. removeBut.addEventListener('click', function () {
  1118. mdui.dialog({
  1119. title: '警告',
  1120. content: `
  1121. <p>请确保您清楚您当前正在执行一个危险操作操作</p>
  1122. <p>您确定要删除用户: <b>` + item.account + `</b> 吗?</p>
  1123. `,
  1124. history: false,
  1125. buttons: [{
  1126. text: '确定',
  1127. onClick: function (inst) {
  1128. let removeUserUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/removeUser',
  1129. data = 'userId=' + item.uuid + '&userNickName=' + item.account;
  1130. this.ajax.post(removeUserUrl, data, function (response) {
  1131. if (response.status == 'success') {
  1132. mdui.dialog({
  1133. content: '执行成功',
  1134. history: false,
  1135. buttons: [{
  1136. text: '确定',
  1137. onClick: function (inst) {
  1138. window.location.reload();
  1139. }
  1140. }]
  1141. });
  1142. } else {
  1143. mdui.dialog({
  1144. content: '执行失败',
  1145. history: false,
  1146. buttons: [{
  1147. text: '确定',
  1148. onClick: function (inst) {
  1149. window.location.reload();
  1150. }
  1151. }]
  1152. });
  1153. }
  1154. });
  1155. }
  1156. },
  1157. {
  1158. text: '取消'
  1159. }
  1160. ]
  1161. });
  1162. }.bind(this), false);
  1163. let actionColContent = [detailBut, removeBut];
  1164. resultsAry.push(item.account, item.uuid, item.dbpassword, item.createDate, actionColContent);
  1165. let tr = this.printer.tableGenerator(resultsAry);
  1166. tableBody.appendChild(tr);
  1167. }, this);
  1168. table.appendChild(tableBody);
  1169. mdui.updateTables(table);
  1170. let pageShowCol = document.querySelector('.mdui-col-xs-12.page-button-row b'),
  1171. pageNumOfNow = document.querySelector('.mdui-col-xs-12.page-button-row input'),
  1172. jumpButton = document.querySelector('.mdui-col-xs-12.page-button-row button'),
  1173. pageNum = Math.ceil(resultsCount / 20);
  1174. pageShowCol.innerHTML = pageNum;
  1175. pageNumOfNow.value = this.arguments.arg.argObj.pageIndex;
  1176. jumpButton.addEventListener('click', (function () {
  1177. this.arguments.arg.argObj.pageIndex = pageNumOfNow.value;
  1178. let argStr = this.arguments.computed();
  1179. window.location.hash = '/index/userList?' + argStr;
  1180. }).bind(this), false);
  1181. } else {
  1182. mdui.snackbar({
  1183. message: response.info
  1184. });
  1185. }
  1186. }.bind(this));
  1187. }
  1188. }
  1189. this.permissionCheck = {
  1190. /**
  1191. * 检测账户登录状态
  1192. *
  1193. * @param {any} next 回调函数
  1194. */
  1195. signStatus: function (next) {
  1196. this.ajax.get('http://admin.cloudsql.1473.cn/v1/index/signStatus', function (response) {
  1197. if (response.status == 'signedin') {
  1198. next.call(this);
  1199. } else {
  1200. window.location.hash = '/signin';
  1201. }
  1202. }.bind(this));
  1203. }
  1204. }
  1205. this.routes = {
  1206. '/': function () {
  1207. window.location.hash = '#/signin';
  1208. },
  1209. '/404': this.printer.pageNotFound,
  1210. '/signin': this.printer.signin.bind(this),
  1211. '/signup': this.printer.signup.bind(this),
  1212. '/index': this.printer.indexInit.bind(this, this.printer.index),
  1213. '/index/databaseList': this.printer.indexInit.bind(this, this.printer.databaseList),
  1214. '/index/userList': this.printer.indexInit.bind(this, this.printer.userList)
  1215. };
  1216. this.arguments = {
  1217. arg: {},
  1218. computed: function () {
  1219. this.arg.argStr = '';
  1220. Object.keys(this.arg.argObj).forEach(function (item, index, array) {
  1221. if (index + 1 === array.length) {
  1222. this.arg.argStr += item + '=' + this.arg.argObj[item];
  1223. } else {
  1224. this.arg.argStr += item + '=' + this.arg.argObj[item] + '&';
  1225. }
  1226. }, this);
  1227. return this.arg.argStr;
  1228. }
  1229. };
  1230. this.route = function (path, callback) {
  1231. this.routes[path] = callback || function () {}
  1232. }
  1233. this.refresh = function () {
  1234. this.currentUrl = location.hash.slice(1) || '/';
  1235. this.currentUrl = this.currentUrl.split("?");
  1236. //路由内无对应路径则跳转至404页面
  1237. if (this.currentUrl.length === 1 && this.routes[this.currentUrl[0]]) {
  1238. this.routes[this.currentUrl[0]]();
  1239. this.arguments.arg = {};
  1240. } else if (this.currentUrl.length === 2 && this.routes[this.currentUrl[0]]) {
  1241. this.routes[this.currentUrl[0]]();
  1242. let argStr = this.currentUrl[1];
  1243. this.arguments.arg = {
  1244. argStr: argStr,
  1245. argObj: {}
  1246. }
  1247. argStr.split("&").forEach(function (item, index) {
  1248. let ary = item.split("=");
  1249. this.arguments.arg.argObj[ary[0]] = ary[1];
  1250. }, this);
  1251. } else {
  1252. this.routes['/404']();
  1253. }
  1254. }
  1255. this.init = function () {
  1256. const windowLoad = this.handleEvent('load', {
  1257. onElement: window,
  1258. withCallback: this.refresh.bind(this)
  1259. });
  1260. const hashChange = this.handleEvent('hashchange', {
  1261. onElement: window,
  1262. withCallback: this.refresh.bind(this)
  1263. });
  1264. }
  1265. }
  1266. CloudAdmin.router.prototype.ajax = {
  1267. /**
  1268. * 发送Get请求
  1269. *
  1270. * @param {!string} url 请求地址
  1271. * @param {?function} next 回调函数
  1272. */
  1273. get: function (url, next) {
  1274. let xhr = new XMLHttpRequest();
  1275. if (url.includes('?')) {
  1276. // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
  1277. xhr.open('GET', url + '&date=' + new Date().getTime(), true);
  1278. } else {
  1279. // 添加一个date参数加入当前时间以避免缓存
  1280. xhr.open('GET', url + '?' + new Date().getTime(), true);
  1281. }
  1282. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  1283. // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
  1284. xhr.withCredentials = true;
  1285. xhr.responseType = 'json';
  1286. xhr.onload = function (res) {
  1287. // 获取请求接口返回值
  1288. let response = res.target.response;
  1289. next.call(this, response);
  1290. };
  1291. xhr.send();
  1292. },
  1293. /**
  1294. * 发送Post请求
  1295. *
  1296. * @param {!string} url 请求地址
  1297. * @param {?string} data post请求参数
  1298. * @param {?function} next 回调函数
  1299. */
  1300. post: function (url, data, next) {
  1301. let xhr = new XMLHttpRequest();
  1302. if (url.includes('?')) {
  1303. // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
  1304. xhr.open("POST", url + '&' + new Date().getTime(), true);
  1305. } else {
  1306. // 添加一个date参数加入当前时间以避免缓存
  1307. xhr.open("POST", url + '?' + new Date().getTime(), true);
  1308. }
  1309. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  1310. // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
  1311. xhr.withCredentials = true;
  1312. xhr.responseType = 'json';
  1313. xhr.onload = function (res) {
  1314. // 获取请求接口返回值
  1315. let response = res.target.response;
  1316. next.call(this, response);
  1317. };
  1318. xhr.send(data);
  1319. }
  1320. }
  1321. /**
  1322. * 监听事件处理函数
  1323. *
  1324. * @param {string} eventName 表示监听事件类型的字符串
  1325. * @param {object} [{ onElement, withCallback, useCapture = false }={}] 监听对象 回调函数 useCapture
  1326. * @param {function} thisArg
  1327. * @returns function
  1328. */
  1329. CloudAdmin.router.prototype.handleEvent = function (eventName, {
  1330. onElement,
  1331. withCallback,
  1332. useCapture = false
  1333. } = {}, thisArg) {
  1334. const element = onElement || document.documentElement;
  1335. function handler(event) {
  1336. if (typeof withCallback === 'function') {
  1337. withCallback.call(thisArg, event);
  1338. }
  1339. }
  1340. handler.destroy = function () {
  1341. return element.removeEventListener(eventName, handler, useCapture);
  1342. }
  1343. element.addEventListener(eventName, handler, useCapture);
  1344. return handler
  1345. }
  1346. CloudAdmin.router.prototype.event = {
  1347. dom: [],
  1348. /**
  1349. * 调用 CloudAdmin.router.prototype.handleEvent.handler.destroy 移除DOM的监听事件
  1350. *
  1351. * @param {any} ary 存放DOM的数组
  1352. */
  1353. removeEventListener: function (ary) {
  1354. }
  1355. }
  1356. //创建并初始化前端路由
  1357. let r = new CloudAdmin.router();
  1358. r.init();