common.ui.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. let password1Encrypted = false;
  2. let password2Encrypted = false;
  3. let verifyEncrypted = false;
  4. /**
  5. * 密码改变时标记密码未加密。
  6. * Mark password unencrypted when password changes.
  7. *
  8. * @param event event
  9. * @access public
  10. * @return void
  11. */
  12. function changePassword(event)
  13. {
  14. const targetID = $(event.target).attr('id');
  15. if(targetID == 'password1') password1Encrypted = false;
  16. if(targetID == 'password2') password2Encrypted = false;
  17. if(targetID == 'verifyPassword') verifyEncrypted = false;
  18. }
  19. /**
  20. * 加密密码并记录密码强度和长度。
  21. * Encrypt password and record password strength and length.
  22. *
  23. * @access public
  24. * @return void
  25. */
  26. function encryptPassword()
  27. {
  28. $('#verifyPassword').on('change', function(){verifyEncrypted = false});
  29. const rand = $('input[name=verifyRand]').val();
  30. /* 加密当前登录用户的密码。*/
  31. /* Encrypt password of current user. */
  32. if($('input#verifyPassword').length > 0)
  33. {
  34. const password = $('input#verifyPassword').val().trim();
  35. if(password && !verifyEncrypted)
  36. {
  37. $('input#verifyPassword').val(md5(md5(password) + rand));
  38. verifyEncrypted = true;
  39. }
  40. }
  41. if($('#password1').length == 0 || $('#password2').length == 0) return;
  42. /* 加密新添加用户或被修改用户的密码 1,并记录密码强度和长度。*/
  43. /* Encrypt password 1 of new or modified user, and record password strength and length. */
  44. const password1 = $('#password1').val().trim();
  45. if(password1 && !password1Encrypted)
  46. {
  47. $('#password1').val(md5(password1) + rand);
  48. $("input[name=passwordStrength]").val(computePasswordStrength(password1));
  49. $("input[name=passwordLength]").val(password1.length);
  50. password1Encrypted = true;
  51. }
  52. /* 加密新添加用户或被修改用户的密码 2。*/
  53. /* Encrypt password 2 of new or modified user. */
  54. const password2 = $('#password2').val().trim();
  55. if(password2 && !password2Encrypted)
  56. {
  57. $('#password2').val(md5(password2) + rand);
  58. password2Encrypted = true;
  59. }
  60. }
  61. /**
  62. * 添加或编辑一个用户时切换显示选择所属公司或添加公司的输入框。
  63. * Toggle display company picker or input when add or edit a user.
  64. *
  65. * @param event event
  66. * @access public
  67. * @return void
  68. */
  69. function toggleNew(event)
  70. {
  71. const checked = $(event.target).prop('checked');
  72. $('[name="company"]').closest('.picker-box').toggleClass('hidden', checked);
  73. $('#newCompany').toggleClass('hidden', !checked);
  74. }
  75. /**
  76. * 更改界面类型时更新权限组。
  77. * Update group when change vision.
  78. *
  79. * @param event event
  80. * @access public
  81. * @return void
  82. */
  83. function changeVision(event)
  84. {
  85. let visions = [];
  86. $('input[name="visions[]"]:checked').each(function()
  87. {
  88. visions.push($(this).val());
  89. });
  90. const link = $.createLink('user', 'ajaxGetGroups', 'visions=' + visions);
  91. $.get(link, function(response)
  92. {
  93. const data = JSON.parse(response);
  94. const group = $('[name^="group"]').val();
  95. const $group = $('[name^="group"]').zui('picker');
  96. $group.render({items: data});
  97. $group.$.setValue(group);
  98. });
  99. }
  100. /**
  101. * 计算密码强度。
  102. *
  103. * @param string password
  104. * @access public
  105. * @return int
  106. */
  107. function computePasswordStrength(password)
  108. {
  109. if(password.length == 0) return 0;
  110. var strength = 0;
  111. var length = password.length;
  112. var complexity = new Array();
  113. for(i = 0; i < length; i++)
  114. {
  115. letter = password.charAt(i);
  116. var asc = letter.charCodeAt();
  117. if(asc >= 48 && asc <= 57)
  118. {
  119. complexity[0] = 1;
  120. }
  121. else if((asc >= 65 && asc <= 90))
  122. {
  123. complexity[1] = 2;
  124. }
  125. else if(asc >= 97 && asc <= 122)
  126. {
  127. complexity[2] = 4;
  128. }
  129. else
  130. {
  131. complexity[3] = 8;
  132. }
  133. }
  134. var sumComplexity = 0;
  135. for(i in complexity) sumComplexity += complexity[i];
  136. if((sumComplexity == 7 || sumComplexity == 15) && password.length >= 6) strength = 1;
  137. if(sumComplexity == 15 && password.length >= 10) strength = 2;
  138. return strength;
  139. }
  140. window.switchAccount = function(account)
  141. {
  142. link = $.createLink('user', method, 'account=' + account);
  143. if(method == 'dynamic') link = $.createLink('user', method, 'account=' + account + '&period=' + pageParams.period);
  144. if(method == 'todo') link = $.createLink('user', method, 'account=' + account + '&type=' + pageParams.type);
  145. if(method == 'story') link = $.createLink('user', method, 'account=' + account + '&storyType=' + pageParams.storyType);
  146. loadPage(link);
  147. };
  148. window.beforePageLoad = function(options)
  149. {
  150. if(options.load === 'table') options.selector += ',.dtable-sub-nav';
  151. }