1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510 |
- var CloudAdmin = CloudAdmin || {};
- CloudAdmin.router = function () {
- this.currentUrl = '';
- this.printer = {
- pageNotFound: function () {
- let body = document.querySelector('body');
- mdui.JQ(body).empty();
- mdui.JQ(body).append(`
- <h1>404 Page Not Found :(</h1>
- <a href="#/index">返回首页</a>
- `);
- },
- /**
- * 内联样式初始化
- *
- * @param {any} next 初始化完成后执行的回调函数
- * @param {any} arg 回调函数参数
- */
- indexInit: function (next, arg) {
- this.permissionCheck.signStatus.call(this, function () {
- let body = document.querySelector('body');
- body.className = 'mdui-appbar-with-toolbar mdui-theme-primary-blue mdui-theme-accent-light-blue mdui-drawer-body-left mdui-loaded';
- mdui.JQ(body).empty();
- mdui.JQ(body).append(`
- <header class="mdui-appbar mdui-appbar-fixed">
- <div class="mdui-toolbar mdui-color-theme">
- <span class="mdui-btn mdui-btn-icon mdui-ripple mdui-ripple-white" mdui-drawer="{target: '#main-drawer', swipe: true}">
- <i class="mdui-icon material-icons">menu</i>
- </span>
- <a href="#/index" class="mdui-typo-headline mdui-hidden-xs">CloudSQL Admin Panel</a>
- <div class="mdui-toolbar-spacer"></div>
- <span class="mdui-btn mdui-btn-icon mdui-ripple mdui-ripple-white">
- <i class="mdui-icon material-icons">exit_to_app</i>
- </span>
- </div>
- </header>
-
- <div class="mdui-drawer mdui-drawer-open" id="main-drawer">
- <div class="mdui-list" mdui-collapse="{accordion: true}" style="margin-bottom: 76px;">
- <li class="mdui-list-item mdui-ripple">
- <i class="mdui-list-item-icon mdui-icon material-icons">view_list</i>
- <div class="mdui-list-item-content">数据库列表</div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <i class="mdui-list-item-icon mdui-icon material-icons">person</i>
- <div class="mdui-list-item-content">用户列表</div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <i class="mdui-list-item-icon mdui-icon material-icons">info</i>
- <div class="mdui-list-item-content">关于</div>
- </li>
- </div>
- </div>
-
- <div class="mdui-container main-container"></div>
- `);
- //左侧菜单栏
- let leftMenuButton = document.querySelector('span:nth-child(1).mdui-btn.mdui-btn-icon.mdui-ripple.mdui-ripple-white'),
- mainDrawer = new mdui.Drawer('#main-drawer');
- /* leftMenuButton.addEventListener('click', function () {
- mainDrawer.toggle();
- }, false); */
- this.handleEvent('click', {
- onElement: leftMenuButton,
- withCallback: function () {
- mainDrawer.toggle();
- }
- })
- let databaseListButton = document.querySelector('#main-drawer li:nth-child(1)');
- /* databaseListButton.addEventListener('click', function () {
- window.location.hash = '/index/databaseList';
- }, false); */
- this.handleEvent('click', {
- onElement: databaseListButton,
- withCallback: function () {
- window.location.hash = '/index/databaseList';
- }
- })
- let userListButton = document.querySelector('#main-drawer li:nth-child(2)');
- /* userListButton.addEventListener('click', function () {
- window.location.hash = '/index/userList';
- }, false); */
- this.handleEvent('click', {
- onElement: userListButton,
- withCallback: function () {
- window.location.hash = '/index/userList';
- }
- })
- //退出登录按钮
- let signoutButton = document.querySelector('span:nth-child(4).mdui-btn.mdui-btn-icon.mdui-ripple.mdui-ripple-white i.mdui-icon.material-icons');
- /* signoutButton.addEventListener('click', function () {
- this.ajax.get('http://admin.cloudsql.1473.cn/v1/signout', function (response) {
- if (response.status == 'successed') {
- mdui.snackbar({
- message: response.info
- });
- window.location.hash = response.url;
- } else {
- console.log(response.info);
- console.log(response.error);
- mdui.snackbar({
- message: response.info
- });
- }
- })
- }.bind(this), false); */
- this.handleEvent('click', {
- onElement: signoutButton,
- withCallback: function () {
- this.ajax.get('http://admin.cloudsql.1473.cn/v1/signout', function (response) {
- if (response.status == 'successed') {
- mdui.snackbar({
- message: response.info
- });
- window.location.hash = response.url;
- } else {
- console.log(response.info);
- console.log(response.error);
- mdui.snackbar({
- message: response.info
- });
- }
- });
- }
- }, this)
- next.call(this, arg);
- });
- },
- /**
- * 表格生成
- *
- * @param {any} data 表格数据
- * @returns DOM
- */
- tableGenerator: function (data) {
- //Generate tag tr
- let tr = document.createElement('tr');
- data.forEach(function (item, index, array) {
- let td = document.createElement('td');
- if (index + 1 === array.length) {
- item.forEach(function (_item, _index, _array) {
- td.appendChild(_item);
- tr.appendChild(td);
- }, this);
- } else {
- td.innerHTML = item;
- tr.appendChild(td);
- }
- }, this);
- return tr;
- },
- signin: function () {
- let html = document.querySelector('html'),
- body = document.querySelector('body');
- html.className = 'index';
- body.className = 'mdui-theme-primary-blue mdui-theme-accent-light-blue index';
- mdui.JQ(body).empty();
- mdui.JQ(body).append(`
- <header class="mdui-appbar mdui-shadow-0">
- <div class="mdui-toolbar mdui-color-theme">
- <a class="mdui-typo-headline" href="#">CloudSQL Admin Panel</a>
- <div class="mdui-toolbar-spacer"></div>
- <a class="mdui-btn mdui-ripple mdui-ripple-white" href="#/signin">登陆</a>
- <a class="mdui-btn mdui-ripple mdui-ripple-white" href="#/signup">注册</a>
- </div>
- </header>
- <main style="height: 85%;">
- <div class="mdui-container main-section">
- <h2 class="mdui-typo-display-1 main-section-tittle">用户登录</h2>
- <div class="mdui-container mdui-col-md-8 mdui-center main-sub-section">
- <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
- <i class="mdui-icon material-icons">person</i>
- <input class="mdui-textfield-input" name="userid" type="text" placeholder="用户名/邮箱" />
- </div>
- <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
- <i class="mdui-icon material-icons">lock</i>
- <input class="mdui-textfield-input" name="password" type="password" placeholder="密码" />
- </div>
- <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row captcha-row">
- <img src="http://admin.cloudsql.1473.cn/v1/captcha?` + new Date().getTime() + `">
- <input class="mdui-textfield-input" name="captcha" type="text" placeholder="验证码" />
- </div>
- <div class="mdui-col-md-6 mdui-center mdui-typo main-a-row">
- <a href="#/signup">还没有账户,点击注册</a>
- </div>
- <div class="mdui-col-md-6 mdui-center main-button-row">
- <button class="mdui-btn mdui-btn-block mdui-color-theme-accent">登陆</button>
- </div>
- </div>
- </div>
- </main>
- <footer>
- <div class="footer-copyright">Copyright © 2016 - 2017 <a href="/">CloudSQL</a> All rights reserved.</div>
- </footer>
- `)
- //验证码刷新逻辑
- let captchaImg = document.querySelector('.captcha-row img');
- captchaImg.addEventListener('click', function () {
- this.src = 'http://admin.cloudsql.1473.cn/v1/captcha?' + new Date().getTime(); //加入当前时间以避免缓存
- }, false);
- //登陆逻辑
- let submitButton = document.querySelector('.main-button-row button');
- submitButton.addEventListener('click', function () {
- let signInUrl = 'http://admin.cloudsql.1473.cn/v1/signin',
- userId = document.querySelector('input[name=userid]').value,
- password = document.querySelector('input[name=password]').value,
- captchaCode = document.querySelector('input[name=captcha]').value,
- postData = 'userId=' + userId + '&password=' + password + '&captcha=' + captchaCode;
- this.ajax.post(signInUrl, postData, function (response) {
- if (response.status == 'signedin') {
- window.location.hash = response.url;
- } else {
- mdui.snackbar({
- message: response.info
- });
- }
- })
- }.bind(this), false)
- },
- signup: function () {
- let html = document.querySelector('html'),
- body = document.querySelector('body');
- html.className = 'index';
- body.className = 'mdui-theme-primary-blue mdui-theme-accent-light-blue index';
- mdui.JQ(body).empty();
- mdui.JQ(body).append(`
- <header class="mdui-appbar mdui-shadow-0">
- <div class="mdui-toolbar mdui-color-theme">
- <a class="mdui-typo-headline" href="#">CloudSQL Admin Panel</a>
- <div class="mdui-toolbar-spacer"></div>
- <a class="mdui-btn mdui-ripple mdui-ripple-white" href="#/signin">登陆</a>
- <a class="mdui-btn mdui-ripple mdui-ripple-white" href='#/signup'>注册</a>
- </div>
- </header>
- <main style="height: 85%;">
- <div class="mdui-container main-section">
- <h2 class="mdui-typo-display-1 main-section-tittle">用户注册</h2>
- <div class="mdui-container mdui-col-md-8 mdui-center main-sub-section">
- <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
- <i class="mdui-icon material-icons">person</i>
- <input class="mdui-textfield-input" name="userid" type="text" placeholder="用户名/邮箱" />
- </div>
- <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
- <i class="mdui-icon material-icons">lock</i>
- <input class="mdui-textfield-input" name="password" type="password" placeholder="密码" />
- </div>
- <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row">
- <i class="mdui-icon material-icons">code</i>
- <input class="mdui-textfield-input" name="invitecode" type="text" placeholder="邀请码" />
- </div>
- <div class="mdui-col-md-6 mdui-center mdui-textfield main-input-row captcha-row">
- <img src="http://admin.cloudsql.1473.cn/v1/captcha?` + new Date().getTime() + `">
- <input class="mdui-textfield-input" name="captcha" type="text" placeholder="验证码" />
- </div>
- <div class="mdui-col-md-6 mdui-center mdui-typo main-a-row">
- <a href="#/signin">已有账户,点击登陆</a>
- </div>
- <div class="mdui-col-md-6 mdui-center main-button-row">
- <button class="mdui-btn mdui-btn-block mdui-color-theme-accent">注册</button>
- </div>
- </div>
- </div>
- </main>
- <footer>
- <div class="footer-copyright">Copyright © 2016 - 2017 <a href="/">CloudSQL</a> All rights reserved.</div>
- </footer>
- `)
- //验证码刷新逻辑
- let captchaImg = document.querySelector('.captcha-row img');
- captchaImg.addEventListener('click', function () {
- this.src = 'http://admin.cloudsql.1473.cn/v1/captcha' + '?' + new Date().getTime(); //加入当前时间以避免缓存
- }, false);
- let submitButton = document.querySelector('.main-button-row button');
- submitButton.addEventListener('click', function () {
- let signupUrl = 'http://admin.cloudsql.1473.cn/v1/signup',
- userId = document.querySelector('input[name=userid]').value,
- password = document.querySelector('input[name=password]').value,
- captchaCode = document.querySelector('input[name=captcha]').value,
- postData = 'userId=' + userId + '&password=' + password + '&captcha=' + captchaCode;
-
- this.ajax.post(signupUrl, postData, function (response) {
- if (response.status == 'successed') {
- mdui.snackbar({
- message: response.info
- });
- window.location.hash = response.url;
- } else {
- mdui.snackbar({
- message: response.info
- });
- }
- })
- }.bind(this), false)
- },
- index: function () {
- let contentAera = document.querySelector('.mdui-container.main-container'),
- getUserCountUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/getUserCount',
- getDatabaseCountUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/getDatabaseCount',
- content = `
- <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">
- <div class="mdui-col mdui-clearfix main-status-col">
- <div class="mdui-float-left icon-row">
- <i class="mdui-list-item-icon mdui-icon material-icons">person</i>
- </div>
- <div class="mdui-float-left info-row">
- <div class="name-col">用户数量</div>
- <div id="numberOfUser" class="value-col">1024</div>
- </div>
- </div>
- <div class="mdui-col mdui-clearfix main-status-col">
- <div class="mdui-float-left icon-row">
- <i class="mdui-list-item-icon mdui-icon material-icons">view_list</i>
- </div>
- <div class="mdui-float-left info-row">
- <div class="name-col">数据库数量</div>
- <div id="numberOfDatabse" class="value-col">1024</div>
- </div>
- </div>
- <div class="mdui-col mdui-clearfix main-status-col">
- <div class="mdui-float-left icon-row">
- <i class="mdui-list-item-icon mdui-icon material-icons">cloud</i>
- </div>
- <div class="mdui-float-left info-row">
- <div class="name-col">服务状态</div>
- <div class="value-col">正常</div>
- </div>
- </div>
- </div>
- `;
- mdui.JQ(contentAera).append(content);
- this.ajax.get(getUserCountUrl, function (response) {
- document.querySelector('#numberOfUser').innerText = response.data.results;
- });
- this.ajax.get(getDatabaseCountUrl, function (response) {
- document.querySelector('#numberOfDatabse').innerText = response.data.results;
- });
- },
- databaseList: function (arg) {
- let contentAera = document.querySelector('.mdui-container.main-container'),
- getDatabaseListUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList';
- if (!this.arguments.arg.argStr) {
- this.arguments.arg = {
- argStr: 'pageIndex=1&orderBy=databaseName',
- argObj: {
- 'pageIndex': 1,
- 'orderBy': 'databaseName',
- 'search': ''
- }
- }
- }
- getDatabaseListUrl += '?' + this.arguments.arg.argStr;
- mdui.JQ(contentAera).empty();
- mdui.JQ(contentAera).append(`
- <h2 class="mdui-text-color-theme">用户数据库列表</h2>
- <div class="mdui-col-xs-12 mdui-table-fluid">
- <div class="mdui-toolbar table-tools-bar">
- <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-menu="{target: '#tools-bar-attr'}">
- <i class="mdui-icon material-icons">menu</i>
- </a>
- <span class="mdui-typo-title">工具栏</span>
- <div class="mdui-toolbar-spacer"></div>
- <div class="mdui-textfield mdui-textfield-expandable table-search-row">
- <button id="serach" class="mdui-textfield-icon mdui-btn mdui-btn-icon">
- <i class="mdui-icon material-icons">search</i>
- </button>
- <input id="search-inner" class="mdui-textfield-input" type="text" placeholder="数据库名/用户ID/创建时间" />
- <button id="serach-done" class="mdui-textfield-close mdui-btn mdui-btn-icon">
- <i class="mdui-icon material-icons">check</i>
- </button>
- </div>
- <a id="refresh" class="mdui-btn mdui-btn-icon">
- <i class="mdui-icon material-icons">refresh</i>
- </a>
-
- <ul class="mdui-menu" id="tools-bar-attr">
- <li class="mdui-menu-item" disabled>
- <a href="javascript:;">排序方式</a>
- </li>
- <li id="userId" class="mdui-menu-item">
- <a href="javascript:;" class="mdui-ripple">用户ID</a>
- </li>
- <li id="databaseName" class="mdui-menu-item">
- <a href="javascript:;" class="mdui-ripple">数据库名称</a>
- </li>
- <li id="createDate" class="mdui-menu-item">
- <a href="javascript:;" class="mdui-ripple">数据库创建时间</a>
- </li>
- <li class="mdui-divider"></li>
- <li class="mdui-menu-item" disabled>
- <a href="javascript:;">操作</a>
- </li>
- <li class="mdui-menu-item">
- <a href="javascript:;" class="mdui-ripple">删除数据库</a>
- </li>
- <li class="mdui-menu-item">
- <a href="javascript:;" class="mdui-ripple">清空数据库</a>
- </li>
- </ul>
- </div>
- <table class="mdui-table mdui-table-selectable">
- <thead>
- <tr>
- <th>数据库名</th>
- <th>用户ID</th>
- <th>用户名称</th>
- <th>创建时间</th>
- <th class="mdui-table-col-numeric">操作</th>
- </tr>
- </thead>
- </table>
- <div class="mdui-col-xs-12 mdui-clearfix page-button-row">
- <div class="mdui-float-right">
- <span class="mdui-float-left">共</span>
- <b class="mdui-float-left"></b>
- <span class="mdui-float-left">页,当前为</span>
- <input class="mdui-float-left" name="pageNum" type="text" />
- <span class="mdui-float-left">页</span>
- <button class="mdui-float-left mdui-btn mdui-btn-raised mdui-ripple">跳转</button>
- </div>
- </div>
- </div>
- `);
- let table = document.querySelector('table'),
- searchInner = document.querySelector('#search-inner'),
- searchDoneButton = document.querySelector('#serach-done'),
- refreshButton = document.querySelector('#refresh'),
- orderByUserIdButton = document.querySelector('#tools-bar-attr > li:nth-child(2)'),
- orderByDbNameButton = document.querySelector('#tools-bar-attr > li:nth-child(3)'),
- orderByCreateDateButton = document.querySelector('#tools-bar-attr > li:nth-child(4)'),
- dropActionButton = document.querySelector('#tools-bar-attr > li:nth-child(7)'),
- emptyActionButton = document.querySelector('#tools-bar-attr > li:nth-child(8)'),
- menuOrderBut = [orderByUserIdButton, orderByDbNameButton, orderByCreateDateButton];
- searchDoneButton.addEventListener('click', (function () {
- this.arguments.arg.argObj.search = searchInner.value;
- let argStr = this.arguments.computed();
- window.location.hash = '/index/databaseList?' + argStr;
- }).bind(this), false);
- refreshButton.addEventListener('click', function () {
- window.location.reload();
- }, false);
- menuOrderBut.forEach(function (item, index) {
- item.addEventListener('click', (function () {
- this.arguments.arg.argObj.orderBy = item.id;
- let argStr = this.arguments.computed();
- window.location.hash = '/index/databaseList?' + argStr;
- }).bind(this), false);
- }, this);
- dropActionButton.addEventListener('click', function () {
- let selectedRow = document.querySelectorAll('tbody tr.mdui-table-row-selected');
- if (selectedRow.length <= 0) {
- mdui.dialog({
- title: '错误',
- content: '您未选择操作对象,无法删除数据库。',
- history: false,
- buttons: [{
- text: '关闭'
- }]
- });
- } else {
- mdui.dialog({
- title: '警告',
- content: '请确保您清楚您当前正在执行一个危险操作操作',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/dropDatabase',
- successAry = [],
- faildAry = [];
- selectedRow.forEach(function (item, index, array) {
- let data = 'dbName=' + item.children[1].innerText;
- this.ajax.post(dropDatabaseUrl, data, function (response) {
- if (response.status == 'success') {
- successAry.push(item.children[1].innerText);
- } else {
- faildAry.push(item.children[1].innerText);
- }
- if (successAry.length + faildAry.length == array.length) {
- let dialogContent = `
- <ul class="mdui-list">
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">执行成功</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + successAry.join() + `</div>
- </div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">执行失败</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + faildAry.join() + `</div>
- </div>
- </li>
- </ul>
- `;
- mdui.dialog({
- title: '完成',
- content: dialogContent,
- history: false,
- buttons: [{
- text: '关闭',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- }
- });
- }, this);
- }
- },
- {
- text: '取消'
- }
- ]
- });
- }
- }, false)
- emptyActionButton.addEventListener('click', function () {
- let selectedRow = document.querySelectorAll('tbody tr.mdui-table-row-selected');
- if (selectedRow.length <= 0) {
- mdui.dialog({
- title: '错误',
- content: '您未选择操作对象,无法删除数据库。',
- history: false,
- buttons: [{
- text: '关闭'
- }]
- });
- } else {
- mdui.dialog({
- title: '警告',
- content: '请确保您清楚您当前正在执行一个危险操作操作',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/emptyDatabase',
- successAry = [],
- faildAry = [];
- selectedRow.forEach(function (item, index, array) {
- let data = 'dbName=' + item.children[1].innerText;
- this.ajax.post(dropDatabaseUrl, data, function (response) {
- if (response.status == 'success') {
- successAry.push(item.children[1].innerText);
- } else {
- faildAry.push(item.children[1].innerText);
- }
- if (successAry.length + faildAry.length == array.length) {
- let dialogContent = `
- <ul class="mdui-list">
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">执行成功</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + successAry.join() + `</div>
- </div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">执行失败</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + faildAry.join() + `</div>
- </div>
- </li>
- </ul>
- `;
- mdui.dialog({
- title: '完成',
- content: dialogContent,
- history: false,
- buttons: [{
- text: '关闭',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- }
- });
- }, this);
- }
- },
- {
- text: '取消'
- }
- ]
- });
- }
- }, false)
- //AJAX向后台数据接口获取数据后执行回调函数
- this.ajax.get(getDatabaseListUrl, function (response) {
- if (response.status == 'success') {
- let tableBody = document.createElement('tbody'),
- results = response.data.results,
- resultsCount = response.data.count;
- if (resultsCount === 0) {
- let subContentAera = document.querySelector('.mdui-col-xs-12.mdui-table-fluid'),
- pageButRow = document.querySelector('.mdui-col-xs-12.mdui-clearfix.page-button-row');
- mdui.JQ(table).remove();
- mdui.JQ(pageButRow).remove();
- mdui.JQ(subContentAera).append(`<p style="text-align: center;">暂无数据</p>`);
- } else {
- results.forEach(function (item, index, array) {
- let resultsAry = [],
- detailBut = document.createElement('button'),
- removeBut = document.createElement('button');
- detailBut.className = 'mdui-btn';
- removeBut.className = 'mdui-btn mdui-color-theme-accent';
- detailBut.innerText = '查看详情';
- removeBut.innerText = '删除';
- detailBut.addEventListener('click', function () {
- let getDatabaseTablesUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/getDatabaseAllTables?dbName=' + item.dbName;
- this.ajax.get(getDatabaseTablesUrl, function (response) {
- let tablesAry = [];
- response.data.results.forEach(function (_item) {
- tablesAry.push(_item.TABLE_NAME);
- });
- if (tablesAry.length === 0) {
- tablesAry.push('该数据库无数据表信息');
- }
- let content = `
- <ul class="mdui-list">
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">数据库名称</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + item.dbName + `</div>
- </div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">数据库拥有者ID</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + item.dbOwnUuid + `</div>
- </div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">数据库创建时间</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + item.createDate + `</div>
- </div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">数据库数据表列表</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + tablesAry.join(' ') + `</div>
- </div>
- </li>
- </ul>
- `;
- mdui.dialog({
- title: '数据库详情',
- content: content,
- history: false,
- buttons: [{
- text: '清空数据库',
- onClick: function (inst) {
- mdui.dialog({
- title: '警告',
- content: '请确保您清楚您当前正在执行一个危险操作操作',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- let emptyDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/emptyDatabase',
- data = 'dbName=' + item.dbName;
- this.ajax.post(emptyDatabaseUrl, data, function (response) {
- if (response.status == 'success') {
- mdui.dialog({
- content: '执行成功',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- } else {
- mdui.dialog({
- content: '执行失败',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- }
- });
- }
- },
- {
- text: '取消'
- }
- ]
- });
- }
- },
- {
- text: '删除数据库',
- onClick: function (inst) {
- mdui.dialog({
- title: '警告',
- content: '请确保您清楚您当前正在执行一个危险操作操作',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/dropDatabase',
- data = 'dbName=' + item.dbName;
- this.ajax.post(dropDatabaseUrl, data, function (response) {
- if (response.status == 'success') {
- mdui.dialog({
- content: '执行成功',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- } else {
- mdui.dialog({
- content: '执行失败',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- }
- });
- }
- },
- {
- text: '取消'
- }
- ]
- });
- }
- },
- {
- text: '关闭'
- }
- ]
- });
- });
- }.bind(this), false);
- removeBut.addEventListener('click', function () {
- mdui.dialog({
- title: '警告',
- content: `
- <p>请确保您清楚您当前正在执行一个危险操作操作</p>
- <p>您确定要删除DB: <b>` + item.dbName + `</b> 吗?</p>
- `,
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/databaseList/dropDatabase',
- data = 'dbName=' + item.dbName;
- this.ajax.post(dropDatabaseUrl, data, function (response) {
- if (response.status == 'success') {
- mdui.dialog({
- content: '执行成功',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- } else {
- mdui.dialog({
- content: '执行失败',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- }
- });
- }
- },
- {
- text: '取消'
- }
- ]
- });
- }, false);
- let actionColContent = [detailBut, removeBut];
- resultsAry.push(item.dbName, item.dbOwnUuid, item.account, item.createDate, actionColContent);
- let tr = this.printer.tableGenerator(resultsAry);
- tableBody.appendChild(tr);
- }, this);
- table.appendChild(tableBody);
- mdui.updateTables(table);
- let pageShowCol = document.querySelector('.mdui-col-xs-12.page-button-row b'),
- pageNumOfNow = document.querySelector('.mdui-col-xs-12.page-button-row input'),
- jumpButton = document.querySelector('.mdui-col-xs-12.page-button-row button'),
- pageNum = Math.ceil(resultsCount / 20);
- pageShowCol.innerHTML = pageNum;
- pageNumOfNow.value = this.arguments.arg.argObj.pageIndex;
- jumpButton.addEventListener('click', (function () {
- this.arguments.arg.argObj.pageIndex = pageNumOfNow.value;
- let argStr = this.arguments.computed();
- window.location.hash = '/index/databaseList?' + argStr;
- }).bind(this), false);
- }
- } else {
- mdui.snackbar({
- message: response.info
- });
- }
- }.bind(this));
- },
- userList: function () {
- let contentAera = document.querySelector('.mdui-container.main-container'),
- getUserListUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList';
- if (!this.arguments.arg.argStr) {
- this.arguments.arg = {
- argStr: 'pageIndex=1&orderBy=userName',
- argObj: {
- 'pageIndex': 1,
- 'orderBy': 'userName',
- 'search': ''
- }
- }
- }
- getUserListUrl += '?' + this.arguments.arg.argStr;
- mdui.JQ(contentAera).empty();
- mdui.JQ(contentAera).append(`
- <h2 class="mdui-text-color-theme">用户数据库列表</h2>
- <div class="mdui-col-xs-12 mdui-table-fluid">
- <div class="mdui-toolbar table-tools-bar">
- <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-menu="{target: '#tools-bar-attr'}">
- <i class="mdui-icon material-icons">menu</i>
- </a>
- <span class="mdui-typo-title">工具栏</span>
- <div class="mdui-toolbar-spacer"></div>
- <div class="mdui-textfield mdui-textfield-expandable table-search-row">
- <button id="serach" class="mdui-textfield-icon mdui-btn mdui-btn-icon">
- <i class="mdui-icon material-icons">search</i>
- </button>
- <input id="search-inner" class="mdui-textfield-input" type="text" placeholder="用户ID/用户昵称/数据库密码" />
- <button id="serach-done" class="mdui-textfield-close mdui-btn mdui-btn-icon">
- <i class="mdui-icon material-icons">check</i>
- </button>
- </div>
- <a id="refresh" class="mdui-btn mdui-btn-icon">
- <i class="mdui-icon material-icons">refresh</i>
- </a>
-
- <ul class="mdui-menu" id="tools-bar-attr">
- <li class="mdui-menu-item" disabled>
- <a href="javascript:;">排序方式</a>
- </li>
- <li id="userId" class="mdui-menu-item">
- <a href="javascript:;" class="mdui-ripple">用户ID</a>
- </li>
- <li id="userNickName" class="mdui-menu-item">
- <a href="javascript:;" class="mdui-ripple">用户昵称</a>
- </li>
- <li id="createDate" class="mdui-menu-item">
- <a href="javascript:;" class="mdui-ripple">用户创建时间</a>
- </li>
- <li class="mdui-divider"></li>
- <li class="mdui-menu-item" disabled>
- <a href="javascript:;">操作</a>
- </li>
- <li class="mdui-menu-item">
- <a href="javascript:;" class="mdui-ripple">删除用户</a>
- </li>
- </ul>
- </div>
-
- <table class="mdui-table mdui-table-selectable">
- <thead>
- <tr>
- <th>用户昵称</th>
- <th>用户ID</th>
- <th>数据库密码</th>
- <th>用户注册时间</th>
- <th class="mdui-table-col-numeric ">操作</th>
- </tr>
- </thead>
- </table>
- <div class="mdui-col-xs-12 mdui-clearfix page-button-row">
- <div class="mdui-float-right">
- <span class="mdui-float-left">共</span>
- <b class="mdui-float-left"></b>
- <span class="mdui-float-left">页,当前为</span>
- <input class="mdui-float-left" name="pageNum" type="text" />
- <span class="mdui-float-left">页</span>
- <button class="mdui-float-left mdui-btn mdui-btn-raised mdui-ripple">跳转</button>
- </div>
- </div>
- </div>
- `);
- //生成表格
- let table = document.querySelector('table'),
- searchInner = document.querySelector('#search-inner'),
- searchDoneButton = document.querySelector('#serach-done'),
- refreshButton = document.querySelector('#refresh'),
- orderByUserIdButton = document.querySelector('#tools-bar-attr > li:nth-child(2)'),
- orderByUserNameButton = document.querySelector('#tools-bar-attr > li:nth-child(3)'),
- orderByUserCreateButton = document.querySelector('#tools-bar-attr > li:nth-child(4)')
- removeActionButton = document.querySelector('#tools-bar-attr > li:nth-child(7)'),
- menuOrderBut = [orderByUserIdButton, orderByUserNameButton, orderByUserCreateButton];
- searchDoneButton.addEventListener('click', (function () {
- this.arguments.arg.argObj.search = searchInner.value;
- let argStr = this.arguments.computed();
- window.location.hash = '/index/userList?' + argStr;
- }).bind(this), false);
- refreshButton.addEventListener('click', function () {
- window.location.reload();
- }, false);
- removeActionButton.addEventListener('click', function () {
- let selectedRow = document.querySelectorAll('tbody tr.mdui-table-row-selected');
- if (selectedRow.length <= 0) {
- mdui.dialog({
- title: '错误',
- content: '您未选择操作对象,无法删除数据库。',
- history: false,
- buttons: [{
- text: '关闭'
- }]
- });
- } else {
- mdui.dialog({
- title: '警告',
- content: '请确保您清楚您当前正在执行一个危险操作操作',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- let dropDatabaseUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/removeUser',
- successAry = [],
- faildAry = [];
- selectedRow.forEach(function (item, index, array) {
- let data = 'userId=' + item.children[2].innerText + '&userNickName=' + item.children[1].innerText;
- this.ajax.post(dropDatabaseUrl, data, function (response) {
- if (response.status == 'success') {
- successAry.push(item.children[1].innerText);
- } else {
- faildAry.push(item.children[1].innerText);
- }
- if (successAry.length + faildAry.length == array.length) {
- let dialogContent = `
- <ul class="mdui-list">
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">执行成功</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + successAry.join() + `</div>
- </div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">执行失败</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + faildAry.join() + `</div>
- </div>
- </li>
- </ul>
- `;
- mdui.dialog({
- title: '完成',
- content: dialogContent,
- history: false,
- buttons: [{
- text: '关闭',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- }
- });
- }, this);
- }
- },
- {
- text: '取消'
- }
- ]
- });
- }
- }, false)
- menuOrderBut.forEach(function (item, index) {
- item.addEventListener('click', (function () {
- this.arguments.arg.argObj.orderBy = item.id;
- let argStr = this.arguments.computed();
- window.location.hash = '/index/userList?' + argStr;
- }).bind(this), false);
- }, this);
- //AJAX向后台数据接口获取数据后执行回调函数
- this.ajax.get(getUserListUrl, function (response) {
- if (response.status == 'success') {
- let tableBody = document.createElement('tbody'),
- results = response.data.results,
- resultsCount = response.data.count;
- if (resultsCount === 0) {
- let subContentAera = document.querySelector('.mdui-col-xs-12.mdui-table-fluid'),
- pageButRow = document.querySelector('.mdui-col-xs-12.mdui-clearfix.page-button-row');
- mdui.JQ(table).remove();
- mdui.JQ(pageButRow).remove();
- mdui.JQ(subContentAera).append(`<p style="text-align: center;">暂无数据</p>`);
- } else {
- }
- results.forEach(function (item, index) {
- let resultsAry = [],
- detailBut = document.createElement('button'),
- removeBut = document.createElement('button');
- detailBut.className = 'mdui-btn';
- removeBut.className = 'mdui-btn mdui-color-theme-accent';
- detailBut.innerText = '查看详情';
- removeBut.innerText = '删除';
- detailBut.addEventListener('click', function () {
- let getUserOwnDbUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/getUserOwnDb?userId=' + item.uuid;
- this.ajax.get(getUserOwnDbUrl, function (response) {
- let dbAry = [];
- response.data.results.forEach(function (_item) {
- dbAry.push(_item.dbName);
- });
- if (dbAry.length === 0) {
- dbAry.push('无');
- }
- let content = `
- <ul class="mdui-list">
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">用户昵称</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + item.account + `</div>
- </div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">用户UUID</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + item.uuid + `</div>
- </div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">数据库密码</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + item.dbpassword + `</div>
- </div>
- </li>
- <li class="mdui-list-item mdui-ripple">
- <div class="mdui-list-item-content">
- <div class="mdui-list-item-title mdui-list-item-one-line">数据库列表</div>
- <div class="mdui-list-item-text mdui-list-item-two-line">` + dbAry.join(' ') + `</div>
- </div>
- </li>
- </ul>
- `;
- mdui.dialog({
- title: '用户详情',
- content: content,
- history: false,
- buttons: [{
- text: '查看数据库',
- onClick: function (inst) {
- window.location.hash = '/index/databaseList?search=' + item.uuid;
- }
- },
- {
- text: '删除用户',
- onClick: function (inst) {
- mdui.dialog({
- title: '警告',
- content: '请确保您清楚您当前正在执行一个危险操作操作',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- let removeUserUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/removeUser',
- data = 'userId=' + item.uuid + '&userNickName=' + item.account;
- this.ajax.post(removeUserUrl, data, function (response) {
- if (response.status == 'success') {
- mdui.dialog({
- content: '执行成功',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- } else {
- mdui.dialog({
- content: '执行失败',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- }
- });
- }
- },
- {
- text: '取消'
- }
- ]
- });
- }
- },
- {
- text: '关闭'
- }
- ]
- });
- });
- }.bind(this), false);
- removeBut.addEventListener('click', function () {
- mdui.dialog({
- title: '警告',
- content: `
- <p>请确保您清楚您当前正在执行一个危险操作操作</p>
- <p>您确定要删除用户: <b>` + item.account + `</b> 吗?</p>
- `,
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- let removeUserUrl = 'http://admin.cloudsql.1473.cn/v1/index/userList/removeUser',
- data = 'userId=' + item.uuid + '&userNickName=' + item.account;
- this.ajax.post(removeUserUrl, data, function (response) {
- if (response.status == 'success') {
- mdui.dialog({
- content: '执行成功',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- } else {
- mdui.dialog({
- content: '执行失败',
- history: false,
- buttons: [{
- text: '确定',
- onClick: function (inst) {
- window.location.reload();
- }
- }]
- });
- }
- });
- }
- },
- {
- text: '取消'
- }
- ]
- });
- }.bind(this), false);
- let actionColContent = [detailBut, removeBut];
- resultsAry.push(item.account, item.uuid, item.dbpassword, item.createDate, actionColContent);
- let tr = this.printer.tableGenerator(resultsAry);
- tableBody.appendChild(tr);
- }, this);
- table.appendChild(tableBody);
- mdui.updateTables(table);
- let pageShowCol = document.querySelector('.mdui-col-xs-12.page-button-row b'),
- pageNumOfNow = document.querySelector('.mdui-col-xs-12.page-button-row input'),
- jumpButton = document.querySelector('.mdui-col-xs-12.page-button-row button'),
- pageNum = Math.ceil(resultsCount / 20);
- pageShowCol.innerHTML = pageNum;
- pageNumOfNow.value = this.arguments.arg.argObj.pageIndex;
- jumpButton.addEventListener('click', (function () {
- this.arguments.arg.argObj.pageIndex = pageNumOfNow.value;
- let argStr = this.arguments.computed();
- window.location.hash = '/index/userList?' + argStr;
- }).bind(this), false);
- } else {
- mdui.snackbar({
- message: response.info
- });
- }
- }.bind(this));
- }
- }
- this.permissionCheck = {
- /**
- * 检测账户登录状态
- *
- * @param {any} next 回调函数
- */
- signStatus: function (next) {
- this.ajax.get('http://admin.cloudsql.1473.cn/v1/index/signStatus', function (response) {
- if (response.status == 'signedin') {
- next.call(this);
- } else {
- window.location.hash = '/signin';
- }
- }.bind(this));
- }
- }
- this.routes = {
- '/': function () {
- window.location.hash = '#/signin';
- },
- '/404': this.printer.pageNotFound,
- '/signin': this.printer.signin.bind(this),
- '/signup': this.printer.signup.bind(this),
- '/index': this.printer.indexInit.bind(this, this.printer.index),
- '/index/databaseList': this.printer.indexInit.bind(this, this.printer.databaseList),
- '/index/userList': this.printer.indexInit.bind(this, this.printer.userList)
- };
- this.arguments = {
- arg: {},
- computed: function () {
- this.arg.argStr = '';
- Object.keys(this.arg.argObj).forEach(function (item, index, array) {
- if (index + 1 === array.length) {
- this.arg.argStr += item + '=' + this.arg.argObj[item];
- } else {
- this.arg.argStr += item + '=' + this.arg.argObj[item] + '&';
- }
- }, this);
- return this.arg.argStr;
- }
- };
- this.route = function (path, callback) {
- this.routes[path] = callback || function () {}
- }
- this.refresh = function () {
- this.currentUrl = location.hash.slice(1) || '/';
- this.currentUrl = this.currentUrl.split("?");
- //路由内无对应路径则跳转至404页面
- if (this.currentUrl.length === 1 && this.routes[this.currentUrl[0]]) {
- this.routes[this.currentUrl[0]]();
- this.arguments.arg = {};
- } else if (this.currentUrl.length === 2 && this.routes[this.currentUrl[0]]) {
- this.routes[this.currentUrl[0]]();
- let argStr = this.currentUrl[1];
- this.arguments.arg = {
- argStr: argStr,
- argObj: {}
- }
- argStr.split("&").forEach(function (item, index) {
- let ary = item.split("=");
- this.arguments.arg.argObj[ary[0]] = ary[1];
- }, this);
- } else {
- this.routes['/404']();
- }
- }
- this.init = function () {
- const windowLoad = this.handleEvent('load', {
- onElement: window,
- withCallback: this.refresh.bind(this)
- });
- const hashChange = this.handleEvent('hashchange', {
- onElement: window,
- withCallback: this.refresh.bind(this)
- });
- }
- }
- CloudAdmin.router.prototype.ajax = {
- /**
- * 发送Get请求
- *
- * @param {!string} url 请求地址
- * @param {?function} next 回调函数
- */
- get: function (url, next) {
- let xhr = new XMLHttpRequest();
- if (url.includes('?')) {
- // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
- xhr.open('GET', url + '&date=' + new Date().getTime(), true);
- } else {
- // 添加一个date参数加入当前时间以避免缓存
- xhr.open('GET', url + '?' + new Date().getTime(), true);
- }
- xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
- xhr.withCredentials = true;
- xhr.responseType = 'json';
- xhr.onload = function (res) {
- // 获取请求接口返回值
- let response = res.target.response;
- next.call(this, response);
- };
- xhr.send();
- },
- /**
- * 发送Post请求
- *
- * @param {!string} url 请求地址
- * @param {?string} data post请求参数
- * @param {?function} next 回调函数
- */
- post: function (url, data, next) {
- let xhr = new XMLHttpRequest();
- if (url.includes('?')) {
- // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存
- xhr.open("POST", url + '&' + new Date().getTime(), true);
- } else {
- // 添加一个date参数加入当前时间以避免缓存
- xhr.open("POST", url + '?' + new Date().getTime(), true);
- }
- xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies
- xhr.withCredentials = true;
- xhr.responseType = 'json';
- xhr.onload = function (res) {
- // 获取请求接口返回值
- let response = res.target.response;
- next.call(this, response);
- };
- xhr.send(data);
- }
- }
- /**
- * 监听事件处理函数
- *
- * @param {string} eventName 表示监听事件类型的字符串
- * @param {object} [{ onElement, withCallback, useCapture = false }={}] 监听对象 回调函数 useCapture
- * @param {function} thisArg
- * @returns function
- */
- CloudAdmin.router.prototype.handleEvent = function (eventName, {
- onElement,
- withCallback,
- useCapture = false
- } = {}, thisArg) {
- const element = onElement || document.documentElement;
- function handler(event) {
- if (typeof withCallback === 'function') {
- withCallback.call(thisArg, event);
- }
- }
- handler.destroy = function () {
- return element.removeEventListener(eventName, handler, useCapture);
- }
- element.addEventListener(eventName, handler, useCapture);
- return handler
- }
- CloudAdmin.router.prototype.event = {
- dom: [],
- /**
- * 调用 CloudAdmin.router.prototype.handleEvent.handler.destroy 移除DOM的监听事件
- *
- * @param {any} ary 存放DOM的数组
- */
- removeEventListener: function (ary) {
-
- }
- }
- //创建并初始化前端路由
- let r = new CloudAdmin.router();
- r.init();
|