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