admin.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * admin 后台js
  3. *
  4. */
  5. //字符串为空
  6. function isNull(str) {
  7. if (str == null || str == "" || str.length < 1)
  8. return true;
  9. else
  10. return false;
  11. }
  12. //获取 str 字符长度,中文单字2字符,英文1字符F
  13. function getblen(str) {
  14. var l = str.length;
  15. var blen = 0;
  16. for (i = 0; i < l; i++) {
  17. if ((str.charCodeAt(i) & 0xff00) != 0) {
  18. blen++;
  19. }
  20. blen++;
  21. }
  22. return blen;
  23. }
  24. //textarea 字数提示
  25. function textarealength(obj, toplength) {
  26. var note = $(obj).val();
  27. var blen = getblen(note);
  28. if (blen <= toplength) {
  29. $(".textarea-length").html(blen);
  30. } else {
  31. $(".textarea-length").css('color', 'red');
  32. $(".textarea-length").html(blen);
  33. }
  34. }
  35. // js 正则验证邮箱
  36. function testEmail(strEmail) {
  37. var myReg = /^[-a-z0-9\._]+@([-a-z0-9\-]+\.)+[a-z0-9]{2,3}$/i;
  38. if (myReg.test(strEmail)) return true;
  39. return false;
  40. }
  41. //检测邮箱格式
  42. function emailFormat(email) {
  43. return email.match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/) ? true : false;
  44. }
  45. //检测手机格式
  46. function phoneFormat(phone) {
  47. return phone.match(/^1(3|4|5|6|7|8)\d{9}$/) ? true : false;
  48. }
  49. //检测密码规则--6位以上
  50. function pwdFormat(pwd) {
  51. return pwd.match(/^.*(?=.{6,})/) ? true : false;
  52. }
  53. //检测密码规则--6位以上数字字母组合
  54. function pwdFormat6Mix(pwd) {
  55. return pwd.match(/^.*(?=.{5,})(?=.*\d)(?=.*[a-z]).*$/) ? true : false;
  56. }
  57. //必须字母开头,长度在6-18之间,只能包含字符(或下划线)、数字
  58. function pwdFormat6MixNew(pwd) {
  59. return pwd.match(/^[a-zA-Z]\w{5,17}$/) ? true : false;
  60. }
  61. // 删除条目
  62. function del(obj, id) {
  63. layer.confirm('确认要删除吗?', function (index) {
  64. $.post('delete', {
  65. 'id': id
  66. }, function (res) {
  67. if (res.code == 0) {
  68. $(obj).parents("tr").remove();
  69. topalert({
  70. type: 0,
  71. content: res.msg,
  72. speed: 1000
  73. });
  74. } else {
  75. topalert({
  76. type: 2,
  77. content: res.msg,
  78. speed: 2000
  79. });
  80. }
  81. layer.close(layer.index);
  82. }, 'json');
  83. });
  84. }
  85. //列表多选删除条目
  86. function del_all() {
  87. var checkbox = $('.text-c input[name="checkbox[]"]');
  88. var ids = new Array();
  89. checkbox.each(function (x) {
  90. if (this.checked)
  91. ids.push(this.value);
  92. })
  93. var length = ids.length;
  94. if (length == 0) {
  95. layer.msg('请选择要删除的选项', {
  96. icon: 5,
  97. time: 1000
  98. });
  99. return false;
  100. }
  101. layer.confirm('确认要删除选中项吗?', function (index) {
  102. $.post('delete', {
  103. 'id': ids
  104. }, function (data) {
  105. if (data.code == 0) {
  106. layer.msg(data.msg, {
  107. icon: 1,
  108. time: 2000
  109. });
  110. window.location.reload();
  111. } else {
  112. layer.msg(data.msg, {
  113. icon: 5,
  114. time: 2000
  115. });
  116. return false;
  117. }
  118. }, 'json');
  119. });
  120. }
  121. // 右上角提示框
  122. function topalert(options, callback) {
  123. var defaults = {
  124. type: 0,
  125. content: '弹窗内容',
  126. speed: "0",
  127. };
  128. var types = ['success', 'danger', 'error', 'info'];
  129. var options = $.extend(defaults, options);
  130. var length = $('.topalert').length;
  131. var htmlstr = '<div class="Huialert Huialert-' + types[defaults.type] + ' topalert topalert' + length + '"><i class="Hui-iconfont">&#xe6a6;</i>' + options.content + '</div>';
  132. if (options.speed == 0) {
  133. $(document.body).addClass("modal-open").append(htmlstr);
  134. $(".topalert" + length).fadeIn();
  135. } else {
  136. $(document.body).addClass("modal-open").append(htmlstr);
  137. $(".topalert" + length).fadeIn();
  138. setTimeout(function () {
  139. $(".topalert" + length).fadeOut("normal", complete => {
  140. $(".topalert" + length).remove();
  141. });
  142. }, options.speed);
  143. }
  144. if (callback) {
  145. callback();
  146. }
  147. }
  148. $(function () {
  149. // 全选
  150. $("input[name='allcheck']").change(function (e, x) {
  151. // console.log(this.checked);
  152. if (this.checked) {
  153. $('.text-c input[name="checkbox[]"]').prop("checked", true);
  154. } else {
  155. $('.text-c input[name="checkbox[]"]').prop("checked", false);
  156. }
  157. })
  158. // 图片预览
  159. $('.pictureview').hover(function () {
  160. console.log($(this).children('img').attr('src'));
  161. var url = $(this).children('img').attr('src');
  162. var img = '<img class="thumbnail" id="thumbview" src="' + url + '">';
  163. $(this).append(img);
  164. }, function () {
  165. $('#thumbview').remove();
  166. });
  167. // 改变状态
  168. $('.td-status .switch').on('switch-change', function () {
  169. var id = $(this).data('id');
  170. var data = {
  171. 'id': parseInt(id)
  172. };
  173. $.post('status', data, function (res) {
  174. if (res.code == 0) {
  175. topalert({
  176. type: 0,
  177. content: res.msg,
  178. speed: 1000
  179. });
  180. } else {
  181. topalert({
  182. type: 1,
  183. content: res.msg,
  184. speed: 1000
  185. });
  186. return false;
  187. }
  188. }, 'json');
  189. });
  190. // 排序
  191. $(".input-sort").change(function () {
  192. var sort = $(this).val();
  193. var id = $(this).data('id');
  194. console.log(id);
  195. console.log(sort);
  196. $.post('sort', {
  197. 'id': id,
  198. 'sort': sort
  199. }, function (res) {
  200. if (res.code == 0) {
  201. topalert({ type: 0, content: res.msg, speed: 2000 });
  202. } else {
  203. topalert({ type: 1, content: res.msg, speed: 2000 });
  204. return false;
  205. }
  206. }, 'json');
  207. });
  208. });