admin.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. $(document).ready(function() {
  2. $('.ui.dropdown').dropdown();
  3. $('#login-form')
  4. .form({
  5. fields: {
  6. account: {
  7. identifier : 'account',
  8. rules: [
  9. {
  10. type : 'empty',
  11. prompt : '账户名不能为空'
  12. },
  13. {
  14. type : 'length[5]',
  15. prompt : '账户名长度不得少于5位'
  16. }
  17. ]
  18. },
  19. password: {
  20. identifier : 'password',
  21. rules: [
  22. {
  23. type : 'empty',
  24. prompt : '密码不能为空'
  25. },
  26. {
  27. type : 'length[8]',
  28. prompt : '密码长度不得少于8位'
  29. }
  30. ]
  31. },
  32. captcha: {
  33. identifier : 'captcha-text',
  34. rules: [
  35. {
  36. type : 'empty',
  37. prompt : '验证码不能为空'
  38. },
  39. {
  40. type : 'length[6]',
  41. prompt : '验证码长度不得少于6位'
  42. }
  43. ]
  44. }
  45. }
  46. });
  47. $('#form-search-url').form({
  48. fields:{
  49. url: {rules:[{
  50. type:'empty',
  51. prompt:'链接不能为空'
  52. }]}
  53. }
  54. });
  55. $('#logs-search-start-date').calendar({
  56. type:'date',
  57. text: {
  58. months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  59. monthsShort: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  60. },
  61. formatter: {
  62. date: function (date, settings) {
  63. if (!date) return '';
  64. var day = date.getDate();
  65. var month = date.getMonth() + 1;
  66. var year = date.getFullYear();
  67. return year + '-' + month + '-' + day;
  68. }
  69. }
  70. });
  71. $('#logs-search-end-date').calendar({
  72. type:'date',
  73. text: {
  74. months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  75. monthsShort: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  76. },
  77. formatter: {
  78. date: function (date, settings) {
  79. if (!date) return '';
  80. var day = date.getDate();
  81. var month = date.getMonth() + 1;
  82. var year = date.getFullYear();
  83. return year + '-' + month + '-' + day;
  84. }
  85. }
  86. });
  87. $('#sidebar-menu').sidebar('attach events', '#sidebar-menu-toggler');
  88. $('#btn-new-shorturl-modal').click(function(){
  89. $('#new-shorturl-modal').modal('show');
  90. });
  91. $('#btn-gen-short-url').click(function() {
  92. var destUrl = $('#input_dest_url');
  93. var memo = $('#input_demo');
  94. var openType = $('#input_open_type')
  95. if( $.trim(destUrl.val()).length <= 0) {
  96. errorToast('目标链接不能为空!');
  97. destUrl.parent().addClass('error');
  98. return
  99. }
  100. var data = {
  101. "dest_url": $.trim(destUrl.val()),
  102. "memo": $.trim(memo.val()),
  103. "open_type": $.trim(openType.val()),
  104. };
  105. $.ajax({
  106. type: "POST",
  107. url: '/admin/urls/generate',
  108. data: data,
  109. dataType: 'json',
  110. success: function() {
  111. successToast('新建成功!')
  112. destUrl.val('');
  113. memo.val('');
  114. $('#new-shorturl-modal').modal('hide');
  115. },
  116. error: function(e) {
  117. errorToast($.parseJSON(e.responseText).message)
  118. }
  119. });
  120. });//end of #btn-gen-short-url click
  121. });
  122. function successToast(message) {
  123. $('body').toast({
  124. class: 'success',
  125. displayTime: 2500,
  126. message: message,
  127. showIcon:'exclamation circle',
  128. showProgress: 'bottom',
  129. onHidden: function() {location.reload()}
  130. });
  131. }
  132. function errorToast(message) {
  133. $('body').toast({
  134. class: 'error',
  135. displayTime: 2500,
  136. message: message,
  137. showIcon:'exclamation circle',
  138. showProgress: 'bottom'
  139. });
  140. }
  141. function enable_url(url,state) {
  142. var data = {
  143. "dest_url": url,
  144. "enable": state
  145. }
  146. $.ajax({
  147. type: "POST",
  148. url: '/admin/urls/state',
  149. data: data,
  150. dataType: 'json',
  151. success: function() {
  152. successToast('操作成功')
  153. },
  154. error: function(e) {
  155. errorToast($.parseJSON(e.responseText).message)
  156. }
  157. });
  158. }
  159. function delete_url(url) {
  160. $('body').modal('confirm','温馨提示','确认删除该短链接吗?<br/>本操作将会同步删除该短链接的所有访问日志。', function(choice){
  161. if (choice) {
  162. $.ajax({
  163. type:"POST",
  164. url: "/admin/urls/delete",
  165. data : {
  166. "short_url": url,
  167. },
  168. success: function() {
  169. successToast('操作成功,再见!')
  170. },
  171. error: function(e) {
  172. errorToast($.parseJSON(e.responseText).message)
  173. }
  174. });
  175. }
  176. });
  177. }
  178. function copy_url(url){
  179. navigator.clipboard.writeText(url);
  180. $('body').toast({
  181. class: 'success',
  182. displayTime: 2500,
  183. message: '复制成功',
  184. showIcon:'exclamation circle',
  185. showProgress: 'bottom'
  186. });
  187. }
  188. function reload_captcha() {
  189. $.ajax({
  190. type: "POST",
  191. url: '/captcha',
  192. dataType: 'json',
  193. success: function(r) {
  194. $('#captcha-image').html('<img src="/captcha/'+r.result+'.png" />');
  195. $('<input>').attr({type: 'hidden', value:r.result ,name: 'captcha-id'}).appendTo('#login-form');
  196. },
  197. error: function(e) {
  198. errorToast($.parseJSON(e.responseText).message)
  199. }
  200. });
  201. }
  202. function sign_out_config() {
  203. $('body').modal('confirm','温馨提示','确认退出 ohUrlShortener 管理后端吗?', function(choice){
  204. if (choice) {
  205. $.ajax({
  206. type:"POST",
  207. url: "/admin/logout",
  208. success: function() {
  209. successToast('操作成功,再见!')
  210. },
  211. error: function(e) {
  212. errorToast($.parseJSON(e.responseText).message)
  213. }
  214. });
  215. }
  216. });
  217. }
  218. function export_accesslog() {
  219. $('body').modal('confirm','温馨提示','确认导出访问日志?', function(choice){
  220. if (choice) {
  221. $("#form-export-logs").submit();
  222. }
  223. });
  224. }