setting.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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', 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. $('.nav-tabs .nav-link').each(function () {
  187. const pageSize = getLocalCache('account-pageSize') ? getLocalCache('account-pageSize') : '';
  188. if (getLocalCache('account-pageSize') && $(this).attr('href').indexOf('pageSize') === -1) {
  189. $(this).attr('href', $(this).attr('href') + '?pageSize=' + getLocalCache('account-pageSize'));
  190. }
  191. });
  192. });
  193. function checkPasswordForm() {
  194. try {
  195. if ($('#edit-password input[name="account"]').val() == '' || $('#edit-password input[name="account"]').hasClass('is-invalid')) {
  196. throw '账号不能为空或已存在';
  197. }
  198. const resetPassword = $('#edit-password input[name="reset_password"]').val();
  199. if (resetPassword.length < 6) {
  200. throw '密码长度不能小于6';
  201. }
  202. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test(resetPassword)) {
  203. throw '密码只支持英文数字及符号';
  204. }
  205. } catch (err) {
  206. toastr.error(err);
  207. return false;
  208. }
  209. }
  210. /**
  211. * 表单检测
  212. */
  213. function checkUserForm(status) {
  214. try {
  215. if (status === 'add') {
  216. if ($('#add-user select[name="account_group"]').val() == 0) {
  217. throw '请选择账号组';
  218. }
  219. if ($('#add-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  220. throw '账号不能为空或已存在';
  221. }
  222. if ($('#add-user input[name="password"]').val() == '' || $('#add-user input[name="password"]').val().length < 6) {
  223. throw '密码不能为空或不能小于6位';
  224. }
  225. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test($('#add-user input[name="password"]').val())) {
  226. throw '密码只支持英文数字及符号';
  227. }
  228. if ($('#add-user input[name="name"]').val() == '') {
  229. throw '姓名不能为空';
  230. }
  231. if ($('#add-user input[name="company"]').val() == '') {
  232. throw '单位名称不能为空';
  233. }
  234. if ($('#add-user input[name="role"]').val() == '') {
  235. throw '职位名称不能为空';
  236. }
  237. $('#add-user input[name="account"]').val(trimInvalidChar($('#add-user input[name="account"]').val()));
  238. $('#add-user input[name="name"]').val(trimInvalidChar($('#add-user input[name="name"]').val()));
  239. $('#add-user input[name="company"]').val(trimInvalidChar($('#add-user input[name="company"]').val()));
  240. $('#add-user input[name="role"]').val(trimInvalidChar($('#add-user input[name="role"]').val()));
  241. $('#add-user input[name="telephone"]').val(trimInvalidChar($('#add-user input[name="telephone"]').val()));
  242. } else {
  243. if ($('#edit-user select[name="account_group"]').val() == 0) {
  244. throw '请选择账号组';
  245. }
  246. if ($('#edit-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  247. throw '账号不能为空或已存在';
  248. }
  249. if ($('#edit-user input[name="name"]').val() == '') {
  250. throw '姓名不能为空';
  251. }
  252. if ($('#edit-user input[name="company"]').val() == '') {
  253. throw '单位名称不能为空';
  254. }
  255. if ($('#edit-user input[name="role"]').val() == '') {
  256. throw '职位名称不能为空';
  257. }
  258. $('#edit-user input[name="account"]').val(trimInvalidChar($('#edit-user input[name="account"]').val()));
  259. $('#edit-user input[name="name"]').val(trimInvalidChar($('#edit-user input[name="name"]').val()));
  260. $('#edit-user input[name="company"]').val(trimInvalidChar($('#edit-user input[name="company"]').val()));
  261. $('#edit-user input[name="role"]').val(trimInvalidChar($('#edit-user input[name="role"]').val()));
  262. $('#edit-user input[name="telephone"]').val(trimInvalidChar($('#edit-user input[name="telephone"]').val()));
  263. }
  264. } catch (err) {
  265. toastr.error(err);
  266. return false;
  267. }
  268. }
  269. /**
  270. * 随机密码
  271. */
  272. function randPassword() {
  273. let result = '';
  274. // 随机6-10位
  275. const length = Math.ceil(Math.random() * 2 + 8);
  276. let numberSeed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  277. let stringSeed = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  278. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  279. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  280. const randSeed = stringSeed.concat(numberSeed);
  281. const seedLength = randSeed.length - 1;
  282. for (let i = 0; i < length; i++) {
  283. const index = Math.ceil(Math.random() * seedLength);
  284. result += randSeed[index];
  285. }
  286. return result;
  287. }