import-categorys.blade.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. @extends('layouts.base')
  2. @section('title', "导入分类")
  3. @section('content')
  4. <link rel="stylesheet" type="text/css" href="{{static_url('yunshop/goods/vue-goods.css')}}"/>
  5. <script type="text/javascript">
  6. function show_task_alert(msg){
  7. $("#task_alert span").html(msg);
  8. $("#task_alert").show();
  9. }
  10. function hide_task_alert(){
  11. $("#task_alert").hide();
  12. }
  13. </script>
  14. <div class="rightlist">
  15. <div id="app" >
  16. <div class="second-list" >
  17. <div class="third-list">
  18. <div class="form-list">
  19. <div class="form-list-con" style="display:flex;margin:0 15px;padding-bottom:15px;">
  20. <div style="flex:1">
  21. <div style="font-size:24px;font-weight:700;">导入分类</div>
  22. <div style="color:red;padding:10px 0;font-weight:600"></div>
  23. </div>
  24. <div style="flex:1;text-align:right"></div>
  25. </div>
  26. </div>
  27. <div class="table-list" style="padding:10px 15px;">
  28. <div id="task_alert" class="alert alert-danger" role="alert" style="display:none"><span></span></div>
  29. <div id="">
  30. <div class="form-group">
  31. <label class="control-label"><b>来源</b></label>
  32. <select id="source" class="form-control" style="width:300px">
  33. <option value="">请选择</option>
  34. <option value="1">云仓</option>
  35. <option value="2">京东</option>
  36. <option value="6">阿里巴巴</option>
  37. </select>
  38. <p class="help-block"></p>
  39. </div>
  40. <div id="import_set" style="display:none">
  41. <div class="form-group">
  42. <label class="control-label"><b>是否启用分类</b></label>
  43. <div>
  44. <label class="radio-inline">
  45. <input type="radio" name="enabled" value="1"> 是
  46. </label>
  47. <label class="radio-inline">
  48. <input type="radio" name="enabled" value="0" checked> 否
  49. </label>
  50. </div>
  51. <p class="help-block"></p>
  52. </div>
  53. </div>
  54. <button onclick="get_categorys(this)" type="button" class="btn btn-primary">获取分类</button>
  55. <button id="import_categorys_btn" onclick="import_categorys(this)" type="button" class="btn btn-danger" style="display:none;">导入分类</button>
  56. </div>
  57. <div id="running_info" style="display:none;margin-top:15px;font-size:11px;background:#eee;padding:10px 10px;">
  58. <div style="border-bottom:1px solid #ddd;margin-bottom:10px;"><b id="running_info_tt">分类获取中...</b><span id="got_total_count"></span></div>
  59. <div id="running_info_body" style="height:300px;overflow:auto;"></div>
  60. </div>
  61. <div id="progress" class="progress" style="display:none;height:20px">
  62. <div class="progress-bar progress-bar-info" style="width: 0%;text-align:center;">
  63. 0%
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. <div id="import_logs_con" style="display:none;margin:30px 10px;background:white;padding:10px 10px;">
  71. <div style="padding:5px 0px;border-bottom:1px solid #ddd;">
  72. <span>导入记录:</span>
  73. </div>
  74. <div style="">
  75. <ul id="import_logs" style="margin:0;padding:0;width:100%;height:300px;overflow:auto;">
  76. </ul>
  77. </div>
  78. </div>
  79. </div>
  80. <script>
  81. var category_list = [], timer;
  82. var fn_calling = false;
  83. function get_categorys(dom){
  84. var source = $("#source").val();
  85. $("#source").closest(".form-group").removeClass("has-error");
  86. $("#source").closest(".form-group").find(".help-block").html("");
  87. if(source.length <= 0){
  88. $("#source").closest(".form-group").addClass("has-error");
  89. $("#source").closest(".form-group").find(".help-block").html("请选择来源");
  90. return;
  91. }
  92. $(dom).attr("disabled", true);
  93. $(dom).hide();
  94. $("#source").attr("disabled", true);
  95. $("#running_info").show();
  96. $("#running_info_tt").html("请稍后,正在计算分类数量...");
  97. get_max_num(source, function(max){
  98. ajax_get(source, 0, function(rs){
  99. var htmls = "";
  100. for(var i=0; i < rs.categorys.length; i++){
  101. htmls += '<p>'+rs.categorys[i].title+'</p>';
  102. rs.categorys[i]['level'] = 0;
  103. category_list.push(rs.categorys[i]);
  104. }
  105. $("#running_info_body").html(htmls);
  106. });
  107. timer = setInterval(function(){
  108. var num = parseInt((100 * (category_list.length/max)));
  109. $("#progress .progress-bar").css("width", num+"%");
  110. $("#progress .progress-bar").html(num+"%");
  111. if(category_list.length > 0){
  112. get_childs(source);
  113. }
  114. $("#got_total_count").html(category_list.length);
  115. }, 10);
  116. show_task_alert("正在执行分类获取任务!请勿进行其他任何操作");
  117. });
  118. }
  119. //通过二分算法获取分类数量
  120. function get_max_num(source, fn){
  121. var url = "{!! yzWebUrl('plugin.jd-supply.admin.import-categorys.test-max-num') !!}";
  122. var n1 = 0, n2 = 100000, timer;
  123. var loop_fn = function(){
  124. if(fn_calling) return;
  125. fn_calling = true;
  126. ajax_json(url, "post", "n1="+n1+"&n2="+n2+"&source="+source, function(rs, status, xhr){
  127. console.log(rs);
  128. if(rs.finished == 0){
  129. n1 = rs.n1;
  130. n2 = rs.n2;
  131. }else{
  132. clearInterval(timer);
  133. $("#running_info_tt").html("一共"+rs.max+"个分类。分类获取中...");
  134. $("#progress").show();
  135. fn.call(this, rs.max);
  136. }
  137. fn_calling = false;
  138. });
  139. };
  140. timer = setInterval(function(){
  141. loop_fn();
  142. }, 10);
  143. }
  144. function get_childs(source){
  145. if(fn_calling) return;
  146. fn_calling = true;
  147. var n = null;
  148. for(var i=0; i < category_list.length; i++){
  149. if(!category_list[i].has_got_child){
  150. n = i;
  151. break;
  152. }
  153. }
  154. if(n == null){
  155. clearInterval(timer);
  156. fn_calling = false;
  157. $("#import_set").show();
  158. $("#import_categorys_btn").show();
  159. $("#import_categorys_btn").html("导入"+category_list.length+"个分类");
  160. $("#running_info").html('<p>获取成功,一共获取到'+category_list.length+'个分类</p>');
  161. hide_task_alert();
  162. $("#progress").hide();
  163. return;
  164. }
  165. /* if(category_list.length >= 500){
  166. clearInterval(timer);
  167. fn_calling = false;
  168. category_list[n].has_got_child = true;
  169. $("#import_set").show();
  170. $("#import_categorys_btn").show();
  171. $("#import_categorys_btn").html("导入"+category_list.length+"个分类");
  172. $("#running_info").html('<p>获取成功,一共获取到'+category_list.length+'个分类</p>');
  173. hide_task_alert();
  174. return;
  175. } */
  176. ajax_get(source, category_list[n].id, function(rs){
  177. category_list[n].has_got_child = true;
  178. fn_calling = false;
  179. var htmls = "";
  180. for(var i=0; i < rs.categorys.length; i++){
  181. htmls += '<p>'+rs.categorys[i].title+'</p>';
  182. rs.categorys[i]['level'] = category_list[n].level + 1;
  183. if(rs.categorys[i]['level'] >= 2){
  184. rs.categorys[i]['has_got_child'] = true;
  185. }
  186. category_list.push(rs.categorys[i]);
  187. }
  188. if(htmls.length <= 0){
  189. htmls += '<p>'+category_list[n].title+' 无子分类</p>';
  190. }
  191. $("#running_info_body").html(htmls);
  192. });
  193. }
  194. function ajax_get(source, parent_id, fn){
  195. var url = "{!! yzWebUrl('plugin.jd-supply.admin.import-categorys.api-get') !!}";
  196. ajax_json(url, "post", "source="+source+"&parent_id="+parent_id, function(rs, status, xhr){
  197. if(rs.status != 1){
  198. $("#running_info_body").html(rs.message);
  199. ajax_get(source, parent_id, fn);
  200. return;
  201. }
  202. for(var i=0; i < rs.categorys.length; i++){
  203. rs.categorys[i]['has_got_child'] = false;
  204. }
  205. fn.call(this, rs);
  206. });
  207. }
  208. function import_categorys(dom){
  209. if(!confirm("确定要导入分类吗?"))
  210. return;
  211. for(var i=0; i < category_list.length; i++){
  212. category_list[i]['has_imported'] = false;
  213. }
  214. $(dom).attr("disabled", true);
  215. $("#progress").show();
  216. $("#import_logs_con").show();
  217. show_task_alert("正在执行分类导入任务!请勿进行其他任何操作");
  218. //获取设置
  219. $("input[name='enabled']").attr("disabled", true);
  220. var enabled = $("input[name='enabled']:checked").val();
  221. timer = setInterval(function(){
  222. var total = category_list.length, imported_count = 0;
  223. for(var i=0; i < category_list.length; i++){
  224. if(category_list[i].has_imported){
  225. imported_count += 1;
  226. }
  227. }
  228. var num = parseInt((100 * (imported_count/total)));
  229. $("#progress .progress-bar").css("width", num+"%");
  230. $("#progress .progress-bar").html(num+"%");
  231. import_func(enabled);
  232. }, 10);
  233. }
  234. function ajax_json(url, method, data, fn){
  235. var options = {};
  236. options['url'] = url;
  237. options['timeout'] = 10000;
  238. options['dataType'] = "json";
  239. options['type'] = method;
  240. options['data'] = data;
  241. options['success'] = function(rs, status, xhr){
  242. fn.call(this, rs, status, xhr);
  243. }
  244. options['error'] = function(xhr, status, error){
  245. console.log(error+":"+status);
  246. ajax_json(url, method, data, fn);
  247. }
  248. $.ajax(options);
  249. }
  250. function import_func(enabled){
  251. if(fn_calling)
  252. return;
  253. fn_calling = true;
  254. var n = null;
  255. for(i=0; i < category_list.length; i++){
  256. if(!category_list[i].has_imported){
  257. n = i;
  258. break;
  259. }
  260. }
  261. if(n == null){
  262. $("#running_info").html("导入完毕!");
  263. clearInterval(timer);
  264. fn_calling = false;
  265. hide_task_alert();
  266. return;
  267. }
  268. var ajax_fn = function(title, parent_id, level, fn){
  269. var url = "{!! yzWebUrl('plugin.jd-supply.admin.import-categorys.import') !!}";
  270. var options = {};
  271. options['url'] = url;
  272. options['timeout'] = 10000;
  273. options['dataType'] = "json";
  274. options['type'] = "post";
  275. options['data'] = "enabled="+enabled+"&title="+title+"&parent_id="+parent_id+"&level="+level;
  276. options['success'] = function(rs, status, xhr){
  277. if(rs.status != 1){
  278. $("#running_info_body").html(rs.message);
  279. ajax_fn(title, parent_id, level, fn);
  280. return;
  281. }
  282. fn.call(this, rs);
  283. }
  284. options['error'] = function(xhr, status, error){
  285. ajax_fn(title, parent_id, level, fn);
  286. }
  287. $.ajax(options);
  288. };
  289. //处理父分类ID
  290. var parent_id = 0, level = 0;
  291. if(category_list[n].parent_id > 0){ //找到本地分类
  292. parent_id = -1;
  293. for(i=0; i < category_list.length; i++){
  294. if(category_list[n].parent_id == category_list[i].id){
  295. parent_id = category_list[i].local_id;
  296. level = category_list[i]['local_level'] + 1;
  297. break;
  298. }
  299. }
  300. }
  301. //剩余未导入
  302. var last_count = 0;
  303. for(i=0; i < category_list.length; i++){
  304. if(!category_list[i].has_imported){
  305. last_count += 1;
  306. }
  307. }
  308. if(parent_id < 0){
  309. alert("分类异常!!!");
  310. return;
  311. }
  312. ajax_fn(category_list[n].title, parent_id, level, function(rs){
  313. $("#running_info").html(category_list[n].title+"导入成功!父ID:"+parent_id+"。剩余:"+(last_count-1)+"个分类");
  314. $("#import_logs").prepend('<li style="margin-bottom:10px;"><font>'+category_list[n].title+'</font></li>');
  315. category_list[n]['local_level'] = rs.level;
  316. category_list[n]['local_id'] = rs.insert_id;
  317. category_list[n]['has_imported'] = true;
  318. fn_calling = false;
  319. });
  320. }
  321. </script>
  322. @endsection