change.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. } else {
  15. $('#bj-code').val('变更申请编号');
  16. }
  17. });
  18. }
  19. class codeRuleSet {
  20. constructor (obj) {
  21. this.body = obj;
  22. // 切换规则组件类型
  23. $('.rule-change', obj).change(function () {
  24. const codeType = this.selectedIndex;
  25. if (codeType === ruleConst.ruleType.addNo) {
  26. $('#format', obj).show();
  27. $('#text', obj).show();
  28. $('#text>label', obj).text('起始编号');
  29. $('#text>input', obj).val('001');
  30. const s = '0000000000' + 1;
  31. $('#text>input', obj).val(s.substr(s.length - $('#format>input', obj).val()));
  32. } else if (codeType === ruleConst.ruleType.text) {
  33. $('#format', obj).hide();
  34. $('#text', obj).show();
  35. $('#text>label', obj).text('文本');
  36. $('#text>input', obj).val('').attr('placeholder', '请在这里输入需要的文本');
  37. } else {
  38. $('#format', obj).hide();
  39. $('#text', obj).hide();
  40. }
  41. });
  42. // 修改编号位数
  43. $('#format>input', obj).change(function () {
  44. const s = '0000000000' + parseInt($('#text>input', obj).val());
  45. $('#text>input', obj).val(s.substr(s.length - $(this).val()));
  46. });
  47. // 修改连接符
  48. $('.connector-change', obj).change(function () {
  49. const connectorType = this.options[this.selectedIndex].text;
  50. const rules = $('span>span', obj), ruleText = [];
  51. for (const r of rules) {
  52. ruleText.push($.trim(r.innerText));
  53. }
  54. if (connectorType === '无') {
  55. $('#preview', obj).text(ruleText.join(''));
  56. } else {
  57. $('#preview', obj).text(ruleText.join(connectorType));
  58. }
  59. connectorRule = this.options[this.selectedIndex].value;
  60. });
  61. // 新增规则组件
  62. $('#addRule', obj).click(function () {
  63. const codeType = $('select', obj)[1].selectedIndex;
  64. const rule = {rule_type: codeType}, html = [];
  65. let preview;
  66. switch (codeType) {
  67. case ruleConst.ruleType.tenderName: {
  68. preview = $('#tenderName').val();
  69. break;
  70. }
  71. case ruleConst.ruleType.text: {
  72. rule.text = $('#text>input', obj).val();
  73. preview = rule.text;
  74. break;
  75. }
  76. case ruleConst.ruleType.inDate: {
  77. preview = moment().format('YYYY');
  78. break;
  79. }
  80. case ruleConst.ruleType.addNo: {
  81. rule.format = parseInt($('#format>input', obj).val());
  82. rule.start = parseInt($('#text>input', obj).val());
  83. const s = '0000000000';
  84. preview = s.substr(s.length - rule.format);
  85. break;
  86. }
  87. default: {
  88. toast('请选择组件再添加', 'error');
  89. return false;
  90. }
  91. }
  92. // 更新规则
  93. codeRule.push(rule);
  94. // 更新规则显示
  95. html.push('<span class="badge badge-light" title="' + ruleConst.ruleString[codeType] + '" rule="' + JSON.stringify(rule) + '">');
  96. html.push('<span>' + preview + '</span>');
  97. html.push('<a href="javascript: void(0);" class="text-danger" title="移除"><i class="fa fa-remove"></i></a>');
  98. html.push('</span>');
  99. const part = $('#ruleParts', obj).append(html.join(''));
  100. // 更新规则预览
  101. const connectorType = connectorRule !== '' && parseInt(connectorRule) !== 3 ? ruleConst.connectorString[connectorRule] : '';
  102. const previewtext = $.trim($('#preview', obj).text()) === '' ? preview : $.trim($('#preview', obj).text()) + connectorType + preview;
  103. $('#preview', obj).text(previewtext);
  104. $('a', part).bind('click', function () {
  105. const index = $('a', obj).index(this);
  106. codeRule.splice(index, 1);
  107. $(this).parent().remove();
  108. const rules = $('span>span', obj), ruleText = [];
  109. for (const r of rules) {
  110. ruleText.push($.trim(r.innerText));
  111. }
  112. const connectorType = connectorRule !== '' && parseInt(connectorRule) !== 3 ? ruleConst.connectorString[connectorRule] : '';
  113. $('#preview', obj).text(ruleText.join(connectorType));
  114. });
  115. });
  116. // 删除规则组件
  117. $('a', $('#ruleParts', obj)).bind('click', function () {
  118. const index = $('a', obj).index(this);
  119. codeRule.splice(index, 1);
  120. $(this).parent().remove();
  121. const rules = $('span>span', obj), ruleText = [];
  122. for (const r of rules) {
  123. ruleText.push($.trim(r.innerText));
  124. }
  125. // const connectorType = $('.connector-change', obj).val() !== null || $('.connector-change', obj).val() === '3' ? $('.connector-change option:selected', obj).text() : '';
  126. const connectorType = connectorRule !== '' && parseInt(connectorRule) !== 3 ? ruleConst.connectorString[connectorRule] : '';
  127. console.log(connectorType);
  128. $('#preview', obj).text(ruleText.join(connectorType));
  129. });
  130. }
  131. }
  132. $(document).ready(() => {
  133. // 首次进入设置
  134. if (cRuleFirst) {
  135. codeRule = [];
  136. const firstSet = new codeRuleSet($('div.modal-body', '#first'));
  137. // 确认规则上传服务器
  138. $('#setRule', '#first').bind('click', function () {
  139. const data = {
  140. rule: ruleType,
  141. connector: connectorRule,
  142. data: JSON.stringify(codeRule),
  143. };
  144. postData('/tender/rule', data, function () {
  145. $('#first').modal('hide');
  146. $('#add-bj').modal('show');
  147. });
  148. });
  149. $('#first').modal('show');
  150. } else if ($('#changList').children.length === 0) {
  151. $('#add-bj').modal('show');
  152. }
  153. // 设置
  154. const ruleSet = new codeRuleSet($('div.modal-body', '#setting'));
  155. $('#setRule', '#setting').bind('click', function () {
  156. const data = {
  157. rule: ruleType,
  158. connector: connectorRule,
  159. data: JSON.stringify(codeRule),
  160. };
  161. postData('/tender/rule', data, function () {
  162. $('#setting').modal('hide');
  163. });
  164. })
  165. // 新增变更令 modal显示
  166. $('#add-bj').on('show.bs.modal', function() {
  167. getNewCode();
  168. if ($('#changeList').children.length === 0) {
  169. $('#addCancel').hide();
  170. } else {
  171. $('#addCancel').show();
  172. }
  173. $('#bj-code').removeClass('is-invalid');
  174. });
  175. // 获取最新可用变更令号
  176. $('#autoCode').click(getNewCode);
  177. // 新增变更令 确认
  178. $('#addOk').click(function () {
  179. const data = {
  180. code: $('#bj-code').val(),
  181. name: $('#bj-name').val(),
  182. };
  183. if (data.code || data.code !== '' || data.name || data.name !== '') {
  184. postData('/tender/'+ $('#tenderId').val() +'/change/add', data, function (rst) {
  185. $('#bj-code').removeClass('is-invalid');
  186. $('#mj-add').modal('hide');
  187. window.location.href = '/tender/'+ $('#tenderId').val() +'/change/' + rst.cid + '/info';
  188. }, function () {
  189. $('#mj-code').addClass('is-invalid');
  190. $('#mj-Hint').show();
  191. });
  192. }
  193. });
  194. //状态切换
  195. $('#status_select').change(function () {
  196. const status = $(this).val();
  197. let url = '/tender/'+ $('#tenderId').val() +'/change';
  198. if (status !== 0) {
  199. url += '/status/'+ status;
  200. }
  201. window.location.href = url;
  202. });
  203. // 不再显示首次使用
  204. $('#changeFirst').click(function () {
  205. postData('/tender/'+ $('#tenderId').val() +'/rule/first', '', function () {
  206. });
  207. })
  208. });