version.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * 版本管理相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/28
  6. * @version
  7. */
  8. $(document).ready(function() {
  9. let isAdding = false;
  10. let model = '';
  11. // 初始化数据
  12. initVersion();
  13. // 新增版本
  14. $("#add-version").click(function() {
  15. try {
  16. let [name, standardBill, rationLib, standardBillString, rationLibString] = getAndValidData(model);
  17. let id = $("#version-id").val();
  18. let postData = {};
  19. let url = '/version/add';
  20. switch (model) {
  21. case 'all':
  22. postData = {model: model, name: name, standard_bill_id: standardBill, ration_lib_id: rationLib,
  23. standard_bill: standardBillString, ration_lib: rationLibString};
  24. break;
  25. case 'bill':
  26. url = '/version/addLib';
  27. postData = {model: model, standard_bill_id: standardBill, standard_bill: standardBillString, id: id};
  28. break;
  29. case 'ration':
  30. url = '/version/addLib';
  31. postData = {model: model, ration_lib_id: rationLib, ration_lib: rationLibString, id: id};
  32. break;
  33. }
  34. $.ajax({
  35. url: url,
  36. type: 'post',
  37. data: postData,
  38. error: function() {
  39. isAdding = false;
  40. },
  41. beforeSend: function() {
  42. isAdding = true;
  43. },
  44. success: function(response) {
  45. isAdding = false;
  46. if (response.err === 0) {
  47. window.location.reload();
  48. } else {
  49. let msg = response.msg === undefined ? '未知错误' : response.msg;
  50. alert(msg);
  51. }
  52. }
  53. });
  54. } catch (error) {
  55. alert(error);
  56. }
  57. });
  58. // 选择省份后读取数据
  59. $("select[name='standard_bill_province'],select[name='ration_lib_province']").change(function() {
  60. let name = $(this).attr('name');
  61. let billListData = billList === undefined ? [] : JSON.parse(billList);
  62. let rationLibData = rationList === undefined ? [] : JSON.parse(rationList);
  63. if (billListData.length <= 0 || rationLibData.length <= 0) {
  64. return false;
  65. }
  66. let sourceData = name === 'standard_bill_province' ? billListData : rationLibData;
  67. let selectedId = $(this).val();
  68. if (sourceData[selectedId] === undefined) {
  69. return false;
  70. }
  71. let defaultString = name === 'standard_bill_province' ? '请选择标准清单' : '请选择定额库';
  72. let html = '<option value="">' + defaultString + '</option>';
  73. for(let tmp of sourceData[selectedId]) {
  74. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  75. html += tmpHtml;
  76. }
  77. // 渲染
  78. let targetSelector = name === 'standard_bill_province' ? $("select[name='standard_bill']") : $("select[name='ration_lib']");
  79. targetSelector.children('option').remove();
  80. targetSelector.html(html);
  81. });
  82. // 添加
  83. $(".add-version").click(function() {
  84. model = $(this).data('model');
  85. switch (model) {
  86. case 'all':
  87. $("#name-area").show();
  88. $("#bill-area").show();
  89. $("#ration-area").show();
  90. $("#add-version-title").text('添加新版本');
  91. break;
  92. case 'bill':
  93. $("#name-area").hide();
  94. $("#bill-area").show();
  95. $("#ration-area").hide();
  96. $("#add-version-title").text('添加标准清单');
  97. break;
  98. case 'ration':
  99. $("#name-area").hide();
  100. $("#bill-area").hide();
  101. $("#ration-area").show();
  102. $("#add-version-title").text('添加定额库');
  103. break;
  104. }
  105. $("#addversion").modal('show');
  106. });
  107. // 移除操作
  108. let isDeleting = false;
  109. $(".remove-version").click(function() {
  110. let model = $(this).data('model');
  111. let id = $("#version-id").val();
  112. let deleteId = $(this).data('id');
  113. deleteId = parseInt(deleteId);
  114. if (model === undefined || model === '' || isNaN(deleteId)) {
  115. return false;
  116. }
  117. if (isDeleting) {
  118. return false;
  119. }
  120. $.ajax({
  121. url: '/version/deleteLib',
  122. type: 'post',
  123. data: {id: id, model: model, delete_id: deleteId},
  124. error: function() {
  125. isDeleting = false;
  126. },
  127. beforeSend: function() {
  128. isDeleting = true;
  129. },
  130. success: function(response) {
  131. isDeleting = false;
  132. if (response.err === 0) {
  133. window.location.reload();
  134. } else {
  135. let msg = response.msg === undefined ? '未知错误' : response.msg;
  136. alert(msg);
  137. }
  138. }
  139. });
  140. });
  141. });
  142. /**
  143. * 初始化
  144. *
  145. * @return {void|boolean}
  146. */
  147. function initVersion() {
  148. if (province === undefined) {
  149. alert('初始化失败!');
  150. return false;
  151. }
  152. province = JSON.parse(province);
  153. if (province.length <= 0) {
  154. alert('省份数据加载失败!');
  155. return false;
  156. }
  157. let billProvinceElement = $("select[name='standard_bill_province']");
  158. let rationProvinceElement = $("select[name='ration_lib_province']");
  159. let html = '';
  160. for (let tmp of province) {
  161. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  162. html += tmpHtml;
  163. }
  164. billProvinceElement.children('option').first().after(html);
  165. rationProvinceElement.children('option').first().after(html);
  166. }
  167. /**
  168. * 校验数据
  169. *
  170. * @param {String} model
  171. * @return {Array}
  172. */
  173. function getAndValidData(model) {
  174. let name = $("input[name='version_name']").val();
  175. let standardBill = $("select[name='standard_bill']").val();
  176. let rationLib = $("select[name='ration_lib']").val();
  177. if (name === '' && model === 'all') {
  178. throw '版本名字不能为空';
  179. }
  180. if ((model === 'all' || model === 'bill') && (standardBill === '' || standardBill === undefined)) {
  181. throw '请选择标准清单库';
  182. }
  183. if ((model === 'all' || model === 'ration') && (rationLib === '' || rationLib === undefined)) {
  184. throw '请选择定额库';
  185. }
  186. let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
  187. let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
  188. return [name, standardBill, rationLib, standardBillString, rationLibString];
  189. }