change.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/21
  7. * @version
  8. */
  9. // 向后端请求中间计量号
  10. function getNewCode() {
  11. postData('/tender/'+ $('#tenderId').val() +'/change/newCode', null, function (code) {
  12. if (code !== '') {
  13. $('#bj-code').val(code);
  14. }
  15. });
  16. }
  17. class codeRuleSet {
  18. constructor (obj) {
  19. this.body = obj;
  20. // 切换规则组件类型
  21. $('.rule-change', obj).change(function () {
  22. const codeType = this.selectedIndex-1;
  23. if (codeType === ruleConst.ruleType.addNo) {
  24. $('#format', obj).show();
  25. $('#text', obj).show();
  26. $('#text>label', obj).text('起始编号');
  27. $('#text>input', obj).val('001');
  28. const s = '0000000000' + 1;
  29. $('#text>input', obj).val(s.substr(s.length - $('#format>input', obj).val()));
  30. } else if (codeType === ruleConst.ruleType.text) {
  31. $('#format', obj).hide();
  32. $('#text', obj).show();
  33. $('#text>label', obj).text('文本');
  34. $('#text>input', obj).val('').attr('placeholder', '请在这里输入需要的文本');
  35. } else {
  36. $('#format', obj).hide();
  37. $('#text', obj).hide();
  38. }
  39. });
  40. // 修改编号位数
  41. $('#format>input', obj).change(function () {
  42. const s = '0000000000' + parseInt($('#text>input', obj).val());
  43. $('#text>input', obj).val(s.substr(s.length - $(this).val()));
  44. });
  45. // 修改连接符
  46. $('.connector-change', obj).change(function () {
  47. const connectorType = this.options[this.selectedIndex].text;
  48. const rules = $('span>span', obj), ruleText = [];
  49. for (const r of rules) {
  50. ruleText.push($.trim(r.innerText));
  51. }
  52. if (connectorType === '无') {
  53. $('#preview', obj).text(ruleText.join(''));
  54. } else {
  55. $('#preview', obj).text(ruleText.join(connectorType));
  56. }
  57. connectorRule = this.options[this.selectedIndex].value;
  58. });
  59. // 新增规则组件
  60. $('#addRule', obj).click(function () {
  61. const codeType = $('select', obj)[1].selectedIndex-1;
  62. const rule = {rule_type: codeType}, html = [];
  63. let preview;
  64. switch (codeType) {
  65. case ruleConst.ruleType.dealCode: {
  66. if ($('#dealCode').val() === '') {
  67. toastr.error('当前标段合同编号为空,请选择其他组件。');
  68. return false;
  69. }
  70. preview = $('#dealCode').val();
  71. break;
  72. }
  73. case ruleConst.ruleType.tenderName: {
  74. preview = $('#tenderName').val();
  75. break;
  76. }
  77. case ruleConst.ruleType.text: {
  78. rule.text = $('#text>input', obj).val();
  79. if (rule.text === '') {
  80. toastr.error('文本内容不允许为空。');
  81. return false;
  82. }
  83. preview = rule.text;
  84. break;
  85. }
  86. case ruleConst.ruleType.inDate: {
  87. preview = moment().format('YYYY');
  88. break;
  89. }
  90. case ruleConst.ruleType.addNo: {
  91. rule.format = parseInt($('#format>input', obj).val());
  92. rule.start = parseInt($('#text>input', obj).val());
  93. if ($('#text>input', obj).val().length !== rule.format) {
  94. toastr.error('起始编号位数和自动编号位数不一致。');
  95. return false;
  96. }
  97. const s = '0000000000';
  98. preview = s.substr(s.length - rule.format);
  99. break;
  100. }
  101. default: {
  102. toastr.error('请选择组件再添加');
  103. return false;
  104. }
  105. }
  106. // 更新规则
  107. codeRule.push(rule);
  108. // 更新规则显示
  109. html.push('<span class="badge badge-light" title="' + ruleConst.ruleString[codeType] + '" rule="' + JSON.stringify(rule) + '">');
  110. html.push('<span>' + preview + '</span>');
  111. html.push('<a href="javascript: void(0);" class="text-danger" title="移除"><i class="fa fa-remove"></i></a>');
  112. html.push('</span>');
  113. const part = $('#ruleParts', obj).append(html.join(''));
  114. // 更新规则预览
  115. const connectorType = connectorRule !== '' && parseInt(connectorRule) !== ruleConst.connectorType.nothing ? ruleConst.connectorString[connectorRule] : '';
  116. const previewtext = $.trim($('#preview', obj).text()) === '' ? preview : $.trim($('#preview', obj).text()) + connectorType + preview;
  117. $('#preview', obj).text(previewtext);
  118. });
  119. // 删除规则组件
  120. $($('#ruleParts', obj)).on('click', 'a', function () {
  121. const index = $('a', obj).index(this);
  122. codeRule.splice(index-1, 1);
  123. $(this).parent().remove();
  124. const rules = $('span>span', obj), ruleText = [];
  125. for (const r of rules) {
  126. ruleText.push($.trim(r.innerText));
  127. }
  128. const connectorType = connectorRule !== '' && parseInt(connectorRule) !== ruleConst.connectorType.nothing ? ruleConst.connectorString[connectorRule] : '';
  129. $('#preview', obj).text(ruleText.join(connectorType));
  130. });
  131. }
  132. }
  133. $(document).ready(() => {
  134. // 首次进入设置
  135. let showNoNeed = false;
  136. if (parseInt(cRuleFirst)) {
  137. codeRule = [];
  138. showNoNeed = true;
  139. // const firstSet = new codeRuleSet($('div.modal-body', '#first'));
  140. // // 确认规则上传服务器
  141. // $('#setRule', '#first').bind('click', function () {
  142. // const data = {
  143. // rule: ruleType,
  144. // connector: connectorRule,
  145. // data: JSON.stringify(codeRule),
  146. //
  147. // };
  148. // postData('/tender/rule', data, function () {
  149. // $('#first').modal('hide');
  150. // $('#add-bj').modal('show');
  151. // });
  152. // });
  153. // $('#first').modal('show');
  154. $('#setting').modal('show');
  155. } else if ($('#changList').children.length === 0) {
  156. $('#add-bj').modal('show');
  157. }
  158. // 设置
  159. const ruleSet = new codeRuleSet($('div.modal-body', '#setting'));
  160. $('#setRule', '#setting').bind('click', function () {
  161. const data = {
  162. rule: ruleType,
  163. connector: connectorRule,
  164. data: JSON.stringify(codeRule),
  165. };
  166. if (codeRule.length !== 0) {
  167. $('#autoCodeShow').show();
  168. }
  169. postData('/tender/rule', data, function () {
  170. if (parseInt(cRuleFirst) && showNoNeed) {
  171. $('#changeFirst').click();
  172. $('#add-bj').modal('show');
  173. } else {
  174. $('#setting').modal('hide');
  175. }
  176. });
  177. })
  178. // 新增变更令 modal显示
  179. $('#add-bj').on('show.bs.modal', function() {
  180. getNewCode();
  181. if ($('#changeList').children.length === 0) {
  182. $('#addCancel').hide();
  183. } else {
  184. $('#addCancel').show();
  185. }
  186. $('#bj-code').removeClass('is-invalid');
  187. });
  188. // 获取最新可用变更令号
  189. $('#autoCode').click(getNewCode);
  190. // 新增变更令 确认
  191. $('#addOk').click(function () {
  192. $(this).attr('disabled', true);
  193. if ($('#bj-name').val().length === 0) {
  194. $('#bj-name').addClass('is-invalid');
  195. $('#name_error_msg').show();
  196. $('#name_error_msg').text('工程名称不能为空。');
  197. $(this).attr('disabled', false);
  198. setTimeout(function () {
  199. $('#bj-name').removeClass('is-invalid');
  200. $('#name_error_msg').hide();
  201. }, 2000);
  202. return;
  203. }
  204. if ($('#bj-name').val().length > 100) {
  205. $('#bj-name').addClass('is-invalid');
  206. $('#name_error_msg').show();
  207. $('#name_error_msg').text('名称超过100个字,请缩减名称。');
  208. $(this).attr('disabled', false);
  209. setTimeout(function () {
  210. $('#bj-name').removeClass('is-invalid');
  211. $('#name_error_msg').hide();
  212. }, 2000);
  213. return;
  214. }
  215. const data = {
  216. code: $('#bj-code').val(),
  217. name: $('#bj-name').val(),
  218. };
  219. if (data.code || data.code !== '' || data.name || data.name !== '') {
  220. postData('/tender/'+ $('#tenderId').val() +'/change/add', data, function (rst) {
  221. $('#bj-code').removeClass('is-invalid');
  222. $('#mj-add').modal('hide');
  223. $(this).attr('disabled', false);
  224. window.location.href = '/tender/'+ $('#tenderId').val() +'/change/' + rst.cid + '/info';
  225. }, function () {
  226. $('#mj-code').addClass('is-invalid');
  227. $('#mj-Hint').show();
  228. $(this).attr('disabled', false);
  229. });
  230. }
  231. });
  232. //状态切换
  233. $('#status_select').change(function () {
  234. const status = $(this).val();
  235. let url = '/tender/'+ $('#tenderId').val() +'/change';
  236. if (status !== 0) {
  237. url += '/status/'+ status;
  238. }
  239. window.location.href = url;
  240. });
  241. // 不再显示首次使用
  242. $('#changeFirst').click(function () {
  243. showNoNeed = false;
  244. $('#changeFirst').remove();
  245. $('#hide_modal').show();
  246. $('#setting').modal('hide');
  247. postData('/tender/'+ $('#tenderId').val() +'/rule/first', '', function () {
  248. });
  249. });
  250. // 弹出删除变更框赋值
  251. $('.delete-cid-modal').on('click', function () {
  252. $('#delete-cid').val($(this).attr('cid'));
  253. });
  254. });