setting.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. 'use strict';
  2. /**
  3. * 项目信息js
  4. *
  5. * @author EllisRan.
  6. * @date 2019/3/19
  7. * @version
  8. */
  9. $(document).ready(() => {
  10. // 启用和停用账号
  11. $('.account-switch-btn').on('click', function () {
  12. const data = {
  13. enable: $(this).hasClass('btn-outline-success') ? 1 : 0,
  14. id: $(this).data('account'),
  15. };
  16. postData('/setting/user/switch', data, function () {
  17. window.location.href = '/setting/user';
  18. });
  19. });
  20. // 编辑账号
  21. $('a[data-target="#edit-user"]').on('click', function () {
  22. const account = $(this).data('account');
  23. $('#edit-user input[name="account"]').val(account.account);
  24. $('#edit-user input[name="name"]').val(account.name);
  25. $('#edit-user input[name="company"]').val(account.company);
  26. $('#edit-user input[name="role"]').val(account.role);
  27. $('#edit-user input[name="mobile"]').val(account.mobile);
  28. $('#edit-user input[name="telephone"]').val(account.telephone);
  29. $('#edit-user input[name="id"]').val(account.id);
  30. $('#edit-user select[name="account_group"]').val(account.account_group);
  31. $('#edit-user input[class="account-check"]').val(account.account);
  32. $('#edit-user input[data-mobile="auth-mobile"]').val(account.auth_mobile);
  33. $('#edit-user input[name="mobile"]').attr('readOnly', account.bind === 1);
  34. if (account.bind === 1) {
  35. $('#edit-user input[name="mobile"]').siblings('small').show();
  36. } else {
  37. $('#edit-user input[name="mobile"]').siblings('small').hide();
  38. }
  39. $('#edit-password input[name="account"]').val(account.account);
  40. $('#edit-password input[class="account-check"]').val(account.account);
  41. $('#edit-password input[name="id"]').val(account.id);
  42. $('#edit-password input[name="reset_password"]').val('');
  43. });
  44. // 分配随机密码
  45. $("#rand-password").click(function() {
  46. const password = randPassword();
  47. $(this).parent().parent().find('input').val(password);
  48. });
  49. // 分配随机密码
  50. $("#rand-password2").click(function() {
  51. const password = randPassword();
  52. $(this).parent().parent().find('input').val(password);
  53. });
  54. // 重置密码
  55. let isChange = false;
  56. $("#reset-password-btn").click(function() {
  57. try {
  58. if (isChange) {
  59. throw '稍后再操作';
  60. }
  61. const resetPassword = $("#reset-password").val();
  62. const id = $("#user-id").val();
  63. if (resetPassword.length < 6) {
  64. throw '密码长度不能小于6';
  65. }
  66. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test(resetPassword)) {
  67. throw '密码只支持英文数字及符号';
  68. }
  69. const btn = $(this);
  70. $.ajax({
  71. url: '/setting/user/reset/password',
  72. type: 'post',
  73. data: { id: id, reset_password: resetPassword },
  74. dataType: 'json',
  75. error: function() {
  76. isChange = false;
  77. btn.html('重置密码');
  78. throw '网络错误!';
  79. },
  80. beforeSend: function(xhr) {
  81. let csrfToken = Cookies.get('csrfToken_j');
  82. xhr.setRequestHeader('x-csrf-token_j', csrfToken);
  83. isChange = true;
  84. btn.html('<i class="fa fa-spinner fa-pulse"></i>');
  85. },
  86. success: function(response) {
  87. isChange = false;
  88. btn.html('重置密码');
  89. if (response.err !== 0) {
  90. throw response.msg;
  91. }
  92. $("#reset-password").val('');
  93. toastr.success('重置成功');
  94. }
  95. });
  96. } catch (error) {
  97. toastr.error(error);
  98. console.log(error);
  99. }
  100. });
  101. // 账号查重
  102. $('input[name="account"]').on('blur', function () {
  103. const self = $(this);
  104. if (self.val() !== self.siblings('input').val()) {
  105. const data = {account: $(this).val()};
  106. postData('/setting/user/exist', data, function (data) {
  107. if (data === null) {
  108. self.removeClass('is-invalid');
  109. } else {
  110. self.addClass('is-invalid');
  111. }
  112. })
  113. } else {
  114. self.removeClass('is-invalid');
  115. }
  116. });
  117. // 选中创建标段才可以选择协作办公
  118. $('a[data-target="#edit-user2"]').on('click', function () {
  119. $('#edit-user2 input:radio').prop('checked', false);
  120. $('#edit-user2 input:checkbox').prop('checked', false);
  121. const account = $(this).data('account');
  122. $('#edit-user2 input[name="id"]').val(account.id);
  123. // 权限赋值
  124. if (account.permission !== '') {
  125. const permission = JSON.parse(account.permission);
  126. for (const pm in permission) {
  127. if (pm === 'tender' && permission[pm].indexOf('1') !== -1) {
  128. $('#edit-user2 input[name="cooperation"]').attr('disabled', false);
  129. } else {
  130. $('#edit-user2 input[name="cooperation"]').attr('disabled', true);
  131. }
  132. if (allPermission[pm].type === 'checkbox') {
  133. for (const index of permission[pm]) {
  134. $('#edit-user2 input:checkbox[id="' + pm + '_' + index + '"]').prop('checked', true);
  135. }
  136. } else if (allPermission[pm].type === 'radio') {
  137. $('#edit-user2 input:radio[id="' + pm + '_' + permission[pm] + '"]').prop('checked', true);
  138. }
  139. }
  140. }
  141. // 协作赋值
  142. $('#edit-user2 input:radio[name="cooperation"][value="' + account.cooperation + '"]').prop('checked', true);
  143. });
  144. // 选择创建标段功能后可选协作办公
  145. $('#edit-user2 input:checkbox').click(function () {
  146. if ($(this).attr('id') === 'tender_1') {
  147. if ($(this).is(':checked')) {
  148. $('#edit-user2 input[name="cooperation"]').attr('disabled', false);
  149. } else {
  150. $('#edit-user2 input[name="cooperation"]').attr('disabled', true);
  151. }
  152. }
  153. });
  154. // 解绑第三方平台
  155. $('.unlink-user').on('click', function () {
  156. const id = $(this).data('account');
  157. const accountData = _.find(accountList, { id });
  158. console.log(accountData);
  159. $('#bind_account').text(accountData.name + ' ' + accountData.mobile);
  160. $('#account_id').val(id);
  161. })
  162. // 设置显示默认
  163. $('body').on('click', '#set-default', function () {
  164. const attid = $(this).data('attid');
  165. const data = {id: attid};
  166. postData('/setting/show/update', data, function (result) {
  167. let html = ''
  168. result.forEach((item, idx) => {
  169. html += `<li class="list-group-item">${item.label_name}`
  170. html+= item.is_default ? `<span class="pull-right">默认</span></li>`:`<a href="javascript:void(0)" id="set-default" class="btn btn-primary btn-sm pull-right" data-attid="${idx}">设为默认</a></li>`
  171. })
  172. // <li class="list-group-item">
  173. // 标段列表<a href="#" class="btn btn-primary btn-sm pull-right">设为默认</a>
  174. // </li>
  175. // <li class="list-group-item">金额概况<span class="pull-right">默认</span></li>
  176. // <li class="list-group-item">
  177. // 计量进度<a href="#" class="btn btn-primary btn-sm pull-right">设为默认</a>
  178. // </li>
  179. $('.list-group').empty()
  180. $('.list-group').append(html)
  181. const item = result.find((i, idx) => idx=== attid)
  182. $('#nav_tender').attr('href', item.path)
  183. });
  184. });
  185. });
  186. function checkPasswordForm() {
  187. try {
  188. if ($('#edit-password input[name="account"]').val() == '' || $('#edit-password input[name="account"]').hasClass('is-invalid')) {
  189. throw '账号不能为空或已存在';
  190. }
  191. const resetPassword = $('#edit-password input[name="reset_password"]').val();
  192. if (resetPassword.length < 6) {
  193. throw '密码长度不能小于6';
  194. }
  195. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test(resetPassword)) {
  196. throw '密码只支持英文数字及符号';
  197. }
  198. } catch (err) {
  199. toastr.error(err);
  200. return false;
  201. }
  202. }
  203. /**
  204. * 表单检测
  205. */
  206. function checkUserForm(status) {
  207. try {
  208. if (status === 'add') {
  209. if ($('#add-user select[name="account_group"]').val() == 0) {
  210. throw '请选择账号组';
  211. }
  212. if ($('#add-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  213. throw '账号不能为空或已存在';
  214. }
  215. if ($('#add-user input[name="password"]').val() == '' || $('#add-user input[name="password"]').val().length < 6) {
  216. throw '密码不能为空或不能小于6位';
  217. }
  218. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test($('#add-user input[name="password"]').val())) {
  219. throw '密码只支持英文数字及符号';
  220. }
  221. if ($('#add-user input[name="name"]').val() == '') {
  222. throw '姓名不能为空';
  223. }
  224. if ($('#add-user input[name="company"]').val() == '') {
  225. throw '单位名称不能为空';
  226. }
  227. if ($('#add-user input[name="role"]').val() == '') {
  228. throw '职位名称不能为空';
  229. }
  230. $('#add-user input[name="account"]').val(trimInvalidChar($('#add-user input[name="account"]').val()));
  231. $('#add-user input[name="name"]').val(trimInvalidChar($('#add-user input[name="name"]').val()));
  232. $('#add-user input[name="company"]').val(trimInvalidChar($('#add-user input[name="company"]').val()));
  233. $('#add-user input[name="role"]').val(trimInvalidChar($('#add-user input[name="role"]').val()));
  234. $('#add-user input[name="telephone"]').val(trimInvalidChar($('#add-user input[name="telephone"]').val()));
  235. } else {
  236. if ($('#edit-user select[name="account_group"]').val() == 0) {
  237. throw '请选择账号组';
  238. }
  239. if ($('#edit-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  240. throw '账号不能为空或已存在';
  241. }
  242. if ($('#edit-user input[name="name"]').val() == '') {
  243. throw '姓名不能为空';
  244. }
  245. if ($('#edit-user input[name="company"]').val() == '') {
  246. throw '单位名称不能为空';
  247. }
  248. if ($('#edit-user input[name="role"]').val() == '') {
  249. throw '职位名称不能为空';
  250. }
  251. $('#edit-user input[name="account"]').val(trimInvalidChar($('#edit-user input[name="account"]').val()));
  252. $('#edit-user input[name="name"]').val(trimInvalidChar($('#edit-user input[name="name"]').val()));
  253. $('#edit-user input[name="company"]').val(trimInvalidChar($('#edit-user input[name="company"]').val()));
  254. $('#edit-user input[name="role"]').val(trimInvalidChar($('#edit-user input[name="role"]').val()));
  255. $('#edit-user input[name="telephone"]').val(trimInvalidChar($('#edit-user input[name="telephone"]').val()));
  256. }
  257. } catch (err) {
  258. toastr.error(err);
  259. return false;
  260. }
  261. }
  262. /**
  263. * 随机密码
  264. */
  265. function randPassword() {
  266. let result = '';
  267. // 随机6-10位
  268. const length = Math.ceil(Math.random() * 2 + 8);
  269. let numberSeed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  270. let stringSeed = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  271. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  272. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  273. const randSeed = stringSeed.concat(numberSeed);
  274. const seedLength = randSeed.length - 1;
  275. for (let i = 0; i < length; i++) {
  276. const index = Math.ceil(Math.random() * seedLength);
  277. result += randSeed[index];
  278. }
  279. return result;
  280. }