Fleaclass.php 20 KB

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