Fleaclass.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Fleaclass extends AdminControl
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/flea.lang.php');
  18. }
  19. public function goods_class()
  20. {
  21. $this->flea_class();
  22. }
  23. /**
  24. * 分类管理
  25. */
  26. public function flea_class()
  27. {
  28. $fleaclass_model = model('fleaclass');
  29. /**
  30. * 删除,编辑
  31. */
  32. if (request()->isPost()) {
  33. /**
  34. * 删除
  35. */
  36. $check_fleaclass_id_array = input('post.check_fleaclass_id/a'); #获取数组
  37. if (input('post.submit_type') == 'del') {
  38. if (!empty($check_fleaclass_id_array)) {
  39. if (is_array($check_fleaclass_id_array)) {
  40. $del_array = $fleaclass_model->getChildClass($check_fleaclass_id_array);
  41. if (is_array($del_array)) {
  42. foreach ($del_array as $k => $v) {
  43. $fleaclass_model->delFleaclass($v['fleaclass_id']);
  44. }
  45. }
  46. }
  47. $this->success(lang('goods_class_index_del_succ'));
  48. } else {
  49. $this->error(lang('goods_class_index_choose_del'));
  50. }
  51. }
  52. /**
  53. * 编辑
  54. */
  55. if (input('post.submit_type') == 'brach_edit') {
  56. if (!empty($check_fleaclass_id_array)) {
  57. View::assign('id', implode(',', $check_fleaclass_id_array));
  58. $this->setAdminCurItem('brach_edit');
  59. return View::fetch('brach_edit');
  60. } else {
  61. $this->error(lang('goods_class_index_choose_edit'));
  62. }
  63. }
  64. /**
  65. * 首页显示
  66. */
  67. if (input('post.submit_type') == 'index_show' or input('post.submit_type') == 'index_hide') {
  68. if (!empty($check_fleaclass_id_array)) {
  69. if (is_array($check_fleaclass_id_array)) {
  70. $param = array();
  71. $param['fleaclass_index_show'] = input('post.submit_type') == 'index_show' ? '1' : '0';
  72. foreach ($check_fleaclass_id_array as $k => $v) {
  73. $fleaclass_model->editFleaclass(array('fleaclass_id' => $v), $param);
  74. }
  75. }
  76. $this->success(lang('goods_class_index_in_homepage') . (input('post.submit_type') == 'index_show' ? lang('goods_class_index_display') : lang('goods_class_index_hide')) . lang('goods_class_index_succ'));
  77. } else {
  78. $this->error(lang('goods_class_index_choose_in_homepage') . (input('post.submit_type') == 'index_show' ? lang('goods_class_index_display') : lang('goods_class_index_hide')) . lang('goods_class_index_content'));
  79. }
  80. }
  81. }
  82. /**
  83. * 父ID
  84. */
  85. $parent_id = input('fleaclass_parent_id') ? input('fleaclass_parent_id') : 0;
  86. /**
  87. * 列表
  88. */
  89. $tmp_list = $fleaclass_model->getTreeClassList(4);
  90. $class_list = array();
  91. if (is_array($tmp_list)) {
  92. foreach ($tmp_list as $k => $v) {
  93. if ($v['fleaclass_parent_id'] == $parent_id) {
  94. /**
  95. * 判断是否有子类
  96. */
  97. if (isset($tmp_list[$k + 1]) && ($tmp_list[$k + 1]['deep'] > $v['deep'])) {
  98. $v['have_child'] = 1;
  99. } else {
  100. $v['have_child'] = 0;
  101. }
  102. $class_list[] = $v;
  103. }
  104. }
  105. }
  106. if (input('ajax') == '1') {
  107. $output = json_encode($class_list);
  108. print_r($output);
  109. exit;
  110. } else {
  111. View::assign('class_list', $class_list);
  112. $this->setAdminCurItem('index');
  113. return View::fetch('index');
  114. }
  115. }
  116. /**
  117. * 保存批量修改分类
  118. */
  119. public function brach_edit_save()
  120. {
  121. if (input('post.fleaclass_show') == '-1') {
  122. $this->success(lang('goods_class_batch_edit_succ'), 'fleaclass/flea_class');
  123. }
  124. if (request()->isPost()) {
  125. $fleaclass_model = model('fleaclass');
  126. $array = explode(',', input('post.id'));
  127. if (is_array($array)) {
  128. foreach ($array as $k => $v) {
  129. $update_array = array();
  130. $update_array['fleaclass_show'] = input('post.fleaclass_show');
  131. $fleaclass_model->editFleaclass(array('fleaclass_id' => $v), $update_array);
  132. }
  133. $this->success(lang('goods_class_batch_edit_succ'));
  134. } else {
  135. $this->success(lang('goods_class_batch_edit_wrong_content'));
  136. }
  137. } else {
  138. $this->success(lang('goods_class_batch_edit_wrong_content'));
  139. }
  140. }
  141. /**
  142. * 商品分类添加
  143. */
  144. public function goods_class_add()
  145. {
  146. $fleaclass_model = model('fleaclass');
  147. if (request()->isPost()) {
  148. /**
  149. * 验证
  150. */
  151. $data = [
  152. 'fleaclass_name' => input('post.fleaclass_name'),
  153. 'fleaclass_sort' => input('post.fleaclass_sort')
  154. ];
  155. $fleaclass_validate = ds_validate('fleaclass');
  156. if (!$fleaclass_validate->scene('goods_class_add')->check($data)) {
  157. $this->error($fleaclass_validate->getError());
  158. } else {
  159. $insert_array = array();
  160. $insert_array['fleaclass_name'] = input('post.fleaclass_name');
  161. $insert_array['fleaclass_parent_id'] = input('post.fleaclass_parent_id');
  162. $insert_array['fleaclass_sort'] = input('post.fleaclass_sort');
  163. $insert_array['fleaclass_show'] = input('post.fleaclass_show');
  164. $insert_array['fleaclass_index_show'] = input('post.fleaclass_index_show');
  165. $result = $fleaclass_model->addFleaclass($insert_array);
  166. if ($result) {
  167. dsLayerOpenSuccess(lang('goods_class_add_succ'));
  168. } else {
  169. $this->error(lang('goods_class_add_fail'));
  170. }
  171. }
  172. }
  173. /**
  174. * 父类列表,只取到第三级
  175. */
  176. $parent_list = $fleaclass_model->getTreeClassList(3);
  177. if (is_array($parent_list)) {
  178. foreach ($parent_list as $k => $v) {
  179. $parent_list[$k]['fleaclass_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['fleaclass_name'];
  180. }
  181. }
  182. View::assign('fleaclass_parent_id', input('fleaclass_parent_id'));
  183. View::assign('parent_list', $parent_list);
  184. $this->setAdminCurItem('add');
  185. return View::fetch('add');
  186. }
  187. /**
  188. * 编辑
  189. */
  190. public function goods_class_edit()
  191. {
  192. $fleaclass_model = model('fleaclass');
  193. if (request()->isPost()) {
  194. /**
  195. * 验证
  196. */
  197. $data = [
  198. 'fleaclass_name' => input('post.fleaclass_name'),
  199. 'fleaclass_sort' => input('post.fleaclass_sort')
  200. ];
  201. $fleaclass_validate = ds_validate('fleaclass');
  202. if (!$fleaclass_validate->scene('goods_class_edit')->check($data)) {
  203. $this->error($fleaclass_validate->getError());
  204. } else {
  205. $update_array = array();
  206. $update_array['fleaclass_name'] = input('post.fleaclass_name');
  207. $fleaclass_parent_id = intval(input('post.fleaclass_parent_id'));
  208. if ($fleaclass_parent_id) {
  209. $update_array['fleaclass_parent_id'] = $fleaclass_parent_id;
  210. }
  211. $update_array['fleaclass_sort'] = input('post.fleaclass_sort');
  212. $update_array['fleaclass_show'] = input('post.fleaclass_show');
  213. $update_array['fleaclass_index_show'] = input('post.fleaclass_index_show');
  214. $result = $fleaclass_model->editFleaclass(array('fleaclass_id' => input('post.fleaclass_id')), $update_array);
  215. if ($result) {
  216. dsLayerOpenSuccess(lang('goods_class_batch_edit_ok'));
  217. } else {
  218. $this->error(lang('goods_class_batch_edit_fail'));
  219. }
  220. }
  221. } else {
  222. $class_array = $fleaclass_model->getOneFleaclass(input('param.fleaclass_id'));
  223. if (empty($class_array)) {
  224. $this->error(lang('goods_class_batch_edit_paramerror'));
  225. }
  226. /**
  227. * 父类列表,只取到第三级
  228. */
  229. $parent_list = $fleaclass_model->getTreeClassList(3);
  230. if (is_array($parent_list)) {
  231. $unset_sign = false;
  232. foreach ($parent_list as $k => $v) {
  233. if ($v['fleaclass_id'] == $class_array['fleaclass_id']) {
  234. $deep = $v['deep'];
  235. $unset_sign = true;
  236. }
  237. if ($unset_sign == true) {
  238. if ($v['deep'] == $deep && $v['fleaclass_id'] != $class_array['fleaclass_id']) {
  239. $unset_sign = false;
  240. }
  241. if ($v['deep'] > $deep || $v['fleaclass_id'] == $class_array['fleaclass_id']) {
  242. unset($parent_list[$k]);
  243. }
  244. } else {
  245. $parent_list[$k]['fleaclass_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['fleaclass_name'];
  246. }
  247. }
  248. }
  249. View::assign('parent_list', $parent_list);
  250. View::assign('class_array', $class_array);
  251. $this->setAdminCurItem('add');
  252. return View::fetch('edit');
  253. }
  254. }
  255. /**
  256. * 分类导入
  257. */
  258. public function goods_class_import()
  259. {
  260. /**
  261. * 实例化模型
  262. */
  263. $fleaclass_model = model('fleaclass');
  264. /**
  265. * 导入
  266. */
  267. if (request()->isPost()) {
  268. /**
  269. * 得到导入文件后缀名
  270. */
  271. $csv_name = explode('.', $_FILES['csv']['name']);
  272. $file_type = end($csv_name);
  273. if (!empty($_FILES['csv']) && !empty($_FILES['csv']['name']) && $file_type == 'csv') {
  274. $fp = @fopen($_FILES['csv']['tmp_name'], 'rb');
  275. /**
  276. * 父ID
  277. */
  278. $parent_id_1 = 0;
  279. while (!feof($fp)) {
  280. $data = fgets($fp, 4096);
  281. switch (strtoupper(input('post.charset'))) {
  282. case 'UTF-8':
  283. if (strtoupper(CHARSET) !== 'UTF-8') {
  284. $data = iconv('UTF-8', strtoupper(CHARSET), $data);
  285. }
  286. break;
  287. case 'GBK':
  288. if (strtoupper(CHARSET) !== 'GBK') {
  289. $data = iconv('GBK', strtoupper(CHARSET), $data);
  290. }
  291. break;
  292. }
  293. if (!empty($data)) {
  294. $data = str_replace('"', '', $data);
  295. /**
  296. * 逗号去除
  297. */
  298. $tmp_array = array();
  299. $tmp_array = explode(',', $data);
  300. if ($tmp_array[0] == 'sort_order') continue;
  301. /**
  302. * 第一位是序号,后面的是内容,最后一位名称
  303. */
  304. $tmp_deep = 'parent_id_' . (count($tmp_array) - 1);
  305. $insert_array = array();
  306. $insert_array['fleaclass_sort'] = $tmp_array[0];
  307. $insert_array['fleaclass_parent_id'] = $$tmp_deep;
  308. $insert_array['fleaclass_name'] = $tmp_array[count($tmp_array) - 1];
  309. $fleaclass_id = $fleaclass_model->addFleaclass($insert_array);
  310. /**
  311. * 赋值这个深度父ID
  312. */
  313. $tmp = 'parent_id_' . count($tmp_array);
  314. $$tmp = $fleaclass_id;
  315. }
  316. }
  317. /**
  318. * 重新生成缓存
  319. */
  320. $this->success(lang('goods_class_import_succ'), 'fleaclass/flea_class');
  321. } else {
  322. $this->error(lang('goods_class_import_csv_null'));
  323. }
  324. }
  325. $this->setAdminCurItem('import');
  326. return View::fetch('import');
  327. }
  328. /**
  329. * 分类导出
  330. */
  331. public function goods_class_export()
  332. {
  333. /**
  334. * 导出
  335. */
  336. if (request()->isPost()) {
  337. /**
  338. * 实例化模型
  339. */
  340. $fleaclass_model = model('fleaclass');
  341. /**
  342. * 分类信息
  343. */
  344. $class_list = $fleaclass_model->getTreeClassList();
  345. @header("Content-type: application/unknown");
  346. @header("Content-Disposition: attachment; filename=flea_class.csv");
  347. if (is_array($class_list)) {
  348. foreach ($class_list as $k => $v) {
  349. $tmp = array();
  350. /**
  351. * 序号
  352. */
  353. $tmp['fleaclass_sort'] = $v['fleaclass_sort'];
  354. /**
  355. * 深度
  356. */
  357. for ($i = 1; $i <= ($v['deep'] - 1); $i++) {
  358. $tmp[] = '';
  359. }
  360. /**
  361. * 分类名称
  362. */
  363. $tmp['fleaclass_name'] = $v['fleaclass_name'];
  364. /**
  365. * 转码 utf-gbk
  366. */
  367. if (strtoupper(CHARSET) == 'UTF-8') {
  368. switch (input('post.if_convert')) {
  369. case '1':
  370. $tmp_line = iconv('UTF-8', 'GB2312//IGNORE', join(',', $tmp));
  371. break;
  372. case '0':
  373. $tmp_line = join(',', $tmp);
  374. break;
  375. }
  376. } else {
  377. $tmp_line = join(',', $tmp);
  378. }
  379. $tmp_line = str_replace("\r\n", '', $tmp_line);
  380. echo $tmp_line . "\r\n";
  381. }
  382. }
  383. exit;
  384. } else {
  385. $this->setAdminCurItem('export');
  386. return View::fetch('export');
  387. }
  388. }
  389. /**
  390. * 删除分类
  391. */
  392. public function goods_class_del()
  393. {
  394. $fleaclass_model = model('fleaclass');
  395. $fleaclass_id = input('get.fleaclass_id');
  396. if ($fleaclass_id > 0) {
  397. /* 删除分类 */
  398. $fleaclass_model->delFleaclass($fleaclass_id);
  399. ds_json_encode(10000, lang('goods_class_index_del_succ'));
  400. } else {
  401. ds_json_encode(10001, lang('goods_class_index_choose_del'));
  402. }
  403. }
  404. /**
  405. * ajax操作
  406. */
  407. public function ajax()
  408. {
  409. switch (input('branch')) {
  410. /**
  411. * 验证是否有重复的名称
  412. */
  413. case 'goods_class_name':
  414. $fleaclass_model = model('fleaclass');
  415. $class_array = $fleaclass_model->getOneFleaclass(input('id'));
  416. $condition = array();
  417. $condition[] = array('fleaclass_name', '=', input('param.value'));
  418. $condition[] = array('fleaclass_parent_id', '=', $class_array['fleaclass_parent_id']);
  419. $condition[] = array('fleaclass_id', 'not in', input('param.id'));
  420. $class_list = $fleaclass_model->getFleaclassList($condition);
  421. if (empty($class_list)) {
  422. $update_array = array();
  423. $update_array['fleaclass_name'] = input('value');
  424. $fleaclass_model->editFleaclass(array('fleaclass_id' => intval(input('param.id'))), $update_array);
  425. echo 'true';
  426. exit;
  427. } else {
  428. echo 'false';
  429. exit;
  430. }
  431. break;
  432. /**
  433. * 分类 排序 显示 设置
  434. */
  435. case 'goods_class_sort':
  436. case 'goods_class_show':
  437. case 'goods_class_index_show':
  438. $fleaclass_model = model('fleaclass');
  439. $update_array = array();
  440. $update_array[input('column')] = input('value');
  441. $fleaclass_model->editFleaclass(array('fleaclass_id' => intval(input('param.id'))), $update_array);
  442. echo 'true';
  443. exit;
  444. break;
  445. /**
  446. * 添加、修改操作中 检测类别名称是否有重复
  447. */
  448. case 'check_class_name':
  449. $fleaclass_model = model('fleaclass');
  450. $condition = array();
  451. $condition[] = array('fleaclass_name', '=', input('param.fleaclass_name'));
  452. $condition[] = array('fleaclass_parent_id', '=', input('param.fleaclass_parent_id'));
  453. $condition[] = array('fleaclass_id', 'not in', input('param.fleaclass_id'));
  454. $class_list = $fleaclass_model->getFleaclassList($condition);
  455. if (empty($class_list)) {
  456. echo 'true';
  457. exit;
  458. } else {
  459. echo 'false';
  460. exit;
  461. }
  462. break;
  463. }
  464. }
  465. protected function getAdminItemList()
  466. {
  467. $menu_array = array(
  468. array(
  469. 'name' => 'index', 'text' => lang('ds_manage'), 'url' => (string)url('Fleaclass/flea_class')
  470. ),
  471. array(
  472. 'name' => 'add', 'text' => lang('ds_add'), 'url' => "javascript:dsLayerOpen('" . (string)url('Fleaclass/goods_class_add') . "','" . lang('ds_add') . "')"
  473. ),
  474. array(
  475. 'name' => 'export', 'text' => lang('ds_export'), 'url' => (string)url('Fleaclass/goods_class_export')
  476. ),
  477. array(
  478. 'name' => 'import', 'text' => lang('ds_import'), 'url' => (string)url('Fleaclass/goods_class_import')
  479. ),
  480. );
  481. return $menu_array;
  482. }
  483. }